1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
20
using System.Collections.Generic;
22
using ICSharpCode.NRefactory.TypeSystem;
23
using ICSharpCode.NRefactory.TypeSystem.Implementation;
24
using ICSharpCode.NRefactory.Utils;
26
namespace ICSharpCode.NRefactory.CSharp.TypeSystem
29
public sealed class MethodTypeParameterWithInheritedConstraints : DefaultUnresolvedTypeParameter
31
public MethodTypeParameterWithInheritedConstraints(int index, string name)
32
: base(EntityType.Method, index, name)
36
static ITypeParameter ResolveBaseTypeParameter(IMethod parentMethod, int index)
38
IMethod baseMethod = null;
39
if (parentMethod.IsOverride) {
40
foreach (IMethod m in InheritanceHelper.GetBaseMembers(parentMethod, false).OfType<IMethod>()) {
46
} else if (parentMethod.IsExplicitInterfaceImplementation && parentMethod.ImplementedInterfaceMembers.Count == 1) {
47
baseMethod = parentMethod.ImplementedInterfaceMembers[0] as IMethod;
49
if (baseMethod != null && index < baseMethod.TypeParameters.Count)
50
return baseMethod.TypeParameters[index];
55
public override ITypeParameter CreateResolvedTypeParameter(ITypeResolveContext context)
57
if (context.CurrentMember is IMethod) {
58
return new ResolvedMethodTypeParameterWithInheritedConstraints(this, context);
60
return base.CreateResolvedTypeParameter(context);
64
sealed class ResolvedMethodTypeParameterWithInheritedConstraints : AbstractTypeParameter
66
volatile ITypeParameter baseTypeParameter;
68
public ResolvedMethodTypeParameterWithInheritedConstraints(MethodTypeParameterWithInheritedConstraints unresolved, ITypeResolveContext context)
69
: base(context.CurrentMember, unresolved.Index, unresolved.Name, unresolved.Variance,
70
unresolved.Attributes.CreateResolvedAttributes(context), unresolved.Region)
74
ITypeParameter GetBaseTypeParameter()
76
ITypeParameter baseTP = this.baseTypeParameter;
78
// ResolveBaseTypeParameter() is idempotent, so this is thread-safe.
79
this.baseTypeParameter = baseTP = ResolveBaseTypeParameter((IMethod)this.Owner, this.Index);
84
public override bool HasValueTypeConstraint {
86
ITypeParameter baseTP = GetBaseTypeParameter();
87
return baseTP != null ? baseTP.HasValueTypeConstraint : false;
91
public override bool HasReferenceTypeConstraint {
93
ITypeParameter baseTP = GetBaseTypeParameter();
94
return baseTP != null ? baseTP.HasReferenceTypeConstraint : false;
98
public override bool HasDefaultConstructorConstraint {
100
ITypeParameter baseTP = GetBaseTypeParameter();
101
return baseTP != null ? baseTP.HasDefaultConstructorConstraint : false;
105
public override IEnumerable<IType> DirectBaseTypes {
107
ITypeParameter baseTP = GetBaseTypeParameter();
108
if (baseTP != null) {
109
// Substitute occurrences of the base method's type parameters in the constraints
110
// with the type parameters from the
111
IMethod owner = (IMethod)this.Owner;
112
var substitution = new TypeParameterSubstitution(null, new ProjectedList<ITypeParameter, IType>(owner.TypeParameters, t => t));
113
return baseTP.DirectBaseTypes.Select(t => t.AcceptVisitor(substitution));
115
return EmptyList<IType>.Instance;