1
ļ»æ// Copyright (c) 2010-2013 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;
25
using ICSharpCode.NRefactory.Utils;
28
namespace ICSharpCode.NRefactory.CSharp.TypeSystem
31
/// Represents a file that was parsed and converted for the type system.
33
[Serializable, FastSerializerVersion(TypeSystemConvertVisitor.version)]
34
public sealed class CSharpUnresolvedFile : AbstractFreezable, IUnresolvedFile, IUnresolvedDocumentationProvider
36
// The 'FastSerializerVersion' attribute on CSharpUnresolvedFile must be incremented when fixing
37
// bugs in the TypeSystemConvertVisitor
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;
48
protected override void FreezeInternal()
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);
58
public CSharpUnresolvedFile(string fileName)
61
throw new ArgumentNullException("fileName");
62
this.fileName = fileName;
63
this.rootUsingScope = new UsingScope();
66
public CSharpUnresolvedFile(string fileName, UsingScope rootUsingScope)
69
throw new ArgumentNullException("fileName");
70
if (rootUsingScope == null)
71
throw new ArgumentNullException("rootUsingScope");
72
this.fileName = fileName;
73
this.rootUsingScope = rootUsingScope;
76
public string FileName {
77
get { return fileName; }
80
DateTime? lastWriteTime;
82
public DateTime? LastWriteTime {
83
get { return lastWriteTime; }
85
FreezableHelper.ThrowIfFrozen(this);
86
lastWriteTime = value;
90
public UsingScope RootUsingScope {
91
get { return rootUsingScope; }
94
public IList<Error> Errors {
95
get { return errors; }
96
internal set { errors = (List<Error>)value; }
99
public IList<UsingScope> UsingScopes {
100
get { return usingScopes; }
103
public IList<IUnresolvedTypeDefinition> TopLevelTypeDefinitions {
104
get { return topLevelTypeDefinitions; }
107
public IList<IUnresolvedAttribute> AssemblyAttributes {
108
get { return assemblyAttributes; }
111
public IList<IUnresolvedAttribute> ModuleAttributes {
112
get { return moduleAttributes; }
115
public void AddDocumentation(IUnresolvedEntity entity, string xmlDocumentation)
117
FreezableHelper.ThrowIfFrozen(this);
118
if (documentation == null)
119
documentation = new Dictionary<IUnresolvedEntity, string>();
120
documentation.Add(entity, xmlDocumentation);
123
public UsingScope GetUsingScope(TextLocation location)
125
foreach (UsingScope scope in usingScopes) {
126
if (scope.Region.IsInside(location.Line, location.Column))
129
return rootUsingScope;
132
public IUnresolvedTypeDefinition GetTopLevelTypeDefinition(TextLocation location)
134
return FindEntity(topLevelTypeDefinitions, location);
137
public IUnresolvedTypeDefinition GetInnermostTypeDefinition(TextLocation location)
139
IUnresolvedTypeDefinition parent = null;
140
IUnresolvedTypeDefinition type = GetTopLevelTypeDefinition(location);
141
while (type != null) {
143
type = FindEntity(parent.NestedTypes, location);
148
public IUnresolvedMember GetMember(TextLocation location)
150
IUnresolvedTypeDefinition type = GetInnermostTypeDefinition(location);
153
return FindEntity(type.Members, location);
156
static T FindEntity<T>(IList<T> list, TextLocation location) where T : class, IUnresolvedEntity
158
// This could be improved using a binary search
159
foreach (T entity in list) {
160
if (entity.Region.IsInside(location.Line, location.Column))
166
public CSharpTypeResolveContext GetTypeResolveContext(ICompilation compilation, TextLocation loc)
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)
175
rctx = rctx.WithCurrentTypeDefinition (resolvedDef);
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);
185
public ICSharpCode.NRefactory.CSharp.Resolver.CSharpResolver GetResolver (ICompilation compilation, TextLocation loc)
187
return new ICSharpCode.NRefactory.CSharp.Resolver.CSharpResolver (GetTypeResolveContext (compilation, loc));
190
public string GetDocumentation(IUnresolvedEntity entity)
193
throw new ArgumentNullException("entity");
194
if (documentation == null)
197
if (documentation.TryGetValue(entity, out xmlDoc))
203
public DocumentationComment GetDocumentation(IUnresolvedEntity entity, IEntity resolvedEntity)
206
throw new ArgumentNullException("entity");
207
if (resolvedEntity == null)
208
throw new ArgumentNullException("resolvedEntity");
209
string xmlDoc = GetDocumentation(entity);
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);
223
return new DocumentationComment(new StringTextSource(xmlDoc), new SimpleTypeResolveContext(resolvedEntity));