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

MissingType.cs « corcompare « tools « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 013ec5503d36a94fc805061307a5b12b47846cef (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
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
// Mono.Util.CorCompare.MissingType
//
// Author(s):
//   Nick Drochak (ndrochak@gol.com)
//
// (C) 2001-2002 Nick Drochak

using System;
using System.Xml;
using System.Reflection;
using System.Collections;

namespace Mono.Util.CorCompare 
{

	/// <summary>
	/// 	Represents a class method that missing.
	/// </summary>
	/// <remarks>
	/// 	created by - Nick
	/// 	created on - 2/20/2002 10:43:57 PM
	/// </remarks>
	class MissingType : MissingBase
	{
		// e.g. <class name="System.Byte" status="missing"/>
		// e.g. <class name="System.Array" status="todo" missing="5" todo="6" complete="45">
		Type typeMono, typeMS;
//		ArrayList rgAttributes = new ArrayList ();
		ArrayList rgMethods = new ArrayList ();
		ArrayList rgProperties = new ArrayList ();
		ArrayList rgEvents = new ArrayList ();
		ArrayList rgFields = new ArrayList ();
		ArrayList rgConstructors = new ArrayList ();
		ArrayList rgNestedTypes = new ArrayList ();
		ArrayList rgInterfaces = new ArrayList ();
//		NodeStatus nsAttributes = new NodeStatus ();
		NodeStatus nsMethods = new NodeStatus ();
		NodeStatus nsProperties = new NodeStatus ();
		NodeStatus nsEvents = new NodeStatus ();
		NodeStatus nsFields = new NodeStatus ();
		NodeStatus nsConstructors = new NodeStatus ();
		NodeStatus nsNestedTypes = new NodeStatus ();
		NodeStatus nsInterfaces = new NodeStatus ();

		public MissingType (Type _typeMono, Type _typeMS)
		{
			typeMono = _typeMono;
			typeMS = _typeMS;
			m_nodeStatus = new NodeStatus (_typeMono, _typeMS);
		}

		public override string Name 
		{
			get
			{
				Type type = TypeInfoBest;
				if (type.DeclaringType != null)
					return type.DeclaringType.Name + "+" + type.Name;
				return type.Name;
			}
		}

		public override string Type
		{
			get
			{
				Type type = TypeInfo;
				if (type.IsEnum)
					return "enum";
				else if (type.IsInterface)
					return "interface";
				else if (type.IsValueType)
					return "struct";
				else if (IsDelegate)
					return "delegate";
				else
					return "class";
			}
		}

		public Type TypeInfo
		{
			get { return (typeMono != null) ? typeMono : typeMS; }
		}

		public Type TypeInfoBest
		{
			get { return (typeMS == null) ? typeMono : typeMS; }
		}

		public bool IsDelegate
		{
			get
			{
				Type typeBest = TypeInfoBest;
				if (typeBest.IsEnum || typeBest.IsInterface || typeBest.IsValueType)
					return false;
				Type type = typeBest.BaseType;
				while (type != null)
				{
					if (type.FullName == "System.Delegate")
						return true;
					type = type.BaseType;
				}
				return false;
			}
		}

		public MissingMember CreateMember (MemberInfo infoMono, MemberInfo infoMS)
		{
			MemberTypes mt = (infoMono != null) ? infoMono.MemberType : infoMS.MemberType;
			MissingMember mm;
			switch (mt)
			{
				case MemberTypes.Method:
					mm = new MissingMethod (infoMono, infoMS);
					break;
				case MemberTypes.Property:
					mm = new MissingProperty (infoMono, infoMS);
					break;
				case MemberTypes.Event:
					mm = new MissingEvent (infoMono, infoMS);
					break;
				case MemberTypes.Field:
					mm = new MissingField (infoMono, infoMS);
					break;
				case MemberTypes.Constructor:
					mm = new MissingConstructor (infoMono, infoMS);
					break;
				case MemberTypes.NestedType:
					mm = new MissingNestedType (infoMono, infoMS);
					break;
				default:
					throw new Exception ("Unexpected MemberType: " + mt.ToString());
			}
			mm.Analyze ();
			return mm;
		}


		public void AddMember (MissingMember mm)
		{
			switch (mm.Info.MemberType)
			{
				case MemberTypes.Method:
					nsMethods.AddChildren (mm.Status);
					rgMethods.Add (mm);
					break;
				case MemberTypes.Property:
					nsProperties.AddChildren (mm.Status);
					rgProperties.Add (mm);
					break;
				case MemberTypes.Event:
					nsEvents.AddChildren (mm.Status);
					rgEvents.Add (mm);
					break;
				case MemberTypes.Field:
					nsFields.AddChildren (mm.Status);
					rgFields.Add (mm);
					break;
				case MemberTypes.Constructor:
					nsConstructors.AddChildren (mm.Status);
					rgConstructors.Add (mm);
					break;
				case MemberTypes.NestedType:
					nsNestedTypes.AddChildren (mm.Status);
					rgNestedTypes.Add (mm);
					break;
				default:
					throw new Exception ("Unexpected MemberType: " + mm.Info.ToString());
			}
		}

		public void AddMember (MemberInfo infoMono, MemberInfo infoMS)
		{
			AddMember (CreateMember (infoMono, infoMS));
		}

		public override XmlElement CreateXML (XmlDocument doc)
		{
			XmlElement eltClass = base.CreateXML (doc);
			XmlElement eltMember;

			eltMember = MissingBase.CreateMemberCollectionElement ("methods", rgMethods, nsMethods, doc);
			if (eltMember != null) 
				eltClass.AppendChild (eltMember);

			eltMember = MissingBase.CreateMemberCollectionElement ("properties", rgProperties, nsProperties, doc);
			if (eltMember != null) 
				eltClass.AppendChild (eltMember);

			eltMember = MissingBase.CreateMemberCollectionElement ("events", rgEvents, nsEvents, doc);
			if (eltMember != null) 
				eltClass.AppendChild (eltMember);

			eltMember = MissingBase.CreateMemberCollectionElement ("fields", rgFields, nsFields, doc);
			if (eltMember != null) 
				eltClass.AppendChild (eltMember);

			eltMember = MissingBase.CreateMemberCollectionElement ("constructors", rgConstructors, nsConstructors, doc);
			if (eltMember != null) 
				eltClass.AppendChild (eltMember);

			eltMember = MissingBase.CreateMemberCollectionElement ("nestedTypes", rgNestedTypes, nsNestedTypes, doc);
			if (eltMember != null) 
				eltClass.AppendChild (eltMember);

			eltMember = MissingBase.CreateMemberCollectionElement ("interfaces", rgInterfaces, nsInterfaces, doc);
			if (eltMember != null) 
				eltClass.AppendChild (eltMember);

			return eltClass;
		}

		public override NodeStatus Analyze ()
		{
			Hashtable htMono = new Hashtable ();
			if (typeMono != null)
			{
				ArrayList rgIgnoreMono = new ArrayList ();
				foreach (MemberInfo miMono in typeMono.GetMembers (BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
				{
					if (typeMono == miMono.DeclaringType)
					{
						string strName = MissingMember.GetUniqueName (miMono);
						htMono.Add (strName, miMono);

						// ignore any property/event accessors
						if (miMono.MemberType == MemberTypes.Property)
						{
							PropertyInfo pi = (PropertyInfo) miMono;
							MemberInfo miGet = pi.GetGetMethod ();
							if (miGet != null)
								rgIgnoreMono.Add (miGet);
							MemberInfo miSet = pi.GetSetMethod ();
							if (miSet != null)
								rgIgnoreMono.Add (miSet);
						}
						else if (miMono.MemberType == MemberTypes.Event)
						{
							EventInfo ei = (EventInfo) miMono;
							MemberInfo miAdd = ei.GetAddMethod ();
							if (miAdd != null)
								rgIgnoreMono.Add (miAdd);
							MemberInfo miRemove = ei.GetRemoveMethod ();
							if (miRemove != null)
								rgIgnoreMono.Add (miRemove);
							MemberInfo miRaise = ei.GetRaiseMethod ();
							if (miRaise != null)
								rgIgnoreMono.Add (miRaise);
						}
					}
				}
				foreach (MemberInfo miIgnore in rgIgnoreMono)
					htMono.Remove (MissingMember.GetUniqueName (miIgnore));
			}
			Hashtable htMethodsMS = new Hashtable ();
			if (typeMS != null)
			{
				ICollection colMembersMS = typeMS.GetMembers (BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				Hashtable htIgnoreMS = new Hashtable ();
				foreach (MemberInfo miMS in colMembersMS)
				{
					// ignore any property/event accessors
					if (miMS.MemberType == MemberTypes.Property)
					{
						PropertyInfo pi = (PropertyInfo) miMS;
						MemberInfo miGet = pi.GetGetMethod ();
						if (miGet != null)
							htIgnoreMS.Add (miGet, miMS);
						MemberInfo miSet = pi.GetSetMethod ();
						if (miSet != null)
							htIgnoreMS.Add (miSet, miMS);
					}
					else if (miMS.MemberType == MemberTypes.Event)
					{
						EventInfo ei = (EventInfo) miMS;
						MemberInfo miAdd = ei.GetAddMethod ();
						if (miAdd != null)
							htIgnoreMS.Add (miAdd, miMS);
						MemberInfo miRemove = ei.GetRemoveMethod ();
						if (miRemove != null)
							htIgnoreMS.Add (miRemove, miMS);
						MemberInfo miRaise = ei.GetRaiseMethod ();
						if (miRaise != null)
							htIgnoreMS.Add (miRaise, miMS);
					}
				}
				foreach (MemberInfo miMS in colMembersMS)
				{
					if (miMS != null && miMS.DeclaringType == typeMS && !htIgnoreMS.Contains (miMS))
					{
						string strNameUnique = MissingMember.GetUniqueName (miMS);
						MemberInfo miMono = (MemberInfo) htMono [strNameUnique];

						MissingMember mm = CreateMember (miMono, miMS);

						bool fVisibleMS = IsVisible (miMS);
						if (miMono == null)
						{
							if (fVisibleMS)
								AddMember (mm);
						}
						else
						{
							if (miMono.MemberType != miMS.MemberType)
							{
								//AddMember (null, miMS);
								//MissingMember mm2 = CreateMember (miMono, null);
								//mm2.Status.AddWarning ("MemberType mismatch, is: '" + miMono.MemberType.ToString () + "' [should be: '" + miMS.MemberType.ToString ()+"']");
								//AddMember (mm2);
								mm.Status.AddWarning ("MemberType mismatch, is: '" + miMono.MemberType.ToString () + "' [should be: '" + miMS.MemberType.ToString ()+"']");
								AddMember (mm);
							}
							else if (fVisibleMS || IsVisible (miMono))
							{
								AddMember (mm);
							}

							htMono.Remove (strNameUnique);
						}

						switch (miMS.MemberType)
						{
							case MemberTypes.Method:
							{
								string strNameMSFull = miMS.ToString ();
								int ichMS = strNameMSFull.IndexOf (' ');
								string strNameMS = strNameMSFull.Substring (ichMS + 1);
								if (!htMethodsMS.Contains (strNameMS))
									htMethodsMS.Add (strNameMSFull.Substring (ichMS + 1), miMS);
								break;
							}
						}
					}
				}
			}
			foreach (MemberInfo miMono in htMono.Values)
			{
				if (IsVisible (miMono))
				{
					MissingMember mm = CreateMember (miMono, null);
					switch (miMono.MemberType)
					{
						case MemberTypes.Method:
						{
							string strNameMonoFull = miMono.ToString ();
							int ichMono = strNameMonoFull.IndexOf (' ');
							string strNameMono = strNameMonoFull.Substring (ichMono + 1);
							MemberInfo miMS = (MemberInfo) htMethodsMS [strNameMono];
							if (miMS != null)
							{
								string strNameMSFull = miMS.ToString ();
								int ichMS = strNameMSFull.IndexOf (' ');
								string strReturnTypeMS = strNameMSFull.Substring (0, ichMS);
								string strReturnTypeMono = strNameMonoFull.Substring (0, ichMono);
								mm.Status.AddWarning ("Return type mismatch, is: '"+strReturnTypeMono+"' [should be: '"+strReturnTypeMS+"']");
								//Console.WriteLine ("WARNING: Return type mismatch on "+miMS.DeclaringType.FullName+"."+strNameMono+", is: '"+strReturnTypeMono+"' [should be: '"+strReturnTypeMS+"']");
							}
							break;
						}
					}
					AddMember (mm);
				}
			}

			// compare the attributes
			rgAttributes = new ArrayList ();
			nsAttributes = MissingAttribute.AnalyzeAttributes (
				(typeMono == null) ? null : typeMono.GetCustomAttributes (false),
				(  typeMS == null) ? null :   typeMS.GetCustomAttributes (false),
				rgAttributes);

			rgInterfaces = new ArrayList ();
			if (typeMono != null && typeMS != null)
			{
				// compare base types
				string strBaseMono = (typeMono.BaseType == null) ? null : typeMono.BaseType.FullName;
				string strBaseMS   = (  typeMS.BaseType == null) ? null :   typeMS.BaseType.FullName;
				if (strBaseMono != strBaseMS)
				{
					m_nodeStatus.AddWarning ("Base class mismatch, is '"+strBaseMono+"' [should be: '"+strBaseMS+"']");
					//Console.WriteLine ("WARNING: Base class mismatch on "+typeMono.FullName+", is: '"+strBaseMono+"' [should be: '"+strBaseMS+"']");
				}

				// compare the interfaces
				Hashtable htInterfacesMono = new Hashtable ();
				Type [] rgInterfacesMono = typeMono.GetInterfaces ();
				foreach (Type ifaceMono in rgInterfacesMono)
				{
					if (ifaceMono != null)
					{
						string strName = ifaceMono.FullName;
						htInterfacesMono.Add (strName, ifaceMono);
					}
				}
				Type [] rgInterfacesMS = typeMS.GetInterfaces ();
				foreach (Type ifaceMS in rgInterfacesMS)
				{
					if (ifaceMS != null)
					{
						string strName = ifaceMS.FullName;
						Type ifaceMono = (Type) htInterfacesMono [strName];
						MissingInterface mi = new MissingInterface (ifaceMono, ifaceMS);
						mi.Analyze ();
						rgInterfaces.Add (mi);
						if (ifaceMono != null)
							htInterfacesMono.Remove (strName);
						nsInterfaces.AddChildren (mi.Status);
					}
				}
				foreach (Type ifaceMono in htInterfacesMono.Values)
				{
					MissingInterface mi = new MissingInterface (ifaceMono, null);
					mi.Analyze ();
					rgInterfaces.Add (mi);
					//Console.WriteLine ("WARNING: additional interface on "+typeMono.FullName+": '"+ifaceMono.FullName+"'");
					nsInterfaces.AddChildren (mi.Status);
				}

				// serializable attribute
				AddFakeAttribute (typeMono.IsSerializable, typeMS.IsSerializable, "System.SerializableAttribute");
				AddFakeAttribute (typeMono.IsAutoLayout, typeMS.IsAutoLayout, "System.AutoLayoutAttribute");
				AddFakeAttribute (typeMono.IsExplicitLayout, typeMS.IsExplicitLayout, "System.ExplicitLayoutAttribute");
				AddFakeAttribute (typeMono.IsLayoutSequential, typeMS.IsLayoutSequential, "System.SequentialLayoutAttribute");

				Accessibility accessibilityMono = GetAccessibility (typeMono);
				Accessibility accessibilityMS   = GetAccessibility (typeMS);
				if (accessibilityMono != accessibilityMS)
					m_nodeStatus.AddWarning ("Should be "+AccessibilityToString (accessibilityMono));

				AddFlagWarning (typeMono.IsSealed, typeMS.IsSealed, "sealed");
				AddFlagWarning (typeMono.IsAbstract, typeMS.IsAbstract, "abstract");
			}

			// sum up the sub-sections
			m_nodeStatus.Add (nsAttributes);
			m_nodeStatus.Add (nsMethods);
			m_nodeStatus.Add (nsProperties);
			m_nodeStatus.Add (nsEvents);
			m_nodeStatus.Add (nsFields);
			m_nodeStatus.Add (nsConstructors);
			m_nodeStatus.Add (nsNestedTypes);
			m_nodeStatus.Add (nsInterfaces);

			return m_nodeStatus;
		}

		static bool IsVisible (MemberInfo mi)
		{
			// this is just embarrasing, couldn't they have virtualized this?
			switch (mi.MemberType)
			{
				case MemberTypes.Constructor:
				case MemberTypes.Method:
					return !((MethodBase) mi).IsPrivate && !((MethodBase) mi).IsFamilyAndAssembly && !((MethodBase) mi).IsAssembly;
				case MemberTypes.Field:
					return !((FieldInfo) mi).IsPrivate && !((FieldInfo) mi).IsFamilyAndAssembly && !((FieldInfo) mi).IsAssembly;
				case MemberTypes.NestedType:
					return !((Type) mi).IsNestedPrivate;
				case MemberTypes.Property:	// great, now we have to look at the methods
					PropertyInfo pi = (PropertyInfo) mi;
					MethodInfo miAccessor = pi.GetGetMethod ();
					if (miAccessor == null)
						miAccessor = pi.GetSetMethod ();
					if (miAccessor == null)
						return false;
					return IsVisible (miAccessor);
				case MemberTypes.Event:	// ditto
					EventInfo ei = (EventInfo) mi;
					MethodInfo eiAccessor = ei.GetAddMethod ();
					if (eiAccessor == null)
						eiAccessor = ei.GetRemoveMethod ();
					if (eiAccessor == null)
						eiAccessor = ei.GetRaiseMethod ();
					if (eiAccessor == null)
						return false;
					return IsVisible (eiAccessor);
				default:
					throw new Exception ("Missing handler for MemberType: "+mi.MemberType.ToString ());
			}
		}

		static Accessibility GetAccessibility (Type type)
		{
			if (type.IsPublic)
				return Accessibility.Public;
			else if (type.IsNotPublic)
				return Accessibility.Private;
			return MissingMember.GetAccessibility (type);
		}
	}
}