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.Generic;
21
using System.Diagnostics;
23
using ICSharpCode.NRefactory.CSharp.TypeSystem;
24
using ICSharpCode.NRefactory.TypeSystem;
25
using ICSharpCode.NRefactory.TypeSystem.Implementation;
26
using ICSharpCode.NRefactory.Utils;
28
namespace ICSharpCode.NRefactory.CSharp.TypeSystem
31
/// Represents a scope that contains "using" statements.
32
/// This is either the file itself, or a namespace declaration.
35
public class UsingScope : AbstractFreezable
37
readonly UsingScope parent;
39
string shortName = "";
40
IList<TypeOrNamespaceReference> usings;
41
IList<KeyValuePair<string, TypeOrNamespaceReference>> usingAliases;
42
IList<string> externAliases;
44
protected override void FreezeInternal()
46
usings = FreezableHelper.FreezeList(usings);
47
usingAliases = FreezableHelper.FreezeList(usingAliases);
48
externAliases = FreezableHelper.FreezeList(externAliases);
50
// In current model (no child scopes), it makes sense to freeze the parent as well
51
// to ensure the whole lookup chain is immutable.
55
base.FreezeInternal();
59
/// Creates a new root using scope.
66
/// Creates a new nested using scope.
68
/// <param name="parent">The parent using scope.</param>
69
/// <param name="shortName">The short namespace name.</param>
70
public UsingScope(UsingScope parent, string shortName)
73
throw new ArgumentNullException("parent");
74
if (shortName == null)
75
throw new ArgumentNullException("shortName");
77
this.shortName = shortName;
80
public UsingScope Parent {
81
get { return parent; }
84
public DomRegion Region {
85
get { return region; }
87
FreezableHelper.ThrowIfFrozen(this);
92
public string ShortNamespaceName {
98
public string NamespaceName {
101
return NamespaceDeclaration.BuildQualifiedName(parent.NamespaceName, shortName);
106
// if (value == null)
107
// throw new ArgumentNullException("NamespaceName");
108
// FreezableHelper.ThrowIfFrozen(this);
109
// namespaceName = value;
113
public IList<TypeOrNamespaceReference> Usings {
116
usings = new List<TypeOrNamespaceReference>();
121
public IList<KeyValuePair<string, TypeOrNamespaceReference>> UsingAliases {
123
if (usingAliases == null)
124
usingAliases = new List<KeyValuePair<string, TypeOrNamespaceReference>>();
129
public IList<string> ExternAliases {
131
if (externAliases == null)
132
externAliases = new List<string>();
133
return externAliases;
137
// public IList<UsingScope> ChildScopes {
139
// if (childScopes == null)
140
// childScopes = new List<UsingScope>();
141
// return childScopes;
146
/// Gets whether this using scope has an alias (either using or extern)
147
/// with the specified name.
149
public bool HasAlias(string identifier)
151
if (usingAliases != null) {
152
foreach (var pair in usingAliases) {
153
if (pair.Key == identifier)
157
return externAliases != null && externAliases.Contains(identifier);
161
/// Resolves the namespace represented by this using scope.
163
public ResolvedUsingScope Resolve(ICompilation compilation)
165
CacheManager cache = compilation.CacheManager;
166
ResolvedUsingScope resolved = cache.GetShared(this) as ResolvedUsingScope;
167
if (resolved == null) {
168
var csContext = new CSharpTypeResolveContext(compilation.MainAssembly, parent != null ? parent.Resolve(compilation) : null);
169
resolved = (ResolvedUsingScope)cache.GetOrAddShared(this, new ResolvedUsingScope(csContext, this));