/Mono.Cecil/ArrayType.cs

http://github.com/jbevain/cecil · C# · 145 lines · 104 code · 32 blank · 9 comment · 13 complexity · ba2aa4c1f57cb6281a1f98876662eafb MD5 · raw file

  1. //
  2. // Author:
  3. // Jb Evain (jbevain@gmail.com)
  4. //
  5. // Copyright (c) 2008 - 2015 Jb Evain
  6. // Copyright (c) 2008 - 2011 Novell, Inc.
  7. //
  8. // Licensed under the MIT/X11 license.
  9. //
  10. using System;
  11. using System.Text;
  12. using System.Threading;
  13. using Mono.Collections.Generic;
  14. using MD = Mono.Cecil.Metadata;
  15. namespace Mono.Cecil {
  16. public struct ArrayDimension {
  17. int? lower_bound;
  18. int? upper_bound;
  19. public int? LowerBound {
  20. get { return lower_bound; }
  21. set { lower_bound = value; }
  22. }
  23. public int? UpperBound {
  24. get { return upper_bound; }
  25. set { upper_bound = value; }
  26. }
  27. public bool IsSized {
  28. get { return lower_bound.HasValue || upper_bound.HasValue; }
  29. }
  30. public ArrayDimension (int? lowerBound, int? upperBound)
  31. {
  32. this.lower_bound = lowerBound;
  33. this.upper_bound = upperBound;
  34. }
  35. public override string ToString ()
  36. {
  37. return !IsSized
  38. ? string.Empty
  39. : lower_bound + "..." + upper_bound;
  40. }
  41. }
  42. public sealed class ArrayType : TypeSpecification {
  43. Collection<ArrayDimension> dimensions;
  44. public Collection<ArrayDimension> Dimensions {
  45. get {
  46. if (dimensions != null)
  47. return dimensions;
  48. var empty_dimensions = new Collection<ArrayDimension> ();
  49. empty_dimensions.Add (new ArrayDimension ());
  50. Interlocked.CompareExchange (ref dimensions, empty_dimensions, null);
  51. return dimensions;
  52. }
  53. }
  54. public int Rank {
  55. get { return dimensions == null ? 1 : dimensions.Count; }
  56. }
  57. public bool IsVector {
  58. get {
  59. if (dimensions == null)
  60. return true;
  61. if (dimensions.Count > 1)
  62. return false;
  63. var dimension = dimensions [0];
  64. return !dimension.IsSized;
  65. }
  66. }
  67. public override bool IsValueType {
  68. get { return false; }
  69. set { throw new InvalidOperationException (); }
  70. }
  71. public override string Name {
  72. get { return base.Name + Suffix; }
  73. }
  74. public override string FullName {
  75. get { return base.FullName + Suffix; }
  76. }
  77. string Suffix {
  78. get {
  79. if (IsVector)
  80. return "[]";
  81. var suffix = new StringBuilder ();
  82. suffix.Append ("[");
  83. for (int i = 0; i < dimensions.Count; i++) {
  84. if (i > 0)
  85. suffix.Append (",");
  86. suffix.Append (dimensions [i].ToString ());
  87. }
  88. suffix.Append ("]");
  89. return suffix.ToString ();
  90. }
  91. }
  92. public override bool IsArray {
  93. get { return true; }
  94. }
  95. public ArrayType (TypeReference type)
  96. : base (type)
  97. {
  98. Mixin.CheckType (type);
  99. this.etype = MD.ElementType.Array;
  100. }
  101. public ArrayType (TypeReference type, int rank)
  102. : this (type)
  103. {
  104. Mixin.CheckType (type);
  105. if (rank == 1)
  106. return;
  107. dimensions = new Collection<ArrayDimension> (rank);
  108. for (int i = 0; i < rank; i++)
  109. dimensions.Add (new ArrayDimension ());
  110. this.etype = MD.ElementType.Array;
  111. }
  112. }
  113. }