File: Convert.cs

package info (click to toggle)
monodevelop-debugger-mdb 2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 332 kB
  • ctags: 563
  • sloc: cs: 3,507; makefile: 365; sh: 148; xml: 42
file content (522 lines) | stat: -rw-r--r-- 15,870 bytes parent folder | download
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Convert.cs
//
// Author:
//   Lluis Sanchez Gual <lluis@novell.com>
//
// Copyright (c) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//

using System;
using Mono.Debugger;
using Mono.Debugger.Languages;

namespace DebuggerServer
{
	public static class TargetObjectConvert
	{
		static bool ImplicitFundamentalConversionExists (FundamentalKind skind,
								 FundamentalKind tkind)
		{
			//
			// See Convert.ImplicitStandardConversionExists in MCS.
			//
			switch (skind) {
			case FundamentalKind.SByte:
				if ((tkind == FundamentalKind.Int16) ||
				    (tkind == FundamentalKind.Int32) ||
				    (tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.Byte:
				if ((tkind == FundamentalKind.Int16) ||
				    (tkind == FundamentalKind.UInt16) ||
				    (tkind == FundamentalKind.Int32) ||
				    (tkind == FundamentalKind.UInt32) ||
				    (tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.UInt64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.Int16:
				if ((tkind == FundamentalKind.Int32) ||
				    (tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.UInt16:
				if ((tkind == FundamentalKind.Int32) ||
				    (tkind == FundamentalKind.UInt32) ||
				    (tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.UInt64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.Int32:
				if ((tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.UInt32:
				if ((tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.UInt64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.Int64:
			case FundamentalKind.UInt64:
				if ((tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.Char:
				if ((tkind == FundamentalKind.UInt16) ||
				    (tkind == FundamentalKind.Int32) ||
				    (tkind == FundamentalKind.UInt32) ||
				    (tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.UInt64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.Single:
				if (tkind == FundamentalKind.Double)
					return true;
				break;

			default:
				break;
			}

			return false;
		}

		static bool ImplicitFundamentalConversionExists (TargetFundamentalType source, TargetFundamentalType target)
		{
			return ImplicitFundamentalConversionExists (
				source.FundamentalKind, target.FundamentalKind);
		}

		static object ImplicitFundamentalConversion (object value, FundamentalKind tkind)
		{
			switch (tkind) {
			case FundamentalKind.Char:
				return System.Convert.ToChar (value);
			case FundamentalKind.SByte:
				return System.Convert.ToSByte (value);
			case FundamentalKind.Byte:
				return System.Convert.ToByte (value);
			case FundamentalKind.Int16:
				return System.Convert.ToInt16 (value);
			case FundamentalKind.UInt16:
				return System.Convert.ToUInt16 (value);
			case FundamentalKind.Int32:
				return System.Convert.ToInt32 (value);
			case FundamentalKind.UInt32:
				return System.Convert.ToUInt32 (value);
			case FundamentalKind.Int64:
				return System.Convert.ToInt64 (value);
			case FundamentalKind.UInt64:
				return System.Convert.ToUInt64 (value);
			case FundamentalKind.Single:
				return System.Convert.ToSingle (value);
			case FundamentalKind.Double:
				return System.Convert.ToDouble (value);
			case FundamentalKind.String:
				return System.Convert.ToString (value);
			default:
				return null;
			}
		}

		static TargetObject ImplicitFundamentalConversion (MdbEvaluationContext ctx,
								   TargetFundamentalObject obj,
								   TargetFundamentalType type)
		{
			FundamentalKind skind = obj.Type.FundamentalKind;
			FundamentalKind tkind = type.FundamentalKind;

			if (!ImplicitFundamentalConversionExists (skind, tkind))
				return null;

			object value = obj.GetObject (ctx.Thread);

			object new_value = ImplicitFundamentalConversion (value, tkind);
			if (new_value == null)
				return null;

			return type.Language.CreateInstance (ctx.Thread, new_value);
		}

		public static TargetObject ExplicitFundamentalConversion (MdbEvaluationContext ctx,
									  TargetFundamentalObject obj,
									  TargetFundamentalType type)
		{
			TargetObject retval = ImplicitFundamentalConversion (ctx, obj, type);
			if (retval != null)
				return retval;

			FundamentalKind tkind = type.FundamentalKind;

			try {
				object value = obj.GetObject (ctx.Thread);
				object new_value = ImplicitFundamentalConversion (value, tkind);
				if (new_value == null)
					return null;

				return type.Language.CreateInstance (ctx.Thread, new_value);
			} catch {
				return null;
			}
		}

		static bool ImplicitReferenceConversionExists (MdbEvaluationContext ctx,
							       TargetStructType source,
							       TargetStructType target)
		{
			if (source == target)
				return true;

			if (source.Module.Name.StartsWith ("mscorlib,") && target.Module.Name.StartsWith ("mscorlib,")) {
				Type t1 = Type.GetType (source.Name);
				Type t2 = Type.GetType (target.Name);
				return t2.IsAssignableFrom (t1);
			}
			
			if (!source.HasParent)
				return false;

			TargetStructType parent_type = source.GetParentType (ctx.Thread);
			return ImplicitReferenceConversionExists (ctx, parent_type, target);
		}

		static TargetObject ImplicitReferenceConversion (MdbEvaluationContext ctx,
								 TargetClassObject obj,
								 TargetClassType type)
		{
			if (obj.Type == type)
				return obj;

			if (obj.Type.HasParent) {
				TargetObject pobj = obj.GetParentObject (ctx.Thread);
				if (pobj != null) {
					pobj = ImplicitConversion (ctx, pobj, type);
					if (pobj != null)
						return pobj;
				}
			}
			
			if (ImplicitReferenceConversionExists (ctx, obj.Type, type))
				return obj;
			return null;
		}

		public static bool ImplicitConversionExists (MdbEvaluationContext ctx,
							     TargetType source, TargetType target)
		{
			if (source.Equals (target))
				return true;
			
			if (source is TargetArrayType && target.Name == "System.Array")
				return true;

			if (ObjectUtil.FixTypeName (target.Name) == "System.Object")
				return true;
			
			if (source is TargetArrayType && target is TargetArrayType) {
				TargetArrayType sa = (TargetArrayType) source;
				TargetArrayType ta = (TargetArrayType) target;
				return sa.ElementType.Equals (ta.ElementType);
			}
			
			if (source is TargetEnumType) {
				TargetEnumType e = (TargetEnumType) source;
				if (ImplicitConversionExists (ctx, e.Value.Type, target))
					return true;
			}

			if (target is TargetEnumType) {
				TargetEnumType e = (TargetEnumType) target;
				if (ImplicitConversionExists (ctx, source, e.Value.Type))
					return true;
			}

			if ((source is TargetFundamentalType) && (target is TargetFundamentalType))
				return ImplicitFundamentalConversionExists (
					(TargetFundamentalType) source,
					(TargetFundamentalType) target);

			if ((source is TargetClassType) && (target is TargetClassType))
				return ImplicitReferenceConversionExists (
					ctx, (TargetClassType) source,
					(TargetClassType) target);

			return false;
		}

		public static TargetObject ImplicitConversion (MdbEvaluationContext ctx,
							       TargetObject obj, TargetType type)
		{
			if (obj.Type.Equals (type))
				return obj;
			
			if (type is TargetObjectType || ObjectUtil.FixTypeName (type.Name) == "System.Object") {
				if (obj.Type.IsByRef)
					return obj;
				return BoxValue (ctx, obj);
			}

			if (obj is TargetEnumObject && type is TargetFundamentalType) {
				TargetEnumObject e = (TargetEnumObject) obj;
				return ImplicitConversion (ctx, e.GetValue (ctx.Thread), type);
			}

			if (type is TargetEnumType) {
				TargetEnumType e = (TargetEnumType) type;
				return ImplicitConversion (ctx, obj, e.Value.Type);
			}
			
			if (obj is TargetArrayObject && type.Name == "System.Array") {
				return obj;
			}
			
			if (obj is TargetArrayObject && type is TargetArrayType) {
				TargetArrayObject sa = (TargetArrayObject) obj;
				TargetArrayType ta = (TargetArrayType) type;
				if (sa.Type.ElementType.Equals (ta.ElementType))
					return obj;
			}
			
			if ((obj is TargetFundamentalObject) && (type is TargetFundamentalType))
				return ImplicitFundamentalConversion (
					ctx, (TargetFundamentalObject) obj,
					(TargetFundamentalType) type);

			if ((obj is TargetClassObject) && (type is TargetClassType)) {
				return ImplicitReferenceConversion (
					ctx, (TargetClassObject) obj,
					(TargetClassType) type);
			}

			return null;
		}

		public static TargetObject ImplicitConversionRequired (MdbEvaluationContext ctx,
								       TargetObject obj, TargetType type)
		{
			TargetObject new_obj = ImplicitConversion (ctx, obj, type);
			if (new_obj != null)
				return new_obj;

			throw new Exception (string.Format ("Cannot implicitly convert `{0}' to `{1}'", obj.Type.Name, type.Name));
		}

		public static TargetClassType ToClassType (TargetType type)
		{
			TargetClassType ctype = type as TargetClassType;
			if (ctype != null)
				return ctype;

			TargetObjectType otype = type as TargetObjectType;
			if (otype != null && otype.HasClassType) {
				ctype = otype.ClassType;
				if (ctype != null)
					return ctype;
			}

			TargetArrayType atype = type as TargetArrayType;
			if (atype != null) {
				if (atype.Language.ArrayType != null)
					return atype.Language.ArrayType;
			}

			throw new Exception (string.Format ("Type `{0}' is not a struct or class.", type.Name));
		}

		public static TargetClassObject ToClassObject (MdbEvaluationContext ctx, TargetObject obj)
		{
			TargetClassObject cobj = obj as TargetClassObject;
			if (cobj != null)
				return cobj;

			TargetObjectObject oobj = obj as TargetObjectObject;
			if (oobj != null)
				return oobj.GetClassObject (ctx.Thread);

			TargetArrayObject aobj = obj as TargetArrayObject;
			if ((aobj != null) && aobj.HasClassObject)
				return aobj.GetClassObject (ctx.Thread);

			return null;
		}

		public static TargetStructObject ToStructObject (MdbEvaluationContext ctx, TargetObject obj)
		{
			TargetStructObject sobj = obj as TargetStructObject;
			if (sobj != null)
				return sobj;

			TargetObjectObject oobj = obj as TargetObjectObject;
			if (oobj != null)
				return oobj.GetClassObject (ctx.Thread);

			TargetArrayObject aobj = obj as TargetArrayObject;
			if ((aobj != null) && aobj.HasClassObject)
				return aobj.GetClassObject (ctx.Thread);

			return null;
		}
		
		public static TargetObject Cast (MdbEvaluationContext ctx, TargetObject obj, TargetType targetType)
		{
			obj = ObjectUtil.GetRealObject (ctx, obj);
			
			if (obj.Type == targetType)
				return obj;
			
			if (targetType is TargetObjectType || ObjectUtil.FixTypeName (targetType.Name) == "System.Object") {
				if (obj.Type.IsByRef)
					return obj;
				return BoxValue (ctx, obj);
			}
			
			if (targetType is TargetPointerType)
				throw new NotSupportedException ();

			if (targetType is TargetFundamentalType) {
				TargetFundamentalObject fobj = obj as TargetFundamentalObject;
				if (fobj == null)
					throw new NotSupportedException ();

				TargetFundamentalType ftype = targetType as TargetFundamentalType;
				TargetObject ob = ExplicitFundamentalConversion (ctx, fobj, ftype);
				if (ob == null)
					throw new NotSupportedException ();
				return ob;
			}
			
			if (targetType is TargetNullableType) {
				TargetNullableType ntype = (TargetNullableType) targetType;
				if (obj.Kind == TargetObjectKind.Null)
					return obj;
				else if (obj.Kind != TargetObjectKind.Nullable)
					return ImplicitConversion (ctx, obj, ntype.ElementType);

				TargetNullableType ntype2 = (TargetNullableType) obj.Type;
				if (ImplicitConversionExists (ctx, ntype2.ElementType, ntype.ElementType))
					return obj;
			}

			TargetClassType ctype = ToClassType (targetType);
			TargetClassObject source = ToClassObject (ctx, obj);

			if (source == null)
				throw new Exception (string.Format ("Variable is not a class type."));

			return TryCast (ctx, source, ctype);
		}
		
		static TargetObject BoxValue (MdbEvaluationContext ctx, TargetObject fobj)
		{
			return ctx.Frame.Language.CreateBoxedObject (ctx.Thread, fobj);
		}
		
		static TargetStructObject TryParentCast (MdbEvaluationContext ctx, TargetStructObject source, TargetStructType source_type, TargetStructType target_type)
		{
			if (source_type == target_type)
				return source;

			if (!source_type.HasParent)
				return null;

			TargetStructType parent_type = source_type.GetParentType (ctx.Thread);
			source = TryParentCast (ctx, source, parent_type, target_type);
			if (source == null)
				return null;

			return source.GetParentObject (ctx.Thread) as TargetClassObject;
		}

		static TargetStructObject TryCurrentCast (MdbEvaluationContext ctx, TargetClassObject source, TargetClassType target_type)
		{
			TargetStructObject current = source.GetCurrentObject (ctx.Thread);
			if (current == null)
				return null;

			return TryParentCast (ctx, current, current.Type, target_type);
		}

		public static TargetObject TryCast (MdbEvaluationContext ctx, TargetObject source, TargetClassType target_type)
		{
			if (source.Type == target_type)
				return source;

			TargetClassObject sobj = ToClassObject (ctx, source);
			if (sobj == null)
				return null;

			TargetStructObject result = TryParentCast (ctx, sobj, sobj.Type, target_type);
			if (result != null)
				return result;

			return TryCurrentCast (ctx, sobj, target_type);
		}

		static bool TryParentCast (MdbEvaluationContext ctx, TargetStructType source_type, TargetStructType target_type)
		{
			if (source_type == target_type)
				return true;

			if (!source_type.HasParent)
				return false;

			TargetStructType parent_type = source_type.GetParentType (ctx.Thread);
			return TryParentCast (ctx, parent_type, target_type);
		}

		public static bool TryCast (MdbEvaluationContext ctx, TargetType source, TargetClassType target_type)
		{
			if (source == target_type)
				return true;

			TargetClassType stype = ToClassType (source);
			if (stype == null)
				return false;

			return TryParentCast (ctx, stype, target_type);
		}
	}
}