Browse Source

Add IMethod.AccessorOwner.

newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
a77fa3103a
  1. 1
      ICSharpCode.NRefactory.CSharp/TypeSystem/TypeSystemConvertVisitor.cs
  2. 2
      ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.TestCase.cs
  3. 11
      ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs
  4. 16
      ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs
  5. 17
      ICSharpCode.NRefactory/TypeSystem/IMethod.cs
  6. 14
      ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultResolvedMethod.cs
  7. 9
      ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedMethod.cs
  8. 9
      ICSharpCode.NRefactory/TypeSystem/Implementation/SpecializedMember.cs
  9. 23
      ICSharpCode.NRefactory/TypeSystem/Implementation/SpecializedMethod.cs

1
ICSharpCode.NRefactory.CSharp/TypeSystem/TypeSystemConvertVisitor.cs

@ -677,6 +677,7 @@ namespace ICSharpCode.NRefactory.CSharp.TypeSystem
if (accessor.IsNull) if (accessor.IsNull)
return null; return null;
var a = new DefaultUnresolvedMethod(currentTypeDefinition, prefix + p.Name); var a = new DefaultUnresolvedMethod(currentTypeDefinition, prefix + p.Name);
a.AccessorOwner = p;
a.Accessibility = GetAccessibility(accessor.Modifiers) ?? p.Accessibility; a.Accessibility = GetAccessibility(accessor.Modifiers) ?? p.Accessibility;
a.IsAbstract = p.IsAbstract; a.IsAbstract = p.IsAbstract;
a.IsOverride = p.IsOverridable; a.IsOverride = p.IsOverridable;

2
ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.TestCase.cs

@ -72,6 +72,8 @@ namespace ICSharpCode.NRefactory.TypeSystem.TestCase
public NestedEnum EnumField; public NestedEnum EnumField;
public A Property { get; set; }
public enum NestedEnum { public enum NestedEnum {
EnumMember EnumMember
} }

11
ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs

@ -351,6 +351,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
Assert.AreEqual(Accessibility.Public, p.Getter.Accessibility); Assert.AreEqual(Accessibility.Public, p.Getter.Accessibility);
Assert.AreEqual(new[] { "index" }, p.Getter.Parameters.Select(x => x.Name).ToArray()); Assert.AreEqual(new[] { "index" }, p.Getter.Parameters.Select(x => x.Name).ToArray());
Assert.AreEqual("System.String", p.Getter.ReturnType.ReflectionName); Assert.AreEqual("System.String", p.Getter.ReturnType.ReflectionName);
Assert.AreEqual(p, p.Getter.AccessorOwner);
} }
[Test] [Test]
@ -365,6 +366,16 @@ namespace ICSharpCode.NRefactory.TypeSystem
Assert.AreEqual(TypeKind.Void, p.Setter.ReturnType.Kind); Assert.AreEqual(TypeKind.Void, p.Setter.ReturnType.Kind);
} }
[Test]
public void GenericPropertyGetter()
{
var type = compilation.FindType(typeof(GenericClass<string, object>));
var prop = type.GetProperties(p => p.Name == "Property").Single();
Assert.AreEqual("System.String", prop.Getter.ReturnType.ReflectionName);
Assert.IsTrue(prop.Getter.IsAccessor);
Assert.AreEqual(prop, prop.Getter.AccessorOwner);
}
[Test] [Test]
public void EnumTest() public void EnumTest()
{ {

16
ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs

@ -1646,11 +1646,17 @@ namespace ICSharpCode.NRefactory.TypeSystem
#region Read Method #region Read Method
[CLSCompliant(false)] [CLSCompliant(false)]
public IUnresolvedMethod ReadMethod(MethodDefinition method, IUnresolvedTypeDefinition parentType, EntityType methodType = EntityType.Method) public IUnresolvedMethod ReadMethod(MethodDefinition method, IUnresolvedTypeDefinition parentType, EntityType methodType = EntityType.Method)
{
return ReadMethod(method, parentType, null, methodType);
}
IUnresolvedMethod ReadMethod(MethodDefinition method, IUnresolvedTypeDefinition parentType, IUnresolvedMember accessorOwner, EntityType methodType = EntityType.Method)
{ {
if (method == null) if (method == null)
return null; return null;
DefaultUnresolvedMethod m = new DefaultUnresolvedMethod(parentType, method.Name); DefaultUnresolvedMethod m = new DefaultUnresolvedMethod(parentType, method.Name);
m.EntityType = methodType; m.EntityType = methodType;
m.AccessorOwner = accessorOwner;
if (method.HasGenericParameters) { if (method.HasGenericParameters) {
for (int i = 0; i < method.GenericParameters.Count; i++) { for (int i = 0; i < method.GenericParameters.Count; i++) {
if (method.GenericParameters[i].Position != i) if (method.GenericParameters[i].Position != i)
@ -1876,8 +1882,8 @@ namespace ICSharpCode.NRefactory.TypeSystem
TranslateModifiers(property.GetMethod ?? property.SetMethod, p); TranslateModifiers(property.GetMethod ?? property.SetMethod, p);
p.ReturnType = ReadTypeReference(property.PropertyType, typeAttributes: property); p.ReturnType = ReadTypeReference(property.PropertyType, typeAttributes: property);
p.Getter = ReadMethod(property.GetMethod, parentType); p.Getter = ReadMethod(property.GetMethod, parentType, p);
p.Setter = ReadMethod(property.SetMethod, parentType); p.Setter = ReadMethod(property.SetMethod, parentType, p);
if (property.HasParameters) { if (property.HasParameters) {
foreach (ParameterDefinition par in property.Parameters) { foreach (ParameterDefinition par in property.Parameters) {
@ -1904,9 +1910,9 @@ namespace ICSharpCode.NRefactory.TypeSystem
TranslateModifiers(ev.AddMethod, e); TranslateModifiers(ev.AddMethod, e);
e.ReturnType = ReadTypeReference(ev.EventType, typeAttributes: ev); e.ReturnType = ReadTypeReference(ev.EventType, typeAttributes: ev);
e.AddAccessor = ReadMethod(ev.AddMethod, parentType); e.AddAccessor = ReadMethod(ev.AddMethod, parentType, e);
e.RemoveAccessor = ReadMethod(ev.RemoveMethod, parentType); e.RemoveAccessor = ReadMethod(ev.RemoveMethod, parentType, e);
e.InvokeAccessor = ReadMethod(ev.InvokeMethod, parentType); e.InvokeAccessor = ReadMethod(ev.InvokeMethod, parentType, e);
AddAttributes(ev, e); AddAttributes(ev, e);

17
ICSharpCode.NRefactory/TypeSystem/IMethod.cs

@ -39,6 +39,12 @@ namespace ICSharpCode.NRefactory.TypeSystem
bool IsPartialMethodDeclaration { get; } bool IsPartialMethodDeclaration { get; }
bool IsPartialMethodImplementation { get; } bool IsPartialMethodImplementation { get; }
/// <summary>
/// If this method is an accessor, returns a reference to the corresponding property/event.
/// Otherwise, returns null.
/// </summary>
IMemberReference AccessorOwner { get; }
/// <summary> /// <summary>
/// Resolves the member. /// Resolves the member.
/// </summary> /// </summary>
@ -75,5 +81,16 @@ namespace ICSharpCode.NRefactory.TypeSystem
bool IsConstructor { get; } bool IsConstructor { get; }
bool IsDestructor { get; } bool IsDestructor { get; }
bool IsOperator { get; } bool IsOperator { get; }
/// <summary>
/// Gets whether the method is a property/event accessor.
/// </summary>
bool IsAccessor { get; }
/// <summary>
/// If this method is an accessor, returns the corresponding property/event.
/// Otherwise, returns null.
/// </summary>
IMember AccessorOwner { get; }
} }
} }

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

@ -190,6 +190,20 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
public bool IsOperator { public bool IsOperator {
get { return ((IUnresolvedMethod)unresolved).IsOperator; } get { return ((IUnresolvedMethod)unresolved).IsOperator; }
} }
public bool IsAccessor {
get { return ((IUnresolvedMethod)unresolved).AccessorOwner != null; }
}
public IMember AccessorOwner {
get {
var reference = ((IUnresolvedMethod)unresolved).AccessorOwner;
if (reference != null)
return reference.Resolve(context);
else
return null;
}
}
public override IMemberReference ToMemberReference() public override IMemberReference ToMemberReference()
{ {

9
ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedMethod.cs

@ -32,6 +32,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
IList<IUnresolvedAttribute> returnTypeAttributes; IList<IUnresolvedAttribute> returnTypeAttributes;
IList<IUnresolvedTypeParameter> typeParameters; IList<IUnresolvedTypeParameter> typeParameters;
IList<IUnresolvedParameter> parameters; IList<IUnresolvedParameter> parameters;
IMemberReference accessorOwner;
protected override void FreezeInternal() protected override void FreezeInternal()
{ {
@ -125,6 +126,14 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
} }
} }
public IMemberReference AccessorOwner {
get { return accessorOwner; }
set {
ThrowIfFrozen();
accessorOwner = value;
}
}
public override string ToString() public override string ToString()
{ {
StringBuilder b = new StringBuilder("["); StringBuilder b = new StringBuilder("[");

9
ICSharpCode.NRefactory/TypeSystem/Implementation/SpecializedMember.cs

@ -101,10 +101,13 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
if (accessorDefinition == null) if (accessorDefinition == null)
return null; return null;
var result = LazyInit.VolatileRead(ref cachingField); var result = LazyInit.VolatileRead(ref cachingField);
if (result != null) if (result != null) {
return result; return result;
else } else {
return LazyInit.GetOrSet(ref cachingField, new SpecializedMethod(accessorDefinition, substitution)); var sm = new SpecializedMethod(accessorDefinition, substitution);
//sm.AccessorOwner = this;
return LazyInit.GetOrSet(ref cachingField, sm);
}
} }
/// <summary> /// <summary>

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

@ -21,7 +21,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using ICSharpCode.NRefactory.Utils; using ICSharpCode.NRefactory.Utils;
namespace ICSharpCode.NRefactory.TypeSystem.Implementation namespace ICSharpCode.NRefactory.TypeSystem.Implementation
@ -121,6 +121,27 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
get { return methodDefinition.IsOperator; } get { return methodDefinition.IsOperator; }
} }
public bool IsAccessor {
get { return methodDefinition.IsAccessor; }
}
IMember accessorOwner;
public IMember AccessorOwner {
get {
var result = LazyInit.VolatileRead(ref accessorOwner);
if (result != null) {
return result;
} else {
result = SpecializedMember.Create(methodDefinition.AccessorOwner, this.Substitution);
return LazyInit.GetOrSet(ref accessorOwner, result);
}
}
internal set {
accessorOwner = value;
}
}
public override IMemberReference ToMemberReference() public override IMemberReference ToMemberReference()
{ {
// Pass the MethodTypeArguments to the SpecializingMemberReference only if // Pass the MethodTypeArguments to the SpecializingMemberReference only if

Loading…
Cancel
Save