Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory-5.5.0/TypeSystem/Implementation/DefaultResolvedMethod.cs @ 11700

Last change on this file since 11700 was 11700, checked in by jkarder, 9 years ago

#2077: created branch and added first version

File size: 9.2 KB
Line 
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
19using System;
20using System.Collections.Generic;
21using System.Linq;
22using System.Text;
23
24using ICSharpCode.NRefactory.Semantics;
25
26namespace ICSharpCode.NRefactory.TypeSystem.Implementation
27{
28  /// <summary>
29  /// Default implementation of <see cref="IMethod"/> that resolves an unresolved method.
30  /// </summary>
31  public class DefaultResolvedMethod : AbstractResolvedMember, IMethod
32  {
33    IUnresolvedMethod[] parts;
34   
35    public DefaultResolvedMethod(DefaultUnresolvedMethod unresolved, ITypeResolveContext parentContext)
36      : this(unresolved, parentContext, unresolved.IsExtensionMethod)
37    {
38    }
39   
40    public DefaultResolvedMethod(IUnresolvedMethod unresolved, ITypeResolveContext parentContext, bool isExtensionMethod)
41      : base(unresolved, parentContext)
42    {
43      this.Parameters = unresolved.Parameters.CreateResolvedParameters(context);
44      this.ReturnTypeAttributes = unresolved.ReturnTypeAttributes.CreateResolvedAttributes(parentContext);
45      this.TypeParameters = unresolved.TypeParameters.CreateResolvedTypeParameters(context);
46      this.IsExtensionMethod = isExtensionMethod;
47    }
48
49    class ListOfLists<T> : IList<T>
50    {
51      List<IList<T>> lists =new List<IList<T>> ();
52
53      public void AddList(IList<T> list)
54      {
55        lists.Add (list);
56      }
57
58      #region IEnumerable implementation
59      System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
60      {
61        return GetEnumerator();
62      }
63      #endregion
64
65      #region IEnumerable implementation
66      public IEnumerator<T> GetEnumerator ()
67      {
68        for (int i = 0; i < this.Count; i++) {
69          yield return this[i];
70        }
71      }
72      #endregion
73
74      #region ICollection implementation
75      public void Add (T item)
76      {
77        throw new NotSupportedException();
78      }
79
80      public void Clear ()
81      {
82        throw new NotSupportedException();
83      }
84
85      public bool Contains (T item)
86      {
87        var comparer = EqualityComparer<T>.Default;
88        for (int i = 0; i < this.Count; i++) {
89          if (comparer.Equals(this[i], item))
90            return true;
91        }
92        return false;
93      }
94
95      public void CopyTo (T[] array, int arrayIndex)
96      {
97        for (int i = 0; i < Count; i++) {
98          array[arrayIndex + i] = this[i];
99        }
100      }
101
102      public bool Remove (T item)
103      {
104        throw new NotSupportedException();
105      }
106
107      public int Count {
108        get {
109          return lists.Sum (l => l.Count);
110        }
111      }
112
113      public bool IsReadOnly {
114        get {
115          return true;
116        }
117      }
118      #endregion
119
120      #region IList implementation
121      public int IndexOf (T item)
122      {
123        var comparer = EqualityComparer<T>.Default;
124        for (int i = 0; i < this.Count; i++) {
125          if (comparer.Equals(this[i], item))
126            return i;
127        }
128        return -1;
129      }
130
131      public void Insert (int index, T item)
132      {
133        throw new NotSupportedException();
134      }
135
136      public void RemoveAt (int index)
137      {
138        throw new NotSupportedException();
139      }
140
141      public T this[int index] {
142        get {
143          foreach (var list in lists){
144            if (index < list.Count)
145              return list[index];
146            index -= list.Count;
147          }
148          throw new IndexOutOfRangeException ();
149        }
150        set {
151          throw new NotSupportedException();
152        }
153      }
154      #endregion
155    }
156
157    public static DefaultResolvedMethod CreateFromMultipleParts(IUnresolvedMethod[] parts, ITypeResolveContext[] contexts, bool isExtensionMethod)
158    {
159      DefaultResolvedMethod method = new DefaultResolvedMethod(parts[0], contexts[0], isExtensionMethod);
160      method.parts = parts;
161      if (parts.Length > 1) {
162        var attrs = new ListOfLists <IAttribute>();
163        attrs.AddList (method.Attributes);
164        for (int i = 1; i < parts.Length; i++) {
165          attrs.AddList (parts[i].Attributes.CreateResolvedAttributes(contexts[i]));
166        }
167        method.Attributes = attrs;
168      }
169      return method;
170    }
171   
172    public IList<IParameter> Parameters { get; private set; }
173    public IList<IAttribute> ReturnTypeAttributes { get; private set; }
174    public IList<ITypeParameter> TypeParameters { get; private set; }
175
176    public IList<IType> TypeArguments {
177      get {
178        // ToList() call is necessary because IList<> isn't covariant
179        return TypeParameters.ToList<IType>();
180      }
181    }
182   
183    bool IMethod.IsParameterized {
184      get { return false; }
185    }
186
187    public bool IsExtensionMethod { get; private set; }
188   
189    public IList<IUnresolvedMethod> Parts {
190      get {
191        return parts ?? new IUnresolvedMethod[] { (IUnresolvedMethod)unresolved };
192      }
193    }
194   
195    public bool IsConstructor {
196      get { return ((IUnresolvedMethod)unresolved).IsConstructor; }
197    }
198   
199    public bool IsDestructor {
200      get { return ((IUnresolvedMethod)unresolved).IsDestructor; }
201    }
202   
203    public bool IsOperator {
204      get { return ((IUnresolvedMethod)unresolved).IsOperator; }
205    }
206   
207    public bool IsPartial {
208      get { return ((IUnresolvedMethod)unresolved).IsPartial; }
209    }
210
211    public bool IsAsync {
212      get { return ((IUnresolvedMethod)unresolved).IsAsync; }
213    }
214
215    public bool HasBody {
216      get { return ((IUnresolvedMethod)unresolved).HasBody; }
217    }
218   
219    public bool IsAccessor {
220      get { return ((IUnresolvedMethod)unresolved).AccessorOwner != null; }
221    }
222
223    IMethod IMethod.ReducedFrom {
224      get { return null; }
225    }
226
227    public virtual IMember AccessorOwner {
228      get {
229        var reference = ((IUnresolvedMethod)unresolved).AccessorOwner;
230        if (reference != null)
231          return reference.Resolve(context);
232        else
233          return null;
234      }
235    }
236   
237    public override ISymbolReference ToReference()
238    {
239      var declTypeRef = this.DeclaringType.ToTypeReference();
240      if (IsExplicitInterfaceImplementation && ImplementedInterfaceMembers.Count == 1) {
241        return new ExplicitInterfaceImplementationMemberReference(declTypeRef, ImplementedInterfaceMembers[0].ToReference());
242      } else {
243        return new DefaultMemberReference(
244          this.SymbolKind, declTypeRef, this.Name, this.TypeParameters.Count,
245          this.Parameters.Select(p => p.Type.ToTypeReference()).ToList());
246      }
247    }
248   
249    public override IMemberReference ToMemberReference()
250    {
251      return (IMemberReference)ToReference();
252    }
253   
254    public override IMember Specialize(TypeParameterSubstitution substitution)
255    {
256      if (TypeParameterSubstitution.Identity.Equals(substitution))
257        return this;
258      return new SpecializedMethod(this, substitution);
259    }
260   
261    IMethod IMethod.Specialize(TypeParameterSubstitution substitution)
262    {
263      if (TypeParameterSubstitution.Identity.Equals(substitution))
264        return this;
265      return new SpecializedMethod(this, substitution);
266    }
267   
268    public override string ToString()
269    {
270      StringBuilder b = new StringBuilder("[");
271      b.Append(this.SymbolKind);
272      b.Append(' ');
273      if (this.DeclaringType.Kind != TypeKind.Unknown) {
274        b.Append(this.DeclaringType.ReflectionName);
275        b.Append('.');
276      }
277      b.Append(this.Name);
278      if (this.TypeParameters.Count > 0) {
279        b.Append("``");
280        b.Append(this.TypeParameters.Count);
281      }
282      b.Append('(');
283      for (int i = 0; i < this.Parameters.Count; i++) {
284        if (i > 0) b.Append(", ");
285        b.Append(this.Parameters[i].ToString());
286      }
287      b.Append("):");
288      b.Append(this.ReturnType.ReflectionName);
289      b.Append(']');
290      return b.ToString();
291    }
292   
293    /// <summary>
294    /// Gets a dummy constructor for the specified compilation.
295    /// </summary>
296    /// <returns>
297    /// A public instance constructor with IsSynthetic=true and no declaring type.
298    /// </returns>
299    /// <seealso cref="DefaultUnresolvedMethod.DummyConstructor"/>
300    public static IMethod GetDummyConstructor(ICompilation compilation)
301    {
302      var dummyConstructor = DefaultUnresolvedMethod.DummyConstructor;
303      // Reuse the same IMethod instance for all dummy constructors
304      // so that two occurrences of 'new T()' refer to the same constructor.
305      return (IMethod)compilation.CacheManager.GetOrAddShared(
306        dummyConstructor, _ => dummyConstructor.CreateResolved(compilation.TypeResolveContext));
307    }
308   
309    /// <summary>
310    /// Gets a dummy constructor for the specified type.
311    /// </summary>
312    /// <returns>
313    /// A public instance constructor with IsSynthetic=true and the specified declaring type.
314    /// </returns>
315    /// <seealso cref="DefaultUnresolvedMethod.DummyConstructor"/>
316    public static IMethod GetDummyConstructor(ICompilation compilation, IType declaringType)
317    {
318      var resolvedCtor = GetDummyConstructor(compilation);
319      return new SpecializedMethod(resolvedCtor, TypeParameterSubstitution.Identity) { DeclaringType = declaringType };
320    }
321  }
322}
Note: See TracBrowser for help on using the repository browser.