Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
Add IMethod.AccessorOwner.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrunwald committed Jun 9, 2012
1 parent 253a5a6 commit 83d3ad6
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ DefaultUnresolvedMethod ConvertAccessor(Accessor accessor, IUnresolvedMember p,
if (accessor.IsNull)
return null;
var a = new DefaultUnresolvedMethod(currentTypeDefinition, prefix + p.Name);
a.AccessorOwner = p;
a.Accessibility = GetAccessibility(accessor.Modifiers) ?? p.Accessibility;
a.IsAbstract = p.IsAbstract;
a.IsOverride = p.IsOverridable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public void GetIndex<T>(T element) where T : IEquatable<T> {}

public NestedEnum EnumField;

public A Property { get; set; }

public enum NestedEnum {
EnumMember
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ public void IndexerGetter()
Assert.AreEqual(Accessibility.Public, p.Getter.Accessibility);
Assert.AreEqual(new[] { "index" }, p.Getter.Parameters.Select(x => x.Name).ToArray());
Assert.AreEqual("System.String", p.Getter.ReturnType.ReflectionName);
Assert.AreEqual(p, p.Getter.AccessorOwner);
}

[Test]
Expand All @@ -365,6 +366,16 @@ public void IndexerSetter()
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]
public void EnumTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1645,11 +1645,17 @@ static bool IsAccessor(MethodSemanticsAttributes semantics)
#region Read Method
[CLSCompliant(false)]
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)
return null;
DefaultUnresolvedMethod m = new DefaultUnresolvedMethod(parentType, method.Name);
m.EntityType = methodType;
m.AccessorOwner = accessorOwner;
if (method.HasGenericParameters) {
for (int i = 0; i < method.GenericParameters.Count; i++) {
if (method.GenericParameters[i].Position != i)
Expand Down Expand Up @@ -1875,8 +1881,8 @@ public IUnresolvedProperty ReadProperty(PropertyDefinition property, IUnresolved
TranslateModifiers(property.GetMethod ?? property.SetMethod, p);
p.ReturnType = ReadTypeReference(property.PropertyType, typeAttributes: property);

p.Getter = ReadMethod(property.GetMethod, parentType);
p.Setter = ReadMethod(property.SetMethod, parentType);
p.Getter = ReadMethod(property.GetMethod, parentType, p);
p.Setter = ReadMethod(property.SetMethod, parentType, p);

if (property.HasParameters) {
foreach (ParameterDefinition par in property.Parameters) {
Expand All @@ -1903,9 +1909,9 @@ public IUnresolvedEvent ReadEvent(EventDefinition ev, IUnresolvedTypeDefinition
TranslateModifiers(ev.AddMethod, e);
e.ReturnType = ReadTypeReference(ev.EventType, typeAttributes: ev);

e.AddAccessor = ReadMethod(ev.AddMethod, parentType);
e.RemoveAccessor = ReadMethod(ev.RemoveMethod, parentType);
e.InvokeAccessor = ReadMethod(ev.InvokeMethod, parentType);
e.AddAccessor = ReadMethod(ev.AddMethod, parentType, e);
e.RemoveAccessor = ReadMethod(ev.RemoveMethod, parentType, e);
e.InvokeAccessor = ReadMethod(ev.InvokeMethod, parentType, e);

AddAttributes(ev, e);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public interface IUnresolvedMethod : IUnresolvedParameterizedMember
bool IsPartialMethodDeclaration { 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>
/// Resolves the member.
/// </summary>
Expand Down Expand Up @@ -75,5 +81,16 @@ public interface IMethod : IParameterizedMember
bool IsConstructor { get; }
bool IsDestructor { 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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,20 @@ public bool IsDestructor {
public bool 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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class DefaultUnresolvedMethod : AbstractUnresolvedMember, IUnresolvedMeth
IList<IUnresolvedAttribute> returnTypeAttributes;
IList<IUnresolvedTypeParameter> typeParameters;
IList<IUnresolvedParameter> parameters;
IMemberReference accessorOwner;

protected override void FreezeInternal()
{
Expand Down Expand Up @@ -125,6 +126,14 @@ public IList<IUnresolvedParameter> Parameters {
}
}

public IMemberReference AccessorOwner {
get { return accessorOwner; }
set {
ThrowIfFrozen();
accessorOwner = value;
}
}

public override string ToString()
{
StringBuilder b = new StringBuilder("[");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,13 @@ internal IMethod WrapAccessor(ref IMethod cachingField, IMethod accessorDefiniti
if (accessorDefinition == null)
return null;
var result = LazyInit.VolatileRead(ref cachingField);
if (result != null)
if (result != null) {
return result;
else
return LazyInit.GetOrSet(ref cachingField, new SpecializedMethod(accessorDefinition, substitution));
} else {
var sm = new SpecializedMethod(accessorDefinition, substitution);
//sm.AccessorOwner = this;
return LazyInit.GetOrSet(ref cachingField, sm);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
using System.Diagnostics;
using System.Linq;
using System.Text;

using System.Threading;
using ICSharpCode.NRefactory.Utils;

namespace ICSharpCode.NRefactory.TypeSystem.Implementation
Expand Down Expand Up @@ -121,6 +121,27 @@ public bool 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()
{
// Pass the MethodTypeArguments to the SpecializingMemberReference only if
Expand Down

0 comments on commit 83d3ad6

Please sign in to comment.