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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/TypeSystem/CSharpUnresolvedFile.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.Generic;
 
21
using ICSharpCode.NRefactory.Documentation;
 
22
using ICSharpCode.NRefactory.Editor;
 
23
using ICSharpCode.NRefactory.TypeSystem;
 
24
using ICSharpCode.NRefactory.TypeSystem.Implementation;
 
25
using ICSharpCode.NRefactory.Utils;
 
26
using System.Linq;
 
27
 
 
28
namespace ICSharpCode.NRefactory.CSharp.TypeSystem
 
29
{
 
30
        /// <summary>
 
31
        /// Represents a file that was parsed and converted for the type system.
 
32
        /// </summary>
 
33
        [Serializable, FastSerializerVersion(TypeSystemConvertVisitor.version)]
 
34
        public sealed class CSharpUnresolvedFile : AbstractFreezable, IUnresolvedFile, IUnresolvedDocumentationProvider
 
35
        {
 
36
                // The 'FastSerializerVersion' attribute on CSharpUnresolvedFile must be incremented when fixing 
 
37
                // bugs in the TypeSystemConvertVisitor
 
38
                
 
39
                readonly string fileName;
 
40
                readonly UsingScope rootUsingScope;
 
41
                IList<IUnresolvedTypeDefinition> topLevelTypeDefinitions = new List<IUnresolvedTypeDefinition>();
 
42
                IList<IUnresolvedAttribute> assemblyAttributes = new List<IUnresolvedAttribute>();
 
43
                IList<IUnresolvedAttribute> moduleAttributes = new List<IUnresolvedAttribute>();
 
44
                IList<UsingScope> usingScopes = new List<UsingScope>();
 
45
                IList<Error> errors = new List<Error> ();
 
46
                Dictionary<IUnresolvedEntity, string> documentation;
 
47
                
 
48
                protected override void FreezeInternal()
 
49
                {
 
50
                        base.FreezeInternal();
 
51
                        rootUsingScope.Freeze();
 
52
                        topLevelTypeDefinitions = FreezableHelper.FreezeListAndElements(topLevelTypeDefinitions);
 
53
                        assemblyAttributes = FreezableHelper.FreezeListAndElements(assemblyAttributes);
 
54
                        moduleAttributes = FreezableHelper.FreezeListAndElements(moduleAttributes);
 
55
                        usingScopes = FreezableHelper.FreezeListAndElements(usingScopes);
 
56
                }
 
57
                
 
58
                public CSharpUnresolvedFile(string fileName)
 
59
                {
 
60
                        if (fileName == null)
 
61
                                throw new ArgumentNullException("fileName");
 
62
                        this.fileName = fileName;
 
63
                        this.rootUsingScope = new UsingScope();
 
64
                }
 
65
                
 
66
                public CSharpUnresolvedFile(string fileName, UsingScope rootUsingScope)
 
67
                {
 
68
                        if (fileName == null)
 
69
                                throw new ArgumentNullException("fileName");
 
70
                        if (rootUsingScope == null)
 
71
                                throw new ArgumentNullException("rootUsingScope");
 
72
                        this.fileName = fileName;
 
73
                        this.rootUsingScope = rootUsingScope;
 
74
                }
 
75
                
 
76
                public string FileName {
 
77
                        get { return fileName; }
 
78
                }
 
79
                
 
80
                DateTime? lastWriteTime;
 
81
                
 
82
                public DateTime? LastWriteTime {
 
83
                        get { return lastWriteTime; }
 
84
                        set {
 
85
                                FreezableHelper.ThrowIfFrozen(this);
 
86
                                lastWriteTime = value;
 
87
                        }
 
88
                }
 
89
                
 
90
                public UsingScope RootUsingScope {
 
91
                        get { return rootUsingScope; }
 
92
                }
 
93
                
 
94
                public IList<Error> Errors {
 
95
                        get { return errors; }
 
96
                        internal set { errors = (List<Error>)value; }
 
97
                }
 
98
                
 
99
                public IList<UsingScope> UsingScopes {
 
100
                        get { return usingScopes; }
 
101
                }
 
102
                
 
103
                public IList<IUnresolvedTypeDefinition> TopLevelTypeDefinitions {
 
104
                        get { return topLevelTypeDefinitions; }
 
105
                }
 
106
                
 
107
                public IList<IUnresolvedAttribute> AssemblyAttributes {
 
108
                        get { return assemblyAttributes; }
 
109
                }
 
110
                
 
111
                public IList<IUnresolvedAttribute> ModuleAttributes {
 
112
                        get { return moduleAttributes; }
 
113
                }
 
114
                
 
115
                public void AddDocumentation(IUnresolvedEntity entity, string xmlDocumentation)
 
116
                {
 
117
                        FreezableHelper.ThrowIfFrozen(this);
 
118
                        if (documentation == null)
 
119
                                documentation = new Dictionary<IUnresolvedEntity, string>();
 
120
                        documentation.Add(entity, xmlDocumentation);
 
121
                }
 
122
                
 
123
                public UsingScope GetUsingScope(TextLocation location)
 
124
                {
 
125
                        foreach (UsingScope scope in usingScopes) {
 
126
                                if (scope.Region.IsInside(location.Line, location.Column))
 
127
                                        return scope;
 
128
                        }
 
129
                        return rootUsingScope;
 
130
                }
 
131
                
 
132
                public IUnresolvedTypeDefinition GetTopLevelTypeDefinition(TextLocation location)
 
133
                {
 
134
                        return FindEntity(topLevelTypeDefinitions, location);
 
135
                }
 
136
                
 
137
                public IUnresolvedTypeDefinition GetInnermostTypeDefinition(TextLocation location)
 
138
                {
 
139
                        IUnresolvedTypeDefinition parent = null;
 
140
                        IUnresolvedTypeDefinition type = GetTopLevelTypeDefinition(location);
 
141
                        while (type != null) {
 
142
                                parent = type;
 
143
                                type = FindEntity(parent.NestedTypes, location);
 
144
                        }
 
145
                        return parent;
 
146
                }
 
147
                
 
148
                public IUnresolvedMember GetMember(TextLocation location)
 
149
                {
 
150
                        IUnresolvedTypeDefinition type = GetInnermostTypeDefinition(location);
 
151
                        if (type == null)
 
152
                                return null;
 
153
                        return FindEntity(type.Members, location);
 
154
                }
 
155
                
 
156
                static T FindEntity<T>(IList<T> list, TextLocation location) where T : class, IUnresolvedEntity
 
157
                {
 
158
                        // This could be improved using a binary search
 
159
                        foreach (T entity in list) {
 
160
                                if (entity.Region.IsInside(location.Line, location.Column))
 
161
                                        return entity;
 
162
                        }
 
163
                        return null;
 
164
                }
 
165
                
 
166
                public CSharpTypeResolveContext GetTypeResolveContext(ICompilation compilation, TextLocation loc)
 
167
                {
 
168
                        var rctx = new CSharpTypeResolveContext (compilation.MainAssembly);
 
169
                        rctx = rctx.WithUsingScope (GetUsingScope (loc).Resolve (compilation));
 
170
                        var curDef = GetInnermostTypeDefinition (loc);
 
171
                        if (curDef != null) {
 
172
                                var resolvedDef = curDef.Resolve (rctx).GetDefinition ();
 
173
                                if (resolvedDef == null)
 
174
                                        return rctx;
 
175
                                rctx = rctx.WithCurrentTypeDefinition (resolvedDef);
 
176
                                
 
177
                                var curMember = resolvedDef.Members.FirstOrDefault (m => m.Region.FileName == FileName && m.Region.Begin <= loc && loc < m.BodyRegion.End);
 
178
                                if (curMember != null)
 
179
                                        rctx = rctx.WithCurrentMember (curMember);
 
180
                        }
 
181
                        
 
182
                        return rctx;
 
183
                }
 
184
                
 
185
                public ICSharpCode.NRefactory.CSharp.Resolver.CSharpResolver GetResolver (ICompilation compilation, TextLocation loc)
 
186
                {
 
187
                        return new ICSharpCode.NRefactory.CSharp.Resolver.CSharpResolver (GetTypeResolveContext (compilation, loc));
 
188
                }
 
189
                
 
190
                public string GetDocumentation(IUnresolvedEntity entity)
 
191
                {
 
192
                        if (entity == null)
 
193
                                throw new ArgumentNullException("entity");
 
194
                        if (documentation == null)
 
195
                                return null;
 
196
                        string xmlDoc;
 
197
                        if (documentation.TryGetValue(entity, out xmlDoc))
 
198
                                return xmlDoc;
 
199
                        else
 
200
                                return null;
 
201
                }
 
202
                
 
203
                public DocumentationComment GetDocumentation(IUnresolvedEntity entity, IEntity resolvedEntity)
 
204
                {
 
205
                        if (entity == null)
 
206
                                throw new ArgumentNullException("entity");
 
207
                        if (resolvedEntity == null)
 
208
                                throw new ArgumentNullException("resolvedEntity");
 
209
                        string xmlDoc = GetDocumentation(entity);
 
210
                        if (xmlDoc == null)
 
211
                                return null;
 
212
                        var unresolvedTypeDef = entity as IUnresolvedTypeDefinition ?? entity.DeclaringTypeDefinition;
 
213
                        var resolvedTypeDef = resolvedEntity as ITypeDefinition ?? resolvedEntity.DeclaringTypeDefinition;
 
214
                        if (unresolvedTypeDef != null && resolvedTypeDef != null) {
 
215
                                // Strictly speaking, we would have to pass the parent context into CreateResolveContext,
 
216
                                // then transform the result using WithTypeDefinition().
 
217
                                // However, we can simplify this here because we know this is a C# type definition.
 
218
                                var context = unresolvedTypeDef.CreateResolveContext(new SimpleTypeResolveContext(resolvedTypeDef));
 
219
                                if (resolvedEntity is IMember)
 
220
                                        context = context.WithCurrentMember((IMember)resolvedEntity);
 
221
                                return new CSharpDocumentationComment(new StringTextSource(xmlDoc), context);
 
222
                        } else {
 
223
                                return new DocumentationComment(new StringTextSource(xmlDoc), new SimpleTypeResolveContext(resolvedEntity));
 
224
                        }
 
225
                }
 
226
        }
 
227
}