/NRefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultAccessor.cs

http://github.com/icsharpcode/ILSpy · C# · 123 lines · 87 code · 13 blank · 23 comment · 19 complexity · 7fddb6a20db2fd865b5881e45812a9b9 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. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. namespace ICSharpCode.NRefactory.TypeSystem.Implementation
  22. {
  23. /// <summary>
  24. /// Default implementation of <see cref="IAccessor"/>.
  25. /// </summary>
  26. [Serializable]
  27. public sealed class DefaultAccessor : AbstractFreezable, IAccessor, ISupportsInterning
  28. {
  29. static readonly DefaultAccessor[] defaultAccessors = CreateDefaultAccessors();
  30. static DefaultAccessor[] CreateDefaultAccessors()
  31. {
  32. DefaultAccessor[] accessors = new DefaultAccessor[(int)Accessibility.ProtectedAndInternal + 1];
  33. for (int i = 0; i < accessors.Length; i++) {
  34. accessors[i] = new DefaultAccessor();
  35. accessors[i].accessibility = (Accessibility)i;
  36. accessors[i].Freeze();
  37. }
  38. return accessors;
  39. }
  40. /// <summary>
  41. /// Gets the default accessor with the specified accessibility (and without attributes or region).
  42. /// </summary>
  43. public static IAccessor GetFromAccessibility(Accessibility accessibility)
  44. {
  45. int index = (int)accessibility;
  46. if (index >= 0 && index < defaultAccessors.Length) {
  47. return defaultAccessors[index];
  48. } else {
  49. DefaultAccessor a = new DefaultAccessor();
  50. a.accessibility = accessibility;
  51. a.Freeze();
  52. return a;
  53. }
  54. }
  55. Accessibility accessibility;
  56. DomRegion region;
  57. IList<IAttribute> attributes;
  58. IList<IAttribute> returnTypeAttributes;
  59. protected override void FreezeInternal()
  60. {
  61. base.FreezeInternal();
  62. this.attributes = FreezeList(this.attributes);
  63. }
  64. public Accessibility Accessibility {
  65. get { return accessibility; }
  66. set {
  67. CheckBeforeMutation();
  68. accessibility = value;
  69. }
  70. }
  71. public DomRegion Region {
  72. get { return region; }
  73. set {
  74. CheckBeforeMutation();
  75. region = value;
  76. }
  77. }
  78. public IList<IAttribute> Attributes {
  79. get {
  80. if (attributes == null)
  81. attributes = new List<IAttribute>();
  82. return attributes;
  83. }
  84. }
  85. public IList<IAttribute> ReturnTypeAttributes {
  86. get {
  87. if (returnTypeAttributes == null)
  88. returnTypeAttributes = new List<IAttribute>();
  89. return returnTypeAttributes;
  90. }
  91. }
  92. void ISupportsInterning.PrepareForInterning(IInterningProvider provider)
  93. {
  94. attributes = provider.InternList(attributes);
  95. returnTypeAttributes = provider.InternList(returnTypeAttributes);
  96. }
  97. int ISupportsInterning.GetHashCodeForInterning()
  98. {
  99. return (attributes != null ? attributes.GetHashCode() : 0)
  100. ^ (returnTypeAttributes != null ? returnTypeAttributes.GetHashCode() : 0)
  101. ^ region.GetHashCode() ^ (int)accessibility;
  102. }
  103. bool ISupportsInterning.EqualsForInterning(ISupportsInterning other)
  104. {
  105. DefaultAccessor a = other as DefaultAccessor;
  106. return a != null && (attributes == a.attributes && returnTypeAttributes == a.returnTypeAttributes
  107. && accessibility == a.accessibility && region == a.region);
  108. }
  109. }
  110. }