~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

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