Browse Source

Implemented type substitution for ConstructedType.

newNRvisualizers
Daniel Grunwald 15 years ago
parent
commit
9f43ffb3eb
  1. 4
      ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj
  2. 5
      ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs
  3. 66
      ICSharpCode.NRefactory/TypeSystem/ConstructedType.cs
  4. 9
      ICSharpCode.NRefactory/TypeSystem/IMember.cs
  5. 4
      ICSharpCode.NRefactory/TypeSystem/ITypeReference.cs
  6. 12
      ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractMember.cs
  7. 14
      ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultEvent.cs
  8. 7
      ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultField.cs
  9. 11
      ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultMethod.cs
  10. 25
      ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultParameter.cs
  11. 10
      ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultProperty.cs
  12. 36
      ICSharpCode.NRefactory/TypeSystem/Implementation/SpecializedEvent.cs
  13. 36
      ICSharpCode.NRefactory/TypeSystem/Implementation/SpecializedField.cs
  14. 51
      ICSharpCode.NRefactory/TypeSystem/Implementation/SpecializedMethod.cs
  15. 51
      ICSharpCode.NRefactory/TypeSystem/Implementation/SpecializedProperty.cs
  16. 2
      ICSharpCode.NRefactory/TypeSystem/TypeVisitor.cs

4
ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -178,6 +178,10 @@ @@ -178,6 +178,10 @@
<Compile Include="TypeSystem\Implementation\DefaultTypeDefinition.cs" />
<Compile Include="TypeSystem\Implementation\SimpleConstantValue.cs" />
<Compile Include="TypeSystem\Implementation\SimpleProjectContent.cs" />
<Compile Include="TypeSystem\Implementation\SpecializedEvent.cs" />
<Compile Include="TypeSystem\Implementation\SpecializedField.cs" />
<Compile Include="TypeSystem\Implementation\SpecializedMethod.cs" />
<Compile Include="TypeSystem\Implementation\SpecializedProperty.cs" />
<Compile Include="TypeSystem\Implementation\TypeStorage.cs" />
<Compile Include="TypeSystem\Implementation\TypeWithElementType.cs" />
<Compile Include="TypeSystem\INamedElement.cs" />

5
ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs

@ -617,9 +617,8 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -617,9 +617,8 @@ namespace ICSharpCode.NRefactory.TypeSystem
{
if (parameter == null)
throw new ArgumentNullException("parameter");
DefaultParameter p = new DefaultParameter();
p.Name = parameter.Name;
p.Type = ReadTypeReference(parameter.ParameterType, typeAttributes: parameter, entity: parentMember);
var type = ReadTypeReference(parameter.ParameterType, typeAttributes: parameter, entity: parentMember);
DefaultParameter p = new DefaultParameter(type, parameter.Name);
if (parameter.HasCustomAttributes)
AddAttributes(parameter, p.Attributes);

66
ICSharpCode.NRefactory/TypeSystem/ConstructedType.cs

@ -23,6 +23,29 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -23,6 +23,29 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// </remarks>
public class ConstructedType : Immutable, IType
{
sealed class Substitution : TypeVisitor
{
readonly IType[] typeArguments;
public Substitution(IType[] typeArguments)
{
this.typeArguments = typeArguments;
}
public override IType VisitTypeParameter(ITypeParameter type)
{
int index = type.Index;
if (type.ParentClass != null) {
if (index >= 0 && index < typeArguments.Length)
return typeArguments[index];
else
return SharedTypes.UnknownType;
} else {
return base.VisitTypeParameter(type);
}
}
}
readonly ITypeDefinition genericType;
readonly IType[] typeArguments;
@ -118,7 +141,8 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -118,7 +141,8 @@ namespace ICSharpCode.NRefactory.TypeSystem
public IEnumerable<IType> GetBaseTypes(ITypeResolveContext context)
{
throw new NotImplementedException();
Substitution substitution = new Substitution(typeArguments);
return genericType.GetBaseTypes(context).Select(t => t.AcceptVisitor(substitution));
}
public IList<IType> GetNestedTypes(ITypeResolveContext context)
@ -128,22 +152,54 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -128,22 +152,54 @@ namespace ICSharpCode.NRefactory.TypeSystem
public IList<IMethod> GetMethods(ITypeResolveContext context)
{
throw new NotImplementedException();
Substitution substitution = new Substitution(typeArguments);
IList<IMethod> methods = genericType.GetMethods(context);
for (int i = 0; i < methods.Count; i++) {
SpecializedMethod m = new SpecializedMethod(methods[i]);
m.SetDeclaringType(this);
m.SubstituteTypes(context, substitution);
methods[i] = m;
}
return methods;
}
public IList<IProperty> GetProperties(ITypeResolveContext context)
{
throw new NotImplementedException();
Substitution substitution = new Substitution(typeArguments);
IList<IProperty> properties = genericType.GetProperties(context);
for (int i = 0; i < properties.Count; i++) {
SpecializedProperty p = new SpecializedProperty(properties[i]);
p.SetDeclaringType(this);
p.SubstituteTypes(context, substitution);
properties[i] = p;
}
return properties;
}
public IList<IField> GetFields(ITypeResolveContext context)
{
throw new NotImplementedException();
Substitution substitution = new Substitution(typeArguments);
IList<IField> fields = genericType.GetFields(context);
for (int i = 0; i < fields.Count; i++) {
SpecializedField f = new SpecializedField(fields[i]);
f.SetDeclaringType(this);
f.ReturnType = f.ReturnType.Resolve(context).AcceptVisitor(substitution);
fields[i] = f;
}
return fields;
}
public IList<IEvent> GetEvents(ITypeResolveContext context)
{
throw new NotImplementedException();
Substitution substitution = new Substitution(typeArguments);
IList<IEvent> events = genericType.GetEvents(context);
for (int i = 0; i < events.Count; i++) {
SpecializedEvent e = new SpecializedEvent(events[i]);
e.SetDeclaringType(this);
e.ReturnType = e.ReturnType.Resolve(context).AcceptVisitor(substitution);
events[i] = e;
}
return events;
}
public override bool Equals(object obj)

9
ICSharpCode.NRefactory/TypeSystem/IMember.cs

@ -27,11 +27,11 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -27,11 +27,11 @@ namespace ICSharpCode.NRefactory.TypeSystem
IType DeclaringType { get; }
/// <summary>
/// Gets the generic member this member is based on.
/// Returns null if this is not a specialized member.
/// Gets the original member definition for this member.
/// Returns <c>this</c> if this is not a specialized member.
/// Specialized members are the result of overload resolution with type substitution.
/// </summary>
IMember GenericMember { get; }
IMember MemberDefinition { get; }
/// <summary>
/// Gets the return type of this member.
@ -81,8 +81,9 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -81,8 +81,9 @@ namespace ICSharpCode.NRefactory.TypeSystem
}
}
IMember IMember.GenericMember {
IMember IMember.MemberDefinition {
get {
Contract.Ensures(Contract.Result<IMember>() != null);
return null;
}
}

4
ICSharpCode.NRefactory/TypeSystem/ITypeReference.cs

@ -28,21 +28,25 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -28,21 +28,25 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// <summary>
/// Gets all methods that can be called on this return type.
/// </summary>
/// <returns>A new mutable list</returns>
IList<IMethod> GetMethods(ITypeResolveContext context);
/// <summary>
/// Gets all properties that can be called on this return type.
/// </summary>
/// <returns>A new mutable list</returns>
IList<IProperty> GetProperties(ITypeResolveContext context);
/// <summary>
/// Gets all fields that can be called on this return type.
/// </summary>
/// <returns>A new mutable list</returns>
IList<IField> GetFields(ITypeResolveContext context);
/// <summary>
/// Gets all events that can be called on this return type.
/// </summary>
/// <returns>A new mutable list</returns>
IList<IEvent> GetEvents(ITypeResolveContext context);
}

12
ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractMember.cs

@ -51,7 +51,9 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -51,7 +51,9 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
this.name = name;
}
/* do we really need copy constructor (for specialized members?)
/// <summary>
/// Copy constructor
/// </summary>
protected AbstractMember(IMember member)
{
if (member == null)
@ -64,6 +66,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -64,6 +66,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
this.bodyRegion = member.BodyRegion;
this.name = member.Name;
this.accessibility = member.Accessibility;
this.entityType = member.EntityType;
this.IsSealed = member.IsSealed;
this.IsAbstract = member.IsAbstract;
this.IsShadowing = member.IsShadowing;
@ -73,14 +76,13 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -73,14 +76,13 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
this.IsStatic = member.IsStatic;
}
static IList<T> CopyList<T>(IList<T> inputList)
protected static IList<T> CopyList<T>(IList<T> inputList)
{
if (inputList.Count == 0)
return null;
else
return new List<T>(inputList);
}
*/
public ITypeDefinition DeclaringTypeDefinition {
get { return declaringTypeDefinition; }
@ -90,8 +92,8 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -90,8 +92,8 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
get { return declaringTypeDefinition; }
}
public virtual IMember GenericMember {
get { return null; }
public virtual IMember MemberDefinition {
get { return this; }
}
public ITypeReference ReturnType {

14
ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultEvent.cs

@ -23,6 +23,20 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -23,6 +23,20 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
{
}
/// <summary>
/// Copy constructor
/// </summary>
protected DefaultEvent(IEvent ev)
: base(ev)
{
this.CanAdd = ev.CanAdd;
this.addAccessibility = ev.AddAccessibility;
this.CanRemove = ev.CanRemove;
this.removeAccessibility = ev.RemoveAccessibility;
this.CanInvoke = ev.CanInvoke;
this.invokeAccessibility = ev.InvokeAccessibility;
}
public bool CanAdd {
get { return flags[FlagCanAdd]; }
set {

7
ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultField.cs

@ -27,6 +27,13 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -27,6 +27,13 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
{
}
protected DefaultField(IField f) : base(f)
{
this.constantValue = f.ConstantValue;
this.IsReadOnly = f.IsReadOnly;
this.IsVolatile = f.IsVolatile;
}
public bool IsConst {
get { return constantValue != null; }
}

11
ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultMethod.cs

@ -31,6 +31,17 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -31,6 +31,17 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
{
}
/// <summary>
/// Copy constructor
/// </summary>
protected DefaultMethod(IMethod method) : base(method)
{
returnTypeAttributes = CopyList(returnTypeAttributes);
typeParameters = CopyList(typeParameters);
parameters = CopyList(parameters);
this.IsExtensionMethod = method.IsExtensionMethod;
}
public IList<IAttribute> ReturnTypeAttributes {
get {
if (returnTypeAttributes == null)

25
ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultParameter.cs

@ -20,6 +20,31 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -20,6 +20,31 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
DomRegion region;
byte flags;
public DefaultParameter(ITypeReference type, string name)
{
if (type == null)
throw new ArgumentNullException("type");
if (name == null)
throw new ArgumentNullException("name");
this.type = type;
this.name = name;
}
/// <summary>
/// Copy constructor
/// </summary>
public DefaultParameter(IParameter p)
{
this.name = p.Name;
this.type = p.Type;
this.attributes = p.Attributes;
this.defaultValue = p.DefaultValue;
this.region = p.Region;
this.IsRef = p.IsRef;
this.IsOut = p.IsOut;
this.IsParams = p.IsParams;
}
protected override void FreezeInternal()
{
type.Freeze();

10
ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultProperty.cs

@ -30,6 +30,16 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -30,6 +30,16 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
{
}
protected DefaultProperty(IProperty p) : base(p)
{
this.getterAccessibility = p.GetterAccessibility;
this.setterAccessibility = p.SetterAccessibility;
this.parameters = CopyList(p.Parameters);
this.IsIndexer = p.IsIndexer;
this.CanGet = p.CanGet;
this.CanSet = p.CanSet;
}
public bool IsIndexer {
get { return flags[FlagIsIndexer]; }
set {

36
ICSharpCode.NRefactory/TypeSystem/Implementation/SpecializedEvent.cs

@ -0,0 +1,36 @@ @@ -0,0 +1,36 @@
// 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
{
/// <summary>
/// Represents a specialized IEvent (e.g. after type substitution).
/// </summary>
public class SpecializedEvent : DefaultEvent
{
readonly IMember memberDefinition;
IType declaringType;
public SpecializedEvent(IEvent e) : base(e)
{
this.memberDefinition = e.MemberDefinition;
this.declaringType = e.DeclaringType;
}
public override IType DeclaringType {
get { return declaringType; }
}
public void SetDeclaringType(IType declaringType)
{
CheckBeforeMutation();
this.declaringType = declaringType;
}
public override IMember MemberDefinition {
get { return memberDefinition; }
}
}
}

36
ICSharpCode.NRefactory/TypeSystem/Implementation/SpecializedField.cs

@ -0,0 +1,36 @@ @@ -0,0 +1,36 @@
// 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
{
/// <summary>
/// Represents a specialized IField (e.g. after type substitution).
/// </summary>
public class SpecializedField : DefaultField
{
readonly IMember memberDefinition;
IType declaringType;
public SpecializedField(IField f) : base(f)
{
this.memberDefinition = f.MemberDefinition;
this.declaringType = f.DeclaringType;
}
public override IType DeclaringType {
get { return declaringType; }
}
public void SetDeclaringType(IType declaringType)
{
CheckBeforeMutation();
this.declaringType = declaringType;
}
public override IMember MemberDefinition {
get { return memberDefinition; }
}
}
}

51
ICSharpCode.NRefactory/TypeSystem/Implementation/SpecializedMethod.cs

@ -0,0 +1,51 @@ @@ -0,0 +1,51 @@
// 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
{
/// <summary>
/// Represents a specialized IMethod (e.g. after type substitution).
/// </summary>
public class SpecializedMethod : DefaultMethod
{
readonly IMember memberDefinition;
IType declaringType;
public SpecializedMethod(IMethod m) : base(m)
{
this.memberDefinition = m.MemberDefinition;
this.declaringType = m.DeclaringType;
}
public override IType DeclaringType {
get { return declaringType; }
}
public void SetDeclaringType(IType declaringType)
{
CheckBeforeMutation();
this.declaringType = declaringType;
}
public override IMember MemberDefinition {
get { return memberDefinition; }
}
/// <summary>
/// Performts type substitution in parameter types and in the return type.
/// </summary>
public void SubstituteTypes(ITypeResolveContext context, TypeVisitor substitution)
{
this.ReturnType = this.ReturnType.Resolve(context).AcceptVisitor(substitution);
var p = this.Parameters;
for (int i = 0; i < p.Count; i++) {
IType newType = p[i].Type.Resolve(context).AcceptVisitor(substitution);
if (newType != p[i].Type) {
p[i] = new DefaultParameter(p[i]) { Type = newType };
}
}
}
}
}

51
ICSharpCode.NRefactory/TypeSystem/Implementation/SpecializedProperty.cs

@ -0,0 +1,51 @@ @@ -0,0 +1,51 @@
// 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
{
/// <summary>
/// Represents a specialized IProperty (e.g. after type substitution).
/// </summary>
public class SpecializedProperty : DefaultProperty
{
readonly IMember memberDefinition;
IType declaringType;
public SpecializedProperty(IProperty p) : base(p)
{
this.memberDefinition = p.MemberDefinition;
this.declaringType = p.DeclaringType;
}
public override IType DeclaringType {
get { return declaringType; }
}
public void SetDeclaringType(IType declaringType)
{
CheckBeforeMutation();
this.declaringType = declaringType;
}
public override IMember MemberDefinition {
get { return memberDefinition; }
}
/// <summary>
/// Performts type substitution in parameter types and in the return type.
/// </summary>
public void SubstituteTypes(ITypeResolveContext context, TypeVisitor substitution)
{
this.ReturnType = this.ReturnType.Resolve(context).AcceptVisitor(substitution);
var p = this.Parameters;
for (int i = 0; i < p.Count; i++) {
IType newType = p[i].Type.Resolve(context).AcceptVisitor(substitution);
if (newType != p[i].Type) {
p[i] = new DefaultParameter(p[i]) { Type = newType };
}
}
}
}
}

2
ICSharpCode.NRefactory/TypeSystem/TypeVisitor.cs

@ -6,7 +6,7 @@ using System; @@ -6,7 +6,7 @@ using System;
namespace ICSharpCode.NRefactory.TypeSystem
{
/// <summary>
/// Description of ITypeVisitor.
/// Base class for the visitor pattern on <see cref="IType"/>.
/// </summary>
public abstract class TypeVisitor
{

Loading…
Cancel
Save