Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory-5.5.0/TypeSystem/Implementation/MergedNamespace.cs @ 11937

Last change on this file since 11937 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 5.5 KB
Line 
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
19using System;
20using System.Collections.Generic;
21using System.Globalization;
22using System.Linq;
23using ICSharpCode.NRefactory.Utils;
24
25namespace ICSharpCode.NRefactory.TypeSystem.Implementation
26{
27  /// <summary>
28  /// A merged namespace.
29  /// </summary>
30  public sealed class MergedNamespace : INamespace
31  {
32    readonly string externAlias;
33    readonly ICompilation compilation;
34    readonly INamespace parentNamespace;
35    readonly INamespace[] namespaces;
36    Dictionary<string, INamespace> childNamespaces;
37   
38    /// <summary>
39    /// Creates a new merged root namespace.
40    /// </summary>
41    /// <param name="compilation">The main compilation.</param>
42    /// <param name="namespaces">The individual namespaces being merged.</param>
43    /// <param name="externAlias">The extern alias for this namespace.</param>
44    public MergedNamespace(ICompilation compilation, INamespace[] namespaces, string externAlias = null)
45    {
46      if (compilation == null)
47        throw new ArgumentNullException("compilation");
48      if (namespaces == null)
49        throw new ArgumentNullException("namespaces");
50      this.compilation = compilation;
51      this.namespaces = namespaces;
52      this.externAlias = externAlias;
53    }
54   
55    /// <summary>
56    /// Creates a new merged child namespace.
57    /// </summary>
58    /// <param name="parentNamespace">The parent merged namespace.</param>
59    /// <param name="namespaces">The individual namespaces being merged.</param>
60    public MergedNamespace(INamespace parentNamespace, INamespace[] namespaces)
61    {
62      if (parentNamespace == null)
63        throw new ArgumentNullException("parentNamespace");
64      if (namespaces == null)
65        throw new ArgumentNullException("namespaces");
66      this.parentNamespace = parentNamespace;
67      this.namespaces = namespaces;
68      this.compilation = parentNamespace.Compilation;
69      this.externAlias = parentNamespace.ExternAlias;
70    }
71   
72    public string ExternAlias {
73      get { return externAlias; }
74    }
75   
76    public string FullName {
77      get { return namespaces[0].FullName; }
78    }
79   
80    public string Name {
81      get { return namespaces[0].Name; }
82    }
83   
84    public INamespace ParentNamespace {
85      get { return parentNamespace; }
86    }
87   
88    public IEnumerable<ITypeDefinition> Types {
89      get {
90        return namespaces.SelectMany(ns => ns.Types);
91      }
92    }
93   
94    public SymbolKind SymbolKind {
95      get { return SymbolKind.Namespace; }
96    }
97   
98    public ICompilation Compilation {
99      get { return compilation; }
100    }
101   
102    public IEnumerable<IAssembly> ContributingAssemblies {
103      get { return namespaces.SelectMany(ns => ns.ContributingAssemblies); }
104    }
105   
106    public IEnumerable<INamespace> ChildNamespaces {
107      get { return GetChildNamespaces().Values; }
108    }
109   
110    public INamespace GetChildNamespace(string name)
111    {
112      INamespace ns;
113      if (GetChildNamespaces().TryGetValue(name, out ns))
114        return ns;
115      else
116        return null;
117    }
118   
119    Dictionary<string, INamespace> GetChildNamespaces()
120    {
121      var result = LazyInit.VolatileRead(ref this.childNamespaces);
122      if (result != null) {
123        return result;
124      } else {
125        result = new Dictionary<string, INamespace>(compilation.NameComparer);
126        foreach (var g in namespaces.SelectMany(ns => ns.ChildNamespaces).GroupBy(ns => ns.Name, compilation.NameComparer)) {
127          result.Add(g.Key, new MergedNamespace(this, g.ToArray()));
128        }
129        return LazyInit.GetOrSet(ref this.childNamespaces, result);
130      }
131    }
132   
133    public ITypeDefinition GetTypeDefinition(string name, int typeParameterCount)
134    {
135      ITypeDefinition anyTypeDef = null;
136      foreach (var ns in namespaces) {
137        ITypeDefinition typeDef = ns.GetTypeDefinition(name, typeParameterCount);
138        if (typeDef != null) {
139          if (typeDef.IsPublic) {
140            // Prefer accessible types over non-accessible types.
141            return typeDef;
142            // || (typeDef.IsInternal && typeDef.ParentAssembly.InternalsVisibleTo(...))
143            // We can't call InternalsVisibleTo() here as we don't know the correct 'current' assembly,
144            // and using the main assembly can cause a stack overflow if there
145            // are internal assembly attributes.
146          }
147          anyTypeDef = typeDef;
148        }
149      }
150      return anyTypeDef;
151    }
152   
153    public override string ToString()
154    {
155      return string.Format(CultureInfo.InvariantCulture, "[MergedNamespace {0}{1} (from {2} assemblies)]",
156                           externAlias != null ? externAlias + "::" : null, this.FullName, this.namespaces.Length);
157    }
158
159    public ISymbolReference ToReference()
160    {
161      return new MergedNamespaceReference(externAlias, FullName);
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.