5 // Jb Evain (jbevain@gmail.com)
7 // Copyright (c) 2008 - 2010 Jb Evain
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using Mono.Cecil.Metadata;
33 namespace Mono.Cecil {
35 public abstract class TypeSystem {
37 sealed class CorlibTypeSystem : TypeSystem {
39 public CorlibTypeSystem (ModuleDefinition module)
44 internal override TypeReference LookupType (string @namespace, string name)
46 var metadata = module.MetadataSystem;
47 if (metadata.Types == null)
48 Initialize (module.Types);
50 var types = metadata.Types;
52 for (int i = 0; i < types.Length; i++) {
57 if (type.Name == name && type.Namespace == @namespace)
64 static void Initialize (object obj)
69 sealed class CommonTypeSystem : TypeSystem {
71 AssemblyNameReference corlib;
73 public CommonTypeSystem (ModuleDefinition module)
78 internal override TypeReference LookupType (string @namespace, string name)
80 return CreateTypeReference (@namespace, name);
83 public AssemblyNameReference GetCorlibReference ()
88 const string mscorlib = "mscorlib";
90 var references = module.AssemblyReferences;
92 for (int i = 0; i < references.Count; i++) {
93 var reference = references [i];
94 if (reference.Name == mscorlib)
95 return corlib = reference;
98 corlib = new AssemblyNameReference {
100 Version = GetCorlibVersion (),
101 PublicKeyToken = new byte [] { 0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89 },
104 references.Add (corlib);
109 Version GetCorlibVersion ()
111 switch (module.Runtime) {
112 case TargetRuntime.Net_1_0:
113 case TargetRuntime.Net_1_1:
114 return new Version (1, 0, 0, 0);
115 case TargetRuntime.Net_2_0:
116 return new Version (2, 0, 0, 0);
117 case TargetRuntime.Net_4_0:
118 return new Version (4, 0, 0, 0);
120 throw new NotSupportedException ();
124 TypeReference CreateTypeReference (string @namespace, string name)
126 return new TypeReference (@namespace, name, module, GetCorlibReference ());
130 readonly ModuleDefinition module;
132 TypeReference type_object;
133 TypeReference type_void;
134 TypeReference type_bool;
135 TypeReference type_char;
136 TypeReference type_sbyte;
137 TypeReference type_byte;
138 TypeReference type_int16;
139 TypeReference type_uint16;
140 TypeReference type_int32;
141 TypeReference type_uint32;
142 TypeReference type_int64;
143 TypeReference type_uint64;
144 TypeReference type_single;
145 TypeReference type_double;
146 TypeReference type_intptr;
147 TypeReference type_uintptr;
148 TypeReference type_string;
149 TypeReference type_typedref;
151 TypeSystem (ModuleDefinition module)
153 this.module = module;
156 internal static TypeSystem CreateTypeSystem (ModuleDefinition module)
158 if (IsCorlib (module))
159 return new CorlibTypeSystem (module);
161 return new CommonTypeSystem (module);
164 static bool IsCorlib (ModuleDefinition module)
166 if (module.Assembly == null)
169 return module.Assembly.Name.Name == "mscorlib";
172 internal abstract TypeReference LookupType (string @namespace, string name);
174 TypeReference LookupSystemType (string name, ElementType element_type)
176 var type = LookupType ("System", name);
177 type.etype = element_type;
181 TypeReference LookupSystemValueType (string name, ElementType element_type)
183 var type = LookupSystemType (name, element_type);
184 type.IsValueType = true;
188 public IMetadataScope Corlib {
190 var common = this as CommonTypeSystem;
194 return common.GetCorlibReference ();
198 public TypeReference Object {
199 get { return type_object ?? (type_object = LookupSystemType ("Object", ElementType.Object)); }
202 public TypeReference Void {
203 get { return type_void ?? (type_void = LookupSystemType ("Void", ElementType.Void)); }
206 public TypeReference Boolean {
207 get { return type_bool ?? (type_bool = LookupSystemValueType ("Boolean", ElementType.Boolean)); }
210 public TypeReference Char {
211 get { return type_char ?? (type_char = LookupSystemValueType ("Char", ElementType.Char)); }
214 public TypeReference SByte {
215 get { return type_sbyte ?? (type_sbyte = LookupSystemValueType ("SByte", ElementType.I1)); }
218 public TypeReference Byte {
219 get { return type_byte ?? (type_byte = LookupSystemValueType ("Byte", ElementType.U1)); }
222 public TypeReference Int16 {
223 get { return type_int16 ?? (type_int16 = LookupSystemValueType ("Int16", ElementType.I2)); }
226 public TypeReference UInt16 {
227 get { return type_uint16 ?? (type_uint16 = LookupSystemValueType ("UInt16", ElementType.U2)); }
230 public TypeReference Int32 {
231 get { return type_int32 ?? (type_int32 = LookupSystemValueType ("Int32", ElementType.I4)); }
234 public TypeReference UInt32 {
235 get { return type_uint32 ?? (type_uint32 = LookupSystemValueType ("UInt32", ElementType.U4)); }
238 public TypeReference Int64 {
239 get { return type_int64 ?? (type_int64 = LookupSystemValueType ("Int64", ElementType.I8)); }
242 public TypeReference UInt64 {
243 get { return type_uint64 ?? (type_uint64 = LookupSystemValueType ("UInt64", ElementType.U8)); }
246 public TypeReference Single {
247 get { return type_single ?? (type_single = LookupSystemValueType ("Single", ElementType.R4)); }
250 public TypeReference Double {
251 get { return type_double ?? (type_double = LookupSystemValueType ("Double", ElementType.R8)); }
254 public TypeReference IntPtr {
255 get { return type_intptr ?? (type_intptr = LookupSystemValueType ("IntPtr", ElementType.I)); }
258 public TypeReference UIntPtr {
259 get { return type_uintptr ?? (type_uintptr = LookupSystemValueType ("UIntPtr", ElementType.U)); }
262 public TypeReference String {
263 get { return type_string ?? (type_string = LookupSystemType ("String", ElementType.String)); }
266 public TypeReference TypedReference {
267 get { return type_typedref ?? (type_typedref = LookupSystemValueType ("TypedReference", ElementType.TypedByRef)); }