Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ModuleBuilder.cs « System.Reflection.Emit « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 52399fdd09f93d0ff6d2e4b07cf932f08e66b1b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

//
// System.Reflection.Emit/ModuleBuilder.cs
//
// Author:
//   Paolo Molaro (lupus@ximian.com)
//
// (C) 2001 Ximian, Inc.  http://www.ximian.com
//

using System;
using System.Reflection;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Diagnostics.SymbolStore;
using System.IO;

namespace System.Reflection.Emit {
	public class ModuleBuilder : Module {
		private TypeBuilder[] types;
		private CustomAttributeBuilder[] cattrs;
		private byte[] guid;
		private int table_idx;
		private AssemblyBuilder assemblyb;
		private ISymbolWriter symbol_writer;
		private MethodInfo symwriter_define_local;
		Hashtable name_cache;

		internal ModuleBuilder (AssemblyBuilder assb, string name, string fullyqname, bool emitSymbolInfo) {
			this.name = this.scopename = name;
			this.fqname = fullyqname;
			this.assembly = this.assemblyb = assb;
			guid = Guid.NewGuid().ToByteArray ();
			table_idx = get_next_table_index (this, 0x00, true);
			name_cache = new Hashtable ();

			if (emitSymbolInfo)
				GetSymbolWriter (fullyqname);
		}

		internal void GetSymbolWriter (string filename)
		{
			Assembly assembly;
			try {
				assembly = Assembly.Load ("Mono.CSharp.Debugger");
			} catch (FileNotFoundException) {
				return;
			}

			Type type = assembly.GetType ("Mono.CSharp.Debugger.MonoSymbolWriter");
			if (type == null)
				return;

			if (assemblyb.methods == null)
				assemblyb.methods = new ArrayList ();

			// First get the constructor.
			{
				Type[] arg_types = new Type [3];
				arg_types [0] = typeof (ModuleBuilder);
				arg_types [1] = typeof (string);
				arg_types [2] = typeof (ArrayList);
				ConstructorInfo constructor = type.GetConstructor (arg_types);

				object[] args = new object [3];
				args [0] = this;
				args [1] = filename;
				args [2] = assemblyb.methods;

				if (constructor == null)
					return;

				Object instance = constructor.Invoke (args);
				if (instance == null)
					return;

				if (!(instance is ISymbolWriter))
					return;

				symbol_writer = (ISymbolWriter) instance;
			}

			// Get the DefineLocalVariable method.
			{
				Type[] arg_types = new Type [6];
				arg_types [0] = typeof (string);
				arg_types [1] = typeof (LocalBuilder);
				arg_types [2] = typeof (FieldAttributes);
				arg_types [3] = typeof (int);
				arg_types [4] = typeof (int);
				arg_types [5] = typeof (int);

				symwriter_define_local = type.GetMethod ("DefineLocalVariable", arg_types);

				if (symwriter_define_local == null)
					throw new NotSupportedException ();
			}
		}

		internal void SymWriter_DefineLocalVariable (string name, LocalBuilder local,
							     FieldAttributes attributes,
							     int position, int startOffset, int endOffset)
		{
			if ((symbol_writer == null) || (symwriter_define_local == null))
				return;

			object[] args = new object [6];
			args [0] = name;
			args [1] = local;
			args [2] = attributes;
			args [3] = position;
			args [4] = startOffset;
			args [5] = endOffset;

			symwriter_define_local.Invoke (symbol_writer, args);
		}							     
	
		public override string FullyQualifiedName {get { return fqname;}}

		[MonoTODO]
		public TypeBuilder DefineType (string name) {
			// FIXME: LAMESPEC: what other attributes should we use here as default?
			return DefineType (name, TypeAttributes.Public, typeof(object), null);
		}

		public TypeBuilder DefineType (string name, TypeAttributes attr) {
			return DefineType (name, attr, typeof(object), null);
		}

		public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent) {
			return DefineType (name, attr, parent, null);
		}

		private TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packsize, int typesize) {
			TypeBuilder res = new TypeBuilder (this, name, attr, parent, interfaces, packsize, typesize);
			if (types != null) {
				TypeBuilder[] new_types = new TypeBuilder [types.Length + 1];
				System.Array.Copy (types, new_types, types.Length);
				new_types [types.Length] = res;
				types = new_types;
			} else {
				types = new TypeBuilder [1];
				types [0] = res;
			}
			name_cache.Add (name, res);
			return res;
		}

		public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
			return DefineType (name, attr, parent, interfaces, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize);
		}

		public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, int typesize) {
			return DefineType (name, attr, parent, null, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize);
		}

		public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
			return DefineType (name, attr, parent, null, packsize, TypeBuilder.UnspecifiedTypeSize);
		}

		public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize, int typesize) {
			return DefineType (name, attr, parent, null, packsize, typesize);
		}

		public MethodInfo GetArrayMethod( Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
			return new MonoArrayMethod (arrayClass, methodName, callingConvention, returnType, parameterTypes);
		}

		public EnumBuilder DefineEnum( string name, TypeAttributes visibility, Type underlyingType) {
			EnumBuilder eb = new EnumBuilder (this, name, visibility, underlyingType);
			return eb;
		}

		public override Type GetType( string className) {
			return GetType (className, false, false);
		}
		
		public override Type GetType( string className, bool ignoreCase) {
			return GetType (className, false, ignoreCase);
		}

		private TypeBuilder search_in_array (TypeBuilder[] arr, string className, bool ignoreCase) {
			int i;
			if (arr == types && !ignoreCase)
				return (TypeBuilder)name_cache [className];
			for (i = 0; i < arr.Length; ++i) {
				if (String.Compare (className, arr [i].FullName, ignoreCase) == 0) {
					return arr [i];
				}
			}
			return null;
		}

		private TypeBuilder search_nested_in_array (TypeBuilder[] arr, string className, bool ignoreCase) {
			int i;
			for (i = 0; i < arr.Length; ++i) {
				if (String.Compare (className, arr [i].Name, ignoreCase) == 0)
					return arr [i];
			}
			return null;
		}

		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private static extern Type create_modified_type (TypeBuilder tb, string modifiers);

		static char[] type_modifiers = {'&', '[', '*'};

		private TypeBuilder GetMaybeNested (TypeBuilder t, string className, bool ignoreCase) {
			int subt;
			string pname, rname;

			subt = className.IndexOf ('+');
			if (subt < 0) {
				if (t.subtypes != null)
					return search_nested_in_array (t.subtypes, className, ignoreCase);
				return null;
			}
			if (t.subtypes != null) {
				pname = className.Substring (0, subt);
				rname = className.Substring (subt + 1);
				TypeBuilder result = search_nested_in_array (t.subtypes, pname, ignoreCase);
				if (result != null)
					return GetMaybeNested (result, rname, ignoreCase);
			}
			return null;
		}
		
		public override Type GetType( string className, bool throwOnError, bool ignoreCase) {
			int subt;
			string orig = className;
			string modifiers;
			TypeBuilder result = null;

			if (types == null && throwOnError)
				throw new TypeLoadException (className);

			subt = className.IndexOfAny (type_modifiers);
			if (subt >= 0) {
				modifiers = className.Substring (subt);
				className = className.Substring (0, subt);
			} else
				modifiers = null;
			
			subt = className.IndexOf ('+');
			if (subt < 0) {
				if (types != null)
					result = search_in_array (types, className, ignoreCase);
			} else {
				string pname, rname;
				pname = className.Substring (0, subt);
				rname = className.Substring (subt + 1);
				result = search_in_array (types, pname, ignoreCase);
				if (result != null)
					result = GetMaybeNested (result, rname, ignoreCase);
			}
			if ((result == null) && throwOnError)
				throw new TypeLoadException (orig);
			if (result != null && (modifiers != null))
				return create_modified_type (result, modifiers);
			return result;
		}

		internal int get_next_table_index (object obj, int table, bool inc) {
			return assemblyb.get_next_table_index (obj, table, inc);
		}

		public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
			if (cattrs != null) {
				CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
				cattrs.CopyTo (new_array, 0);
				new_array [cattrs.Length] = customBuilder;
				cattrs = new_array;
			} else {
				cattrs = new CustomAttributeBuilder [1];
				cattrs [0] = customBuilder;
			}
		}
		public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
			SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
		}

		public ISymbolWriter GetSymWriter () {
			return symbol_writer;
		}

		public ISymbolDocumentWriter DefineDocument (string url, Guid language, Guid languageVendor, Guid documentType) {
			if (symbol_writer == null)
				throw new InvalidOperationException ();

			return symbol_writer.DefineDocument (url, language, languageVendor, documentType);
		}
	}
}