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