PageRenderTime 40ms CodeModel.GetById 19ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/NRefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractMember.cs

http://github.com/icsharpcode/ILSpy
C# | 317 lines | 245 code | 41 blank | 31 comment | 26 complexity | ea6225e5022b4e1db141886993e83889 MD5 | raw file
  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
 19using System;
 20using System.Collections.Generic;
 21using ICSharpCode.NRefactory.Utils;
 22
 23namespace ICSharpCode.NRefactory.TypeSystem.Implementation
 24{
 25	/// <summary>
 26	/// Base class for <see cref="IMember"/> implementations.
 27	/// </summary>
 28	[Serializable]
 29	public abstract class AbstractMember : AbstractFreezable, IMember
 30	{
 31		// possible optimizations to reduce the memory usage of AbstractMember:
 32		// - put 'bool isFrozen' into flags
 33		// - store regions in more compact form (e.g. assume both file names are identical; use ushort for columns)
 34		
 35		ITypeDefinition declaringTypeDefinition;
 36		ITypeReference returnType = SharedTypes.UnknownType;
 37		IList<IAttribute> attributes;
 38		IList<IExplicitInterfaceImplementation> interfaceImplementations;
 39		DomRegion region;
 40		DomRegion bodyRegion;
 41		string name;
 42		
 43		// 1 byte per enum + 2 bytes for flags
 44		Accessibility accessibility;
 45		EntityType entityType;
 46		
 47		protected BitVector16 flags;
 48		const ushort FlagSealed    = 0x0001;
 49		const ushort FlagAbstract  = 0x0002;
 50		const ushort FlagShadowing = 0x0004;
 51		const ushort FlagSynthetic = 0x0008;
 52		const ushort FlagVirtual   = 0x0010;
 53		const ushort FlagOverride  = 0x0020;
 54		const ushort FlagStatic    = 0x0040;
 55		// Flags of form 0xY000 are reserved for use in derived classes (DefaultMethod etc.)
 56		
 57		protected override void FreezeInternal()
 58		{
 59			attributes = FreezeList(attributes);
 60			interfaceImplementations = FreezeList(interfaceImplementations);
 61			base.FreezeInternal();
 62		}
 63		
 64		protected AbstractMember(ITypeDefinition declaringTypeDefinition, string name, EntityType entityType)
 65		{
 66			if (declaringTypeDefinition == null)
 67				throw new ArgumentNullException("declaringTypeDefinition");
 68			if (name == null)
 69				throw new ArgumentNullException("name");
 70			this.declaringTypeDefinition = declaringTypeDefinition;
 71			this.entityType = entityType;
 72			this.name = name;
 73		}
 74		
 75		/// <summary>
 76		/// Copy constructor
 77		/// </summary>
 78		protected AbstractMember(IMember member)
 79		{
 80			if (member == null)
 81				throw new ArgumentNullException("member");
 82			this.declaringTypeDefinition = member.DeclaringTypeDefinition;
 83			this.returnType = member.ReturnType;
 84			this.attributes = CopyList(member.Attributes);
 85			this.interfaceImplementations = CopyList(member.InterfaceImplementations);
 86			this.region = member.Region;
 87			this.bodyRegion = member.BodyRegion;
 88			this.name = member.Name;
 89			this.accessibility = member.Accessibility;
 90			this.entityType = member.EntityType;
 91			this.IsSealed = member.IsSealed;
 92			this.IsAbstract = member.IsAbstract;
 93			this.IsShadowing = member.IsShadowing;
 94			this.IsSynthetic = member.IsSynthetic;
 95			this.IsVirtual = member.IsVirtual;
 96			this.IsOverride = member.IsOverride;
 97			this.IsStatic = member.IsStatic;
 98		}
 99		
100		public ITypeDefinition DeclaringTypeDefinition {
101			get { return declaringTypeDefinition; }
102		}
103		
104		public virtual IType DeclaringType {
105			get { return declaringTypeDefinition; }
106		}
107		
108		public virtual IMember MemberDefinition {
109			get { return this; }
110		}
111		
112		public ITypeReference ReturnType {
113			get { return returnType; }
114			set {
115				CheckBeforeMutation();
116				if (value == null)
117					throw new ArgumentNullException();
118				returnType = value;
119			}
120		}
121		
122		public IList<IExplicitInterfaceImplementation> InterfaceImplementations {
123			get {
124				if (interfaceImplementations == null)
125					interfaceImplementations = new List<IExplicitInterfaceImplementation>();
126				return interfaceImplementations;
127			}
128		}
129		
130		public bool IsVirtual {
131			get { return flags[FlagVirtual]; }
132			set {
133				CheckBeforeMutation();
134				flags[FlagVirtual] = value;
135			}
136		}
137		
138		public bool IsOverride {
139			get { return flags[FlagOverride]; }
140			set {
141				CheckBeforeMutation();
142				flags[FlagOverride] = value;
143			}
144		}
145		
146		public bool IsOverridable {
147			get {
148				return (IsVirtual || IsOverride) && !IsSealed;
149			}
150		}
151		
152		public EntityType EntityType {
153			get { return entityType; }
154			set {
155				CheckBeforeMutation();
156				entityType = value;
157			}
158		}
159		
160		public DomRegion Region {
161			get { return region; }
162			set {
163				CheckBeforeMutation();
164				region = value;
165			}
166		}
167		
168		public DomRegion BodyRegion {
169			get { return bodyRegion; }
170			set {
171				CheckBeforeMutation();
172				bodyRegion = value;
173			}
174		}
175		
176		public IList<IAttribute> Attributes {
177			get {
178				if (attributes == null)
179					attributes = new List<IAttribute>();
180				return attributes;
181			}
182		}
183		
184		public virtual string Documentation {
185			get {
186				// To save memory, we don't store the documentation provider within the member,
187				// but simply use our declaring type definition as documentation provider.
188				// If that fails, we try if the project content is a documentation provider:
189				IDocumentationProvider provider = declaringTypeDefinition as IDocumentationProvider
190					?? declaringTypeDefinition.ProjectContent as IDocumentationProvider;
191				if (provider != null)
192					return provider.GetDocumentation(this);
193				else
194					return null;
195			}
196		}
197		
198		public Accessibility Accessibility {
199			get { return accessibility; }
200			set {
201				CheckBeforeMutation();
202				accessibility = value;
203			}
204		}
205		
206		public bool IsStatic {
207			get { return flags[FlagStatic]; }
208			set {
209				CheckBeforeMutation();
210				flags[FlagStatic] = value;
211			}
212		}
213		
214		public bool IsAbstract {
215			get { return flags[FlagAbstract]; }
216			set {
217				CheckBeforeMutation();
218				flags[FlagAbstract] = value;
219			}
220		}
221		
222		public bool IsSealed {
223			get { return flags[FlagSealed]; }
224			set {
225				CheckBeforeMutation();
226				flags[FlagSealed] = value;
227			}
228		}
229		
230		public bool IsShadowing {
231			get { return flags[FlagShadowing]; }
232			set {
233				CheckBeforeMutation();
234				flags[FlagShadowing] = value;
235			}
236		}
237		
238		public bool IsSynthetic {
239			get { return flags[FlagSynthetic]; }
240			set {
241				CheckBeforeMutation();
242				flags[FlagSynthetic] = value;
243			}
244		}
245		
246		public bool IsPrivate {
247			get { return Accessibility == Accessibility.Private; } 
248		}
249		
250		public bool IsPublic {
251			get { return Accessibility == Accessibility.Public; }
252		}
253		
254		public bool IsProtected {
255			get { return Accessibility == Accessibility.Protected; }
256		}
257		
258		public bool IsInternal {
259			get { return Accessibility == Accessibility.Internal; }
260		}
261		
262		public bool IsProtectedOrInternal {
263			get { return Accessibility == Accessibility.ProtectedOrInternal; }
264		}
265		
266		public bool IsProtectedAndInternal {
267			get { return Accessibility == Accessibility.ProtectedAndInternal; }
268		}
269		
270		public IProjectContent ProjectContent {
271			get { return declaringTypeDefinition.ProjectContent; }
272		}
273		
274		public IParsedFile ParsedFile {
275			get { return declaringTypeDefinition.ParsedFile; }
276		}
277		
278		public string Name {
279			get { return name; }
280			set {
281				CheckBeforeMutation();
282				if (value == null)
283					throw new ArgumentNullException();
284				name = value;
285			}
286		}
287		
288		public virtual string FullName {
289			get {
290				return this.DeclaringType.FullName + "." + this.Name;
291			}
292		}
293		
294		public string Namespace {
295			get { return declaringTypeDefinition.Namespace; }
296		}
297		
298		public virtual string ReflectionName {
299			get { return this.DeclaringType.ReflectionName + "." + this.Name; }
300		}
301		
302		public override string ToString()
303		{
304			return "[" + EntityType + " " + ReflectionName + ":" + ReturnType + "]";
305		}
306		
307		public virtual void ApplyInterningProvider(IInterningProvider provider)
308		{
309			if (provider != null) {
310				returnType = provider.Intern(returnType);
311				attributes = provider.InternList(attributes);
312				interfaceImplementations = provider.InternList(interfaceImplementations);
313				name = provider.Intern(name);
314			}
315		}
316	}
317}