2 // mono-api-info.cs - Dumps public assembly information to an xml file.
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Copyright (C) 2003-2008 Novell, Inc (http://www.novell.com)
11 using System.Collections;
12 using System.Collections.Generic;
13 using System.Globalization;
15 using System.Runtime.CompilerServices;
16 using System.Runtime.InteropServices;
17 using System.Security.Permissions;
29 public static int Main (string [] args)
36 AssemblyCollection acoll = new AssemblyCollection ();
38 string windir = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
39 string pf = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
40 TypeHelper.Resolver.AddSearchDirectory (Path.Combine (windir, @"assembly\GAC\MSDATASRC\7.0.3300.0__b03f5f7f11d50a3a"));
42 foreach (string arg in args) {
48 if (arg.Contains ("v3.0")) {
49 TypeHelper.Resolver.AddSearchDirectory (Path.Combine (windir, @"Microsoft.NET\Framework\v2.0.50727"));
50 } else if (arg.Contains ("v3.5")) {
51 TypeHelper.Resolver.AddSearchDirectory (Path.Combine (windir, @"Microsoft.NET\Framework\v2.0.50727"));
52 TypeHelper.Resolver.AddSearchDirectory (Path.Combine (windir, @"Microsoft.NET\Framework\v3.0\Windows Communication Foundation"));
53 } else if (arg.Contains ("v4.0")) {
54 if (arg.Contains ("Silverlight")) {
55 TypeHelper.Resolver.AddSearchDirectory (Path.Combine (pf, @"Microsoft Silverlight\4.0.51204.0"));
57 TypeHelper.Resolver.AddSearchDirectory (Path.Combine (windir, @"Microsoft.NET\Framework\v4.0.30319"));
58 TypeHelper.Resolver.AddSearchDirectory (Path.Combine (windir, @"Microsoft.NET\Framework\v4.0.30319\WPF"));
65 XmlDocument doc = new XmlDocument ();
69 var writer = new WellFormedXmlWriter (new XmlTextWriter (Console.Out) { Formatting = Formatting.Indented });
70 XmlNode decl = doc.CreateXmlDeclaration ("1.0", "utf-8", null);
71 doc.InsertBefore (decl, doc.DocumentElement);
76 internal static bool AbiMode { get; private set; }
81 public static string CleanupTypeName (TypeReference type)
83 return CleanupTypeName (type.FullName);
86 public static string CleanupTypeName (string t)
88 return t.Replace ('<', '[').Replace ('>', ']').Replace ('/', '+');
92 class AssemblyCollection
95 List<AssemblyDefinition> assemblies = new List<AssemblyDefinition> ();
97 public AssemblyCollection ()
101 public bool Add (string name)
103 AssemblyDefinition ass = LoadAssembly (name);
105 Console.Error.WriteLine ("Cannot load assembly file " + name);
109 assemblies.Add (ass);
113 public void DoOutput ()
115 if (document == null)
116 throw new InvalidOperationException ("Document not set");
118 XmlNode nassemblies = document.CreateElement ("assemblies", null);
119 document.AppendChild (nassemblies);
120 foreach (AssemblyDefinition a in assemblies) {
121 AssemblyData data = new AssemblyData (document, nassemblies, a);
126 public XmlDocument Document {
127 set { document = value; }
130 AssemblyDefinition LoadAssembly (string assembly)
133 return TypeHelper.Resolver.Resolve (assembly);
140 abstract class BaseData
142 protected XmlDocument document;
143 protected XmlNode parent;
145 protected BaseData (XmlDocument doc, XmlNode parent)
148 this.parent = parent;
151 public abstract void DoOutput ();
153 protected void AddAttribute (XmlNode node, string name, string value)
155 XmlAttribute attr = document.CreateAttribute (name);
157 node.Attributes.Append (attr);
161 class TypeForwardedToData : BaseData
163 AssemblyDefinition ass;
165 public TypeForwardedToData (XmlDocument document, XmlNode parent, AssemblyDefinition ass)
166 : base (document, parent)
171 public override void DoOutput ()
173 XmlNode natts = parent.SelectSingleNode("attributes");
175 natts = document.CreateElement ("attributes", null);
176 parent.AppendChild (natts);
179 foreach (ExportedType type in ass.MainModule.ExportedTypes) {
181 if (((uint)type.Attributes & 0x200000u) == 0)
184 XmlNode node = document.CreateElement ("attribute");
185 AddAttribute (node, "name", typeof (TypeForwardedToAttribute).FullName);
186 XmlNode properties = node.AppendChild (document.CreateElement ("properties"));
187 XmlNode property = properties.AppendChild (document.CreateElement ("property"));
188 AddAttribute (property, "name", "Destination");
189 AddAttribute (property, "value", Utils.CleanupTypeName (type.FullName));
190 natts.AppendChild (node);
194 public static void OutputForwarders (XmlDocument document, XmlNode parent, AssemblyDefinition ass)
196 TypeForwardedToData tftd = new TypeForwardedToData (document, parent, ass);
201 class AssemblyData : BaseData
203 AssemblyDefinition ass;
205 public AssemblyData (XmlDocument document, XmlNode parent, AssemblyDefinition ass)
206 : base (document, parent)
211 public override void DoOutput ()
213 if (document == null)
214 throw new InvalidOperationException ("Document not set");
216 XmlNode nassembly = document.CreateElement ("assembly", null);
217 AssemblyNameDefinition aname = ass.Name;
218 AddAttribute (nassembly, "name", aname.Name);
219 AddAttribute (nassembly, "version", aname.Version.ToString ());
220 parent.AppendChild (nassembly);
221 TypeForwardedToData.OutputForwarders (document, nassembly, ass);
222 AttributeData.OutputAttributes (document, nassembly, ass.CustomAttributes);
223 var typesCollection = ass.MainModule.Types;
224 if (typesCollection == null || typesCollection.Count == 0)
226 object [] typesArray = new object [typesCollection.Count];
227 for (int i = 0; i < typesCollection.Count; i++) {
228 typesArray [i] = typesCollection [i];
230 Array.Sort (typesArray, TypeReferenceComparer.Default);
232 XmlNode nss = document.CreateElement ("namespaces", null);
233 nassembly.AppendChild (nss);
235 string current_namespace = "$%&$&";
237 XmlNode classes = null;
238 foreach (TypeDefinition t in typesArray) {
239 if (string.IsNullOrEmpty (t.Namespace))
242 if (!Driver.AbiMode && ((t.Attributes & TypeAttributes.VisibilityMask) != TypeAttributes.Public))
245 if (t.DeclaringType != null)
246 continue; // enforce !nested
248 if (t.Namespace != current_namespace) {
249 current_namespace = t.Namespace;
250 ns = document.CreateElement ("namespace", null);
251 AddAttribute (ns, "name", current_namespace);
252 nss.AppendChild (ns);
253 classes = document.CreateElement ("classes", null);
254 ns.AppendChild (classes);
257 TypeData bd = new TypeData (document, classes, t);
263 abstract class MemberData : BaseData
265 MemberReference [] members;
267 public MemberData (XmlDocument document, XmlNode parent, MemberReference [] members)
268 : base (document, parent)
270 this.members = members;
273 public override void DoOutput ()
275 XmlNode mclass = document.CreateElement (ParentTag, null);
276 parent.AppendChild (mclass);
278 foreach (MemberReference member in members) {
279 XmlNode mnode = document.CreateElement (Tag, null);
280 mclass.AppendChild (mnode);
281 AddAttribute (mnode, "name", GetName (member));
282 if (!NoMemberAttributes)
283 AddAttribute (mnode, "attrib", GetMemberAttributes (member));
285 AttributeData.OutputAttributes (document, mnode, GetCustomAttributes (member));
287 AddExtraData (mnode, member);
292 protected abstract IList<CustomAttribute> GetCustomAttributes (MemberReference member);
294 protected virtual void AddExtraData (XmlNode p, MemberReference memberDefenition)
298 protected virtual string GetName (MemberReference memberDefenition)
303 protected virtual string GetMemberAttributes (MemberReference memberDefenition)
308 public virtual bool NoMemberAttributes {
309 get { return false; }
313 public virtual string ParentTag {
314 get { return "NoPARENTTAG"; }
317 public virtual string Tag {
318 get { return "NoTAG"; }
321 public static void OutputGenericParameters (XmlDocument document, XmlNode nclass, IGenericParameterProvider provider)
323 if (provider.GenericParameters.Count == 0)
326 var gparameters = provider.GenericParameters;
328 XmlElement ngeneric = document.CreateElement (string.Format ("generic-parameters"));
329 nclass.AppendChild (ngeneric);
331 foreach (GenericParameter gp in gparameters) {
332 XmlElement nparam = document.CreateElement (string.Format ("generic-parameter"));
333 nparam.SetAttribute ("name", gp.Name);
334 nparam.SetAttribute ("attributes", ((int) gp.Attributes).ToString ());
336 AttributeData.OutputAttributes (document, nparam, gp.CustomAttributes);
338 ngeneric.AppendChild (nparam);
340 var constraints = gp.Constraints;
341 if (constraints.Count == 0)
344 XmlElement nconstraint = document.CreateElement ("generic-parameter-constraints");
346 foreach (TypeReference constraint in constraints) {
347 XmlElement ncons = document.CreateElement ("generic-parameter-constraint");
348 ncons.SetAttribute ("name", Utils.CleanupTypeName (constraint));
349 nconstraint.AppendChild (ncons);
352 nparam.AppendChild (nconstraint);
357 class TypeData : MemberData
361 public TypeData (XmlDocument document, XmlNode parent, TypeDefinition type)
362 : base (document, parent, null)
367 protected override IList<CustomAttribute> GetCustomAttributes (MemberReference member) {
368 return ((TypeDefinition) member).CustomAttributes;
371 public override void DoOutput ()
373 if (document == null)
374 throw new InvalidOperationException ("Document not set");
376 XmlNode nclass = document.CreateElement ("class", null);
377 AddAttribute (nclass, "name", type.Name);
378 string classType = GetClassType (type);
379 AddAttribute (nclass, "type", classType);
381 if (type.BaseType != null)
382 AddAttribute (nclass, "base", Utils.CleanupTypeName (type.BaseType));
385 AddAttribute (nclass, "sealed", "true");
388 AddAttribute (nclass, "abstract", "true");
390 if ( (type.Attributes & TypeAttributes.Serializable) != 0 || type.IsEnum)
391 AddAttribute (nclass, "serializable", "true");
393 string charSet = GetCharSet (type);
394 AddAttribute (nclass, "charset", charSet);
396 string layout = GetLayout (type);
398 AddAttribute (nclass, "layout", layout);
400 if (type.PackingSize >= 0) {
401 AddAttribute (nclass, "pack", type.PackingSize.ToString ());
404 if (type.ClassSize >= 0) {
405 AddAttribute (nclass, "size", type.ClassSize.ToString ());
408 parent.AppendChild (nclass);
410 AttributeData.OutputAttributes (document, nclass, GetCustomAttributes(type));
412 XmlNode ifaces = null;
414 foreach (TypeReference iface in TypeHelper.GetInterfaces (type)) {
415 if (!TypeHelper.IsPublic (iface))
416 // we're only interested in public interfaces
419 if (ifaces == null) {
420 ifaces = document.CreateElement ("interfaces", null);
421 nclass.AppendChild (ifaces);
424 XmlNode iface_node = document.CreateElement ("interface", null);
425 AddAttribute (iface_node, "name", Utils.CleanupTypeName (iface));
426 ifaces.AppendChild (iface_node);
429 MemberData.OutputGenericParameters (document, nclass, type);
431 ArrayList members = new ArrayList ();
433 FieldDefinition [] fields = GetFields (type);
434 if (fields.Length > 0) {
435 Array.Sort (fields, MemberReferenceComparer.Default);
436 FieldData fd = new FieldData (document, nclass, fields);
441 var value_type = GetEnumValueField (type);
442 if (value_type == null)
443 throw new NotSupportedException ();
445 AddAttribute (nclass, "enumtype", Utils.CleanupTypeName (value_type.FieldType));
448 if (!Driver.AbiMode) {
450 MethodDefinition [] ctors = GetConstructors (type);
451 if (ctors.Length > 0) {
452 Array.Sort (ctors, MemberReferenceComparer.Default);
453 members.Add (new ConstructorData (document, nclass, ctors));
456 PropertyDefinition[] properties = GetProperties (type);
457 if (properties.Length > 0) {
458 Array.Sort (properties, MemberReferenceComparer.Default);
459 members.Add (new PropertyData (document, nclass, properties));
462 EventDefinition [] events = GetEvents (type);
463 if (events.Length > 0) {
464 Array.Sort (events, MemberReferenceComparer.Default);
465 members.Add (new EventData (document, nclass, events));
468 MethodDefinition [] methods = GetMethods (type);
469 if (methods.Length > 0) {
470 Array.Sort (methods, MemberReferenceComparer.Default);
471 members.Add (new MethodData (document, nclass, methods));
475 foreach (MemberData md in members)
478 var nested = type.NestedTypes;
479 //remove non public(familiy) and nested in second degree
480 for (int i = nested.Count - 1; i >= 0; i--) {
481 TypeDefinition t = nested [i];
482 if ((t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic ||
483 (t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily ||
484 (t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem) {
486 if (t.DeclaringType == type)
487 continue; // not nested of nested
494 if (nested.Count > 0) {
495 XmlNode classes = document.CreateElement ("classes", null);
496 nclass.AppendChild (classes);
497 foreach (TypeDefinition t in nested) {
498 TypeData td = new TypeData (document, classes, t);
504 static FieldReference GetEnumValueField (TypeDefinition type)
506 foreach (FieldDefinition field in type.Fields)
507 if (field.IsSpecialName && field.Name == "value__")
513 protected override string GetMemberAttributes (MemberReference member)
516 throw new InvalidOperationException ("odd");
518 return ((int) type.Attributes).ToString (CultureInfo.InvariantCulture);
521 public static bool MustDocumentMethod (MethodDefinition method) {
523 MethodAttributes maskedAccess = method.Attributes & MethodAttributes.MemberAccessMask;
524 return maskedAccess == MethodAttributes.Public
525 || maskedAccess == MethodAttributes.Family
526 || maskedAccess == MethodAttributes.FamORAssem;
529 static string GetClassType (TypeDefinition t)
540 if (TypeHelper.IsDelegate(t))
546 static string GetCharSet (TypeDefinition type)
548 TypeAttributes maskedStringFormat = type.Attributes & TypeAttributes.StringFormatMask;
549 if (maskedStringFormat == TypeAttributes.AnsiClass)
550 return CharSet.Ansi.ToString ();
552 if (maskedStringFormat == TypeAttributes.AutoClass)
553 return CharSet.Auto.ToString ();
555 if (maskedStringFormat == TypeAttributes.UnicodeClass)
556 return CharSet.Unicode.ToString ();
558 return CharSet.None.ToString ();
561 static string GetLayout (TypeDefinition type)
563 TypeAttributes maskedLayout = type.Attributes & TypeAttributes.LayoutMask;
564 if (maskedLayout == TypeAttributes.AutoLayout)
565 return LayoutKind.Auto.ToString ();
567 if (maskedLayout == TypeAttributes.ExplicitLayout)
568 return LayoutKind.Explicit.ToString ();
570 if (maskedLayout == TypeAttributes.SequentialLayout)
571 return LayoutKind.Sequential.ToString ();
576 FieldDefinition [] GetFields (TypeDefinition type) {
577 ArrayList list = new ArrayList ();
579 var fields = type.Fields;
580 foreach (FieldDefinition field in fields) {
581 if (field.IsSpecialName)
584 if (Driver.AbiMode && field.IsStatic)
587 // we're only interested in public or protected members
588 FieldAttributes maskedVisibility = (field.Attributes & FieldAttributes.FieldAccessMask);
589 if (Driver.AbiMode && !field.IsNotSerialized) {
592 if (maskedVisibility == FieldAttributes.Public
593 || maskedVisibility == FieldAttributes.Family
594 || maskedVisibility == FieldAttributes.FamORAssem) {
600 return (FieldDefinition []) list.ToArray (typeof (FieldDefinition));
604 internal static PropertyDefinition [] GetProperties (TypeDefinition type) {
605 ArrayList list = new ArrayList ();
607 var properties = type.Properties;//type.GetProperties (flags);
608 foreach (PropertyDefinition property in properties) {
609 MethodDefinition getMethod = property.GetMethod;
610 MethodDefinition setMethod = property.SetMethod;
612 bool hasGetter = (getMethod != null) && MustDocumentMethod (getMethod);
613 bool hasSetter = (setMethod != null) && MustDocumentMethod (setMethod);
615 // if neither the getter or setter should be documented, then
617 if (hasGetter || hasSetter) {
622 return (PropertyDefinition []) list.ToArray (typeof (PropertyDefinition));
625 private MethodDefinition[] GetMethods (TypeDefinition type)
627 ArrayList list = new ArrayList ();
629 var methods = type.Methods;//type.GetMethods (flags);
630 foreach (MethodDefinition method in methods) {
631 if (method.IsSpecialName && !method.Name.StartsWith ("op_"))
634 // we're only interested in public or protected members
635 if (!MustDocumentMethod(method))
638 if (IsFinalizer (method)) {
639 string name = method.DeclaringType.Name;
640 int arity = name.IndexOf ('`');
642 name = name.Substring (0, arity);
644 method.Name = "~" + name;
650 return (MethodDefinition []) list.ToArray (typeof (MethodDefinition));
653 static bool IsFinalizer (MethodDefinition method)
655 if (method.Name != "Finalize")
658 if (!method.IsVirtual)
661 if (method.Parameters.Count != 0)
667 private MethodDefinition [] GetConstructors (TypeDefinition type)
669 ArrayList list = new ArrayList ();
671 var ctors = type.Methods.Where (m => m.IsConstructor);//type.GetConstructors (flags);
672 foreach (MethodDefinition constructor in ctors) {
673 // we're only interested in public or protected members
674 if (!MustDocumentMethod(constructor))
677 list.Add (constructor);
680 return (MethodDefinition []) list.ToArray (typeof (MethodDefinition));
683 private EventDefinition[] GetEvents (TypeDefinition type)
685 ArrayList list = new ArrayList ();
687 var events = type.Events;//type.GetEvents (flags);
688 foreach (EventDefinition eventDef in events) {
689 MethodDefinition addMethod = eventDef.AddMethod;//eventInfo.GetAddMethod (true);
691 if (addMethod == null || !MustDocumentMethod (addMethod))
697 return (EventDefinition []) list.ToArray (typeof (EventDefinition));
701 class FieldData : MemberData
703 public FieldData (XmlDocument document, XmlNode parent, FieldDefinition [] members)
704 : base (document, parent, members)
708 protected override IList<CustomAttribute> GetCustomAttributes (MemberReference member) {
709 return ((FieldDefinition) member).CustomAttributes;
712 protected override string GetName (MemberReference memberDefenition)
714 FieldDefinition field = (FieldDefinition) memberDefenition;
718 protected override string GetMemberAttributes (MemberReference memberDefenition)
720 FieldDefinition field = (FieldDefinition) memberDefenition;
721 return ((int) field.Attributes).ToString (CultureInfo.InvariantCulture);
724 protected override void AddExtraData (XmlNode p, MemberReference memberDefenition)
726 base.AddExtraData (p, memberDefenition);
727 FieldDefinition field = (FieldDefinition) memberDefenition;
728 AddAttribute (p, "fieldtype", Utils.CleanupTypeName (field.FieldType));
730 if (field.IsLiteral) {
731 object value = field.Constant;//object value = field.GetValue (null);
732 string stringValue = null;
733 //if (value is Enum) {
734 // // FIXME: when Mono bug #60090 has been
735 // // fixed, we should just be able to use
736 // // Convert.ToString
737 // stringValue = ((Enum) value).ToString ("D", CultureInfo.InvariantCulture);
740 stringValue = Convert.ToString (value, CultureInfo.InvariantCulture);
743 if (stringValue != null)
744 AddAttribute (p, "value", stringValue);
748 public override string ParentTag {
749 get { return "fields"; }
752 public override string Tag {
753 get { return "field"; }
757 class PropertyData : MemberData
759 public PropertyData (XmlDocument document, XmlNode parent, PropertyDefinition [] members)
760 : base (document, parent, members)
764 protected override IList<CustomAttribute> GetCustomAttributes (MemberReference member) {
765 return ((PropertyDefinition) member).CustomAttributes;
768 protected override string GetName (MemberReference memberDefenition)
770 PropertyDefinition prop = (PropertyDefinition) memberDefenition;
774 protected override void AddExtraData (XmlNode p, MemberReference memberDefenition)
776 base.AddExtraData (p, memberDefenition);
777 PropertyDefinition prop = (PropertyDefinition) memberDefenition;
778 AddAttribute (p, "ptype", Utils.CleanupTypeName (prop.PropertyType));
779 MethodDefinition _get = prop.GetMethod;
780 MethodDefinition _set = prop.SetMethod;
781 bool haveGet = (_get != null && TypeData.MustDocumentMethod(_get));
782 bool haveSet = (_set != null && TypeData.MustDocumentMethod(_set));
783 MethodDefinition [] methods;
785 if (haveGet && haveSet) {
786 methods = new MethodDefinition [] { _get, _set };
787 } else if (haveGet) {
788 methods = new MethodDefinition [] { _get };
789 } else if (haveSet) {
790 methods = new MethodDefinition [] { _set };
796 string parms = Parameters.GetSignature (methods [0].Parameters);
797 if (!string.IsNullOrEmpty (parms))
798 AddAttribute (p, "params", parms);
800 MethodData data = new MethodData (document, p, methods);
801 //data.NoMemberAttributes = true;
805 protected override string GetMemberAttributes (MemberReference memberDefenition)
807 PropertyDefinition prop = (PropertyDefinition) memberDefenition;
808 return ((int) prop.Attributes).ToString (CultureInfo.InvariantCulture);
811 public override string ParentTag {
812 get { return "properties"; }
815 public override string Tag {
816 get { return "property"; }
820 class EventData : MemberData
822 public EventData (XmlDocument document, XmlNode parent, EventDefinition [] members)
823 : base (document, parent, members)
827 protected override IList<CustomAttribute> GetCustomAttributes (MemberReference member) {
828 return ((EventDefinition) member).CustomAttributes;
831 protected override string GetName (MemberReference memberDefenition)
833 EventDefinition evt = (EventDefinition) memberDefenition;
837 protected override string GetMemberAttributes (MemberReference memberDefenition)
839 EventDefinition evt = (EventDefinition) memberDefenition;
840 return ((int) evt.Attributes).ToString (CultureInfo.InvariantCulture);
843 protected override void AddExtraData (XmlNode p, MemberReference memberDefenition)
845 base.AddExtraData (p, memberDefenition);
846 EventDefinition evt = (EventDefinition) memberDefenition;
847 AddAttribute (p, "eventtype", Utils.CleanupTypeName (evt.EventType));
850 public override string ParentTag {
851 get { return "events"; }
854 public override string Tag {
855 get { return "event"; }
859 class MethodData : MemberData
863 public MethodData (XmlDocument document, XmlNode parent, MethodDefinition [] members)
864 : base (document, parent, members)
868 protected override IList<CustomAttribute> GetCustomAttributes (MemberReference member) {
869 return ((MethodDefinition) member).CustomAttributes;
872 protected override string GetName (MemberReference memberDefenition)
874 MethodDefinition method = (MethodDefinition) memberDefenition;
875 string name = method.Name;
876 string parms = Parameters.GetSignature (method.Parameters);
878 return string.Format ("{0}({1})", name, parms);
881 protected override string GetMemberAttributes (MemberReference memberDefenition)
883 MethodDefinition method = (MethodDefinition) memberDefenition;
884 return ((int)( method.Attributes)).ToString (CultureInfo.InvariantCulture);
887 protected override void AddExtraData (XmlNode p, MemberReference memberDefenition)
889 base.AddExtraData (p, memberDefenition);
891 if (!(memberDefenition is MethodDefinition))
894 MethodDefinition mbase = (MethodDefinition) memberDefenition;
896 ParameterData parms = new ParameterData (document, p, mbase.Parameters);
899 if (mbase.IsAbstract)
900 AddAttribute (p, "abstract", "true");
902 AddAttribute (p, "virtual", "true");
904 AddAttribute (p, "static", "true");
906 string rettype = Utils.CleanupTypeName (mbase.MethodReturnType.ReturnType);
907 if (rettype != "System.Void" || !mbase.IsConstructor)
908 AddAttribute (p, "returntype", (rettype));
910 AttributeData.OutputAttributes (document, p, mbase.MethodReturnType.CustomAttributes);
912 MemberData.OutputGenericParameters (document, p, mbase);
915 public override bool NoMemberAttributes {
916 get { return noAtts; }
917 set { noAtts = value; }
920 public override string ParentTag {
921 get { return "methods"; }
924 public override string Tag {
925 get { return "method"; }
929 class ConstructorData : MethodData
931 public ConstructorData (XmlDocument document, XmlNode parent, MethodDefinition [] members)
932 : base (document, parent, members)
936 public override string ParentTag {
937 get { return "constructors"; }
940 public override string Tag {
941 get { return "constructor"; }
945 class ParameterData : BaseData
947 private IList<ParameterDefinition> parameters;
949 public ParameterData (XmlDocument document, XmlNode parent, IList<ParameterDefinition> parameters)
950 : base (document, parent)
952 this.parameters = parameters;
955 public override void DoOutput ()
957 XmlNode parametersNode = document.CreateElement ("parameters");
958 parent.AppendChild (parametersNode);
960 foreach (ParameterDefinition parameter in parameters) {
961 XmlNode paramNode = document.CreateElement ("parameter");
962 parametersNode.AppendChild (paramNode);
963 AddAttribute (paramNode, "name", parameter.Name);
964 AddAttribute (paramNode, "position", parameter.Method.Parameters.IndexOf(parameter).ToString(CultureInfo.InvariantCulture));
965 AddAttribute (paramNode, "attrib", ((int) parameter.Attributes).ToString());
967 string direction = "in";
969 if (parameter.ParameterType is ByReferenceType)
970 direction = parameter.IsOut ? "out" : "ref";
972 TypeReference t = parameter.ParameterType;
973 AddAttribute (paramNode, "type", Utils.CleanupTypeName (t));
975 if (parameter.IsOptional) {
976 AddAttribute (paramNode, "optional", "true");
977 if (parameter.HasConstant)
978 AddAttribute (paramNode, "defaultValue", parameter.Constant == null ? "NULL" : parameter.Constant.ToString ());
981 if (direction != "in")
982 AddAttribute (paramNode, "direction", direction);
984 AttributeData.OutputAttributes (document, paramNode, parameter.CustomAttributes);
989 class AttributeData : BaseData
991 IList<CustomAttribute> atts;
993 AttributeData (XmlDocument doc, XmlNode parent, IList<CustomAttribute> attributes)
999 public override void DoOutput ()
1001 if (document == null)
1002 throw new InvalidOperationException ("Document not set");
1004 if (atts == null || atts.Count == 0)
1007 XmlNode natts = parent.SelectSingleNode("attributes");
1008 if (natts == null) {
1009 natts = document.CreateElement ("attributes", null);
1010 parent.AppendChild (natts);
1013 for (int i = 0; i < atts.Count; ++i) {
1014 CustomAttribute att = atts [i];
1016 string attName = Utils.CleanupTypeName (att.Constructor.DeclaringType);
1017 if (SkipAttribute (att))
1020 XmlNode node = document.CreateElement ("attribute");
1021 AddAttribute (node, "name", attName);
1023 XmlNode properties = null;
1025 Dictionary<string, object> attribute_mapping = CreateAttributeMapping (att);
1027 foreach (string name in attribute_mapping.Keys) {
1028 if (name == "TypeId")
1031 if (properties == null) {
1032 properties = node.AppendChild (document.CreateElement ("properties"));
1035 object o = attribute_mapping [name];
1037 XmlNode n = properties.AppendChild (document.CreateElement ("property"));
1038 AddAttribute (n, "name", name);
1041 AddAttribute (n, "value", "null");
1044 string value = o.ToString ();
1045 if (attName.EndsWith ("GuidAttribute"))
1046 value = value.ToUpper ();
1047 AddAttribute (n, "value", value);
1050 natts.AppendChild (node);
1054 static Dictionary<string, object> CreateAttributeMapping (CustomAttribute attribute)
1056 var mapping = new Dictionary<string, object> ();
1058 PopulateMapping (mapping, attribute);
1060 var constructor = attribute.Constructor.Resolve ();
1061 if (constructor == null || constructor.Parameters.Count == 0)
1064 PopulateMapping (mapping, constructor, attribute);
1069 static void PopulateMapping (Dictionary<string, object> mapping, CustomAttribute attribute)
1071 foreach (var named_argument in attribute.Properties) {
1072 var name = named_argument.Name;
1074 mapping.Add (name, GetArgumentValue (named_argument.Argument.Type, named_argument.Argument.Value));
1078 static Dictionary<FieldReference, int> CreateArgumentFieldMapping (MethodDefinition constructor)
1080 Dictionary<FieldReference, int> field_mapping = new Dictionary<FieldReference, int> ();
1082 int? argument = null;
1084 foreach (Instruction instruction in constructor.Body.Instructions) {
1085 switch (instruction.OpCode.Code) {
1097 argument = ((ParameterDefinition) instruction.Operand).Index + 1;
1101 FieldReference field = (FieldReference) instruction.Operand;
1102 if (field.DeclaringType.FullName != constructor.DeclaringType.FullName)
1105 if (!argument.HasValue)
1108 if (!field_mapping.ContainsKey (field))
1109 field_mapping.Add (field, (int) argument - 1);
1116 return field_mapping;
1119 static Dictionary<PropertyDefinition, FieldReference> CreatePropertyFieldMapping (TypeDefinition type)
1121 Dictionary<PropertyDefinition, FieldReference> property_mapping = new Dictionary<PropertyDefinition, FieldReference> ();
1123 foreach (PropertyDefinition property in type.Properties) {
1124 if (property.GetMethod == null)
1126 if (!property.GetMethod.HasBody)
1129 foreach (Instruction instruction in property.GetMethod.Body.Instructions) {
1130 if (instruction.OpCode.Code != Code.Ldfld)
1133 FieldReference field = (FieldReference) instruction.Operand;
1134 if (field.DeclaringType.FullName != type.FullName)
1137 property_mapping.Add (property, field);
1142 return property_mapping;
1145 static void PopulateMapping (Dictionary<string, object> mapping, MethodDefinition constructor, CustomAttribute attribute)
1147 if (!constructor.HasBody)
1150 var field_mapping = CreateArgumentFieldMapping (constructor);
1151 var property_mapping = CreatePropertyFieldMapping ((TypeDefinition) constructor.DeclaringType);
1153 foreach (var pair in property_mapping) {
1155 if (!field_mapping.TryGetValue (pair.Value, out argument))
1158 var ca_arg = attribute.ConstructorArguments [argument];
1159 mapping.Add (pair.Key.Name, GetArgumentValue (ca_arg.Type, ca_arg.Value));
1163 static object GetArgumentValue (TypeReference reference, object value)
1165 var type = reference.Resolve ();
1170 if (IsFlaggedEnum (type))
1171 return GetFlaggedEnumValue (type, value);
1173 return GetEnumValue (type, value);
1179 static bool IsFlaggedEnum (TypeDefinition type)
1184 if (type.CustomAttributes.Count == 0)
1187 foreach (CustomAttribute attribute in type.CustomAttributes)
1188 if (attribute.Constructor.DeclaringType.FullName == "System.FlagsAttribute")
1194 static object GetFlaggedEnumValue (TypeDefinition type, object value)
1196 long flags = Convert.ToInt64 (value);
1197 var signature = new StringBuilder ();
1199 for (int i = type.Fields.Count - 1; i >= 0; i--) {
1200 FieldDefinition field = type.Fields [i];
1202 if (!field.HasConstant)
1205 long flag = Convert.ToInt64 (field.Constant);
1210 if ((flags & flag) == flag) {
1211 if (signature.Length != 0)
1212 signature.Append (", ");
1214 signature.Append (field.Name);
1219 return signature.ToString ();
1222 static object GetEnumValue (TypeDefinition type, object value)
1224 foreach (FieldDefinition field in type.Fields) {
1225 if (!field.HasConstant)
1228 if (Comparer.Default.Compare (field.Constant, value) == 0)
1235 static bool SkipAttribute (CustomAttribute attribute)
1237 var type_name = Utils.CleanupTypeName (attribute.Constructor.DeclaringType);
1239 return !TypeHelper.IsPublic (attribute)
1240 || type_name.EndsWith ("TODOAttribute");
1243 public static void OutputAttributes (XmlDocument doc, XmlNode parent, IList<CustomAttribute> attributes)
1245 AttributeData ad = new AttributeData (doc, parent, attributes);
1250 static class Parameters {
1252 public static string GetSignature (IList<ParameterDefinition> infos)
1254 if (infos == null || infos.Count == 0)
1257 var signature = new StringBuilder ();
1258 for (int i = 0; i < infos.Count; i++) {
1261 signature.Append (", ");
1263 ParameterDefinition info = infos [i];
1266 if ((info.Attributes & ParameterAttributes.In) != 0)
1268 else if ((info.Attributes & ParameterAttributes.Out) != 0)
1271 modifier = string.Empty;
1273 if (modifier.Length > 0)
1274 signature.AppendFormat ("{0} ", modifier);
1276 signature.Append (Utils.CleanupTypeName (info.ParameterType));
1279 return signature.ToString ();
1284 class TypeReferenceComparer : IComparer
1286 public static TypeReferenceComparer Default = new TypeReferenceComparer ();
1288 public int Compare (object a, object b)
1290 TypeReference ta = (TypeReference) a;
1291 TypeReference tb = (TypeReference) b;
1292 int result = String.Compare (ta.Namespace, tb.Namespace);
1296 return String.Compare (ta.Name, tb.Name);
1300 class MemberReferenceComparer : IComparer
1302 public static MemberReferenceComparer Default = new MemberReferenceComparer ();
1304 public int Compare (object a, object b)
1306 MemberReference ma = (MemberReference) a;
1307 MemberReference mb = (MemberReference) b;
1308 return String.Compare (ma.Name, mb.Name);
1312 class MethodDefinitionComparer : IComparer
1314 public static MethodDefinitionComparer Default = new MethodDefinitionComparer ();
1316 public int Compare (object a, object b)
1318 MethodDefinition ma = (MethodDefinition) a;
1319 MethodDefinition mb = (MethodDefinition) b;
1320 int res = String.Compare (ma.Name, mb.Name);
1324 IList<ParameterDefinition> pia = ma.Parameters ;
1325 IList<ParameterDefinition> pib = mb.Parameters;
1326 res = pia.Count - pib.Count;
1330 string siga = Parameters.GetSignature (pia);
1331 string sigb = Parameters.GetSignature (pib);
1332 return String.Compare (siga, sigb);