1
ļ»æ// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
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;
27
namespace ICSharpCode.NRefactory.CSharp.TypeSystem
30
/// Represents a file that was parsed and converted for the type system.
33
public sealed class CSharpParsedFile : AbstractFreezable, IParsedFile, IUnresolvedDocumentationProvider
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;
44
protected override void FreezeInternal()
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);
54
public CSharpParsedFile(string fileName)
57
throw new ArgumentNullException("fileName");
58
this.fileName = fileName;
59
this.rootUsingScope = new UsingScope();
62
public CSharpParsedFile(string fileName, UsingScope rootUsingScope)
65
throw new ArgumentNullException("fileName");
66
if (rootUsingScope == null)
67
throw new ArgumentNullException("rootUsingScope");
68
this.fileName = fileName;
69
this.rootUsingScope = rootUsingScope;
72
public string FileName {
73
get { return fileName; }
76
DateTime? lastWriteTime;
78
public DateTime? LastWriteTime {
79
get { return lastWriteTime; }
81
FreezableHelper.ThrowIfFrozen(this);
82
lastWriteTime = value;
86
public UsingScope RootUsingScope {
87
get { return rootUsingScope; }
90
public IList<Error> Errors {
91
get { return errors; }
92
internal set { errors = (List<Error>)value; }
95
public IList<UsingScope> UsingScopes {
96
get { return usingScopes; }
99
public IList<IUnresolvedTypeDefinition> TopLevelTypeDefinitions {
100
get { return topLevelTypeDefinitions; }
103
public IList<IUnresolvedAttribute> AssemblyAttributes {
104
get { return assemblyAttributes; }
107
public IList<IUnresolvedAttribute> ModuleAttributes {
108
get { return moduleAttributes; }
111
public void AddDocumentation(IUnresolvedEntity entity, string xmlDocumentation)
113
FreezableHelper.ThrowIfFrozen(this);
114
if (documentation == null)
115
documentation = new Dictionary<IUnresolvedEntity, string>();
116
documentation.Add(entity, xmlDocumentation);
119
public UsingScope GetUsingScope(TextLocation location)
121
foreach (UsingScope scope in usingScopes) {
122
if (scope.Region.IsInside(location.Line, location.Column))
125
return rootUsingScope;
128
public IUnresolvedTypeDefinition GetTopLevelTypeDefinition(TextLocation location)
130
return FindEntity(topLevelTypeDefinitions, location);
133
public IUnresolvedTypeDefinition GetInnermostTypeDefinition(TextLocation location)
135
IUnresolvedTypeDefinition parent = null;
136
IUnresolvedTypeDefinition type = GetTopLevelTypeDefinition(location);
137
while (type != null) {
139
type = FindEntity(parent.NestedTypes, location);
144
public IUnresolvedMember GetMember(TextLocation location)
146
IUnresolvedTypeDefinition type = GetInnermostTypeDefinition(location);
149
return FindEntity(type.Members, location);
152
static T FindEntity<T>(IList<T> list, TextLocation location) where T : class, IUnresolvedEntity
154
// This could be improved using a binary search
155
foreach (T entity in list) {
156
if (entity.Region.IsInside(location.Line, location.Column))
162
public CSharpTypeResolveContext GetTypeResolveContext(ICompilation compilation, TextLocation loc)
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)
171
rctx = rctx.WithCurrentTypeDefinition (resolvedDef);
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);
181
ITypeResolveContext IParsedFile.GetTypeResolveContext (ICompilation compilation, TextLocation loc)
183
return GetTypeResolveContext (compilation, loc);
186
public ICSharpCode.NRefactory.CSharp.Resolver.CSharpResolver GetResolver (ICompilation compilation, TextLocation loc)
188
return new ICSharpCode.NRefactory.CSharp.Resolver.CSharpResolver (GetTypeResolveContext (compilation, loc));
191
public string GetDocumentation(IUnresolvedEntity entity)
194
throw new ArgumentNullException("entity");
195
if (documentation == null)
198
if (documentation.TryGetValue(entity, out xmlDoc))
204
public DocumentationComment GetDocumentation(IUnresolvedEntity entity, IEntity resolvedEntity)
207
throw new ArgumentNullException("entity");
208
if (resolvedEntity == null)
209
throw new ArgumentNullException("resolvedEntity");
210
string xmlDoc = GetDocumentation(entity);
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);
224
return new DocumentationComment(new StringTextSource(xmlDoc), new SimpleTypeResolveContext(resolvedEntity));