[11700] | 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 System.Collections.Generic; |
---|
| 21 | using System.Diagnostics; |
---|
| 22 | using System.Linq; |
---|
| 23 | using ICSharpCode.NRefactory.CSharp.TypeSystem; |
---|
| 24 | using ICSharpCode.NRefactory.TypeSystem; |
---|
| 25 | using ICSharpCode.NRefactory.TypeSystem.Implementation; |
---|
| 26 | using ICSharpCode.NRefactory.Utils; |
---|
| 27 | |
---|
| 28 | namespace ICSharpCode.NRefactory.CSharp.TypeSystem |
---|
| 29 | { |
---|
| 30 | /// <summary> |
---|
| 31 | /// Represents a scope that contains "using" statements. |
---|
| 32 | /// This is either the file itself, or a namespace declaration. |
---|
| 33 | /// </summary> |
---|
| 34 | [Serializable] |
---|
| 35 | public class UsingScope : AbstractFreezable |
---|
| 36 | { |
---|
| 37 | readonly UsingScope parent; |
---|
| 38 | DomRegion region; |
---|
| 39 | string shortName = ""; |
---|
| 40 | IList<TypeOrNamespaceReference> usings; |
---|
| 41 | IList<KeyValuePair<string, TypeOrNamespaceReference>> usingAliases; |
---|
| 42 | IList<string> externAliases; |
---|
| 43 | |
---|
| 44 | protected override void FreezeInternal() |
---|
| 45 | { |
---|
| 46 | usings = FreezableHelper.FreezeList(usings); |
---|
| 47 | usingAliases = FreezableHelper.FreezeList(usingAliases); |
---|
| 48 | externAliases = FreezableHelper.FreezeList(externAliases); |
---|
| 49 | |
---|
| 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. |
---|
| 52 | if (parent != null) |
---|
| 53 | parent.Freeze(); |
---|
| 54 | |
---|
| 55 | base.FreezeInternal(); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /// <summary> |
---|
| 59 | /// Creates a new root using scope. |
---|
| 60 | /// </summary> |
---|
| 61 | public UsingScope() |
---|
| 62 | { |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /// <summary> |
---|
| 66 | /// Creates a new nested using scope. |
---|
| 67 | /// </summary> |
---|
| 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) |
---|
| 71 | { |
---|
| 72 | if (parent == null) |
---|
| 73 | throw new ArgumentNullException("parent"); |
---|
| 74 | if (shortName == null) |
---|
| 75 | throw new ArgumentNullException("shortName"); |
---|
| 76 | this.parent = parent; |
---|
| 77 | this.shortName = shortName; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | public UsingScope Parent { |
---|
| 81 | get { return parent; } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | public DomRegion Region { |
---|
| 85 | get { return region; } |
---|
| 86 | set { |
---|
| 87 | FreezableHelper.ThrowIfFrozen(this); |
---|
| 88 | region = value; |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | public string ShortNamespaceName { |
---|
| 93 | get { |
---|
| 94 | return shortName; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | public string NamespaceName { |
---|
| 99 | get { |
---|
| 100 | if (parent != null) |
---|
| 101 | return NamespaceDeclaration.BuildQualifiedName(parent.NamespaceName, shortName); |
---|
| 102 | else |
---|
| 103 | return shortName; |
---|
| 104 | } |
---|
| 105 | // set { |
---|
| 106 | // if (value == null) |
---|
| 107 | // throw new ArgumentNullException("NamespaceName"); |
---|
| 108 | // FreezableHelper.ThrowIfFrozen(this); |
---|
| 109 | // namespaceName = value; |
---|
| 110 | // } |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | public IList<TypeOrNamespaceReference> Usings { |
---|
| 114 | get { |
---|
| 115 | if (usings == null) |
---|
| 116 | usings = new List<TypeOrNamespaceReference>(); |
---|
| 117 | return usings; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | public IList<KeyValuePair<string, TypeOrNamespaceReference>> UsingAliases { |
---|
| 122 | get { |
---|
| 123 | if (usingAliases == null) |
---|
| 124 | usingAliases = new List<KeyValuePair<string, TypeOrNamespaceReference>>(); |
---|
| 125 | return usingAliases; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | public IList<string> ExternAliases { |
---|
| 130 | get { |
---|
| 131 | if (externAliases == null) |
---|
| 132 | externAliases = new List<string>(); |
---|
| 133 | return externAliases; |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | // public IList<UsingScope> ChildScopes { |
---|
| 138 | // get { |
---|
| 139 | // if (childScopes == null) |
---|
| 140 | // childScopes = new List<UsingScope>(); |
---|
| 141 | // return childScopes; |
---|
| 142 | // } |
---|
| 143 | // } |
---|
| 144 | |
---|
| 145 | /// <summary> |
---|
| 146 | /// Gets whether this using scope has an alias (either using or extern) |
---|
| 147 | /// with the specified name. |
---|
| 148 | /// </summary> |
---|
| 149 | public bool HasAlias(string identifier) |
---|
| 150 | { |
---|
| 151 | if (usingAliases != null) { |
---|
| 152 | foreach (var pair in usingAliases) { |
---|
| 153 | if (pair.Key == identifier) |
---|
| 154 | return true; |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | return externAliases != null && externAliases.Contains(identifier); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /// <summary> |
---|
| 161 | /// Resolves the namespace represented by this using scope. |
---|
| 162 | /// </summary> |
---|
| 163 | public ResolvedUsingScope Resolve(ICompilation compilation) |
---|
| 164 | { |
---|
| 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)); |
---|
| 170 | } |
---|
| 171 | return resolved; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | } |
---|