Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory-5.5.0/TypeSystem/Implementation/GetClassTypeReference.cs @ 11937

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

#2077: created branch and added first version

File size: 6.7 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.Linq;
21using System.Threading;
22
23namespace ICSharpCode.NRefactory.TypeSystem.Implementation
24{
25  /// <summary>
26  /// Type Reference used when the fully qualified type name is known.
27  /// </summary>
28  [Serializable]
29  public sealed class GetClassTypeReference : ITypeReference, ISymbolReference, ISupportsInterning
30  {
31    readonly IAssemblyReference assembly;
32    readonly FullTypeName fullTypeName;
33   
34    /// <summary>
35    /// Creates a new GetClassTypeReference that searches a type definition.
36    /// </summary>
37    /// <param name="fullTypeName">The full name of the type.</param>
38    /// <param name="assembly">A reference to the assembly containing this type.
39    /// If this parameter is null, the GetClassTypeReference will search in all
40    /// assemblies belonging to the compilation.
41    /// </param>
42    public GetClassTypeReference(FullTypeName fullTypeName, IAssemblyReference assembly = null)
43    {
44      this.fullTypeName = fullTypeName;
45      this.assembly = assembly;
46    }
47   
48    /// <summary>
49    /// Creates a new GetClassTypeReference that searches a top-level type in all assemblies.
50    /// </summary>
51    /// <param name="namespaceName">The namespace name containing the type, e.g. "System.Collections.Generic".</param>
52    /// <param name="name">The name of the type, e.g. "List".</param>
53    /// <param name="typeParameterCount">The number of type parameters, (e.g. 1 for List&lt;T&gt;).</param>
54    public GetClassTypeReference(string namespaceName, string name, int typeParameterCount = 0)
55    {
56      this.fullTypeName = new TopLevelTypeName(namespaceName, name, typeParameterCount);
57    }
58   
59    /// <summary>
60    /// Creates a new GetClassTypeReference that searches a top-level type in the specified assembly.
61    /// </summary>
62    /// <param name="assembly">A reference to the assembly containing this type.
63    /// If this parameter is null, the GetClassTypeReference will search in all assemblies belonging to the ICompilation.</param>
64    /// <param name="namespaceName">The namespace name containing the type, e.g. "System.Collections.Generic".</param>
65    /// <param name="name">The name of the type, e.g. "List".</param>
66    /// <param name="typeParameterCount">The number of type parameters, (e.g. 1 for List&lt;T&gt;).</param>
67    public GetClassTypeReference(IAssemblyReference assembly, string namespaceName, string name, int typeParameterCount = 0)
68    {
69      this.assembly = assembly;
70      this.fullTypeName = new TopLevelTypeName(namespaceName, name, typeParameterCount);
71    }
72   
73    /// <summary>
74    /// Gets the assembly reference.
75    /// This property returns null if the GetClassTypeReference is searching in all assemblies
76    /// of the compilation.
77    /// </summary>
78    public IAssemblyReference Assembly { get { return assembly; } }
79   
80    /// <summary>
81    /// Gets the full name of the type this reference is searching for.
82    /// </summary>
83    public FullTypeName FullTypeName { get { return fullTypeName; } }
84   
85    [Obsolete("Use the FullTypeName property instead. GetClassTypeReference now supports nested types, where the Namespace/Name/TPC tripel isn't sufficient for identifying the type.")]
86    public string Namespace { get { return fullTypeName.TopLevelTypeName.Namespace; } }
87    [Obsolete("Use the FullTypeName property instead. GetClassTypeReference now supports nested types, where the Namespace/Name/TPC tripel isn't sufficient for identifying the type.")]
88    public string Name { get { return fullTypeName.Name; } }
89    [Obsolete("Use the FullTypeName property instead. GetClassTypeReference now supports nested types, where the Namespace/Name/TPC tripel isn't sufficient for identifying the type.")]
90    public int TypeParameterCount { get { return fullTypeName.TypeParameterCount; } }
91
92    IType ResolveInAllAssemblies(ITypeResolveContext context)
93    {
94      var compilation = context.Compilation;
95      foreach (var asm in compilation.Assemblies) {
96        IType type = asm.GetTypeDefinition(fullTypeName);
97        if (type != null)
98          return type;
99      }
100      return null;
101    }
102
103    public IType Resolve(ITypeResolveContext context)
104    {
105      if (context == null)
106        throw new ArgumentNullException("context");
107     
108      IType type = null;
109      if (assembly == null) {
110        // No assembly specified: look in all assemblies, but prefer the current assembly
111        if (context.CurrentAssembly != null) {
112          type = context.CurrentAssembly.GetTypeDefinition(fullTypeName);
113        }
114        if (type == null) {
115          type = ResolveInAllAssemblies(context);
116        }
117      } else {
118        // Assembly specified: only look in the specified assembly.
119        // But if that's not loaded in the compilation, allow fall back to other assemblies.
120        // (the non-loaded assembly might be a facade containing type forwarders -
121        //  for example, when referencing a portable library from a non-portable project)
122        IAssembly asm = assembly.Resolve(context);
123        if (asm != null) {
124          type = asm.GetTypeDefinition(fullTypeName);
125        } else {
126          type = ResolveInAllAssemblies(context);
127        }
128      }
129      return type ?? new UnknownType(fullTypeName);
130    }
131   
132    ISymbol ISymbolReference.Resolve(ITypeResolveContext context)
133    {
134      var type = Resolve(context);
135      if (type is ITypeDefinition)
136        return (ISymbol)type;
137      return null;
138    }
139   
140    public override string ToString()
141    {
142      return fullTypeName.ToString() + (assembly != null ? ", " + assembly.ToString() : null);
143    }
144   
145    int ISupportsInterning.GetHashCodeForInterning()
146    {
147      unchecked {
148        return 33 * assembly.GetHashCode() + fullTypeName.GetHashCode();
149      }
150    }
151   
152    bool ISupportsInterning.EqualsForInterning(ISupportsInterning other)
153    {
154      GetClassTypeReference o = other as GetClassTypeReference;
155      return o != null && assembly == o.assembly && fullTypeName == o.fullTypeName;
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.