1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using System.Collections.Concurrent;
21
using System.Collections.Generic;
22
using System.Collections.ObjectModel;
23
using System.Diagnostics;
24
using ICSharpCode.NRefactory.CSharp.Resolver;
25
using ICSharpCode.NRefactory.Semantics;
26
using ICSharpCode.NRefactory.TypeSystem;
27
using ICSharpCode.NRefactory.Utils;
29
namespace ICSharpCode.NRefactory.CSharp.TypeSystem
32
/// Resolved version of using scope.
34
public class ResolvedUsingScope
36
readonly CSharpTypeResolveContext parentContext;
37
readonly UsingScope usingScope;
39
internal readonly ConcurrentDictionary<string, ResolveResult> ResolveCache = new ConcurrentDictionary<string, ResolveResult>();
40
internal List<List<IMethod>> AllExtensionMethods;
42
public ResolvedUsingScope(CSharpTypeResolveContext context, UsingScope usingScope)
45
throw new ArgumentNullException("context");
46
if (usingScope == null)
47
throw new ArgumentNullException("usingScope");
48
this.parentContext = context;
49
this.usingScope = usingScope;
50
if (usingScope.Parent != null) {
51
if (context.CurrentUsingScope == null)
52
throw new InvalidOperationException();
54
if (context.CurrentUsingScope != null)
55
throw new InvalidOperationException();
59
public UsingScope UnresolvedUsingScope {
60
get { return usingScope; }
63
INamespace @namespace;
65
public INamespace Namespace {
67
INamespace result = LazyInit.VolatileRead(ref this.@namespace);
71
if (parentContext.CurrentUsingScope != null) {
72
result = parentContext.CurrentUsingScope.Namespace.GetChildNamespace(usingScope.ShortNamespaceName);
74
result = new DummyNamespace(parentContext.CurrentUsingScope.Namespace, usingScope.ShortNamespaceName);
76
result = parentContext.Compilation.RootNamespace;
78
Debug.Assert(result != null);
79
return LazyInit.GetOrSet(ref this.@namespace, result);
84
public ResolvedUsingScope Parent {
85
get { return parentContext.CurrentUsingScope; }
88
IList<INamespace> usings;
90
public IList<INamespace> Usings {
92
var result = LazyInit.VolatileRead(ref this.usings);
96
result = new List<INamespace>();
97
CSharpResolver resolver = new CSharpResolver(parentContext.WithUsingScope(this));
98
foreach (var u in usingScope.Usings) {
99
INamespace ns = u.ResolveNamespace(resolver);
100
if (ns != null && !result.Contains(ns))
103
return LazyInit.GetOrSet(ref this.usings, new ReadOnlyCollection<INamespace>(result));
108
IList<KeyValuePair<string, ResolveResult>> usingAliases;
110
public IList<KeyValuePair<string, ResolveResult>> UsingAliases {
112
var result = LazyInit.VolatileRead(ref this.usingAliases);
113
if (result != null) {
116
CSharpResolver resolver = new CSharpResolver(parentContext.WithUsingScope(this));
117
result = new KeyValuePair<string, ResolveResult>[usingScope.UsingAliases.Count];
118
for (int i = 0; i < result.Count; i++) {
119
var rr = usingScope.UsingAliases[i].Value.Resolve(resolver);
120
if (rr is TypeResolveResult) {
121
rr = new AliasTypeResolveResult (usingScope.UsingAliases[i].Key, (TypeResolveResult)rr);
122
} else if (rr is NamespaceResolveResult) {
123
rr = new AliasNamespaceResolveResult (usingScope.UsingAliases[i].Key, (NamespaceResolveResult)rr);
125
result[i] = new KeyValuePair<string, ResolveResult>(
126
usingScope.UsingAliases[i].Key,
130
return LazyInit.GetOrSet(ref this.usingAliases, result);
135
public IList<string> ExternAliases {
136
get { return usingScope.ExternAliases; }
140
/// Gets whether this using scope has an alias (either using or extern)
141
/// with the specified name.
143
public bool HasAlias(string identifier)
145
return usingScope.HasAlias(identifier);
148
sealed class DummyNamespace : INamespace
150
readonly INamespace parentNamespace;
151
readonly string name;
153
public DummyNamespace(INamespace parentNamespace, string name)
155
this.parentNamespace = parentNamespace;
159
public string ExternAlias { get; set; }
161
string INamespace.FullName {
162
get { return NamespaceDeclaration.BuildQualifiedName(parentNamespace.FullName, name); }
165
string INamespace.Name {
169
INamespace INamespace.ParentNamespace {
170
get { return parentNamespace; }
173
IEnumerable<INamespace> INamespace.ChildNamespaces {
174
get { return EmptyList<INamespace>.Instance; }
177
IEnumerable<ITypeDefinition> INamespace.Types {
178
get { return EmptyList<ITypeDefinition>.Instance; }
181
IEnumerable<IAssembly> INamespace.ContributingAssemblies {
182
get { return EmptyList<IAssembly>.Instance; }
185
ICompilation ICompilationProvider.Compilation {
186
get { return parentNamespace.Compilation; }
189
INamespace INamespace.GetChildNamespace(string name)
194
ITypeDefinition INamespace.GetTypeDefinition(string name, int typeParameterCount)