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.Semantics; |
---|
23 | using ICSharpCode.NRefactory.TypeSystem; |
---|
24 | |
---|
25 | namespace ICSharpCode.NRefactory.CSharp.Resolver |
---|
26 | { |
---|
27 | /// <summary> |
---|
28 | /// Represents the result of a method, constructor or indexer invocation. |
---|
29 | /// Provides additional C#-specific information for InvocationResolveResult. |
---|
30 | /// </summary> |
---|
31 | public class CSharpInvocationResolveResult : InvocationResolveResult |
---|
32 | { |
---|
33 | public readonly OverloadResolutionErrors OverloadResolutionErrors; |
---|
34 | |
---|
35 | /// <summary> |
---|
36 | /// Gets whether this invocation is calling an extension method using extension method syntax. |
---|
37 | /// </summary> |
---|
38 | public readonly bool IsExtensionMethodInvocation; |
---|
39 | |
---|
40 | /// <summary> |
---|
41 | /// Gets whether this invocation is calling a delegate (without explicitly calling ".Invoke()"). |
---|
42 | /// </summary> |
---|
43 | public readonly bool IsDelegateInvocation; |
---|
44 | |
---|
45 | /// <summary> |
---|
46 | /// Gets whether a params-Array is being used in its expanded form. |
---|
47 | /// </summary> |
---|
48 | public readonly bool IsExpandedForm; |
---|
49 | |
---|
50 | readonly IList<int> argumentToParameterMap; |
---|
51 | |
---|
52 | /// <summary> |
---|
53 | /// If IsExtensionMethodInvocation is true this property holds the reduced method. |
---|
54 | /// </summary> |
---|
55 | IMethod reducedMethod; |
---|
56 | public IMethod ReducedMethod { |
---|
57 | get { |
---|
58 | if (!IsExtensionMethodInvocation) |
---|
59 | return null; |
---|
60 | if (reducedMethod == null && Member is IMethod) |
---|
61 | reducedMethod = new ReducedExtensionMethod ((IMethod)Member); |
---|
62 | return reducedMethod; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | public CSharpInvocationResolveResult( |
---|
67 | ResolveResult targetResult, IParameterizedMember member, |
---|
68 | IList<ResolveResult> arguments, |
---|
69 | OverloadResolutionErrors overloadResolutionErrors = OverloadResolutionErrors.None, |
---|
70 | bool isExtensionMethodInvocation = false, |
---|
71 | bool isExpandedForm = false, |
---|
72 | bool isDelegateInvocation = false, |
---|
73 | IList<int> argumentToParameterMap = null, |
---|
74 | IList<ResolveResult> initializerStatements = null, |
---|
75 | IType returnTypeOverride = null |
---|
76 | ) |
---|
77 | : base(targetResult, member, arguments, initializerStatements, returnTypeOverride) |
---|
78 | { |
---|
79 | this.OverloadResolutionErrors = overloadResolutionErrors; |
---|
80 | this.IsExtensionMethodInvocation = isExtensionMethodInvocation; |
---|
81 | this.IsExpandedForm = isExpandedForm; |
---|
82 | this.IsDelegateInvocation = isDelegateInvocation; |
---|
83 | this.argumentToParameterMap = argumentToParameterMap; |
---|
84 | } |
---|
85 | |
---|
86 | public override bool IsError { |
---|
87 | get { return this.OverloadResolutionErrors != OverloadResolutionErrors.None; } |
---|
88 | } |
---|
89 | |
---|
90 | /// <summary> |
---|
91 | /// Gets an array that maps argument indices to parameter indices. |
---|
92 | /// For arguments that could not be mapped to any parameter, the value will be -1. |
---|
93 | /// |
---|
94 | /// parameterIndex = ArgumentToParameterMap[argumentIndex] |
---|
95 | /// </summary> |
---|
96 | public IList<int> GetArgumentToParameterMap() |
---|
97 | { |
---|
98 | return argumentToParameterMap; |
---|
99 | } |
---|
100 | |
---|
101 | public override IList<ResolveResult> GetArgumentsForCall() |
---|
102 | { |
---|
103 | ResolveResult[] results = new ResolveResult[Member.Parameters.Count]; |
---|
104 | List<ResolveResult> paramsArguments = IsExpandedForm ? new List<ResolveResult>() : null; |
---|
105 | // map arguments to parameters: |
---|
106 | for (int i = 0; i < Arguments.Count; i++) { |
---|
107 | int mappedTo; |
---|
108 | if (argumentToParameterMap != null) |
---|
109 | mappedTo = argumentToParameterMap[i]; |
---|
110 | else |
---|
111 | mappedTo = IsExpandedForm ? Math.Min(i, results.Length - 1) : i; |
---|
112 | |
---|
113 | if (mappedTo >= 0 && mappedTo < results.Length) { |
---|
114 | if (IsExpandedForm && mappedTo == results.Length - 1) { |
---|
115 | paramsArguments.Add(Arguments[i]); |
---|
116 | } else { |
---|
117 | var narr = Arguments[i] as NamedArgumentResolveResult; |
---|
118 | if (narr != null) |
---|
119 | results[mappedTo] = narr.Argument; |
---|
120 | else |
---|
121 | results[mappedTo] = Arguments[i]; |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | if (IsExpandedForm){ |
---|
126 | IType arrayType = Member.Parameters.Last().Type; |
---|
127 | IType int32 = Member.Compilation.FindType(KnownTypeCode.Int32); |
---|
128 | ResolveResult[] sizeArguments = { new ConstantResolveResult(int32, paramsArguments.Count) }; |
---|
129 | results[results.Length - 1] = new ArrayCreateResolveResult(arrayType, sizeArguments, paramsArguments); |
---|
130 | } |
---|
131 | |
---|
132 | for (int i = 0; i < results.Length; i++) { |
---|
133 | if (results[i] == null) { |
---|
134 | if (Member.Parameters[i].IsOptional) { |
---|
135 | results[i] = new ConstantResolveResult(Member.Parameters[i].Type, Member.Parameters[i].ConstantValue); |
---|
136 | } else { |
---|
137 | results[i] = ErrorResolveResult.UnknownError; |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | return results; |
---|
143 | } |
---|
144 | } |
---|
145 | } |
---|