Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/TypeSystem/ResolvedUsingScope.cs @ 11700

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

#2077: created branch and added first version

File size: 6.6 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.Concurrent;
21using System.Collections.Generic;
22using System.Collections.ObjectModel;
23using System.Diagnostics;
24using ICSharpCode.NRefactory.TypeSystem.Implementation;
25using ICSharpCode.NRefactory.CSharp.Resolver;
26using ICSharpCode.NRefactory.Semantics;
27using ICSharpCode.NRefactory.TypeSystem;
28using ICSharpCode.NRefactory.Utils;
29
30namespace ICSharpCode.NRefactory.CSharp.TypeSystem
31{
32  /// <summary>
33  /// Resolved version of using scope.
34  /// </summary>
35  public class ResolvedUsingScope
36  {
37    readonly CSharpTypeResolveContext parentContext;
38    readonly UsingScope usingScope;
39   
40    internal readonly ConcurrentDictionary<string, ResolveResult> ResolveCache = new ConcurrentDictionary<string, ResolveResult>();
41    internal List<List<IMethod>> AllExtensionMethods;
42   
43    public ResolvedUsingScope(CSharpTypeResolveContext context, UsingScope usingScope)
44    {
45      if (context == null)
46        throw new ArgumentNullException("context");
47      if (usingScope == null)
48        throw new ArgumentNullException("usingScope");
49      this.parentContext = context;
50      this.usingScope = usingScope;
51      if (usingScope.Parent != null) {
52        if (context.CurrentUsingScope == null)
53          throw new InvalidOperationException();
54      } else {
55        if (context.CurrentUsingScope != null)
56          throw new InvalidOperationException();
57      }
58    }
59   
60    public UsingScope UnresolvedUsingScope {
61      get { return usingScope; }
62    }
63   
64    INamespace @namespace;
65   
66    public INamespace Namespace {
67      get {
68        INamespace result = LazyInit.VolatileRead(ref this.@namespace);
69        if (result != null) {
70          return result;
71        } else {
72          if (parentContext.CurrentUsingScope != null) {
73            result = parentContext.CurrentUsingScope.Namespace.GetChildNamespace(usingScope.ShortNamespaceName);
74            if (result == null)
75              result = new DummyNamespace(parentContext.CurrentUsingScope.Namespace, usingScope.ShortNamespaceName);
76          } else {
77            result = parentContext.Compilation.RootNamespace;
78          }
79          Debug.Assert(result != null);
80          return LazyInit.GetOrSet(ref this.@namespace, result);
81        }
82      }
83    }
84   
85    public ResolvedUsingScope Parent {
86      get { return parentContext.CurrentUsingScope; }
87    }
88   
89    IList<INamespace> usings;
90   
91    public IList<INamespace> Usings {
92      get {
93        var result = LazyInit.VolatileRead(ref this.usings);
94        if (result != null) {
95          return result;
96        } else {
97          result = new List<INamespace>();
98          CSharpResolver resolver = new CSharpResolver(parentContext.WithUsingScope(this));
99          foreach (var u in usingScope.Usings) {
100            INamespace ns = u.ResolveNamespace(resolver);
101            if (ns != null && !result.Contains(ns))
102              result.Add(ns);
103          }
104          return LazyInit.GetOrSet(ref this.usings, new ReadOnlyCollection<INamespace>(result));
105        }
106      }
107    }
108   
109    IList<KeyValuePair<string, ResolveResult>> usingAliases;
110   
111    public IList<KeyValuePair<string, ResolveResult>> UsingAliases {
112      get {
113        var result = LazyInit.VolatileRead(ref this.usingAliases);
114        if (result != null) {
115          return result;
116        } else {
117          CSharpResolver resolver = new CSharpResolver(parentContext.WithUsingScope(this));
118          result = new KeyValuePair<string, ResolveResult>[usingScope.UsingAliases.Count];
119          for (int i = 0; i < result.Count; i++) {
120            var rr = usingScope.UsingAliases[i].Value.Resolve(resolver);
121            if (rr is TypeResolveResult) {
122              rr = new AliasTypeResolveResult (usingScope.UsingAliases[i].Key, (TypeResolveResult)rr);
123            } else if (rr is NamespaceResolveResult) {
124              rr = new AliasNamespaceResolveResult (usingScope.UsingAliases[i].Key, (NamespaceResolveResult)rr);
125            }
126            result[i] = new KeyValuePair<string, ResolveResult>(
127              usingScope.UsingAliases[i].Key,
128              rr
129            );
130          }
131          return LazyInit.GetOrSet(ref this.usingAliases, result);
132        }
133      }
134    }
135   
136    public IList<string> ExternAliases {
137      get { return usingScope.ExternAliases; }
138    }
139   
140    /// <summary>
141    /// Gets whether this using scope has an alias (either using or extern)
142    /// with the specified name.
143    /// </summary>
144    public bool HasAlias(string identifier)
145    {
146      return usingScope.HasAlias(identifier);
147    }
148   
149    sealed class DummyNamespace : INamespace
150    {
151      readonly INamespace parentNamespace;
152      readonly string name;
153     
154      public DummyNamespace(INamespace parentNamespace, string name)
155      {
156        this.parentNamespace = parentNamespace;
157        this.name = name;
158      }
159     
160      public string ExternAlias { get; set; }
161     
162      string INamespace.FullName {
163        get { return NamespaceDeclaration.BuildQualifiedName(parentNamespace.FullName, name); }
164      }
165     
166      public string Name {
167        get { return name; }
168      }
169     
170      SymbolKind ISymbol.SymbolKind {
171        get { return SymbolKind.Namespace; }
172      }
173     
174      INamespace INamespace.ParentNamespace {
175        get { return parentNamespace; }
176      }
177     
178      IEnumerable<INamespace> INamespace.ChildNamespaces {
179        get { return EmptyList<INamespace>.Instance; }
180      }
181     
182      IEnumerable<ITypeDefinition> INamespace.Types {
183        get { return EmptyList<ITypeDefinition>.Instance; }
184      }
185     
186      IEnumerable<IAssembly> INamespace.ContributingAssemblies {
187        get { return EmptyList<IAssembly>.Instance; }
188      }
189     
190      ICompilation ICompilationProvider.Compilation {
191        get { return parentNamespace.Compilation; }
192      }
193     
194      INamespace INamespace.GetChildNamespace(string name)
195      {
196        return null;
197      }
198     
199      ITypeDefinition INamespace.GetTypeDefinition(string name, int typeParameterCount)
200      {
201        return null;
202      }
203
204      public ISymbolReference ToReference()
205      {
206        return new MergedNamespaceReference(ExternAlias, ((INamespace)this).FullName);
207      }
208    }
209  }
210}
Note: See TracBrowser for help on using the repository browser.