~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/TypeSystem/MethodTypeParameterWithInheritedConstraints.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
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:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
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.
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Linq;
 
22
using ICSharpCode.NRefactory.TypeSystem;
 
23
using ICSharpCode.NRefactory.TypeSystem.Implementation;
 
24
using ICSharpCode.NRefactory.Utils;
 
25
 
 
26
namespace ICSharpCode.NRefactory.CSharp.TypeSystem
 
27
{
 
28
        [Serializable]
 
29
        public sealed class MethodTypeParameterWithInheritedConstraints : DefaultUnresolvedTypeParameter
 
30
        {
 
31
                public MethodTypeParameterWithInheritedConstraints(int index, string name)
 
32
                        : base(EntityType.Method, index, name)
 
33
                {
 
34
                }
 
35
                
 
36
                static ITypeParameter ResolveBaseTypeParameter(IMethod parentMethod, int index)
 
37
                {
 
38
                        IMethod baseMethod = null;
 
39
                        if (parentMethod.IsOverride) {
 
40
                                foreach (IMethod m in InheritanceHelper.GetBaseMembers(parentMethod, false).OfType<IMethod>()) {
 
41
                                        if (!m.IsOverride) {
 
42
                                                baseMethod = m;
 
43
                                                break;
 
44
                                        }
 
45
                                }
 
46
                        } else if (parentMethod.IsExplicitInterfaceImplementation && parentMethod.ImplementedInterfaceMembers.Count == 1) {
 
47
                                baseMethod = parentMethod.ImplementedInterfaceMembers[0] as IMethod;
 
48
                        }
 
49
                        if (baseMethod != null && index < baseMethod.TypeParameters.Count)
 
50
                                return baseMethod.TypeParameters[index];
 
51
                        else
 
52
                                return null;
 
53
                }
 
54
                
 
55
                public override ITypeParameter CreateResolvedTypeParameter(ITypeResolveContext context)
 
56
                {
 
57
                        if (context.CurrentMember is IMethod) {
 
58
                                return new ResolvedMethodTypeParameterWithInheritedConstraints(this, context);
 
59
                        } else {
 
60
                                return base.CreateResolvedTypeParameter(context);
 
61
                        }
 
62
                }
 
63
                
 
64
                sealed class ResolvedMethodTypeParameterWithInheritedConstraints : AbstractTypeParameter
 
65
                {
 
66
                        volatile ITypeParameter baseTypeParameter;
 
67
                        
 
68
                        public ResolvedMethodTypeParameterWithInheritedConstraints(MethodTypeParameterWithInheritedConstraints unresolved, ITypeResolveContext context)
 
69
                                : base(context.CurrentMember, unresolved.Index, unresolved.Name, unresolved.Variance,
 
70
                                       unresolved.Attributes.CreateResolvedAttributes(context), unresolved.Region)
 
71
                        {
 
72
                        }
 
73
                        
 
74
                        ITypeParameter GetBaseTypeParameter()
 
75
                        {
 
76
                                ITypeParameter baseTP = this.baseTypeParameter;
 
77
                                if (baseTP == null) {
 
78
                                        // ResolveBaseTypeParameter() is idempotent, so this is thread-safe.
 
79
                                        this.baseTypeParameter = baseTP = ResolveBaseTypeParameter((IMethod)this.Owner, this.Index);
 
80
                                }
 
81
                                return baseTP;
 
82
                        }
 
83
                        
 
84
                        public override bool HasValueTypeConstraint {
 
85
                                get {
 
86
                                        ITypeParameter baseTP = GetBaseTypeParameter();
 
87
                                        return baseTP != null ? baseTP.HasValueTypeConstraint : false;
 
88
                                }
 
89
                        }
 
90
                        
 
91
                        public override bool HasReferenceTypeConstraint {
 
92
                                get {
 
93
                                        ITypeParameter baseTP = GetBaseTypeParameter();
 
94
                                        return baseTP != null ? baseTP.HasReferenceTypeConstraint : false;
 
95
                                }
 
96
                        }
 
97
                        
 
98
                        public override bool HasDefaultConstructorConstraint {
 
99
                                get {
 
100
                                        ITypeParameter baseTP = GetBaseTypeParameter();
 
101
                                        return baseTP != null ? baseTP.HasDefaultConstructorConstraint : false;
 
102
                                }
 
103
                        }
 
104
                        
 
105
                        public override IEnumerable<IType> DirectBaseTypes {
 
106
                                get {
 
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));
 
114
                                        } else {
 
115
                                                return EmptyList<IType>.Instance;
 
116
                                        }
 
117
                                }
 
118
                        }
 
119
                }
 
120
        }
 
121
}