1
// Copyright (c) 2010-2013 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.Semantics;
23
using ICSharpCode.NRefactory.TypeSystem;
25
namespace ICSharpCode.NRefactory.CSharp.Resolver
28
/// Represents the result of a method, constructor or indexer invocation.
29
/// Provides additional C#-specific information for InvocationResolveResult.
31
public class CSharpInvocationResolveResult : InvocationResolveResult
33
public readonly OverloadResolutionErrors OverloadResolutionErrors;
36
/// Gets whether this invocation is calling an extension method using extension method syntax.
38
public readonly bool IsExtensionMethodInvocation;
41
/// Gets whether this invocation is calling a delegate (without explicitly calling ".Invoke()").
43
public readonly bool IsDelegateInvocation;
46
/// Gets whether a params-Array is being used in its expanded form.
48
public readonly bool IsExpandedForm;
50
readonly IList<int> argumentToParameterMap;
53
/// If IsExtensionMethodInvocation is true this property holds the reduced method.
55
IMethod reducedMethod;
56
public IMethod ReducedMethod {
58
if (!IsExtensionMethodInvocation)
60
if (reducedMethod == null && Member is IMethod)
61
reducedMethod = new ReducedExtensionMethod ((IMethod)Member);
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
77
: base(targetResult, member, arguments, initializerStatements, returnTypeOverride)
79
this.OverloadResolutionErrors = overloadResolutionErrors;
80
this.IsExtensionMethodInvocation = isExtensionMethodInvocation;
81
this.IsExpandedForm = isExpandedForm;
82
this.IsDelegateInvocation = isDelegateInvocation;
83
this.argumentToParameterMap = argumentToParameterMap;
86
public override bool IsError {
87
get { return this.OverloadResolutionErrors != OverloadResolutionErrors.None; }
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.
94
/// parameterIndex = ArgumentToParameterMap[argumentIndex]
96
public IList<int> GetArgumentToParameterMap()
98
return argumentToParameterMap;
101
public override IList<ResolveResult> GetArgumentsForCall()
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++) {
108
if (argumentToParameterMap != null)
109
mappedTo = argumentToParameterMap[i];
111
mappedTo = IsExpandedForm ? Math.Min(i, results.Length - 1) : i;
113
if (mappedTo >= 0 && mappedTo < results.Length) {
114
if (IsExpandedForm && mappedTo == results.Length - 1) {
115
paramsArguments.Add(Arguments[i]);
117
var narr = Arguments[i] as NamedArgumentResolveResult;
119
results[mappedTo] = narr.Argument;
121
results[mappedTo] = Arguments[i];
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);
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);
137
results[i] = ErrorResolveResult.UnknownError;