1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using ICSharpCode.NRefactory.CSharp.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
using ICSharpCode.NRefactory.Utils;
namespace ICSharpCode.NRefactory.CSharp.TypeSystem
{
/// <summary>
/// Represents a scope that contains "using" statements.
/// This is either the file itself, or a namespace declaration.
/// </summary>
[Serializable]
public class UsingScope : AbstractFreezable
{
readonly UsingScope parent;
DomRegion region;
string shortName = "";
IList<TypeOrNamespaceReference> usings;
IList<KeyValuePair<string, TypeOrNamespaceReference>> usingAliases;
IList<string> externAliases;
protected override void FreezeInternal()
{
usings = FreezableHelper.FreezeList(usings);
usingAliases = FreezableHelper.FreezeList(usingAliases);
externAliases = FreezableHelper.FreezeList(externAliases);
// In current model (no child scopes), it makes sense to freeze the parent as well
// to ensure the whole lookup chain is immutable.
if (parent != null)
parent.Freeze();
base.FreezeInternal();
}
/// <summary>
/// Creates a new root using scope.
/// </summary>
public UsingScope()
{
}
/// <summary>
/// Creates a new nested using scope.
/// </summary>
/// <param name="parent">The parent using scope.</param>
/// <param name="shortName">The short namespace name.</param>
public UsingScope(UsingScope parent, string shortName)
{
if (parent == null)
throw new ArgumentNullException("parent");
if (shortName == null)
throw new ArgumentNullException("shortName");
this.parent = parent;
this.shortName = shortName;
}
public UsingScope Parent {
get { return parent; }
}
public DomRegion Region {
get { return region; }
set {
FreezableHelper.ThrowIfFrozen(this);
region = value;
}
}
public string ShortNamespaceName {
get {
return shortName;
}
}
public string NamespaceName {
get {
if (parent != null)
return NamespaceDeclaration.BuildQualifiedName(parent.NamespaceName, shortName);
else
return shortName;
}
// set {
// if (value == null)
// throw new ArgumentNullException("NamespaceName");
// FreezableHelper.ThrowIfFrozen(this);
// namespaceName = value;
// }
}
public IList<TypeOrNamespaceReference> Usings {
get {
if (usings == null)
usings = new List<TypeOrNamespaceReference>();
return usings;
}
}
public IList<KeyValuePair<string, TypeOrNamespaceReference>> UsingAliases {
get {
if (usingAliases == null)
usingAliases = new List<KeyValuePair<string, TypeOrNamespaceReference>>();
return usingAliases;
}
}
public IList<string> ExternAliases {
get {
if (externAliases == null)
externAliases = new List<string>();
return externAliases;
}
}
// public IList<UsingScope> ChildScopes {
// get {
// if (childScopes == null)
// childScopes = new List<UsingScope>();
// return childScopes;
// }
// }
/// <summary>
/// Gets whether this using scope has an alias (either using or extern)
/// with the specified name.
/// </summary>
public bool HasAlias(string identifier)
{
if (usingAliases != null) {
foreach (var pair in usingAliases) {
if (pair.Key == identifier)
return true;
}
}
return externAliases != null && externAliases.Contains(identifier);
}
/// <summary>
/// Resolves the namespace represented by this using scope.
/// </summary>
public ResolvedUsingScope Resolve(ICompilation compilation)
{
CacheManager cache = compilation.CacheManager;
ResolvedUsingScope resolved = cache.GetShared(this) as ResolvedUsingScope;
if (resolved == null) {
var csContext = new CSharpTypeResolveContext(compilation.MainAssembly, parent != null ? parent.Resolve(compilation) : null);
resolved = (ResolvedUsingScope)cache.GetOrAddShared(this, new ResolvedUsingScope(csContext, this));
}
return resolved;
}
}
}
|