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

« back to all changes in this revision

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