~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/TypeSystem/ResolvedUsingScope.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

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