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 System.Text; |
---|
22 | using ICSharpCode.NRefactory.Utils; |
---|
23 | |
---|
24 | namespace ICSharpCode.NRefactory.TypeSystem.Implementation |
---|
25 | { |
---|
26 | /// <summary> |
---|
27 | /// Base class for <see cref="IUnresolvedEntity"/> implementations. |
---|
28 | /// </summary> |
---|
29 | [Serializable] |
---|
30 | public abstract class AbstractUnresolvedEntity : IUnresolvedEntity, IFreezable |
---|
31 | { |
---|
32 | // possible optimizations to reduce the memory usage of AbstractUnresolvedEntity: |
---|
33 | // - store regions in more compact form (e.g. assume both file names are identical; use ushort for columns) |
---|
34 | |
---|
35 | IUnresolvedTypeDefinition declaringTypeDefinition; |
---|
36 | |
---|
37 | string name = string.Empty; |
---|
38 | IList<IUnresolvedAttribute> attributes; |
---|
39 | internal RareFields rareFields; |
---|
40 | |
---|
41 | // 1 byte per enum + 2 bytes for flags |
---|
42 | SymbolKind symbolKind; |
---|
43 | Accessibility accessibility; |
---|
44 | internal BitVector16 flags; |
---|
45 | |
---|
46 | // flags for AbstractUnresolvedEntity: |
---|
47 | internal const ushort FlagFrozen = 0x0001; |
---|
48 | internal const ushort FlagSealed = 0x0002; |
---|
49 | internal const ushort FlagAbstract = 0x0004; |
---|
50 | internal const ushort FlagShadowing = 0x0008; |
---|
51 | internal const ushort FlagSynthetic = 0x0010; |
---|
52 | internal const ushort FlagStatic = 0x0020; |
---|
53 | // flags for DefaultUnresolvedTypeDefinition/LazyCecilTypeDefinition |
---|
54 | internal const ushort FlagAddDefaultConstructorIfRequired = 0x0040; |
---|
55 | internal const ushort FlagHasExtensionMethods = 0x0080; |
---|
56 | internal const ushort FlagHasNoExtensionMethods = 0x0100; |
---|
57 | internal const ushort FlagPartialTypeDefinition = 0x0200; |
---|
58 | // flags for AbstractUnresolvedMember: |
---|
59 | internal const ushort FlagExplicitInterfaceImplementation = 0x0040; |
---|
60 | internal const ushort FlagVirtual = 0x0080; |
---|
61 | internal const ushort FlagOverride = 0x0100; |
---|
62 | // flags for DefaultField: |
---|
63 | internal const ushort FlagFieldIsReadOnly = 0x1000; |
---|
64 | internal const ushort FlagFieldIsVolatile = 0x2000; |
---|
65 | internal const ushort FlagFieldIsFixedSize = 0x4000; |
---|
66 | // flags for DefaultMethod: |
---|
67 | internal const ushort FlagExtensionMethod = 0x1000; |
---|
68 | internal const ushort FlagPartialMethod = 0x2000; |
---|
69 | internal const ushort FlagHasBody = 0x4000; |
---|
70 | internal const ushort FlagAsyncMethod = 0x8000; |
---|
71 | |
---|
72 | public bool IsFrozen { |
---|
73 | get { return flags[FlagFrozen]; } |
---|
74 | } |
---|
75 | |
---|
76 | public void Freeze() |
---|
77 | { |
---|
78 | if (!flags[FlagFrozen]) { |
---|
79 | FreezeInternal(); |
---|
80 | flags[FlagFrozen] = true; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | protected virtual void FreezeInternal() |
---|
85 | { |
---|
86 | attributes = FreezableHelper.FreezeListAndElements(attributes); |
---|
87 | if (rareFields != null) |
---|
88 | rareFields.FreezeInternal(); |
---|
89 | } |
---|
90 | |
---|
91 | /// <summary> |
---|
92 | /// Uses the specified interning provider to intern |
---|
93 | /// strings and lists in this entity. |
---|
94 | /// This method does not test arbitrary objects to see if they implement ISupportsInterning; |
---|
95 | /// instead we assume that those are interned immediately when they are created (before they are added to this entity). |
---|
96 | /// </summary> |
---|
97 | public virtual void ApplyInterningProvider(InterningProvider provider) |
---|
98 | { |
---|
99 | if (provider == null) |
---|
100 | throw new ArgumentNullException("provider"); |
---|
101 | ThrowIfFrozen(); |
---|
102 | name = provider.Intern(name); |
---|
103 | attributes = provider.InternList(attributes); |
---|
104 | if (rareFields != null) |
---|
105 | rareFields.ApplyInterningProvider(provider); |
---|
106 | } |
---|
107 | |
---|
108 | /// <summary> |
---|
109 | /// Creates a shallow clone of this entity. |
---|
110 | /// Collections (e.g. a type's member list) will be cloned as well, but the elements |
---|
111 | /// of said list will not be. |
---|
112 | /// If this instance is frozen, the clone will be unfrozen. |
---|
113 | /// </summary> |
---|
114 | public virtual object Clone() |
---|
115 | { |
---|
116 | var copy = (AbstractUnresolvedEntity)MemberwiseClone(); |
---|
117 | copy.flags[FlagFrozen] = false; |
---|
118 | if (attributes != null) |
---|
119 | copy.attributes = new List<IUnresolvedAttribute>(attributes); |
---|
120 | if (rareFields != null) |
---|
121 | copy.rareFields = (RareFields)rareFields.Clone(); |
---|
122 | return copy; |
---|
123 | } |
---|
124 | |
---|
125 | [Serializable] |
---|
126 | internal class RareFields |
---|
127 | { |
---|
128 | internal DomRegion region; |
---|
129 | internal DomRegion bodyRegion; |
---|
130 | internal IUnresolvedFile unresolvedFile; |
---|
131 | |
---|
132 | protected internal virtual void FreezeInternal() |
---|
133 | { |
---|
134 | } |
---|
135 | |
---|
136 | public virtual void ApplyInterningProvider(InterningProvider provider) |
---|
137 | { |
---|
138 | } |
---|
139 | |
---|
140 | public virtual object Clone() |
---|
141 | { |
---|
142 | return MemberwiseClone(); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | protected void ThrowIfFrozen() |
---|
147 | { |
---|
148 | FreezableHelper.ThrowIfFrozen(this); |
---|
149 | } |
---|
150 | |
---|
151 | public SymbolKind SymbolKind { |
---|
152 | get { return symbolKind; } |
---|
153 | set { |
---|
154 | ThrowIfFrozen(); |
---|
155 | symbolKind = value; |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | internal virtual RareFields WriteRareFields() |
---|
160 | { |
---|
161 | ThrowIfFrozen(); |
---|
162 | if (rareFields == null) rareFields = new RareFields(); |
---|
163 | return rareFields; |
---|
164 | } |
---|
165 | |
---|
166 | public DomRegion Region { |
---|
167 | get { return rareFields != null ? rareFields.region : DomRegion.Empty; } |
---|
168 | set { |
---|
169 | if (value != DomRegion.Empty || rareFields != null) |
---|
170 | WriteRareFields().region = value; |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | public DomRegion BodyRegion { |
---|
175 | get { return rareFields != null ? rareFields.bodyRegion : DomRegion.Empty; } |
---|
176 | set { |
---|
177 | if (value != DomRegion.Empty || rareFields != null) |
---|
178 | WriteRareFields().bodyRegion = value; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | public IUnresolvedFile UnresolvedFile { |
---|
183 | get { return rareFields != null ? rareFields.unresolvedFile : null; } |
---|
184 | set { |
---|
185 | if (value != null || rareFields != null) |
---|
186 | WriteRareFields().unresolvedFile = value; |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | public IUnresolvedTypeDefinition DeclaringTypeDefinition { |
---|
191 | get { return declaringTypeDefinition; } |
---|
192 | set { |
---|
193 | ThrowIfFrozen(); |
---|
194 | declaringTypeDefinition = value; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | public IList<IUnresolvedAttribute> Attributes { |
---|
199 | get { |
---|
200 | if (attributes == null) |
---|
201 | attributes = new List<IUnresolvedAttribute>(); |
---|
202 | return attributes; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | public string Name { |
---|
207 | get { return name; } |
---|
208 | set { |
---|
209 | if (value == null) |
---|
210 | throw new ArgumentNullException("value"); |
---|
211 | ThrowIfFrozen(); |
---|
212 | name = value; |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | public virtual string FullName { |
---|
217 | get { |
---|
218 | if (declaringTypeDefinition != null) |
---|
219 | return declaringTypeDefinition.FullName + "." + name; |
---|
220 | else if (!string.IsNullOrEmpty(this.Namespace)) |
---|
221 | return this.Namespace + "." + name; |
---|
222 | else |
---|
223 | return name; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | public virtual string Namespace { |
---|
228 | get { |
---|
229 | if (declaringTypeDefinition != null) |
---|
230 | return declaringTypeDefinition.Namespace; |
---|
231 | else |
---|
232 | return string.Empty; |
---|
233 | } |
---|
234 | set { |
---|
235 | throw new NotSupportedException(); |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | public virtual string ReflectionName { |
---|
240 | get { |
---|
241 | if (declaringTypeDefinition != null) |
---|
242 | return declaringTypeDefinition.ReflectionName + "." + name; |
---|
243 | else |
---|
244 | return name; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | public Accessibility Accessibility { |
---|
249 | get { return accessibility; } |
---|
250 | set { |
---|
251 | ThrowIfFrozen(); |
---|
252 | accessibility = value; |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | public bool IsStatic { |
---|
257 | get { return flags[FlagStatic]; } |
---|
258 | set { |
---|
259 | ThrowIfFrozen(); |
---|
260 | flags[FlagStatic] = value; |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | public bool IsAbstract { |
---|
265 | get { return flags[FlagAbstract]; } |
---|
266 | set { |
---|
267 | ThrowIfFrozen(); |
---|
268 | flags[FlagAbstract] = value; |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | public bool IsSealed { |
---|
273 | get { return flags[FlagSealed]; } |
---|
274 | set { |
---|
275 | ThrowIfFrozen(); |
---|
276 | flags[FlagSealed] = value; |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | public bool IsShadowing { |
---|
281 | get { return flags[FlagShadowing]; } |
---|
282 | set { |
---|
283 | ThrowIfFrozen(); |
---|
284 | flags[FlagShadowing] = value; |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | public bool IsSynthetic { |
---|
289 | get { return flags[FlagSynthetic]; } |
---|
290 | set { |
---|
291 | ThrowIfFrozen(); |
---|
292 | flags[FlagSynthetic] = value; |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | bool IHasAccessibility.IsPrivate { |
---|
297 | get { return accessibility == Accessibility.Private; } |
---|
298 | } |
---|
299 | |
---|
300 | bool IHasAccessibility.IsPublic { |
---|
301 | get { return accessibility == Accessibility.Public; } |
---|
302 | } |
---|
303 | |
---|
304 | bool IHasAccessibility.IsProtected { |
---|
305 | get { return accessibility == Accessibility.Protected; } |
---|
306 | } |
---|
307 | |
---|
308 | bool IHasAccessibility.IsInternal { |
---|
309 | get { return accessibility == Accessibility.Internal; } |
---|
310 | } |
---|
311 | |
---|
312 | bool IHasAccessibility.IsProtectedOrInternal { |
---|
313 | get { return accessibility == Accessibility.ProtectedOrInternal; } |
---|
314 | } |
---|
315 | |
---|
316 | bool IHasAccessibility.IsProtectedAndInternal { |
---|
317 | get { return accessibility == Accessibility.ProtectedAndInternal; } |
---|
318 | } |
---|
319 | |
---|
320 | public override string ToString() |
---|
321 | { |
---|
322 | StringBuilder b = new StringBuilder("["); |
---|
323 | b.Append(GetType().Name); |
---|
324 | b.Append(' '); |
---|
325 | if (this.DeclaringTypeDefinition != null) { |
---|
326 | b.Append(this.DeclaringTypeDefinition.Name); |
---|
327 | b.Append('.'); |
---|
328 | } |
---|
329 | b.Append(this.Name); |
---|
330 | b.Append(']'); |
---|
331 | return b.ToString(); |
---|
332 | } |
---|
333 | } |
---|
334 | } |
---|