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

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/Resolver/MethodGroupResolveResult.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) 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.Collections.ObjectModel;
22
 
using System.Diagnostics;
23
 
using System.Linq;
24
 
using System.Text;
25
 
using ICSharpCode.NRefactory.CSharp.TypeSystem;
26
 
using ICSharpCode.NRefactory.Semantics;
27
 
using ICSharpCode.NRefactory.TypeSystem;
28
 
using ICSharpCode.NRefactory.TypeSystem.Implementation;
29
 
 
30
 
namespace ICSharpCode.NRefactory.CSharp.Resolver
31
 
{
32
 
        public class MethodListWithDeclaringType : List<IParameterizedMember>
33
 
        {
34
 
                readonly IType declaringType;
35
 
                
36
 
                public IType DeclaringType {
37
 
                        get { return declaringType; }
38
 
                }
39
 
                
40
 
                public MethodListWithDeclaringType(IType declaringType)
41
 
                {
42
 
                        this.declaringType = declaringType;
43
 
                }
44
 
                
45
 
                public MethodListWithDeclaringType(IType declaringType, IEnumerable<IParameterizedMember> methods)
46
 
                        : base(methods)
47
 
                {
48
 
                        this.declaringType = declaringType;
49
 
                }
50
 
        }
51
 
        
52
 
        /// <summary>
53
 
        /// Represents a group of methods.
54
 
        /// </summary>
55
 
        public class MethodGroupResolveResult : ResolveResult
56
 
        {
57
 
                readonly IList<MethodListWithDeclaringType> methodLists;
58
 
                readonly IList<IType> typeArguments;
59
 
                readonly ResolveResult targetResult;
60
 
                readonly string methodName;
61
 
                
62
 
                public MethodGroupResolveResult(ResolveResult targetResult, string methodName, IList<MethodListWithDeclaringType> methods, IList<IType> typeArguments) : base(SpecialType.UnknownType)
63
 
                {
64
 
                        if (targetResult == null)
65
 
                                throw new ArgumentNullException("targetResult");
66
 
                        if (methods == null)
67
 
                                throw new ArgumentNullException("methods");
68
 
                        this.targetResult = targetResult;
69
 
                        this.methodName = methodName;
70
 
                        this.methodLists = methods;
71
 
                        this.typeArguments = typeArguments ?? EmptyList<IType>.Instance;
72
 
                }
73
 
                
74
 
                /// <summary>
75
 
                /// Gets the resolve result for the target object.
76
 
                /// </summary>
77
 
                public ResolveResult TargetResult {
78
 
                        get { return targetResult; }
79
 
                }
80
 
                
81
 
                /// <summary>
82
 
                /// Gets the type of the reference to the target object.
83
 
                /// </summary>
84
 
                public IType TargetType {
85
 
                        get { return targetResult.Type; }
86
 
                }
87
 
                
88
 
                /// <summary>
89
 
                /// Gets the name of the methods in this group.
90
 
                /// </summary>
91
 
                public string MethodName {
92
 
                        get { return methodName; }
93
 
                }
94
 
                
95
 
                /// <summary>
96
 
                /// Gets the methods that were found.
97
 
                /// This list does not include extension methods.
98
 
                /// </summary>
99
 
                public IEnumerable<IMethod> Methods {
100
 
                        get { return methodLists.SelectMany(m => m.Cast<IMethod>()); }
101
 
                }
102
 
                
103
 
                /// <summary>
104
 
                /// Gets the methods that were found, grouped by their declaring type.
105
 
                /// This list does not include extension methods.
106
 
                /// Base types come first in the list.
107
 
                /// </summary>
108
 
                public IEnumerable<MethodListWithDeclaringType> MethodsGroupedByDeclaringType {
109
 
                        get { return methodLists; }
110
 
                }
111
 
                
112
 
                /// <summary>
113
 
                /// Gets the type arguments that were explicitly provided.
114
 
                /// </summary>
115
 
                public IList<IType> TypeArguments {
116
 
                        get { return typeArguments; }
117
 
                }
118
 
                
119
 
                /// <summary>
120
 
                /// List of extension methods, used to avoid re-calculating it in ResolveInvocation() when it was already
121
 
                /// calculated by ResolveMemberAccess().
122
 
                /// </summary>
123
 
                internal List<List<IMethod>> extensionMethods;
124
 
                
125
 
                // the resolver is used to fetch extension methods on demand
126
 
                internal CSharpResolver resolver;
127
 
                
128
 
                /// <summary>
129
 
                /// Gets all candidate extension methods.
130
 
                /// Note: this includes candidates that are not eligible due to an inapplicable
131
 
                /// this argument.
132
 
                /// </summary>
133
 
                /// <remarks>
134
 
                /// The results are stored in nested lists because they are grouped by using scope.
135
 
                /// That is, for "using SomeExtensions; namespace X { using MoreExtensions; ... }",
136
 
                /// the return value will be
137
 
                /// new List {
138
 
                ///    new List { all extensions from MoreExtensions },
139
 
                ///    new List { all extensions from SomeExtensions }
140
 
                /// }
141
 
                /// </remarks>
142
 
                public IEnumerable<IEnumerable<IMethod>> GetExtensionMethods()
143
 
                {
144
 
                        if (resolver != null) {
145
 
                                Debug.Assert(extensionMethods == null);
146
 
                                try {
147
 
                                        extensionMethods = resolver.GetExtensionMethods(methodName, typeArguments);
148
 
                                } finally {
149
 
                                        resolver = null;
150
 
                                }
151
 
                        }
152
 
                        return extensionMethods ?? Enumerable.Empty<IEnumerable<IMethod>>();
153
 
                }
154
 
                
155
 
                public IEnumerable<IEnumerable<IMethod>> GetEligibleExtensionMethods(bool substituteInferredTypes)
156
 
                {
157
 
                        var result = new List<List<IMethod>>();
158
 
                        foreach (var methodGroup in GetExtensionMethods()) {
159
 
                                var outputGroup = new List<IMethod>();
160
 
                                foreach (var method in methodGroup) {
161
 
                                        IType[] inferredTypes;
162
 
                                        if (CSharpResolver.IsEligibleExtensionMethod(this.TargetType, method, true, out inferredTypes)) {
163
 
                                                if (substituteInferredTypes && inferredTypes != null) {
164
 
                                                        outputGroup.Add(new SpecializedMethod(method, new TypeParameterSubstitution(null, inferredTypes)));
165
 
                                                } else {
166
 
                                                        outputGroup.Add(method);
167
 
                                                }
168
 
                                        }
169
 
                                }
170
 
                                if (outputGroup.Count > 0)
171
 
                                        result.Add(outputGroup);
172
 
                        }
173
 
                        return result;
174
 
                }
175
 
                
176
 
                public override string ToString()
177
 
                {
178
 
                        return string.Format("[{0} with {1} method(s)]", GetType().Name, this.Methods.Count());
179
 
                }
180
 
                
181
 
                public OverloadResolution PerformOverloadResolution(ICompilation compilation, ResolveResult[] arguments, string[] argumentNames = null, bool allowExtensionMethods = true, bool allowExpandingParams = true, CSharpConversions conversions = null)
182
 
                {
183
 
                        Log.WriteLine("Performing overload resolution for " + this);
184
 
                        Log.WriteCollection("  Arguments: ", arguments);
185
 
                        
186
 
                        var typeArgumentArray = this.TypeArguments.ToArray();
187
 
                        OverloadResolution or = new OverloadResolution(compilation, arguments, argumentNames, typeArgumentArray, conversions);
188
 
                        or.AllowExpandingParams = allowExpandingParams;
189
 
                        
190
 
                        or.AddMethodLists(methodLists);
191
 
                        
192
 
                        if (allowExtensionMethods && !or.FoundApplicableCandidate) {
193
 
                                // No applicable match found, so let's try extension methods.
194
 
                                
195
 
                                var extensionMethods = this.GetExtensionMethods();
196
 
                                
197
 
                                if (extensionMethods.Any()) {
198
 
                                        Log.WriteLine("No candidate is applicable, trying {0} extension methods groups...", extensionMethods.Count());
199
 
                                        ResolveResult[] extArguments = new ResolveResult[arguments.Length + 1];
200
 
                                        extArguments[0] = new ResolveResult(this.TargetType);
201
 
                                        arguments.CopyTo(extArguments, 1);
202
 
                                        string[] extArgumentNames = null;
203
 
                                        if (argumentNames != null) {
204
 
                                                extArgumentNames = new string[argumentNames.Length + 1];
205
 
                                                argumentNames.CopyTo(extArgumentNames, 1);
206
 
                                        }
207
 
                                        var extOr = new OverloadResolution(compilation, extArguments, extArgumentNames, typeArgumentArray, conversions);
208
 
                                        extOr.AllowExpandingParams = allowExpandingParams;
209
 
                                        extOr.IsExtensionMethodInvocation = true;
210
 
                                        
211
 
                                        foreach (var g in extensionMethods) {
212
 
                                                foreach (var method in g) {
213
 
                                                        Log.Indent();
214
 
                                                        OverloadResolutionErrors errors = extOr.AddCandidate(method);
215
 
                                                        Log.Unindent();
216
 
                                                        or.LogCandidateAddingResult("  Extension", method, errors);
217
 
                                                }
218
 
                                                if (extOr.FoundApplicableCandidate)
219
 
                                                        break;
220
 
                                        }
221
 
                                        // For the lack of a better comparison function (the one within OverloadResolution
222
 
                                        // cannot be used as it depends on the argument set):
223
 
                                        if (extOr.FoundApplicableCandidate || or.BestCandidate == null) {
224
 
                                                // Consider an extension method result better than the normal result only
225
 
                                                // if it's applicable; or if there is no normal result.
226
 
                                                or = extOr;
227
 
                                        }
228
 
                                }
229
 
                        }
230
 
                        Log.WriteLine("Overload resolution finished, best candidate is {0}.", or.GetBestCandidateWithSubstitutedTypeArguments());
231
 
                        return or;
232
 
                }
233
 
                
234
 
                public override IEnumerable<ResolveResult> GetChildResults()
235
 
                {
236
 
                        if (targetResult != null)
237
 
                                return new[] { targetResult };
238
 
                        else
239
 
                                return Enumerable.Empty<ResolveResult>();
240
 
                }
241
 
        }
242
 
}