/NRefactory/ICSharpCode.NRefactory/TypeSystem/SharedTypes.cs

http://github.com/icsharpcode/ILSpy · C# · 111 lines · 47 code · 13 blank · 51 comment · 3 complexity · eef75226749bfe5fbd421d08bf570a61 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 ICSharpCode.NRefactory.TypeSystem.Implementation;
  20. namespace ICSharpCode.NRefactory.TypeSystem
  21. {
  22. /// <summary>
  23. /// Contains static implementations of well-known types.
  24. /// </summary>
  25. public static class SharedTypes
  26. {
  27. /// <summary>
  28. /// Gets the type representing resolve errors.
  29. /// </summary>
  30. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "It's immutable")]
  31. public readonly static IType UnknownType = new SharedTypeImpl(TypeKind.Unknown, "?", isReferenceType: null);
  32. /// <summary>
  33. /// The null type is used as type of the null literal. It is a reference type without any members; and it is a subtype of all reference types.
  34. /// </summary>
  35. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "It's immutable")]
  36. public readonly static IType Null = new SharedTypeImpl(TypeKind.Null, "null", isReferenceType: true);
  37. /// <summary>
  38. /// Type representing the C# 'dynamic' type.
  39. /// </summary>
  40. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "It's immutable")]
  41. public readonly static IType Dynamic = new SharedTypeImpl(TypeKind.Dynamic, "dynamic", isReferenceType: true);
  42. /// <summary>
  43. /// A type used for unbound type arguments in partially parameterized types.
  44. /// </summary>
  45. /// <see cref="IType.GetNestedTypes"/>
  46. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "It's immutable")]
  47. public readonly static IType UnboundTypeArgument = new SharedTypeImpl(TypeKind.UnboundTypeArgument, "", isReferenceType: null);
  48. /*
  49. * I'd like to define static instances for common types like
  50. * void, int, etc.; but there are two problems with this:
  51. *
  52. * SharedTypes.Void.GetDefinition().ProjectContent should return mscorlib, but
  53. * we can't do that without providing a context.
  54. *
  55. * Assuming we add a context parameter to GetDefinition():
  56. *
  57. * SharedType.Void.Equals(SharedType.Void.GetDefinition(x))
  58. * SharedType.Void.GetDefinition(y).Equals(SharedType.Void)
  59. * should both return true.
  60. * But if the type can have multiple definitions (multiple mscorlib versions loaded),
  61. * then this is not possible without violating transitivity of Equals():
  62. *
  63. * SharedType.Void.GetDefinition(x).Equals(SharedType.Void.GetDefinition(y))
  64. * would have to return true even though these are two distinct definitions.
  65. */
  66. [Serializable]
  67. sealed class SharedTypeImpl : AbstractType
  68. {
  69. readonly TypeKind kind;
  70. readonly string name;
  71. readonly bool? isReferenceType;
  72. public SharedTypeImpl(TypeKind kind, string name, bool? isReferenceType)
  73. {
  74. this.kind = kind;
  75. this.name = name;
  76. this.isReferenceType = isReferenceType;
  77. }
  78. public override TypeKind Kind {
  79. get { return kind; }
  80. }
  81. public override string Name {
  82. get { return name; }
  83. }
  84. public override bool? IsReferenceType(ITypeResolveContext context)
  85. {
  86. return isReferenceType;
  87. }
  88. public override bool Equals(IType other)
  89. {
  90. return other != null && other.Kind == kind;
  91. }
  92. public override int GetHashCode()
  93. {
  94. return (int)kind;
  95. }
  96. }
  97. }
  98. }