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 System.Text; |
---|
23 | |
---|
24 | namespace ICSharpCode.NRefactory.TypeSystem.Implementation |
---|
25 | { |
---|
26 | /// <summary> |
---|
27 | /// Default implementation of <see cref="IUnresolvedMethod" /> interface. |
---|
28 | /// </summary> |
---|
29 | [Serializable] |
---|
30 | public class DefaultUnresolvedMethod : AbstractUnresolvedMember, IUnresolvedMethod |
---|
31 | { |
---|
32 | IList<IUnresolvedAttribute> returnTypeAttributes; |
---|
33 | IList<IUnresolvedTypeParameter> typeParameters; |
---|
34 | IList<IUnresolvedParameter> parameters; |
---|
35 | IUnresolvedMember accessorOwner; |
---|
36 | |
---|
37 | protected override void FreezeInternal() |
---|
38 | { |
---|
39 | returnTypeAttributes = FreezableHelper.FreezeListAndElements(returnTypeAttributes); |
---|
40 | typeParameters = FreezableHelper.FreezeListAndElements(typeParameters); |
---|
41 | parameters = FreezableHelper.FreezeListAndElements(parameters); |
---|
42 | base.FreezeInternal(); |
---|
43 | } |
---|
44 | |
---|
45 | public override object Clone() |
---|
46 | { |
---|
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); |
---|
54 | return copy; |
---|
55 | } |
---|
56 | |
---|
57 | public override void ApplyInterningProvider(InterningProvider provider) |
---|
58 | { |
---|
59 | base.ApplyInterningProvider(provider); |
---|
60 | if (provider != null) { |
---|
61 | returnTypeAttributes = provider.InternList(returnTypeAttributes); |
---|
62 | typeParameters = provider.InternList(typeParameters); |
---|
63 | parameters = provider.InternList(parameters); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | public DefaultUnresolvedMethod() |
---|
68 | { |
---|
69 | this.SymbolKind = SymbolKind.Method; |
---|
70 | } |
---|
71 | |
---|
72 | public DefaultUnresolvedMethod(IUnresolvedTypeDefinition declaringType, string name) |
---|
73 | { |
---|
74 | this.SymbolKind = SymbolKind.Method; |
---|
75 | this.DeclaringTypeDefinition = declaringType; |
---|
76 | this.Name = name; |
---|
77 | if (declaringType != null) |
---|
78 | this.UnresolvedFile = declaringType.UnresolvedFile; |
---|
79 | } |
---|
80 | |
---|
81 | public IList<IUnresolvedAttribute> ReturnTypeAttributes { |
---|
82 | get { |
---|
83 | if (returnTypeAttributes == null) |
---|
84 | returnTypeAttributes = new List<IUnresolvedAttribute>(); |
---|
85 | return returnTypeAttributes; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | public IList<IUnresolvedTypeParameter> TypeParameters { |
---|
90 | get { |
---|
91 | if (typeParameters == null) |
---|
92 | typeParameters = new List<IUnresolvedTypeParameter>(); |
---|
93 | return typeParameters; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | public bool IsExtensionMethod { |
---|
98 | get { return flags[FlagExtensionMethod]; } |
---|
99 | set { |
---|
100 | ThrowIfFrozen(); |
---|
101 | flags[FlagExtensionMethod] = value; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | public bool IsConstructor { |
---|
106 | get { return this.SymbolKind == SymbolKind.Constructor; } |
---|
107 | } |
---|
108 | |
---|
109 | public bool IsDestructor { |
---|
110 | get { return this.SymbolKind == SymbolKind.Destructor; } |
---|
111 | } |
---|
112 | |
---|
113 | public bool IsOperator { |
---|
114 | get { return this.SymbolKind == SymbolKind.Operator; } |
---|
115 | } |
---|
116 | |
---|
117 | public bool IsPartial { |
---|
118 | get { return flags[FlagPartialMethod]; } |
---|
119 | set { |
---|
120 | ThrowIfFrozen(); |
---|
121 | flags[FlagPartialMethod] = value; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | public bool IsAsync { |
---|
126 | get { return flags[FlagAsyncMethod]; } |
---|
127 | set { |
---|
128 | ThrowIfFrozen(); |
---|
129 | flags[FlagAsyncMethod] = value; |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | public bool HasBody { |
---|
134 | get { return flags[FlagHasBody]; } |
---|
135 | set { |
---|
136 | ThrowIfFrozen(); |
---|
137 | flags[FlagHasBody] = value; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | [Obsolete] |
---|
142 | public bool IsPartialMethodDeclaration { |
---|
143 | get { return IsPartial && !HasBody; } |
---|
144 | set { |
---|
145 | if (value) { |
---|
146 | IsPartial = true; |
---|
147 | HasBody = false; |
---|
148 | } else if (!value && IsPartial && !HasBody) { |
---|
149 | IsPartial = false; |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | [Obsolete] |
---|
155 | public bool IsPartialMethodImplementation { |
---|
156 | get { return IsPartial && HasBody; } |
---|
157 | set { |
---|
158 | if (value) { |
---|
159 | IsPartial = true; |
---|
160 | HasBody = true; |
---|
161 | } else if (!value && IsPartial && HasBody) { |
---|
162 | IsPartial = false; |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | public IList<IUnresolvedParameter> Parameters { |
---|
168 | get { |
---|
169 | if (parameters == null) |
---|
170 | parameters = new List<IUnresolvedParameter>(); |
---|
171 | return parameters; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | public IUnresolvedMember AccessorOwner { |
---|
176 | get { return accessorOwner; } |
---|
177 | set { |
---|
178 | ThrowIfFrozen(); |
---|
179 | accessorOwner = value; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | public override string ToString() |
---|
184 | { |
---|
185 | StringBuilder b = new StringBuilder("["); |
---|
186 | b.Append(SymbolKind.ToString()); |
---|
187 | b.Append(' '); |
---|
188 | if (DeclaringTypeDefinition != null) { |
---|
189 | b.Append(DeclaringTypeDefinition.Name); |
---|
190 | b.Append('.'); |
---|
191 | } |
---|
192 | b.Append(Name); |
---|
193 | b.Append('('); |
---|
194 | b.Append(string.Join(", ", this.Parameters)); |
---|
195 | b.Append("):"); |
---|
196 | b.Append(ReturnType.ToString()); |
---|
197 | b.Append(']'); |
---|
198 | return b.ToString(); |
---|
199 | } |
---|
200 | |
---|
201 | public override IMember CreateResolved(ITypeResolveContext context) |
---|
202 | { |
---|
203 | return new DefaultResolvedMethod(this, context); |
---|
204 | } |
---|
205 | |
---|
206 | public override IMember Resolve(ITypeResolveContext context) |
---|
207 | { |
---|
208 | if (accessorOwner != null) { |
---|
209 | var owner = accessorOwner.Resolve(context); |
---|
210 | if (owner != null) { |
---|
211 | IProperty p = owner as IProperty; |
---|
212 | if (p != null) { |
---|
213 | if (p.CanGet && p.Getter.Name == this.Name) |
---|
214 | return p.Getter; |
---|
215 | if (p.CanSet && p.Setter.Name == this.Name) |
---|
216 | return p.Setter; |
---|
217 | } |
---|
218 | IEvent e = owner as IEvent; |
---|
219 | if (e != null) { |
---|
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; |
---|
226 | } |
---|
227 | } |
---|
228 | return null; |
---|
229 | } |
---|
230 | |
---|
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.SymbolKind, this.Name, interfaceTypeReference, |
---|
236 | this.TypeParameters.Select(tp => tp.Name).ToList(), |
---|
237 | this.Parameters.Select(p => p.Type).ToList()); |
---|
238 | } |
---|
239 | |
---|
240 | IMethod IUnresolvedMethod.Resolve(ITypeResolveContext context) |
---|
241 | { |
---|
242 | return (IMethod)Resolve(context); |
---|
243 | } |
---|
244 | |
---|
245 | public static DefaultUnresolvedMethod CreateDefaultConstructor(IUnresolvedTypeDefinition typeDefinition) |
---|
246 | { |
---|
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 | SymbolKind = SymbolKind.Constructor, |
---|
253 | Accessibility = typeDefinition.IsAbstract ? Accessibility.Protected : Accessibility.Public, |
---|
254 | IsSynthetic = true, |
---|
255 | HasBody = true, |
---|
256 | Region = region, |
---|
257 | BodyRegion = region, |
---|
258 | ReturnType = KnownTypeReference.Void |
---|
259 | }; |
---|
260 | } |
---|
261 | |
---|
262 | static readonly IUnresolvedMethod dummyConstructor = CreateDummyConstructor(); |
---|
263 | |
---|
264 | /// <summary> |
---|
265 | /// Returns a dummy constructor instance: |
---|
266 | /// </summary> |
---|
267 | /// <returns> |
---|
268 | /// A public instance constructor with IsSynthetic=true and no declaring type. |
---|
269 | /// </returns> |
---|
270 | public static IUnresolvedMethod DummyConstructor { |
---|
271 | get { return dummyConstructor; } |
---|
272 | } |
---|
273 | |
---|
274 | static IUnresolvedMethod CreateDummyConstructor() |
---|
275 | { |
---|
276 | var m = new DefaultUnresolvedMethod { |
---|
277 | SymbolKind = SymbolKind.Constructor, |
---|
278 | Name = ".ctor", |
---|
279 | Accessibility = Accessibility.Public, |
---|
280 | IsSynthetic = true, |
---|
281 | ReturnType = KnownTypeReference.Void |
---|
282 | }; |
---|
283 | m.Freeze(); |
---|
284 | return m; |
---|
285 | } |
---|
286 | } |
---|
287 | } |
---|