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"));
61 TypeHelper.Resolver.AddSearchDirectory (Path.GetDirectoryName (arg));
66 XmlDocument doc = new XmlDocument ();
70 var writer = new WellFormedXmlWriter (new XmlTextWriter (Console.Out) { Formatting = Formatting.Indented });
71 XmlNode decl = doc.CreateXmlDeclaration ("1.0", "utf-8", null);
72 doc.InsertBefore (decl, doc.DocumentElement);
77 internal static bool AbiMode { get; private set; }
82 public static string CleanupTypeName (TypeReference type)
84 return CleanupTypeName (type.FullName);
87 public static string CleanupTypeName (string t)
89 return t.Replace ('<', '[').Replace ('>', ']').Replace ('/', '+');
93 class AssemblyCollection
96 List<AssemblyDefinition> assemblies = new List<AssemblyDefinition> ();
98 public AssemblyCollection ()
102 public bool Add (string name)
104 AssemblyDefinition ass = LoadAssembly (name);
106 Console.Error.WriteLine ("Cannot load assembly file " + name);
110 assemblies.Add (ass);
114 public void DoOutput ()
116 if (document == null)
117 throw new InvalidOperationException ("Document not set");
119 XmlNode nassemblies = document.CreateElement ("assemblies", null);
120 document.AppendChild (nassemblies);
121 foreach (AssemblyDefinition a in assemblies) {
122 AssemblyData data = new AssemblyData (document, nassemblies, a);
127 public XmlDocument Document {
128 set { document = value; }
131 AssemblyDefinition LoadAssembly (string assembly)
134 if (File.Exists (assembly))
135 return TypeHelper.Resolver.ResolveFile (assembly);
137 return TypeHelper.Resolver.Resolve (assembly);
138 } catch (Exception e) {
139 Console.WriteLine (e);
145 abstract class BaseData
147 protected XmlDocument document;
148 protected XmlNode parent;
150 protected BaseData (XmlDocument doc, XmlNode parent)
153 this.parent = parent;
156 public abstract void DoOutput ();
158 protected void AddAttribute (XmlNode node, string name, string value)
160 XmlAttribute attr = document.CreateAttribute (name);
162 node.Attributes.Append (attr);
166 class TypeForwardedToData : BaseData
168 AssemblyDefinition ass;
170 public TypeForwardedToData (XmlDocument document, XmlNode parent, AssemblyDefinition ass)
171 : base (document, parent)
176 public override void DoOutput ()
178 XmlNode natts = parent.SelectSingleNode("attributes");
180 natts = document.CreateElement ("attributes", null);
181 parent.AppendChild (natts);
184 foreach (ExportedType type in ass.MainModule.ExportedTypes) {
186 if (((uint)type.Attributes & 0x200000u) == 0)
189 XmlNode node = document.CreateElement ("attribute");
190 AddAttribute (node, "name", typeof (TypeForwardedToAttribute).FullName);
191 XmlNode properties = node.AppendChild (document.CreateElement ("properties"));
192 XmlNode property = properties.AppendChild (document.CreateElement ("property"));
193 AddAttribute (property, "name", "Destination");
194 AddAttribute (property, "value", Utils.CleanupTypeName (type.FullName));
195 natts.AppendChild (node);
199 public static void OutputForwarders (XmlDocument document, XmlNode parent, AssemblyDefinition ass)
201 TypeForwardedToData tftd = new TypeForwardedToData (document, parent, ass);
206 class AssemblyData : BaseData
208 AssemblyDefinition ass;
210 public AssemblyData (XmlDocument document, XmlNode parent, AssemblyDefinition ass)
211 : base (document, parent)
216 public override void DoOutput ()
218 if (document == null)
219 throw new InvalidOperationException ("Document not set");
221 XmlNode nassembly = document.CreateElement ("assembly", null);
222 AssemblyNameDefinition aname = ass.Name;
223 AddAttribute (nassembly, "name", aname.Name);
224 AddAttribute (nassembly, "version", aname.Version.ToString ());
225 parent.AppendChild (nassembly);
226 TypeForwardedToData.OutputForwarders (document, nassembly, ass);
227 AttributeData.OutputAttributes (document, nassembly, ass.CustomAttributes);
228 var typesCollection = ass.MainModule.Types;
229 if (typesCollection == null || typesCollection.Count == 0)
231 object [] typesArray = new object [typesCollection.Count];
232 for (int i = 0; i < typesCollection.Count; i++) {
233 typesArray [i] = typesCollection [i];
235 Array.Sort (typesArray, TypeReferenceComparer.Default);
237 XmlNode nss = document.CreateElement ("namespaces", null);
238 nassembly.AppendChild (nss);
240 string current_namespace = "$%&$&";
242 XmlNode classes = null;
243 foreach (TypeDefinition t in typesArray) {
244 if (string.IsNullOrEmpty (t.Namespace))
247 if (!Driver.AbiMode && ((t.Attributes & TypeAttributes.VisibilityMask) != TypeAttributes.Public))
250 if (t.DeclaringType != null)
251 continue; // enforce !nested
253 if (t.Namespace != current_namespace) {
254 current_namespace = t.Namespace;
255 ns = document.CreateElement ("namespace", null);
256 AddAttribute (ns, "name", current_namespace);
257 nss.AppendChild (ns);
258 classes = document.CreateElement ("classes", null);
259 ns.AppendChild (classes);
262 TypeData bd = new TypeData (document, classes, t);
268 abstract class MemberData : BaseData
270 MemberReference [] members;
272 public MemberData (XmlDocument document, XmlNode parent, MemberReference [] members)
273 : base (document, parent)
275 this.members = members;
278 public override void DoOutput ()
280 XmlNode mclass = document.CreateElement (ParentTag, null);
281 parent.AppendChild (mclass);
283 foreach (MemberReference member in members) {
284 XmlNode mnode = document.CreateElement (Tag, null);
285 mclass.AppendChild (mnode);
286 AddAttribute (mnode, "name", GetName (member));
287 if (!NoMemberAttributes)
288 AddAttribute (mnode, "attrib", GetMemberAttributes (member));
290 AttributeData.OutputAttributes (document, mnode, GetCustomAttributes (member));
292 AddExtraData (mnode, member);
297 protected abstract IList<CustomAttribute> GetCustomAttributes (MemberReference member);
299 protected virtual void AddExtraData (XmlNode p, MemberReference memberDefenition)
303 protected virtual string GetName (MemberReference memberDefenition)
308 protected virtual string GetMemberAttributes (MemberReference memberDefenition)
313 public virtual bool NoMemberAttributes {
314 get { return false; }
318 public virtual string ParentTag {
319 get { return "NoPARENTTAG"; }
322 public virtual string Tag {
323 get { return "NoTAG"; }
326 public static void OutputGenericParameters (XmlDocument document, XmlNode nclass, IGenericParameterProvider provider)
328 if (provider.GenericParameters.Count == 0)
331 var gparameters = provider.GenericParameters;
333 XmlElement ngeneric = document.CreateElement (string.Format ("generic-parameters"));
334 nclass.AppendChild (ngeneric);
336 foreach (GenericParameter gp in gparameters) {
337 XmlElement nparam = document.CreateElement (string.Format ("generic-parameter"));
338 nparam.SetAttribute ("name", gp.Name);
339 nparam.SetAttribute ("attributes", ((int) gp.Attributes).ToString ());
341 AttributeData.OutputAttributes (document, nparam, gp.CustomAttributes);
343 ngeneric.AppendChild (nparam);
345 var constraints = gp.Constraints;
346 if (constraints.Count == 0)
349 XmlElement nconstraint = document.CreateElement ("generic-parameter-constraints");
351 foreach (TypeReference constraint in constraints) {
352 XmlElement ncons = document.CreateElement ("generic-parameter-constraint");
353 ncons.SetAttribute ("name", Utils.CleanupTypeName (constraint));
354 nconstraint.AppendChild (ncons);
357 nparam.AppendChild (nconstraint);
362 class TypeData : MemberData
366 public TypeData (XmlDocument document, XmlNode parent, TypeDefinition type)
367 : base (document, parent, null)
372 protected override IList<CustomAttribute> GetCustomAttributes (MemberReference member) {
373 return ((TypeDefinition) member).CustomAttributes;
376 public override void DoOutput ()
378 if (document == null)
379 throw new InvalidOperationException ("Document not set");
381 XmlNode nclass = document.CreateElement ("class", null);
382 AddAttribute (nclass, "name", type.Name);
383 string classType = GetClassType (type);
384 AddAttribute (nclass, "type", classType);
386 if (type.BaseType != null)
387 AddAttribute (nclass, "base", Utils.CleanupTypeName (type.BaseType));
390 AddAttribute (nclass, "sealed", "true");
393 AddAttribute (nclass, "abstract", "true");
395 if ( (type.Attributes & TypeAttributes.Serializable) != 0 || type.IsEnum)
396 AddAttribute (nclass, "serializable", "true");
398 string charSet = GetCharSet (type);
399 AddAttribute (nclass, "charset", charSet);
401 string layout = GetLayout (type);
403 AddAttribute (nclass, "layout", layout);
405 if (type.PackingSize >= 0) {
406 AddAttribute (nclass, "pack", type.PackingSize.ToString ());
409 if (type.ClassSize >= 0) {
410 AddAttribute (nclass, "size", type.ClassSize.ToString ());
413 parent.AppendChild (nclass);
415 AttributeData.OutputAttributes (document, nclass, GetCustomAttributes(type));
417 XmlNode ifaces = null;
419 foreach (TypeReference iface in TypeHelper.GetInterfaces (type)) {
420 if (!TypeHelper.IsPublic (iface))
421 // we're only interested in public interfaces
424 if (ifaces == null) {
425 ifaces = document.CreateElement ("interfaces", null);
426 nclass.AppendChild (ifaces);
429 XmlNode iface_node = document.CreateElement ("interface", null);
430 AddAttribute (iface_node, "name", Utils.CleanupTypeName (iface));
431 ifaces.AppendChild (iface_node);
434 MemberData.OutputGenericParameters (document, nclass, type);
436 ArrayList members = new ArrayList ();
438 FieldDefinition [] fields = GetFields (type);
439 if (fields.Length > 0) {
440 Array.Sort (fields, MemberReferenceComparer.Default);
441 FieldData fd = new FieldData (document, nclass, fields);
446 var value_type = GetEnumValueField (type);
447 if (value_type == null)
448 throw new NotSupportedException ();
450 AddAttribute (nclass, "enumtype", Utils.CleanupTypeName (value_type.FieldType));
453 if (!Driver.AbiMode) {
455 MethodDefinition [] ctors = GetConstructors (type);
456 if (ctors.Length > 0) {
457 Array.Sort (ctors, MemberReferenceComparer.Default);
458 members.Add (new ConstructorData (document, nclass, ctors));
461 PropertyDefinition[] properties = GetProperties (type);
462 if (properties.Length > 0) {
463 Array.Sort (properties, MemberReferenceComparer.Default);
464 members.Add (new PropertyData (document, nclass, properties));
467 EventDefinition [] events = GetEvents (type);
468 if (events.Length > 0) {
469 Array.Sort (events, MemberReferenceComparer.Default);
470 members.Add (new EventData (document, nclass, events));
473 MethodDefinition [] methods = GetMethods (type);
474 if (methods.Length > 0) {
475 Array.Sort (methods, MemberReferenceComparer.Default);
476 members.Add (new MethodData (document, nclass, methods));
480 foreach (MemberData md in members)
483 var nested = type.NestedTypes;
484 //remove non public(familiy) and nested in second degree
485 for (int i = nested.Count - 1; i >= 0; i--) {
486 TypeDefinition t = nested [i];
487 if ((t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic ||
488 (t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily ||
489 (t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem) {
491 if (t.DeclaringType == type)
492 continue; // not nested of nested
499 if (nested.Count > 0) {
500 XmlNode classes = document.CreateElement ("classes", null);
501 nclass.AppendChild (classes);
502 foreach (TypeDefinition t in nested) {
503 TypeData td = new TypeData (document, classes, t);
509 static FieldReference GetEnumValueField (TypeDefinition type)
511 foreach (FieldDefinition field in type.Fields)
512 if (field.IsSpecialName && field.Name == "value__")
518 protected override string GetMemberAttributes (MemberReference member)
521 throw new InvalidOperationException ("odd");
523 return ((int) type.Attributes).ToString (CultureInfo.InvariantCulture);
526 public static bool MustDocumentMethod (MethodDefinition method) {
528 MethodAttributes maskedAccess = method.Attributes & MethodAttributes.MemberAccessMask;
529 return maskedAccess == MethodAttributes.Public
530 || maskedAccess == MethodAttributes.Family
531 || maskedAccess == MethodAttributes.FamORAssem;
534 static string GetClassType (TypeDefinition t)
545 if (TypeHelper.IsDelegate(t))
551 static string GetCharSet (TypeDefinition type)
553 TypeAttributes maskedStringFormat = type.Attributes & TypeAttributes.StringFormatMask;
554 if (maskedStringFormat == TypeAttributes.AnsiClass)
555 return CharSet.Ansi.ToString ();
557 if (maskedStringFormat == TypeAttributes.AutoClass)
558 return CharSet.Auto.ToString ();
560 if (maskedStringFormat == TypeAttributes.UnicodeClass)
561 return CharSet.Unicode.ToString ();
563 return CharSet.None.ToString ();
566 static string GetLayout (TypeDefinition type)
568 TypeAttributes maskedLayout = type.Attributes & TypeAttributes.LayoutMask;
569 if (maskedLayout == TypeAttributes.AutoLayout)
570 return LayoutKind.Auto.ToString ();
572 if (maskedLayout == TypeAttributes.ExplicitLayout)
573 return LayoutKind.Explicit.ToString ();
575 if (maskedLayout == TypeAttributes.SequentialLayout)
576 return LayoutKind.Sequential.ToString ();
581 FieldDefinition [] GetFields (TypeDefinition type) {
582 ArrayList list = new ArrayList ();
584 var fields = type.Fields;
585 foreach (FieldDefinition field in fields) {
586 if (field.IsSpecialName)
589 if (Driver.AbiMode && field.IsStatic)
592 // we're only interested in public or protected members
593 FieldAttributes maskedVisibility = (field.Attributes & FieldAttributes.FieldAccessMask);
594 if (Driver.AbiMode && !field.IsNotSerialized) {
597 if (maskedVisibility == FieldAttributes.Public
598 || maskedVisibility == FieldAttributes.Family
599 || maskedVisibility == FieldAttributes.FamORAssem) {
605 return (FieldDefinition []) list.ToArray (typeof (FieldDefinition));
609 internal static PropertyDefinition [] GetProperties (TypeDefinition type) {
610 ArrayList list = new ArrayList ();
612 var properties = type.Properties;//type.GetProperties (flags);
613 foreach (PropertyDefinition property in properties) {
614 MethodDefinition getMethod = property.GetMethod;
615 MethodDefinition setMethod = property.SetMethod;
617 bool hasGetter = (getMethod != null) && MustDocumentMethod (getMethod);
618 bool hasSetter = (setMethod != null) && MustDocumentMethod (setMethod);
620 // if neither the getter or setter should be documented, then
622 if (hasGetter || hasSetter) {
627 return (PropertyDefinition []) list.ToArray (typeof (PropertyDefinition));
630 private MethodDefinition[] GetMethods (TypeDefinition type)
632 ArrayList list = new ArrayList ();
634 var methods = type.Methods;//type.GetMethods (flags);
635 foreach (MethodDefinition method in methods) {
636 if (method.IsSpecialName && !method.Name.StartsWith ("op_"))
639 // we're only interested in public or protected members
640 if (!MustDocumentMethod(method))
643 if (IsFinalizer (method)) {
644 string name = method.DeclaringType.Name;
645 int arity = name.IndexOf ('`');
647 name = name.Substring (0, arity);
649 method.Name = "~" + name;
655 return (MethodDefinition []) list.ToArray (typeof (MethodDefinition));
658 static bool IsFinalizer (MethodDefinition method)
660 if (method.Name != "Finalize")
663 if (!method.IsVirtual)
666 if (method.Parameters.Count != 0)
672 private MethodDefinition [] GetConstructors (TypeDefinition type)
674 ArrayList list = new ArrayList ();
676 var ctors = type.Methods.Where (m => m.IsConstructor);//type.GetConstructors (flags);
677 foreach (MethodDefinition constructor in ctors) {
678 // we're only interested in public or protected members
679 if (!MustDocumentMethod(constructor))
682 list.Add (constructor);
685 return (MethodDefinition []) list.ToArray (typeof (MethodDefinition));
688 private EventDefinition[] GetEvents (TypeDefinition type)
690 ArrayList list = new ArrayList ();
692 var events = type.Events;//type.GetEvents (flags);
693 foreach (EventDefinition eventDef in events) {
694 MethodDefinition addMethod = eventDef.AddMethod;//eventInfo.GetAddMethod (true);
696 if (addMethod == null || !MustDocumentMethod (addMethod))
702 return (EventDefinition []) list.ToArray (typeof (EventDefinition));
706 class FieldData : MemberData
708 public FieldData (XmlDocument document, XmlNode parent, FieldDefinition [] members)
709 : base (document, parent, members)
713 protected override IList<CustomAttribute> GetCustomAttributes (MemberReference member) {
714 return ((FieldDefinition) member).CustomAttributes;
717 protected override string GetName (MemberReference memberDefenition)
719 FieldDefinition field = (FieldDefinition) memberDefenition;
723 protected override string GetMemberAttributes (MemberReference memberDefenition)
725 FieldDefinition field = (FieldDefinition) memberDefenition;
726 return ((int) field.Attributes).ToString (CultureInfo.InvariantCulture);
729 protected override void AddExtraData (XmlNode p, MemberReference memberDefenition)
731 base.AddExtraData (p, memberDefenition);
732 FieldDefinition field = (FieldDefinition) memberDefenition;
733 AddAttribute (p, "fieldtype", Utils.CleanupTypeName (field.FieldType));
735 if (field.IsLiteral) {
736 object value = field.Constant;//object value = field.GetValue (null);
737 string stringValue = null;
738 //if (value is Enum) {
739 // // FIXME: when Mono bug #60090 has been
740 // // fixed, we should just be able to use
741 // // Convert.ToString
742 // stringValue = ((Enum) value).ToString ("D", CultureInfo.InvariantCulture);
745 stringValue = Convert.ToString (value, CultureInfo.InvariantCulture);
748 if (stringValue != null)
749 AddAttribute (p, "value", stringValue);
753 public override string ParentTag {
754 get { return "fields"; }
757 public override string Tag {
758 get { return "field"; }
762 class PropertyData : MemberData
764 public PropertyData (XmlDocument document, XmlNode parent, PropertyDefinition [] members)
765 : base (document, parent, members)
769 protected override IList<CustomAttribute> GetCustomAttributes (MemberReference member) {
770 return ((PropertyDefinition) member).CustomAttributes;
773 protected override string GetName (MemberReference memberDefenition)
775 PropertyDefinition prop = (PropertyDefinition) memberDefenition;
779 protected override void AddExtraData (XmlNode p, MemberReference memberDefenition)
781 base.AddExtraData (p, memberDefenition);
782 PropertyDefinition prop = (PropertyDefinition) memberDefenition;
783 AddAttribute (p, "ptype", Utils.CleanupTypeName (prop.PropertyType));
784 MethodDefinition _get = prop.GetMethod;
785 MethodDefinition _set = prop.SetMethod;
786 bool haveGet = (_get != null && TypeData.MustDocumentMethod(_get));
787 bool haveSet = (_set != null && TypeData.MustDocumentMethod(_set));
788 MethodDefinition [] methods;
790 if (haveGet && haveSet) {
791 methods = new MethodDefinition [] { _get, _set };
792 } else if (haveGet) {
793 methods = new MethodDefinition [] { _get };
794 } else if (haveSet) {
795 methods = new MethodDefinition [] { _set };
801 if (haveGet || _set.Parameters.Count > 1) {
802 string parms = Parameters.GetSignature (methods [0].Parameters);
803 if (!string.IsNullOrEmpty (parms))
804 AddAttribute (p, "params", parms);
807 MethodData data = new MethodData (document, p, methods);
808 //data.NoMemberAttributes = true;
812 protected override string GetMemberAttributes (MemberReference memberDefenition)
814 PropertyDefinition prop = (PropertyDefinition) memberDefenition;
815 return ((int) prop.Attributes).ToString (CultureInfo.InvariantCulture);
818 public override string ParentTag {
819 get { return "properties"; }
822 public override string Tag {
823 get { return "property"; }
827 class EventData : MemberData
829 public EventData (XmlDocument document, XmlNode parent, EventDefinition [] members)
830 : base (document, parent, members)
834 protected override IList<CustomAttribute> GetCustomAttributes (MemberReference member) {
835 return ((EventDefinition) member).CustomAttributes;
838 protected override string GetName (MemberReference memberDefenition)
840 EventDefinition evt = (EventDefinition) memberDefenition;
844 protected override string GetMemberAttributes (MemberReference memberDefenition)
846 EventDefinition evt = (EventDefinition) memberDefenition;
847 return ((int) evt.Attributes).ToString (CultureInfo.InvariantCulture);
850 protected override void AddExtraData (XmlNode p, MemberReference memberDefenition)
852 base.AddExtraData (p, memberDefenition);
853 EventDefinition evt = (EventDefinition) memberDefenition;
854 AddAttribute (p, "eventtype", Utils.CleanupTypeName (evt.EventType));
857 public override string ParentTag {
858 get { return "events"; }
861 public override string Tag {
862 get { return "event"; }
866 class MethodData : MemberData
870 public MethodData (XmlDocument document, XmlNode parent, MethodDefinition [] members)
871 : base (document, parent, members)
875 protected override IList<CustomAttribute> GetCustomAttributes (MemberReference member) {
876 return ((MethodDefinition) member).CustomAttributes;
879 protected override string GetName (MemberReference memberDefenition)
881 MethodDefinition method = (MethodDefinition) memberDefenition;
882 string name = method.Name;
883 string parms = Parameters.GetSignature (method.Parameters);
885 return string.Format ("{0}({1})", name, parms);
888 protected override string GetMemberAttributes (MemberReference memberDefenition)
890 MethodDefinition method = (MethodDefinition) memberDefenition;
891 return ((int)( method.Attributes)).ToString (CultureInfo.InvariantCulture);
894 protected override void AddExtraData (XmlNode p, MemberReference memberDefenition)
896 base.AddExtraData (p, memberDefenition);
898 if (!(memberDefenition is MethodDefinition))
901 MethodDefinition mbase = (MethodDefinition) memberDefenition;
903 ParameterData parms = new ParameterData (document, p, mbase.Parameters);
906 if (mbase.IsAbstract)
907 AddAttribute (p, "abstract", "true");
909 AddAttribute (p, "virtual", "true");
911 AddAttribute (p, "static", "true");
913 string rettype = Utils.CleanupTypeName (mbase.MethodReturnType.ReturnType);
914 if (rettype != "System.Void" || !mbase.IsConstructor)
915 AddAttribute (p, "returntype", (rettype));
917 AttributeData.OutputAttributes (document, p, mbase.MethodReturnType.CustomAttributes);
919 MemberData.OutputGenericParameters (document, p, mbase);
922 public override bool NoMemberAttributes {
923 get { return noAtts; }
924 set { noAtts = value; }
927 public override string ParentTag {
928 get { return "methods"; }
931 public override string Tag {
932 get { return "method"; }
936 class ConstructorData : MethodData
938 public ConstructorData (XmlDocument document, XmlNode parent, MethodDefinition [] members)
939 : base (document, parent, members)
943 public override string ParentTag {
944 get { return "constructors"; }
947 public override string Tag {
948 get { return "constructor"; }
952 class ParameterData : BaseData
954 private IList<ParameterDefinition> parameters;
956 public ParameterData (XmlDocument document, XmlNode parent, IList<ParameterDefinition> parameters)
957 : base (document, parent)
959 this.parameters = parameters;
962 public override void DoOutput ()
964 XmlNode parametersNode = document.CreateElement ("parameters");
965 parent.AppendChild (parametersNode);
967 foreach (ParameterDefinition parameter in parameters) {
968 XmlNode paramNode = document.CreateElement ("parameter");
969 parametersNode.AppendChild (paramNode);
970 AddAttribute (paramNode, "name", parameter.Name);
971 AddAttribute (paramNode, "position", parameter.Method.Parameters.IndexOf(parameter).ToString(CultureInfo.InvariantCulture));
972 AddAttribute (paramNode, "attrib", ((int) parameter.Attributes).ToString());
974 string direction = "in";
976 if (parameter.ParameterType is ByReferenceType)
977 direction = parameter.IsOut ? "out" : "ref";
979 TypeReference t = parameter.ParameterType;
980 AddAttribute (paramNode, "type", Utils.CleanupTypeName (t));
982 if (parameter.IsOptional) {
983 AddAttribute (paramNode, "optional", "true");
984 if (parameter.HasConstant)
985 AddAttribute (paramNode, "defaultValue", parameter.Constant == null ? "NULL" : parameter.Constant.ToString ());
988 if (direction != "in")
989 AddAttribute (paramNode, "direction", direction);
991 AttributeData.OutputAttributes (document, paramNode, parameter.CustomAttributes);
996 class AttributeData : BaseData
998 IList<CustomAttribute> atts;
1000 AttributeData (XmlDocument doc, XmlNode parent, IList<CustomAttribute> attributes)
1001 : base (doc, parent)
1006 public override void DoOutput ()
1008 if (document == null)
1009 throw new InvalidOperationException ("Document not set");
1011 if (atts == null || atts.Count == 0)
1014 XmlNode natts = parent.SelectSingleNode("attributes");
1015 if (natts == null) {
1016 natts = document.CreateElement ("attributes", null);
1017 parent.AppendChild (natts);
1020 for (int i = 0; i < atts.Count; ++i) {
1021 CustomAttribute att = atts [i];
1023 string attName = Utils.CleanupTypeName (att.Constructor.DeclaringType);
1024 if (SkipAttribute (att))
1027 XmlNode node = document.CreateElement ("attribute");
1028 AddAttribute (node, "name", attName);
1030 XmlNode properties = null;
1032 Dictionary<string, object> attribute_mapping = CreateAttributeMapping (att);
1034 foreach (string name in attribute_mapping.Keys) {
1035 if (name == "TypeId")
1038 if (properties == null) {
1039 properties = node.AppendChild (document.CreateElement ("properties"));
1042 object o = attribute_mapping [name];
1044 XmlNode n = properties.AppendChild (document.CreateElement ("property"));
1045 AddAttribute (n, "name", name);
1048 AddAttribute (n, "value", "null");
1052 string value = o.ToString ();
1053 if (attName.EndsWith ("GuidAttribute"))
1054 value = value.ToUpper ();
1055 AddAttribute (n, "value", value);
1058 natts.AppendChild (node);
1062 static Dictionary<string, object> CreateAttributeMapping (CustomAttribute attribute)
1064 var mapping = new Dictionary<string, object> ();
1066 PopulateMapping (mapping, attribute);
1068 var constructor = attribute.Constructor.Resolve ();
1069 if (constructor == null || constructor.Parameters.Count == 0)
1072 PopulateMapping (mapping, constructor, attribute);
1077 static void PopulateMapping (Dictionary<string, object> mapping, CustomAttribute attribute)
1079 foreach (var named_argument in attribute.Properties) {
1080 var name = named_argument.Name;
1081 var arg = named_argument.Argument;
1083 if (arg.Value is CustomAttributeArgument)
1084 arg = (CustomAttributeArgument) arg.Value;
1086 mapping.Add (name, GetArgumentValue (arg.Type, arg.Value));
1090 static Dictionary<FieldReference, int> CreateArgumentFieldMapping (MethodDefinition constructor)
1092 Dictionary<FieldReference, int> field_mapping = new Dictionary<FieldReference, int> ();
1094 int? argument = null;
1096 foreach (Instruction instruction in constructor.Body.Instructions) {
1097 switch (instruction.OpCode.Code) {
1109 argument = ((ParameterDefinition) instruction.Operand).Index + 1;
1113 FieldReference field = (FieldReference) instruction.Operand;
1114 if (field.DeclaringType.FullName != constructor.DeclaringType.FullName)
1117 if (!argument.HasValue)
1120 if (!field_mapping.ContainsKey (field))
1121 field_mapping.Add (field, (int) argument - 1);
1128 return field_mapping;
1131 static Dictionary<PropertyDefinition, FieldReference> CreatePropertyFieldMapping (TypeDefinition type)
1133 Dictionary<PropertyDefinition, FieldReference> property_mapping = new Dictionary<PropertyDefinition, FieldReference> ();
1135 foreach (PropertyDefinition property in type.Properties) {
1136 if (property.GetMethod == null)
1138 if (!property.GetMethod.HasBody)
1141 foreach (Instruction instruction in property.GetMethod.Body.Instructions) {
1142 if (instruction.OpCode.Code != Code.Ldfld)
1145 FieldReference field = (FieldReference) instruction.Operand;
1146 if (field.DeclaringType.FullName != type.FullName)
1149 property_mapping.Add (property, field);
1154 return property_mapping;
1157 static void PopulateMapping (Dictionary<string, object> mapping, MethodDefinition constructor, CustomAttribute attribute)
1159 if (!constructor.HasBody)
1162 // Custom handling for attributes with arguments which cannot be easily extracted
1163 var ca = attribute.ConstructorArguments;
1164 switch (constructor.DeclaringType.FullName) {
1165 case "System.Runtime.CompilerServices.DecimalConstantAttribute":
1166 var dca = constructor.Parameters[2].ParameterType == constructor.Module.TypeSystem.Int32 ?
1167 new DecimalConstantAttribute ((byte) ca[0].Value, (byte) ca[1].Value, (int) ca[2].Value, (int) ca[3].Value, (int) ca[4].Value) :
1168 new DecimalConstantAttribute ((byte) ca[0].Value, (byte) ca[1].Value, (uint) ca[2].Value, (uint) ca[3].Value, (uint) ca[4].Value);
1170 mapping.Add ("Value", dca.Value);
1172 case "System.ComponentModel.BindableAttribute":
1176 if (constructor.Parameters[0].ParameterType == constructor.Module.TypeSystem.Boolean) {
1177 mapping.Add ("Bindable", ca[0].Value);
1179 throw new NotImplementedException ();
1185 var field_mapping = CreateArgumentFieldMapping (constructor);
1186 var property_mapping = CreatePropertyFieldMapping ((TypeDefinition) constructor.DeclaringType);
1188 foreach (var pair in property_mapping) {
1190 if (!field_mapping.TryGetValue (pair.Value, out argument))
1193 var ca_arg = ca [argument];
1194 if (ca_arg.Value is CustomAttributeArgument)
1195 ca_arg = (CustomAttributeArgument) ca_arg.Value;
1197 mapping.Add (pair.Key.Name, GetArgumentValue (ca_arg.Type, ca_arg.Value));
1201 static object GetArgumentValue (TypeReference reference, object value)
1203 var type = reference.Resolve ();
1208 if (IsFlaggedEnum (type))
1209 return GetFlaggedEnumValue (type, value);
1211 return GetEnumValue (type, value);
1217 static bool IsFlaggedEnum (TypeDefinition type)
1222 if (type.CustomAttributes.Count == 0)
1225 foreach (CustomAttribute attribute in type.CustomAttributes)
1226 if (attribute.Constructor.DeclaringType.FullName == "System.FlagsAttribute")
1232 static object GetFlaggedEnumValue (TypeDefinition type, object value)
1235 return GetFlaggedEnumValue (type, (ulong)value);
1237 long flags = Convert.ToInt64 (value);
1238 var signature = new StringBuilder ();
1240 for (int i = type.Fields.Count - 1; i >= 0; i--) {
1241 FieldDefinition field = type.Fields [i];
1243 if (!field.HasConstant)
1246 long flag = Convert.ToInt64 (field.Constant);
1251 if ((flags & flag) == flag) {
1252 if (signature.Length != 0)
1253 signature.Append (", ");
1255 signature.Append (field.Name);
1260 return signature.ToString ();
1263 static object GetFlaggedEnumValue (TypeDefinition type, ulong flags)
1265 var signature = new StringBuilder ();
1267 for (int i = type.Fields.Count - 1; i >= 0; i--) {
1268 FieldDefinition field = type.Fields [i];
1270 if (!field.HasConstant)
1273 ulong flag = Convert.ToUInt64 (field.Constant);
1278 if ((flags & flag) == flag) {
1279 if (signature.Length != 0)
1280 signature.Append (", ");
1282 signature.Append (field.Name);
1287 return signature.ToString ();
1290 static object GetEnumValue (TypeDefinition type, object value)
1292 foreach (FieldDefinition field in type.Fields) {
1293 if (!field.HasConstant)
1296 if (Comparer.Default.Compare (field.Constant, value) == 0)
1303 static bool SkipAttribute (CustomAttribute attribute)
1305 var type_name = Utils.CleanupTypeName (attribute.Constructor.DeclaringType);
1307 return !TypeHelper.IsPublic (attribute)
1308 || type_name.EndsWith ("TODOAttribute");
1311 public static void OutputAttributes (XmlDocument doc, XmlNode parent, IList<CustomAttribute> attributes)
1313 AttributeData ad = new AttributeData (doc, parent, attributes);
1318 static class Parameters {
1320 public static string GetSignature (IList<ParameterDefinition> infos)
1322 if (infos == null || infos.Count == 0)
1325 var signature = new StringBuilder ();
1326 for (int i = 0; i < infos.Count; i++) {
1329 signature.Append (", ");
1331 ParameterDefinition info = infos [i];
1334 if ((info.Attributes & ParameterAttributes.In) != 0)
1336 else if ((info.Attributes & ParameterAttributes.Out) != 0)
1339 modifier = string.Empty;
1341 if (modifier.Length > 0)
1342 signature.AppendFormat ("{0} ", modifier);
1344 signature.Append (Utils.CleanupTypeName (info.ParameterType));
1347 return signature.ToString ();
1352 class TypeReferenceComparer : IComparer
1354 public static TypeReferenceComparer Default = new TypeReferenceComparer ();
1356 public int Compare (object a, object b)
1358 TypeReference ta = (TypeReference) a;
1359 TypeReference tb = (TypeReference) b;
1360 int result = String.Compare (ta.Namespace, tb.Namespace);
1364 return String.Compare (ta.Name, tb.Name);
1368 class MemberReferenceComparer : IComparer
1370 public static MemberReferenceComparer Default = new MemberReferenceComparer ();
1372 public int Compare (object a, object b)
1374 MemberReference ma = (MemberReference) a;
1375 MemberReference mb = (MemberReference) b;
1376 return String.Compare (ma.Name, mb.Name);
1380 class MethodDefinitionComparer : IComparer
1382 public static MethodDefinitionComparer Default = new MethodDefinitionComparer ();
1384 public int Compare (object a, object b)
1386 MethodDefinition ma = (MethodDefinition) a;
1387 MethodDefinition mb = (MethodDefinition) b;
1388 int res = String.Compare (ma.Name, mb.Name);
1392 IList<ParameterDefinition> pia = ma.Parameters ;
1393 IList<ParameterDefinition> pib = mb.Parameters;
1394 res = pia.Count - pib.Count;
1398 string siga = Parameters.GetSignature (pia);
1399 string sigb = Parameters.GetSignature (pib);
1400 return String.Compare (siga, sigb);