From 5e329ed3436a57ddc67facfce1a105d6d4481cac Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Thu, 5 Aug 2010 18:19:02 +0200 Subject: [PATCH] Make ITypeParameter inherit from IType. --- .../ICSharpCode.NRefactory.csproj | 2 + .../TypeSystem/CecilProjectContent.cs | 38 +++----------- .../TypeSystem/ITypeParameter.cs | 50 ++++++++++++------- .../Implementation/AbstractFreezable.cs | 8 +-- .../TypeSystem/Implementation/AbstractType.cs | 8 +-- .../Implementation/AbstractTypeReference.cs | 8 +-- .../TypeSystem/Implementation/BitVector16.cs | 9 ++-- .../DefaultExplicitInterfaceImplementation.cs | 8 +-- .../Implementation/DefaultParameter.cs | 8 +-- .../Implementation/DefaultTypeDefinition.cs | 8 +-- .../TypeSystem/Implementation/DynamicType.cs | 31 ++++++++++++ .../Implementation/GetClassTypeReference.cs | 9 ++-- .../Implementation/NestedTypeReference.cs | 9 ++-- .../TypeSystem/Implementation/NullType.cs | 8 +-- .../Implementation/ProxyTypeResolveContext.cs | 9 ++-- .../Implementation/SimpleConstantValue.cs | 37 ++++++++++++++ .../Implementation/TypeWithElementType.cs | 9 ++-- .../TypeSystem/Implementation/UnknownType.cs | 8 +-- .../TypeSystem/SharedTypes.cs | 7 ++- 19 files changed, 146 insertions(+), 128 deletions(-) create mode 100644 ICSharpCode.NRefactory/TypeSystem/Implementation/DynamicType.cs create mode 100644 ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleConstantValue.cs diff --git a/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj b/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj index 5d34c8a676..610613fede 100644 --- a/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj +++ b/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj @@ -151,12 +151,14 @@ + + diff --git a/ICSharpCode.NRefactory/TypeSystem/CecilProjectContent.cs b/ICSharpCode.NRefactory/TypeSystem/CecilProjectContent.cs index fe7e6fb54f..384a4f0d80 100644 --- a/ICSharpCode.NRefactory/TypeSystem/CecilProjectContent.cs +++ b/ICSharpCode.NRefactory/TypeSystem/CecilProjectContent.cs @@ -166,8 +166,7 @@ namespace ICSharpCode.NRefactory.TypeSystem return new ConstructedReturnType(baseType, para);*/ throw new NotImplementedException(); } else if (type is GenericParameter) { - throw new NotImplementedException(); - /*GenericParameter typeGP = type as GenericParameter; + GenericParameter typeGP = type as GenericParameter; if (typeGP.Owner is MethodDefinition) { IMethod method = entity as IMethod; if (method != null) { @@ -184,7 +183,7 @@ namespace ICSharpCode.NRefactory.TypeSystem } } return SharedTypes.UnknownType; - }*/ + } } else { string name = type.FullName; if (name == null) @@ -331,33 +330,12 @@ namespace ICSharpCode.NRefactory.TypeSystem #region Read Constant Value static IConstantValue ReadConstantValue(CustomAttributeArgument arg, ITypeResolveContext earlyBindContext) { - return new CecilConstantValue(arg.Type, arg.Value, earlyBindContext); - } - - sealed class CecilConstantValue : Immutable, IConstantValue - { - readonly ITypeReference type; - readonly object value; - - public CecilConstantValue(TypeReference type, object value, ITypeResolveContext earlyBindContext) - { - this.type = ReadTypeReference(type, earlyBindContext: earlyBindContext); - TypeReference valueType = value as TypeReference; - if (valueType != null) - this.value = ReadTypeReference(valueType, earlyBindContext: earlyBindContext); - else - this.value = value; - } - - public IType GetValueType(ITypeResolveContext context) - { - return type.Resolve(context); - } - - public object GetValue(ITypeResolveContext context) - { - return value; - } + ITypeReference type = ReadTypeReference(arg.Type, earlyBindContext: earlyBindContext); + object value = arg.Value; + TypeReference valueType = value as TypeReference; + if (valueType != null) + value = ReadTypeReference(valueType, earlyBindContext: earlyBindContext); + return new SimpleConstantValue(type, value); } #endregion diff --git a/ICSharpCode.NRefactory/TypeSystem/ITypeParameter.cs b/ICSharpCode.NRefactory/TypeSystem/ITypeParameter.cs index 6136095bfa..4cd2d62252 100644 --- a/ICSharpCode.NRefactory/TypeSystem/ITypeParameter.cs +++ b/ICSharpCode.NRefactory/TypeSystem/ITypeParameter.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.Contracts; +using ICSharpCode.NRefactory.TypeSystem; namespace ICSharpCode.NRefactory.TypeSystem { @@ -11,13 +12,8 @@ namespace ICSharpCode.NRefactory.TypeSystem /// Type parameter of a generic class/method. /// [ContractClass(typeof(ITypeParameterContract))] - public interface ITypeParameter : IFreezable + public interface ITypeParameter : IType, IFreezable { - /// - /// The name of the type parameter (for example "T") - /// - string Name { get; } - /// /// Gets the index of the type parameter in the type parameter list of the owning method/class. /// @@ -30,14 +26,13 @@ namespace ICSharpCode.NRefactory.TypeSystem /// /// The method this type parameter is defined for. - /// This property is null when the type parameter is for a class. + /// This property returns null if the type parameter belong to a class. /// IMethod ParentMethod { get; } /// /// The class this type parameter is defined for. - /// When the type parameter is defined for a method, this is the class containing - /// that method. + /// This property returns null if the type parameter belong to a method. /// ITypeDefinition ParentClass { get; } @@ -61,6 +56,11 @@ namespace ICSharpCode.NRefactory.TypeSystem /// bool HasValueTypeConstraint { get; } + /// + /// Gets the variance of this type parameter. + /// + VarianceModifier Variance { get; } + /// /// Gets the type that was used to bind this type parameter. /// This property returns null for generic methods/classes, it @@ -74,17 +74,28 @@ namespace ICSharpCode.NRefactory.TypeSystem ITypeParameter UnboundTypeParameter { get; } } + /// + /// Represents the variance of a type parameter. + /// + public enum VarianceModifier + { + /// + /// The type parameter is not variant. + /// + Invariant, + /// + /// The type parameter is covariant (used in output position). + /// + Covariant, + /// + /// The type parameter is contravariant (used in input position). + /// + Contravariant + }; [ContractClassFor(typeof(ITypeParameter))] - abstract class ITypeParameterContract : IFreezableContract, ITypeParameter + abstract class ITypeParameterContract : ITypeContract, ITypeParameter { - string ITypeParameter.Name { - get { - Contract.Ensures(Contract.Result() != null); - return null; - } - } - int ITypeParameter.Index { get { Contract.Ensures(Contract.Result() >= 0); @@ -107,7 +118,6 @@ namespace ICSharpCode.NRefactory.TypeSystem ITypeDefinition ITypeParameter.ParentClass { get { - Contract.Ensures(Contract.Result() != null); return null; } } @@ -142,5 +152,9 @@ namespace ICSharpCode.NRefactory.TypeSystem return null; } } + + VarianceModifier ITypeParameter.Variance { + get { return VarianceModifier.Invariant; } + } } } diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractFreezable.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractFreezable.cs index 51839cf90c..23439da204 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractFreezable.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractFreezable.cs @@ -1,9 +1,5 @@ -// -// -// -// -// $Revision$ -// +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) using System; using System.Collections.Generic; diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractType.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractType.cs index 2080495803..1aeb56fd4e 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractType.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractType.cs @@ -1,9 +1,5 @@ -// -// -// -// -// $Revision$ -// +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) using System; using System.Collections.Generic; diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractTypeReference.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractTypeReference.cs index a3f71865a8..b395018121 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractTypeReference.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractTypeReference.cs @@ -1,9 +1,5 @@ -// -// -// -// -// $Revision$ -// +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) using System; using System.Collections.Generic; diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/BitVector16.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/BitVector16.cs index ad4aeb45ca..80bbb7ab3e 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/BitVector16.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/BitVector16.cs @@ -1,9 +1,6 @@ -// -// -// -// -// $Revision$ -// +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) + using System; namespace ICSharpCode.NRefactory.TypeSystem.Implementation diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultExplicitInterfaceImplementation.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultExplicitInterfaceImplementation.cs index 51d4299990..61ae2e6504 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultExplicitInterfaceImplementation.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultExplicitInterfaceImplementation.cs @@ -1,9 +1,5 @@ -// -// -// -// -// $Revision$ -// +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) using System; diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultParameter.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultParameter.cs index dfd498c1b7..532a00db9c 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultParameter.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultParameter.cs @@ -1,9 +1,5 @@ -// -// -// -// -// $Revision$ -// +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) using System; using System.Collections.Generic; diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeDefinition.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeDefinition.cs index ed6f5c7968..4abf28e3d6 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeDefinition.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeDefinition.cs @@ -1,9 +1,5 @@ -// -// -// -// -// $Revision$ -// +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) using System; using System.Collections.Generic; diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/DynamicType.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/DynamicType.cs new file mode 100644 index 0000000000..fe9cceb509 --- /dev/null +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/DynamicType.cs @@ -0,0 +1,31 @@ +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) + +using System; + +namespace ICSharpCode.NRefactory.TypeSystem.Implementation +{ + /// + /// Type representing the C# 'dynamic' type. + /// + sealed class DynamicType : AbstractType + { + public override string Name { + get { return "dynamic"; } + } + + public override bool? IsReferenceType { + get { return true; } + } + + public override bool Equals(IType other) + { + return other is DynamicType; + } + + public override int GetHashCode() + { + return 31986112; + } + } +} diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs index 9314fcf4a9..372a327da2 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs @@ -1,9 +1,6 @@ -// -// -// -// -// $Revision$ -// +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) + using System; namespace ICSharpCode.NRefactory.TypeSystem.Implementation diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/NestedTypeReference.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/NestedTypeReference.cs index f42e25b057..420e753aa3 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/NestedTypeReference.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/NestedTypeReference.cs @@ -1,9 +1,6 @@ -// -// -// -// -// $Revision$ -// +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) + using System; namespace ICSharpCode.NRefactory.TypeSystem.Implementation diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/NullType.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/NullType.cs index 3811304387..b1f598c59c 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/NullType.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/NullType.cs @@ -1,9 +1,5 @@ -// -// -// -// -// $Revision$ -// +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) using System; diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/ProxyTypeResolveContext.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/ProxyTypeResolveContext.cs index aacd87c0dc..24fbb205a0 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/ProxyTypeResolveContext.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/ProxyTypeResolveContext.cs @@ -1,9 +1,6 @@ -// -// -// -// -// $Revision$ -// +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) + using System; namespace ICSharpCode.NRefactory.TypeSystem.Implementation diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleConstantValue.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleConstantValue.cs new file mode 100644 index 0000000000..420f904674 --- /dev/null +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleConstantValue.cs @@ -0,0 +1,37 @@ +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) + +using System; + +namespace ICSharpCode.NRefactory.TypeSystem.Implementation +{ + /// + /// A simple constant value that is independent of the resolve context. + /// + public sealed class SimpleConstantValue : Immutable, IConstantValue + { + readonly ITypeReference type; + readonly object value; + + public SimpleConstantValue(ITypeReference type, object value) + { + if (type == null) + throw new ArgumentNullException("type"); + this.type = type; + this.value = value; + } + + public IType GetValueType(ITypeResolveContext context) + { + return type.Resolve(context); + } + + public object GetValue(ITypeResolveContext context) + { + if (value is ITypeReference) + return ((ITypeReference)value).Resolve(context); + else + return value; + } + } +} diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/TypeWithElementType.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/TypeWithElementType.cs index 99947c76a3..aea07d8b67 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/TypeWithElementType.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/TypeWithElementType.cs @@ -1,9 +1,6 @@ -// -// -// -// -// $Revision$ -// +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) + using System; namespace ICSharpCode.NRefactory.TypeSystem.Implementation diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/UnknownType.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/UnknownType.cs index ce899c98ee..6b9dde775c 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/UnknownType.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/UnknownType.cs @@ -1,9 +1,5 @@ -// -// -// -// -// $Revision$ -// +// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) using System; diff --git a/ICSharpCode.NRefactory/TypeSystem/SharedTypes.cs b/ICSharpCode.NRefactory/TypeSystem/SharedTypes.cs index 4c186ef1c7..2d8ac77d9c 100644 --- a/ICSharpCode.NRefactory/TypeSystem/SharedTypes.cs +++ b/ICSharpCode.NRefactory/TypeSystem/SharedTypes.cs @@ -23,8 +23,11 @@ namespace ICSharpCode.NRefactory.TypeSystem [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "It's immutable")] public readonly static IType Null = new NullType(); - // TODO: implement DynamicType - public readonly static IType Dynamic = new UnknownType(); + /// + /// Type representing the C# 'dynamic' type. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "It's immutable")] + public readonly static IType Dynamic = new DynamicType(); /* * I'd like to define static instances for common types like