diff --git a/Microsoft.Research/ContractAdornments/CSharp.Roslyn/Contracts/ContractsProvider.cs b/Microsoft.Research/ContractAdornments/CSharp.Roslyn/Contracts/ContractsProvider.cs index 3cde68b1..24ad93c5 100644 --- a/Microsoft.Research/ContractAdornments/CSharp.Roslyn/Contracts/ContractsProvider.cs +++ b/Microsoft.Research/ContractAdornments/CSharp.Roslyn/Contracts/ContractsProvider.cs @@ -624,7 +624,7 @@ public bool TryGetTypeReference(ITypeSymbol semanticType, IAssemblyReference cci } } ITypeReference genericType = null; - if (!TryGetTypeReference(semanticType.ContainingType, out genericType)) { + if (!TryGetTypeReference(semanticType.DefiningType(), out genericType)) { goto ReturnFalse; } cciType = new Microsoft.Cci.MutableCodeModel.GenericTypeInstanceReference() { @@ -659,20 +659,20 @@ public bool TryGetTypeReference(ITypeSymbol semanticType, IAssemblyReference cci #endregion #region If type parameter if (semanticType.TypeKind == TypeKind.TypeParameter) { - if (semanticType.ContainingSymbol != null && semanticType.ContainingSymbol.Kind == SymbolKind.Method) { + if (semanticType.DefiningMember() != null) { cciType = new Microsoft.Cci.MutableCodeModel.GenericMethodParameterReference() { - Index = (ushort)(!semanticType.ContainingSymbol.TypeParameters().IsDefault ? semanticType.ContainingSymbol.TypeParameters().IndexOf((ITypeParameterSymbol)semanticType) : 0), + Index = (ushort)(!semanticType.DefiningMember().TypeParameters().IsDefault ? semanticType.DefiningMember().TypeParameters().IndexOf((ITypeParameterSymbol)semanticType) : 0), InternFactory = this.Host.InternFactory, Name = Host.NameTable.GetNameFor(semanticType.Name != null ? semanticType.Name : "T"), }; goto ReturnTrue; - } else if (semanticType.ContainingType != null) { + } else if (semanticType.DefiningType() != null) { ITypeReference cciDefiningType; - if (!TryGetTypeReference(semanticType.ContainingType, out cciDefiningType)) + if (!TryGetTypeReference(semanticType.DefiningType(), out cciDefiningType)) goto ReturnFalse; cciType = new Microsoft.Cci.MutableCodeModel.GenericTypeParameterReference() { DefiningType = cciDefiningType, - Index = (ushort)(semanticType.ContainingType.TypeParameters != null ? semanticType.ContainingType.TypeParameters.IndexOf((ITypeParameterSymbol)semanticType) : 0), + Index = (ushort)(!semanticType.DefiningType().TypeParameters().IsDefaultOrEmpty ? semanticType.DefiningType().TypeParameters().IndexOf((ITypeParameterSymbol)semanticType) : 0), InternFactory = this.Host.InternFactory, Name = Host.NameTable.GetNameFor(semanticType.Name != null ? semanticType.Name : "T"), }; diff --git a/Microsoft.Research/ContractAdornments/CSharp.Roslyn/ISymbolExtensions.cs b/Microsoft.Research/ContractAdornments/CSharp.Roslyn/ISymbolExtensions.cs index 06ea6b14..31b6522d 100644 --- a/Microsoft.Research/ContractAdornments/CSharp.Roslyn/ISymbolExtensions.cs +++ b/Microsoft.Research/ContractAdornments/CSharp.Roslyn/ISymbolExtensions.cs @@ -79,6 +79,28 @@ public static ITypeSymbol ElementType(this ITypeSymbol symbol) } } + public static IMethodSymbol DefiningMember(this ITypeSymbol symbol) + { + ITypeParameterSymbol typeParameterSymbol = symbol as ITypeParameterSymbol; + if (typeParameterSymbol != null) + return typeParameterSymbol.DeclaringMethod; + + return null; + } + + public static ITypeSymbol DefiningType(this ITypeSymbol symbol) + { + ITypeParameterSymbol typeParameterSymbol = symbol as ITypeParameterSymbol; + if (typeParameterSymbol != null) + return typeParameterSymbol.DeclaringType; + + ITypeSymbol originalDefinition = symbol.OriginalDefinition; + if (originalDefinition != symbol) + return originalDefinition; + + return null; + } + public static ImmutableArray TypeParameters(this ISymbol symbol) { switch (symbol.Kind)