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 ICSharpCode.NRefactory.CSharp.Resolver; |
---|
21 | using ICSharpCode.NRefactory.Semantics; |
---|
22 | using ICSharpCode.NRefactory.TypeSystem; |
---|
23 | |
---|
24 | namespace ICSharpCode.NRefactory.CSharp.TypeSystem |
---|
25 | { |
---|
26 | /// <summary> |
---|
27 | /// Represents a reference which could point to a type or namespace. |
---|
28 | /// </summary> |
---|
29 | [Serializable] |
---|
30 | public abstract class TypeOrNamespaceReference : ITypeReference |
---|
31 | { |
---|
32 | /// <summary> |
---|
33 | /// Resolves the reference and returns the ResolveResult. |
---|
34 | /// </summary> |
---|
35 | public abstract ResolveResult Resolve(CSharpResolver resolver); |
---|
36 | |
---|
37 | /// <summary> |
---|
38 | /// Returns the type that is referenced; or an <c>UnknownType</c> if the type isn't found. |
---|
39 | /// </summary> |
---|
40 | public abstract IType ResolveType(CSharpResolver resolver); |
---|
41 | |
---|
42 | /// <summary> |
---|
43 | /// Returns the namespace that is referenced; or null if no such namespace is found. |
---|
44 | /// </summary> |
---|
45 | public INamespace ResolveNamespace(CSharpResolver resolver) |
---|
46 | { |
---|
47 | NamespaceResolveResult nrr = Resolve(resolver) as NamespaceResolveResult; |
---|
48 | return nrr != null ? nrr.Namespace : null; |
---|
49 | } |
---|
50 | |
---|
51 | IType ITypeReference.Resolve(ITypeResolveContext context) |
---|
52 | { |
---|
53 | // Strictly speaking, we might have to resolve the type in a nested compilation, similar |
---|
54 | // to what we're doing with ConstantExpression. |
---|
55 | // However, in almost all cases this will work correctly - if the resulting type is only available in the |
---|
56 | // nested compilation and not in this, we wouldn't be able to map it anyways. |
---|
57 | var ctx = context as CSharpTypeResolveContext; |
---|
58 | if (ctx == null) { |
---|
59 | ctx = new CSharpTypeResolveContext(context.CurrentAssembly ?? context.Compilation.MainAssembly, null, context.CurrentTypeDefinition, context.CurrentMember); |
---|
60 | } |
---|
61 | return ResolveType(new CSharpResolver(ctx)); |
---|
62 | |
---|
63 | // A potential issue might be this scenario: |
---|
64 | |
---|
65 | // Assembly 1: |
---|
66 | // class A { public class Nested {} } |
---|
67 | |
---|
68 | // Assembly 2: (references asm 1) |
---|
69 | // class B : A {} |
---|
70 | |
---|
71 | // Assembly 3: (references asm 1 and 2) |
---|
72 | // class C { public B.Nested Field; } |
---|
73 | |
---|
74 | // Assembly 4: (references asm 1 and 3, but not 2): |
---|
75 | // uses C.Field; |
---|
76 | |
---|
77 | // Here we would not be able to resolve 'B.Nested' in the compilation of assembly 4, as type B is missing there. |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|