Browse Source

Add ITypeDefinition.GetInterfaceImplementation().

newNRvisualizers
Daniel Grunwald 13 years ago
parent
commit
3f782ce21c
  1. 1
      ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj
  2. 1
      ICSharpCode.NRefactory/TypeSystem/IAttribute.cs
  3. 1
      ICSharpCode.NRefactory/TypeSystem/IType.cs
  4. 20
      ICSharpCode.NRefactory/TypeSystem/ITypeDefinition.cs
  5. 41
      ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultResolvedTypeDefinition.cs
  6. 130
      ICSharpCode.NRefactory/Utils/MultiDictionary.cs

1
ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -219,6 +219,7 @@ @@ -219,6 +219,7 @@
<Compile Include="Utils\ImmutableStack.cs" />
<Compile Include="Utils\KeyComparer.cs" />
<Compile Include="Utils\LazyInit.cs" />
<Compile Include="Utils\MultiDictionary.cs" />
<Compile Include="Utils\Platform.cs" />
<Compile Include="Utils\ProjectedList.cs" />
<Compile Include="Utils\ReferenceComparer.cs" />

1
ICSharpCode.NRefactory/TypeSystem/IAttribute.cs

@ -18,7 +18,6 @@ @@ -18,7 +18,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using ICSharpCode.NRefactory.Semantics;

1
ICSharpCode.NRefactory/TypeSystem/IType.cs

@ -18,7 +18,6 @@ @@ -18,7 +18,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
namespace ICSharpCode.NRefactory.TypeSystem
{

20
ICSharpCode.NRefactory/TypeSystem/ITypeDefinition.cs

@ -130,5 +130,25 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -130,5 +130,25 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// </summary>
/// <remarks>This property is used to speed up the search for extension methods.</remarks>
bool HasExtensionMethods { get; }
/// <summary>
/// Determines how this type is implementing the specified interface member.
/// </summary>
/// <returns>
/// The method on this type that implements the interface member;
/// or null if the type does not implement the interface.
/// </returns>
IMember GetInterfaceImplementation(IMember interfaceMember);
/// <summary>
/// Determines how this type is implementing the specified interface members.
/// </summary>
/// <returns>
/// For each interface member, this method returns the class member
/// that implements the interface member.
/// For interface members that are missing an implementation, the
/// result collection will contain a null element.
/// </returns>
IList<IMember> GetInterfaceImplementation(IList<IMember> interfaceMembers);
}
}

41
ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultResolvedTypeDefinition.cs

@ -18,7 +18,6 @@ @@ -18,7 +18,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using ICSharpCode.NRefactory.Documentation;
using ICSharpCode.NRefactory.Utils;
@ -861,6 +860,46 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -861,6 +860,46 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
}
#endregion
#region GetInterfaceImplementation
public IMember GetInterfaceImplementation(IMember interfaceMember)
{
return GetInterfaceImplementation(new[] { interfaceMember })[0];
}
public IList<IMember> GetInterfaceImplementation(IList<IMember> interfaceMembers)
{
// TODO: review the subtle rules for interface reimplementation,
// write tests and fix this method.
// Also virtual/override is going to be tricky -
// I think we'll need to consider the 'virtual' method first for
// reimplemenatation purposes, but then actually return the 'override'
// (as that's the method that ends up getting called)
interfaceMembers = interfaceMembers.ToList(); // avoid evaluating more than once
var result = new IMember[interfaceMembers.Count];
var signatureToIndexDict = new MultiDictionary<IMember, int>(SignatureComparer.Ordinal);
for (int i = 0; i < interfaceMembers.Count; i++) {
signatureToIndexDict.Add(interfaceMembers[i], i);
}
foreach (var member in GetMembers(m => !m.IsExplicitInterfaceImplementation)) {
foreach (int interfaceMemberIndex in signatureToIndexDict[member]) {
result[interfaceMemberIndex] = member;
}
}
foreach (var explicitImpl in GetMembers(m => m.IsExplicitInterfaceImplementation)) {
foreach (var interfaceMember in explicitImpl.ImplementedInterfaceMembers) {
foreach (int potentialMatchingIndex in signatureToIndexDict[interfaceMember]) {
if (interfaceMember.Equals(interfaceMembers[potentialMatchingIndex])) {
result[potentialMatchingIndex] = explicitImpl;
}
}
}
}
return result;
}
#endregion
public bool Equals(IType other)
{
return this == other;

130
ICSharpCode.NRefactory/Utils/MultiDictionary.cs

@ -0,0 +1,130 @@ @@ -0,0 +1,130 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
namespace ICSharpCode.NRefactory.Utils
{
/// <summary>
/// A dictionary that allows multiple pairs with the same key.
/// </summary>
public class MultiDictionary<TKey, TValue> : ILookup<TKey, TValue>
{
readonly Dictionary<TKey, List<TValue>> dict;
public MultiDictionary()
{
dict = new Dictionary<TKey, List<TValue>>();
}
public MultiDictionary(IEqualityComparer<TKey> comparer)
{
dict = new Dictionary<TKey, List<TValue>>(comparer);
}
public void Add(TKey key, TValue value)
{
List<TValue> valueList;
if (!dict.TryGetValue(key, out valueList)) {
valueList = new List<TValue>();
dict.Add(key, valueList);
}
valueList.Add(value);
}
public bool Remove(TKey key, TValue value)
{
List<TValue> valueList;
if (dict.TryGetValue(key, out valueList)) {
if (valueList.Remove(value)) {
if (valueList.Count == 0)
dict.Remove(key);
return true;
}
}
return false;
}
public void Clear()
{
dict.Clear();
}
public IList<TValue> this[TKey key] {
get {
List<TValue> list;
if (dict.TryGetValue(key, out list))
return list;
else
return EmptyList<TValue>.Instance;
}
}
public int Count {
get { return dict.Count; }
}
IEnumerable<TValue> ILookup<TKey, TValue>.this[TKey key] {
get { return this[key]; }
}
bool ILookup<TKey, TValue>.Contains(TKey key)
{
return dict.ContainsKey(key);
}
public IEnumerator<IGrouping<TKey, TValue>> GetEnumerator()
{
foreach (var pair in dict)
yield return new Grouping(pair.Key, pair.Value);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
sealed class Grouping : IGrouping<TKey, TValue>
{
readonly TKey key;
readonly List<TValue> values;
public Grouping(TKey key, List<TValue> values)
{
this.key = key;
this.values = values;
}
public TKey Key {
get { return key; }
}
public IEnumerator<TValue> GetEnumerator()
{
return values.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return values.GetEnumerator();
}
}
}
}
Loading…
Cancel
Save