// Copyright (c) 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.Linq; using ICSharpCode.NRefactory.TypeSystem; using ICSharpCode.NRefactory.TypeSystem.Implementation; namespace ICSharpCode.NRefactory.CSharp.Resolver { /// /// Represents a scope that contains "using" statements. /// This is either the file itself, or a namespace declaration. /// public class UsingScope : AbstractFreezable { readonly IProjectContent projectContent; readonly UsingScope parent; DomRegion region; string namespaceName = ""; IList usings; IList> usingAliases; IList externAliases; //IList childScopes; protected override void FreezeInternal() { if (usings == null || usings.Count == 0) usings = EmptyList.Instance; else usings = Array.AsReadOnly(usings.ToArray()); if (usingAliases == null || usingAliases.Count == 0) usingAliases = EmptyList>.Instance; else usingAliases = Array.AsReadOnly(usingAliases.ToArray()); externAliases = FreezeList(externAliases); //childScopes = FreezeList(childScopes); // In current model (no child scopes), it makes sense to freeze the parent as well // to ensure the whole lookup chain is immutable. // But we probably shouldn't do this if we add back childScopes. if (parent != null) parent.Freeze(); base.FreezeInternal(); } /// /// Creates a new root using scope. /// public UsingScope(IProjectContent projectContent) { if (projectContent == null) throw new ArgumentNullException("projectContent"); this.projectContent = projectContent; } /// /// Creates a new nested using scope. /// /// The parent using scope. /// The full namespace name. public UsingScope(UsingScope parent, string namespaceName) { if (parent == null) throw new ArgumentNullException("parent"); if (namespaceName == null) throw new ArgumentNullException("namespaceName"); this.parent = parent; this.projectContent = parent.projectContent; this.namespaceName = namespaceName; } public UsingScope Parent { get { return parent; } } public IProjectContent ProjectContent { get { return projectContent; } } public DomRegion Region { get { return region; } set { CheckBeforeMutation(); region = value; } } public string NamespaceName { get { return namespaceName; } set { if (value == null) throw new ArgumentNullException("NamespaceName"); CheckBeforeMutation(); namespaceName = value; } } public IList Usings { get { if (usings == null) usings = new List(); return usings; } } public IList> UsingAliases { get { if (usingAliases == null) usingAliases = new List>(); return usingAliases; } } public IList ExternAliases { get { if (externAliases == null) externAliases = new List(); return externAliases; } } // public IList ChildScopes { // get { // if (childScopes == null) // childScopes = new List(); // return childScopes; // } // } /// /// Gets whether this using scope has an alias (either using or extern) /// with the specified name. /// 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); } } }