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;
24
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
27
/// Default implementation of <see cref="IUnresolvedMethod" /> interface.
30
public class DefaultUnresolvedMethod : AbstractUnresolvedMember, IUnresolvedMethod
32
IList<IUnresolvedAttribute> returnTypeAttributes;
33
IList<IUnresolvedTypeParameter> typeParameters;
34
IList<IUnresolvedParameter> parameters;
35
IUnresolvedMember accessorOwner;
37
protected override void FreezeInternal()
39
returnTypeAttributes = FreezableHelper.FreezeListAndElements(returnTypeAttributes);
40
typeParameters = FreezableHelper.FreezeListAndElements(typeParameters);
41
parameters = FreezableHelper.FreezeListAndElements(parameters);
42
base.FreezeInternal();
45
public override object Clone()
47
var copy = (DefaultUnresolvedMethod)base.Clone();
48
if (returnTypeAttributes != null)
49
copy.returnTypeAttributes = new List<IUnresolvedAttribute>(returnTypeAttributes);
50
if (typeParameters != null)
51
copy.typeParameters = new List<IUnresolvedTypeParameter>(typeParameters);
52
if (parameters != null)
53
copy.parameters = new List<IUnresolvedParameter>(parameters);
57
public override void ApplyInterningProvider(InterningProvider provider)
59
base.ApplyInterningProvider(provider);
60
if (provider != null) {
61
returnTypeAttributes = provider.InternList(returnTypeAttributes);
62
typeParameters = provider.InternList(typeParameters);
63
parameters = provider.InternList(parameters);
67
public DefaultUnresolvedMethod()
69
this.EntityType = EntityType.Method;
72
public DefaultUnresolvedMethod(IUnresolvedTypeDefinition declaringType, string name)
74
this.EntityType = EntityType.Method;
75
this.DeclaringTypeDefinition = declaringType;
77
if (declaringType != null)
78
this.UnresolvedFile = declaringType.UnresolvedFile;
81
public IList<IUnresolvedAttribute> ReturnTypeAttributes {
83
if (returnTypeAttributes == null)
84
returnTypeAttributes = new List<IUnresolvedAttribute>();
85
return returnTypeAttributes;
89
public IList<IUnresolvedTypeParameter> TypeParameters {
91
if (typeParameters == null)
92
typeParameters = new List<IUnresolvedTypeParameter>();
93
return typeParameters;
97
public bool IsExtensionMethod {
98
get { return flags[FlagExtensionMethod]; }
101
flags[FlagExtensionMethod] = value;
105
public bool IsConstructor {
106
get { return this.EntityType == EntityType.Constructor; }
109
public bool IsDestructor {
110
get { return this.EntityType == EntityType.Destructor; }
113
public bool IsOperator {
114
get { return this.EntityType == EntityType.Operator; }
117
public bool IsPartial {
118
get { return flags[FlagPartialMethod]; }
121
flags[FlagPartialMethod] = value;
125
public bool IsAsync {
126
get { return flags[FlagAsyncMethod]; }
129
flags[FlagAsyncMethod] = value;
133
public bool HasBody {
134
get { return flags[FlagHasBody]; }
137
flags[FlagHasBody] = value;
142
public bool IsPartialMethodDeclaration {
143
get { return IsPartial && !HasBody; }
148
} else if (!value && IsPartial && !HasBody) {
155
public bool IsPartialMethodImplementation {
156
get { return IsPartial && HasBody; }
161
} else if (!value && IsPartial && HasBody) {
167
public IList<IUnresolvedParameter> Parameters {
169
if (parameters == null)
170
parameters = new List<IUnresolvedParameter>();
175
public IUnresolvedMember AccessorOwner {
176
get { return accessorOwner; }
179
accessorOwner = value;
183
public override string ToString()
185
StringBuilder b = new StringBuilder("[");
186
b.Append(EntityType.ToString());
188
if (DeclaringTypeDefinition != null) {
189
b.Append(DeclaringTypeDefinition.Name);
194
b.Append(string.Join(", ", this.Parameters));
196
b.Append(ReturnType.ToString());
201
public override IMember CreateResolved(ITypeResolveContext context)
203
return new DefaultResolvedMethod(this, context);
206
public override IMember Resolve(ITypeResolveContext context)
208
if (accessorOwner != null) {
209
var owner = accessorOwner.Resolve(context);
211
IProperty p = owner as IProperty;
213
if (p.CanGet && p.Getter.Name == this.Name)
215
if (p.CanSet && p.Setter.Name == this.Name)
218
IEvent e = owner as IEvent;
220
if (e.CanAdd && e.AddAccessor.Name == this.Name)
221
return e.AddAccessor;
222
if (e.CanRemove && e.RemoveAccessor.Name == this.Name)
223
return e.RemoveAccessor;
224
if (e.CanInvoke && e.InvokeAccessor.Name == this.Name)
225
return e.InvokeAccessor;
231
ITypeReference interfaceTypeReference = null;
232
if (this.IsExplicitInterfaceImplementation && this.ExplicitInterfaceImplementations.Count == 1)
233
interfaceTypeReference = this.ExplicitInterfaceImplementations[0].DeclaringTypeReference;
234
return Resolve(ExtendContextForType(context, this.DeclaringTypeDefinition),
235
this.EntityType, this.Name, interfaceTypeReference,
236
this.TypeParameters.Select(tp => tp.Name).ToList(),
237
this.Parameters.Select(p => p.Type).ToList());
240
IMethod IUnresolvedMethod.Resolve(ITypeResolveContext context)
242
return (IMethod)Resolve(context);
245
public static DefaultUnresolvedMethod CreateDefaultConstructor(IUnresolvedTypeDefinition typeDefinition)
247
if (typeDefinition == null)
248
throw new ArgumentNullException("typeDefinition");
249
DomRegion region = typeDefinition.Region;
250
region = new DomRegion(region.FileName, region.BeginLine, region.BeginColumn); // remove endline/endcolumn
251
return new DefaultUnresolvedMethod(typeDefinition, ".ctor") {
252
EntityType = EntityType.Constructor,
253
Accessibility = typeDefinition.IsAbstract ? Accessibility.Protected : Accessibility.Public,
258
ReturnType = KnownTypeReference.Void
262
static readonly IUnresolvedMethod dummyConstructor = CreateDummyConstructor();
265
/// Returns a dummy constructor instance:
268
/// A public instance constructor with IsSynthetic=true and no declaring type.
270
public static IUnresolvedMethod DummyConstructor {
271
get { return dummyConstructor; }
274
static IUnresolvedMethod CreateDummyConstructor()
276
var m = new DefaultUnresolvedMethod {
277
EntityType = EntityType.Constructor,
279
Accessibility = Accessibility.Public,
281
ReturnType = KnownTypeReference.Void