PageRenderTime 22ms CodeModel.GetById 2ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 1ms

/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
 11using System;
 12using System.Text;
 13using System.Threading;
 14using Mono.Collections.Generic;
 15using MD = Mono.Cecil.Metadata;
 16
 17namespace Mono.Cecil {
 18
 19	public struct ArrayDimension {
 20
 21		int? lower_bound;
 22		int? upper_bound;
 23
 24		public int? LowerBound {
 25			get { return lower_bound; }
 26			set { lower_bound = value; }
 27		}
 28
 29		public int? UpperBound {
 30			get { return upper_bound; }
 31			set { upper_bound = value; }
 32		}
 33
 34		public bool IsSized {
 35			get { return lower_bound.HasValue || upper_bound.HasValue; }
 36		}
 37
 38		public ArrayDimension (int? lowerBound, int? upperBound)
 39		{
 40			this.lower_bound = lowerBound;
 41			this.upper_bound = upperBound;
 42		}
 43
 44		public override string ToString ()
 45		{
 46			return !IsSized
 47				? string.Empty
 48				: lower_bound + "..." + upper_bound;
 49		}
 50	}
 51
 52	public sealed class ArrayType : TypeSpecification {
 53
 54		Collection<ArrayDimension> dimensions;
 55
 56		public Collection<ArrayDimension> Dimensions {
 57			get {
 58				if (dimensions != null)
 59					return dimensions;
 60
 61				var empty_dimensions = new Collection<ArrayDimension> ();
 62				empty_dimensions.Add (new ArrayDimension ());
 63
 64				Interlocked.CompareExchange (ref dimensions, empty_dimensions, null);
 65
 66				return dimensions;
 67			}
 68		}
 69
 70		public int Rank {
 71			get { return dimensions == null ? 1 : dimensions.Count; }
 72		}
 73
 74		public bool IsVector {
 75			get {
 76				if (dimensions == null)
 77					return true;
 78
 79				if (dimensions.Count > 1)
 80					return false;
 81
 82				var dimension = dimensions [0];
 83
 84				return !dimension.IsSized;
 85			}
 86		}
 87
 88		public override bool IsValueType {
 89			get { return false; }
 90			set { throw new InvalidOperationException (); }
 91		}
 92
 93		public override string Name {
 94			get { return base.Name + Suffix; }
 95		}
 96
 97		public override string FullName {
 98			get { return base.FullName + Suffix; }
 99		}
100
101		string Suffix {
102			get {
103				if (IsVector)
104					return "[]";
105
106				var suffix = new StringBuilder ();
107				suffix.Append ("[");
108				for (int i = 0; i < dimensions.Count; i++) {
109					if (i > 0)
110						suffix.Append (",");
111
112					suffix.Append (dimensions [i].ToString ());
113				}
114				suffix.Append ("]");
115
116				return suffix.ToString ();
117			}
118		}
119
120		public override bool IsArray {
121			get { return true; }
122		}
123
124		public ArrayType (TypeReference type)
125			: base (type)
126		{
127			Mixin.CheckType (type);
128			this.etype = MD.ElementType.Array;
129		}
130
131		public ArrayType (TypeReference type, int rank)
132			: this (type)
133		{
134			Mixin.CheckType (type);
135
136			if (rank == 1)
137				return;
138
139			dimensions = new Collection<ArrayDimension> (rank);
140			for (int i = 0; i < rank; i++)
141				dimensions.Add (new ArrayDimension ());
142			this.etype = MD.ElementType.Array;
143		}
144	}
145}