1
ļ»æ// Copyright (c) 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);
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
result[i] = new KeyValuePair<string, ResolveResult>(
120
usingScope.UsingAliases[i].Key,
121
usingScope.UsingAliases[i].Value.Resolve(resolver)
124
return LazyInit.GetOrSet(ref this.usingAliases, result);
129
public IList<string> ExternAliases {
130
get { return usingScope.ExternAliases; }
134
/// Gets whether this using scope has an alias (either using or extern)
135
/// with the specified name.
137
public bool HasAlias(string identifier)
139
return usingScope.HasAlias(identifier);
142
sealed class DummyNamespace : INamespace
144
readonly INamespace parentNamespace;
145
readonly string name;
147
public DummyNamespace(INamespace parentNamespace, string name)
149
this.parentNamespace = parentNamespace;
153
public string ExternAlias { get; set; }
155
string INamespace.FullName {
156
get { return NamespaceDeclaration.BuildQualifiedName(parentNamespace.FullName, name); }
159
string INamespace.Name {
163
INamespace INamespace.ParentNamespace {
164
get { return parentNamespace; }
167
IEnumerable<INamespace> INamespace.ChildNamespaces {
168
get { return EmptyList<INamespace>.Instance; }
171
IEnumerable<ITypeDefinition> INamespace.Types {
172
get { return EmptyList<ITypeDefinition>.Instance; }
175
ICompilation IResolved.Compilation {
176
get { return parentNamespace.Compilation; }
179
INamespace INamespace.GetChildNamespace(string name)
184
ITypeDefinition INamespace.GetTypeDefinition(string name, int typeParameterCount)