Changed link from GUID to URL
[mono.git] / mcs / tools / mdoc / Mono.Documentation / monodocer.cs
1 // Updater program for syncing Mono's ECMA-style documentation files
2 // with an assembly.
3 // By Joshua Tauberer <tauberer@for.net>
4
5 using System;
6 using System.Collections;
7 using System.Collections.Generic;
8 using System.Collections.ObjectModel;
9 using System.Diagnostics;
10 using System.Globalization;
11 using System.IO;
12 using System.Linq;
13 using System.Text;
14 using System.Xml;
15 using System.Xml.XPath;
16
17 using Mono.Cecil;
18 using Mono.Options;
19
20 using MyXmlNodeList        = System.Collections.Generic.List<System.Xml.XmlNode>;
21 using StringList           = System.Collections.Generic.List<string>;
22 using StringToStringMap    = System.Collections.Generic.Dictionary<string, string>;
23 using StringToXmlNodeMap   = System.Collections.Generic.Dictionary<string, System.Xml.XmlNode>;
24
25 namespace Mono.Documentation {
26         static class NativeTypeManager {
27
28                 static Dictionary<string, string> toNativeType = new Dictionary<string,string>(){
29
30                         {"int", "nint"},
31                         {"Int32", "nint"},
32                         {"System.Int32", "System.nint"},
33                         {"uint", "nuint"},
34                         {"UInt32", "nuint"},
35                         {"System.UInt32", "System.nuint"},
36                         {"float", "nfloat"},
37                         {"Single", "nfloat"},
38                         {"System.Single", "System.nfloat"},
39                         {"SizeF", "CoreGraphics.CGSize"},
40                         {"System.Drawing.SizeF", "CoreGraphics.CGSize"},
41                         {"PointF", "CoreGraphics.CGPoint"},
42                         {"System.Drawing.PointF", "CoreGraphics.CGPoint"},
43                         {"RectangleF", "CoreGraphics.CGRect" },
44                         {"System.Drawing.RectangleF", "CoreGraphics.CGRect"}
45                 };              
46
47                 static Dictionary<string, string> fromNativeType = new Dictionary<string,string>(){
48
49                         {"nint", "int"},
50                         {"System.nint", "System.Int32"},
51                         {"nuint", "uint"},
52                         {"System.nuint", "System.UInt32"},
53                         {"nfloat", "float"},
54                         {"System.nfloat", "System.Single"},
55                         {"CoreGraphics.CGSize", "System.Drawing.SizeF"},
56                         {"CoreGraphics.CGPoint", "System.Drawing.PointF"},
57                         {"CoreGraphics.CGRect", "System.Drawing.RectangleF"},
58                         {"MonoTouch.CoreGraphics.CGSize", "System.Drawing.SizeF"},
59                         {"MonoTouch.CoreGraphics.CGPoint", "System.Drawing.PointF"},
60                         {"MonoTouch.CoreGraphics.CGRect", "System.Drawing.RectangleF"}
61                 };
62
63                 public static string ConvertToNativeType(string typename) {
64                         string nvalue;
65
66                         bool isOut=false;
67                         bool isArray=false;
68                         string valueToCompare = StripToComparableType (typename, ref isOut, ref isArray);
69
70                         if (toNativeType.TryGetValue (valueToCompare, out nvalue)) {
71
72                                 if (isArray) {
73                                         nvalue += "[]";
74                                 }
75                                 if (isOut) {
76                                         nvalue += "&";
77                                 }
78                                 return nvalue;
79                         }
80                         return typename;
81                 }
82                 public static string ConvertFromNativeType(string typename) {
83                         string nvalue;
84
85                         bool isOut=false;
86                         bool isArray=false;
87                         string valueToCompare = StripToComparableType (typename, ref isOut, ref isArray);
88
89                         if (fromNativeType.TryGetValue (valueToCompare, out nvalue)) {
90                                 if (isArray) {
91                                         nvalue += "[]";
92                                 }
93                                 if (isOut) {
94                                         nvalue += "&";
95                                 }
96                                 return nvalue;
97                         }
98                         // it wasn't one of the native types ... just return it
99                         return typename;
100                 }
101
102                 static string StripToComparableType (string typename, ref bool isOut, ref bool isArray)
103                 {
104                         string valueToCompare = typename;
105                         if (typename.EndsWith ("[]")) {
106                                 valueToCompare = typename.Substring (0, typename.Length - 2);
107                                 isArray = true;
108                         }
109                         if (typename.EndsWith ("&")) {
110                                 valueToCompare = typename.Substring (0, typename.Length - 1);
111                                 isOut = true;
112                         }
113                         if (typename.Contains ("<")) {
114                                 // TODO: Need to recursively process generic parameters
115                         }
116                         return valueToCompare;
117                 }
118
119                 public static string GetTranslatedName(TypeReference t) {
120                         string typename = t.FullName;
121
122                         bool isInAssembly = MDocUpdater.IsInAssemblies (t.Module.Name);
123                         if (isInAssembly && !typename.StartsWith ("System") && MDocUpdater.HasDroppedNamespace (t)) {
124                                 string nameWithDropped = string.Format ("{0}.{1}", MDocUpdater.droppedNamespace, typename);
125                                 return nameWithDropped;
126                         }
127                         return typename;
128                 }
129         }
130 class MDocUpdater : MDocCommand
131 {
132         string srcPath;
133         List<AssemblyDefinition> assemblies;
134         readonly DefaultAssemblyResolver assemblyResolver = new DefaultAssemblyResolver();
135         
136         bool multiassembly;
137         bool delete;
138         bool show_exceptions;
139         bool no_assembly_versions, ignore_missing_types;
140         ExceptionLocations? exceptions;
141         
142         internal int additions = 0, deletions = 0;
143
144         List<DocumentationImporter> importers = new List<DocumentationImporter> ();
145
146         DocumentationEnumerator docEnum;
147
148         string since;
149
150         static readonly MemberFormatter docTypeFormatter     = new DocTypeMemberFormatter ();
151         static readonly MemberFormatter filenameFormatter    = new FileNameMemberFormatter ();
152
153         static MemberFormatter[] typeFormatters = new MemberFormatter[]{
154                 new CSharpMemberFormatter (),
155                 new ILMemberFormatter (),
156         };
157
158         static MemberFormatter[] memberFormatters = new MemberFormatter[]{
159                 new CSharpFullMemberFormatter (),
160                 new ILFullMemberFormatter (),
161         };
162
163         internal static readonly MemberFormatter slashdocFormatter    = new SlashDocMemberFormatter ();
164
165         MyXmlNodeList extensionMethods = new MyXmlNodeList ();
166
167         HashSet<string> forwardedTypes = new HashSet<string> ();
168
169         public static string droppedNamespace = string.Empty;
170
171         public static bool HasDroppedNamespace(TypeDefinition forType) 
172         {
173                 return HasDroppedNamespace(forType.Module);
174         }
175
176         public static bool HasDroppedNamespace(MemberReference forMember) 
177         {
178                 return HasDroppedNamespace(forMember.Module);
179         }
180
181         public static bool HasDroppedNamespace(AssemblyDefinition forAssembly) 
182         {
183                 return HasDroppedNamespace(forAssembly.MainModule);
184         }
185
186         public static bool HasDroppedNamespace(ModuleDefinition forModule) 
187         {
188                 return !string.IsNullOrWhiteSpace (droppedNamespace) && droppedAssemblies.Any(da => da == forModule.Name);
189         }
190
191         public static bool HasDroppedAnyNamespace ()
192         {
193                 return !string.IsNullOrWhiteSpace (droppedNamespace);
194         }
195
196         
197         static List<string> droppedAssemblies = new List<string>();
198
199         public string PreserveTag { get; set; }
200         public static MDocUpdater Instance { get; private set; }
201         public static bool SwitchingToMagicTypes { get; private set; }
202
203         public override void Run (IEnumerable<string> args)
204         {
205                 Instance = this;
206                 show_exceptions = DebugOutput;
207                 var types = new List<string> ();
208                 var p = new OptionSet () {
209                         { "delete",
210                                 "Delete removed members from the XML files.",
211                                 v => delete = v != null },
212                         { "exceptions:",
213                           "Document potential exceptions that members can generate.  {SOURCES} " +
214                                 "is a comma-separated list of:\n" +
215                                 "  asm      Method calls in same assembly\n" +
216                                 "  depasm   Method calls in dependent assemblies\n" +
217                                 "  all      Record all possible exceptions\n" +
218                                 "  added    Modifier; only create <exception/>s\n" +
219                                 "             for NEW types/members\n" +
220                                 "If nothing is specified, then only exceptions from the member will " +
221                                 "be listed.",
222                                 v => exceptions = ParseExceptionLocations (v) },
223                         { "f=",
224                                 "Specify a {FLAG} to alter behavior.  See later -f* options for available flags.",
225                                 v => {
226                                         switch (v) {
227                                                 case "ignore-missing-types":
228                                                         ignore_missing_types = true;
229                                                         break;
230                                                 case "no-assembly-versions":
231                                                         no_assembly_versions = true;
232                                                         break;
233                                                 default:
234                                                         throw new Exception ("Unsupported flag `" + v + "'.");
235                                         }
236                                 } },
237                         { "fignore-missing-types",
238                                 "Do not report an error if a --type=TYPE type\nwas not found.",
239                                 v => ignore_missing_types = v != null },
240                         { "fno-assembly-versions",
241                                 "Do not generate //AssemblyVersion elements.",
242                                 v => no_assembly_versions = v != null },
243                         { "i|import=", 
244                                 "Import documentation from {FILE}.",
245                                 v => AddImporter (v) },
246                         { "L|lib=",
247                                 "Check for assembly references in {DIRECTORY}.",
248                                 v => assemblyResolver.AddSearchDirectory (v) },
249                         { "library=",
250                                 "Ignored for compatibility with update-ecma-xml.",
251                                 v => {} },
252                         { "o|out=",
253                                 "Root {DIRECTORY} to generate/update documentation.",
254                                 v => srcPath = v },
255                         { "r=",
256                                 "Search for dependent assemblies in the directory containing {ASSEMBLY}.\n" +
257                                 "(Equivalent to '-L `dirname ASSEMBLY`'.)",
258                                 v => assemblyResolver.AddSearchDirectory (Path.GetDirectoryName (v)) },
259                         { "since=",
260                                 "Manually specify the assembly {VERSION} that new members were added in.",
261                                 v => since = v },
262                         { "type=",
263                           "Only update documentation for {TYPE}.",
264                                 v => types.Add (v) },
265                         { "dropns=",
266                           "When processing assembly {ASSEMBLY}, strip off leading namespace {PREFIX}:\n" +
267                           "  e.g. --dropns ASSEMBLY=PREFIX",
268                           v => {
269                             var parts = v.Split ('=');
270                             if (parts.Length != 2) { Console.Error.WriteLine ("Invalid dropns input"); return; }
271                             var assembly = Path.GetFileName (parts [0].Trim ());
272                             var prefix = parts [1].Trim();
273                             droppedAssemblies.Add (assembly);
274                             droppedNamespace = prefix;
275                         } },
276                         { "ntypes",
277                                 "If the new assembly is switching to 'magic types', then this switch should be defined.",
278                                 v => SwitchingToMagicTypes = true },
279                         { "preserve",
280                                 "Do not delete members that don't exist in the assembly, but rather mark them as preserved.",
281                                 v => PreserveTag = "true" },
282                         { "multiassembly",
283                                 "Allow types to be in multiple assemblies.",
284                                 v => multiassembly = true },
285                 };
286                 var assemblies = Parse (p, args, "update", 
287                                 "[OPTIONS]+ ASSEMBLIES",
288                                 "Create or update documentation from ASSEMBLIES.");
289                 if (assemblies == null)
290                         return;
291                 if (assemblies.Count == 0)
292                         Error ("No assemblies specified.");
293
294                 foreach (var dir in assemblies
295                                 .Where (a => a.Contains (Path.DirectorySeparatorChar))
296                                 .Select (a => Path.GetDirectoryName (a)))
297                         assemblyResolver.AddSearchDirectory (dir);
298
299                 // PARSE BASIC OPTIONS AND LOAD THE ASSEMBLY TO DOCUMENT
300                 
301                 if (srcPath == null)
302                         throw new InvalidOperationException("The --out option is required.");
303                 
304                 this.assemblies = assemblies.Select (a => LoadAssembly (a)).ToList ();
305
306                 // Store types that have been forwarded to avoid duplicate generation
307                 GatherForwardedTypes ();
308
309                 docEnum = docEnum ?? new DocumentationEnumerator ();
310                 
311                 // PERFORM THE UPDATES
312                 
313                 if (types.Count > 0) {
314                         types.Sort ();
315                         DoUpdateTypes (srcPath, types, srcPath);
316                 }
317 #if false
318                 else if (opts.@namespace != null)
319                         DoUpdateNS (opts.@namespace, Path.Combine (opts.path, opts.@namespace),
320                                         Path.Combine (dest_dir, opts.@namespace));
321 #endif
322                 else
323                         DoUpdateAssemblies (srcPath, srcPath);
324
325                 Console.WriteLine("Members Added: {0}, Members Deleted: {1}", additions, deletions);
326         }
327                 public static bool IsInAssemblies(string name) {
328                         var query = Instance.assemblies.Where (a => a.MainModule.Name == name).ToArray ();
329                         return query.Length > 0;
330                 }
331         void AddImporter (string path)
332         {
333                 try {
334                         XmlReader r = new XmlTextReader (path);
335                         if (r.Read ()) {
336                                 while (r.NodeType != XmlNodeType.Element) {
337                                         if (!r.Read ())
338                                                 Error ("Unable to read XML file: {0}.", path);
339                                 }
340                                 if (r.LocalName == "doc") {
341                                         importers.Add (new MsxdocDocumentationImporter (path));
342                                 }
343                                 else if (r.LocalName == "Libraries") {
344                                         var ecmadocs = new XmlTextReader (path);
345                                         docEnum = new EcmaDocumentationEnumerator (this, ecmadocs);
346                                         importers.Add (new EcmaDocumentationImporter (ecmadocs));
347                                 }
348                                 else
349                                         Error ("Unsupported XML format within {0}.", path);
350                         }
351                         r.Close ();
352                 } catch (Exception e) {
353                         Environment.ExitCode = 1;
354                         Error ("Could not load XML file: {0}.", e.Message);
355                 }
356         }
357
358         void GatherForwardedTypes ()
359         {
360                 foreach (var asm in assemblies)
361                         foreach (var type in asm.MainModule.ExportedTypes.Where (t => t.IsForwarder).Select (t => t.FullName))
362                                 forwardedTypes.Add (type);
363         }
364
365         static ExceptionLocations ParseExceptionLocations (string s)
366         {
367                 ExceptionLocations loc = ExceptionLocations.Member;
368                 if (s == null)
369                         return loc;
370                 foreach (var type in s.Split (',')) {
371                         switch (type) {
372                                 case "added":   loc |= ExceptionLocations.AddedMembers; break;
373                                 case "all":     loc |= ExceptionLocations.Assembly | ExceptionLocations.DependentAssemblies; break;
374                                 case "asm":     loc |= ExceptionLocations.Assembly; break;
375                                 case "depasm":  loc |= ExceptionLocations.DependentAssemblies; break;
376                                 default:        throw new NotSupportedException ("Unsupported --exceptions value: " + type);
377                         }
378                 }
379                 return loc;
380         }
381
382         internal void Warning (string format, params object[] args)
383         {
384                 Message (TraceLevel.Warning, "mdoc: " + format, args);
385         }
386         
387         private AssemblyDefinition LoadAssembly (string name)
388         {
389                 AssemblyDefinition assembly = null;
390                 try {
391                         assembly = AssemblyDefinition.ReadAssembly (name, new ReaderParameters { AssemblyResolver = assemblyResolver });
392                 } catch (System.IO.FileNotFoundException) { }
393
394                 if (assembly == null)
395                         throw new InvalidOperationException("Assembly " + name + " not found.");
396
397                 return assembly;
398         }
399
400         private static void WriteXml(XmlElement element, System.IO.TextWriter output) {
401                 OrderTypeAttributes (element);
402                 XmlTextWriter writer = new XmlTextWriter(output);
403                 writer.Formatting = Formatting.Indented;
404                 writer.Indentation = 2;
405                 writer.IndentChar = ' ';
406                 element.WriteTo(writer);
407                 output.WriteLine();     
408         }
409
410         private static void WriteFile (string filename, FileMode mode, Action<TextWriter> action)
411         {
412                 Action<string> creator = file => {
413                         using (var writer = OpenWrite (file, mode))
414                                 action (writer);
415                 };
416
417                 MdocFile.UpdateFile (filename, creator);
418         }
419
420         private static void OrderTypeAttributes (XmlElement e)
421         {
422                 foreach (XmlElement type in e.SelectNodes ("//Type")) {
423                         OrderTypeAttributes (type.Attributes);
424                 }
425         }
426
427         static readonly string[] TypeAttributeOrder = {
428                 "Name", "FullName", "FullNameSP", "Maintainer"
429         };
430
431         private static void OrderTypeAttributes (XmlAttributeCollection c)
432         {
433                 XmlAttribute[] attrs = new XmlAttribute [TypeAttributeOrder.Length];
434                 for (int i = 0; i < c.Count; ++i) {
435                         XmlAttribute a = c [i];
436                         for (int j = 0; j < TypeAttributeOrder.Length; ++j) {
437                                 if (a.Name == TypeAttributeOrder [j]) {
438                                         attrs [j] = a;
439                                         break;
440                                 }
441                         }
442                 }
443                 for (int i = attrs.Length-1; i >= 0; --i) {
444                         XmlAttribute n = attrs [i];
445                         if (n == null)
446                                 continue;
447                         XmlAttribute r = null;
448                         for (int j = i+1; j < attrs.Length; ++j) {
449                                 if (attrs [j] != null) {
450                                         r = attrs [j];
451                                         break;
452                                 }
453                         }
454                         if (r == null)
455                                 continue;
456                         if (c [n.Name] != null) {
457                                 c.RemoveNamedItem (n.Name);
458                                 c.InsertBefore (n, r);
459                         }
460                 }
461         }
462         
463         private XmlDocument CreateIndexStub()
464         {
465                 XmlDocument index = new XmlDocument();
466
467                 XmlElement index_root = index.CreateElement("Overview");
468                 index.AppendChild(index_root);
469
470                 if (assemblies.Count == 0)
471                         throw new Exception ("No assembly");
472
473                 XmlElement index_assemblies = index.CreateElement("Assemblies");
474                 index_root.AppendChild(index_assemblies);
475
476                 XmlElement index_remarks = index.CreateElement("Remarks");
477                 index_remarks.InnerText = "To be added.";
478                 index_root.AppendChild(index_remarks);
479
480                 XmlElement index_copyright = index.CreateElement("Copyright");
481                 index_copyright.InnerText = "To be added.";
482                 index_root.AppendChild(index_copyright);
483
484                 XmlElement index_types = index.CreateElement("Types");
485                 index_root.AppendChild(index_types);
486                 
487                 return index;
488         }
489         
490         private static void WriteNamespaceStub(string ns, string outdir) {
491                 XmlDocument index = new XmlDocument();
492
493                 XmlElement index_root = index.CreateElement("Namespace");
494                 index.AppendChild(index_root);
495                 
496                 index_root.SetAttribute("Name", ns);
497
498                 XmlElement index_docs = index.CreateElement("Docs");
499                 index_root.AppendChild(index_docs);
500
501                 XmlElement index_summary = index.CreateElement("summary");
502                 index_summary.InnerText = "To be added.";
503                 index_docs.AppendChild(index_summary);
504
505                 XmlElement index_remarks = index.CreateElement("remarks");
506                 index_remarks.InnerText = "To be added.";
507                 index_docs.AppendChild(index_remarks);
508
509                 WriteFile (outdir + "/ns-" + ns + ".xml", FileMode.CreateNew, 
510                                 writer => WriteXml (index.DocumentElement, writer));
511         }
512
513         public void DoUpdateTypes (string basepath, List<string> typenames, string dest)
514         {
515                 var index = CreateIndexForTypes (dest);
516
517                 var found = new HashSet<string> ();
518                 foreach (AssemblyDefinition assembly in assemblies) {
519                         foreach (TypeDefinition type in docEnum.GetDocumentationTypes (assembly, typenames)) {
520                                 string relpath = DoUpdateType (type, basepath, dest);
521                                 if (relpath == null)
522                                         continue;
523
524                                 found.Add (type.FullName);
525
526                                 if (index == null)
527                                         continue;
528
529                                 index.Add (assembly);
530                                 index.Add (type);
531                         }
532                 }
533
534                 if (index != null)
535                         index.Write ();
536                 
537                 if (ignore_missing_types)
538                         return;
539
540                 var notFound = from n in typenames where !found.Contains (n) select n;
541                 if (notFound.Any ())
542                         throw new InvalidOperationException("Type(s) not found: " + string.Join (", ", notFound.ToArray ()));
543         }
544
545         class IndexForTypes {
546
547                 MDocUpdater app;
548                 string indexFile;
549
550                 XmlDocument index;
551                 XmlElement index_types;
552                 XmlElement index_assemblies;
553
554                 public IndexForTypes (MDocUpdater app, string indexFile, XmlDocument index)
555                 {
556                         this.app        = app;
557                         this.indexFile  = indexFile;
558                         this.index      = index;
559
560                         index_types = WriteElement (index.DocumentElement, "Types");
561                         index_assemblies = WriteElement (index.DocumentElement, "Assemblies");
562                 }
563
564                 public void Add (AssemblyDefinition assembly)
565                 {
566                         if (index_assemblies.SelectSingleNode ("Assembly[@Name='" + assembly.Name.Name + "']") != null)
567                                 return;
568
569                         app.AddIndexAssembly (assembly, index_assemblies);
570                 }
571
572                 public void Add (TypeDefinition type)
573                 {
574                         app.AddIndexType (type, index_types);
575                 }
576
577                 public void Write ()
578                 {
579                         SortIndexEntries (index_types);
580                         WriteFile (indexFile, FileMode.Create, 
581                                         writer => WriteXml (index.DocumentElement, writer));
582                 }
583         }
584
585         IndexForTypes CreateIndexForTypes (string dest)
586         {
587                 string indexFile = Path.Combine (dest, "index.xml");
588                 if (File.Exists (indexFile))
589                         return null;
590                 return new IndexForTypes (this, indexFile, CreateIndexStub ());
591         }
592
593         /// <summary>Constructs the presumed path to the type's documentation file</summary>
594         /// <returns><c>true</c>, if the type file was found, <c>false</c> otherwise.</returns>
595         /// <param name="result">A typle that contains 1) the 'reltypefile', 2) the 'typefile', and 3) the file info</param>
596         bool TryFindTypeFile(string nsname, string typename, string basepath, out Tuple<string, string, FileInfo> result) {
597                 string reltypefile = DocUtils.PathCombine (nsname, typename + ".xml");
598                 string typefile = Path.Combine (basepath, reltypefile);
599                 System.IO.FileInfo file = new System.IO.FileInfo(typefile);
600
601                 result = new Tuple<string, string, FileInfo> (reltypefile, typefile, file);
602
603                 return file.Exists;
604         }
605         
606         public string DoUpdateType (TypeDefinition type, string basepath, string dest)
607         {
608                 if (type.Namespace == null)
609                         Warning ("warning: The type `{0}' is in the root namespace.  This may cause problems with display within monodoc.",
610                                         type.FullName);
611                 if (!IsPublic (type))
612                         return null;
613                 
614                 // Must get the A+B form of the type name.
615                 string typename = GetTypeFileName(type);
616                 string nsname = DocUtils.GetNamespace (type);
617
618                 // Find the file, if it exists
619                 string[] searchLocations = new string[] {
620                         nsname
621                 };
622
623                 if (MDocUpdater.HasDroppedNamespace (type)) {
624                         // If dropping namespace, types may have moved into a couple of different places.
625                         var newSearchLocations = searchLocations.Union (new string[] {
626                                 string.Format ("{0}.{1}", droppedNamespace, nsname),
627                                 nsname.Replace (droppedNamespace + ".", string.Empty),
628                                 MDocUpdater.droppedNamespace
629                         });
630
631                         searchLocations = newSearchLocations.ToArray ();
632                 }
633
634                 string reltypefile="", typefile="";
635                 System.IO.FileInfo file = null;
636
637                 foreach (var f in searchLocations) {
638                         Tuple<string, string, FileInfo> result;
639                         bool fileExists = TryFindTypeFile (f, typename, basepath, out result);
640
641                         if (fileExists) {
642                                 reltypefile = result.Item1;
643                                 typefile = result.Item2;
644                                 file = result.Item3;
645
646                                 break;
647                         }
648                 }
649
650                 if (file == null || !file.Exists) {
651                         // we were not able to find a file, let's use the original type informatio.
652                         // so that we create the stub in the right place.
653                         Tuple<string, string, FileInfo> result;
654                         TryFindTypeFile (nsname, typename, basepath, out result);
655
656                         reltypefile = result.Item1;
657                         typefile = result.Item2;
658                         file = result.Item3;
659                 }
660                 
661                 string output = null;
662                 if (dest == null) {
663                         output = typefile;
664                 } else if (dest == "-") {
665                         output = null;
666                 } else {
667                         output = Path.Combine (dest, reltypefile);
668                 }       
669
670                 if (file != null && file.Exists) {
671                         // Update
672                         XmlDocument basefile = new XmlDocument();
673                         try {
674                                 basefile.Load(typefile);
675                         } catch (Exception e) {
676                                 throw new InvalidOperationException("Error loading " + typefile + ": " + e.Message, e);
677                         }
678                         
679                         DoUpdateType2("Updating", basefile, type, output, false);
680                 } else {
681                         // Stub
682                         XmlElement td = StubType(type, output);
683                         if (td == null)
684                                 return null;
685                 }
686                 return reltypefile;
687         }
688
689         public void DoUpdateNS (string ns, string nspath, string outpath)
690         {
691                 Dictionary<TypeDefinition, object> seenTypes = new Dictionary<TypeDefinition,object> ();
692                 AssemblyDefinition                  assembly = assemblies [0];
693
694                 foreach (System.IO.FileInfo file in new System.IO.DirectoryInfo(nspath).GetFiles("*.xml")) {
695                         XmlDocument basefile = new XmlDocument();
696                         string typefile = Path.Combine(nspath, file.Name);
697                         try {
698                                 basefile.Load(typefile);
699                         } catch (Exception e) {
700                                 throw new InvalidOperationException("Error loading " + typefile + ": " + e.Message, e);
701                         }
702
703                         string typename = 
704                                 GetTypeFileName (basefile.SelectSingleNode("Type/@FullName").InnerText);
705                         TypeDefinition type = assembly.GetType(typename);
706                         if (type == null) {
707                                         // --
708                                         if (!string.IsNullOrWhiteSpace (droppedNamespace)) {
709                                                 string nameWithNs = string.Format ("{0}.{1}", droppedNamespace, typename);
710                                                 type = assembly.GetType (nameWithNs);
711                                                 if (type == null) {
712                                                         Warning ("Type no longer in assembly: " + typename);
713                                                         continue;
714                                                 }
715                                         }
716                                         //--
717                         }                       
718
719                         seenTypes[type] = seenTypes;
720                         DoUpdateType2("Updating", basefile, type, Path.Combine(outpath, file.Name), false);
721                 }
722                 
723                 // Stub types not in the directory
724                 foreach (TypeDefinition type in docEnum.GetDocumentationTypes (assembly, null)) {
725                         if (type.Namespace != ns || seenTypes.ContainsKey(type))
726                                 continue;
727
728                         XmlElement td = StubType(type, Path.Combine(outpath, GetTypeFileName(type) + ".xml"));
729                         if (td == null) continue;
730                 }
731         }
732         
733         private static string GetTypeFileName (TypeReference type)
734         {
735                 return filenameFormatter.GetName (type);
736         }
737
738         public static string GetTypeFileName (string typename)
739         {
740                 StringBuilder filename = new StringBuilder (typename.Length);
741                 int numArgs = 0;
742                 int numLt = 0;
743                 bool copy = true;
744                 for (int i = 0; i < typename.Length; ++i) {
745                         char c = typename [i];
746                         switch (c) {
747                                 case '<':
748                                         copy = false;
749                                         ++numLt;
750                                         break;
751                                 case '>':
752                                         --numLt;
753                                         if (numLt == 0) {
754                                                 filename.Append ('`').Append ((numArgs+1).ToString());
755                                                 numArgs = 0;
756                                                 copy = true;
757                                         }
758                                         break;
759                                 case ',':
760                                         if (numLt == 1)
761                                                 ++numArgs;
762                                         break;
763                                 default:
764                                         if (copy)
765                                                 filename.Append (c);
766                                         break;
767                         }
768                 }
769                 return filename.ToString ();
770         }
771
772         private void AddIndexAssembly (AssemblyDefinition assembly, XmlElement parent)
773         {
774                 XmlElement index_assembly = null;
775                 if (multiassembly) 
776                         index_assembly = (XmlElement)parent.SelectSingleNode ("Assembly[@Name='"+ assembly.Name.Name +"']");
777                 
778                 if (index_assembly == null) 
779                         index_assembly = parent.OwnerDocument.CreateElement ("Assembly");
780
781                 index_assembly.SetAttribute ("Name", assembly.Name.Name);
782                 index_assembly.SetAttribute ("Version", assembly.Name.Version.ToString());
783
784                 AssemblyNameDefinition name = assembly.Name;
785                 if (name.HasPublicKey) {
786                         XmlElement pubkey = parent.OwnerDocument.CreateElement ("AssemblyPublicKey");
787                         var key = new StringBuilder (name.PublicKey.Length*3 + 2);
788                         key.Append ("[");
789                         foreach (byte b in name.PublicKey)
790                                 key.AppendFormat ("{0,2:x2} ", b);
791                         key.Append ("]");
792                         pubkey.InnerText = key.ToString ();
793                         index_assembly.AppendChild (pubkey);
794                 }
795
796                 if (!string.IsNullOrEmpty (name.Culture)) {
797                         XmlElement culture = parent.OwnerDocument.CreateElement ("AssemblyCulture");
798                         culture.InnerText = name.Culture;
799                         index_assembly.AppendChild (culture);
800                 }
801
802                 MakeAttributes (index_assembly, GetCustomAttributes (assembly.CustomAttributes, ""));
803                 parent.AppendChild(index_assembly);
804         }
805
806         private void AddIndexType (TypeDefinition type, XmlElement index_types)
807         {
808                 string typename = GetTypeFileName(type);
809
810                 // Add namespace and type nodes into the index file as needed
811                 string ns = DocUtils.GetNamespace (type);
812                 XmlElement nsnode = (XmlElement) index_types.SelectSingleNode ("Namespace[@Name='" + ns + "']");
813                 if (nsnode == null) {
814                         nsnode = index_types.OwnerDocument.CreateElement("Namespace");
815                         nsnode.SetAttribute ("Name", ns);
816                         index_types.AppendChild (nsnode);
817                 }
818                 string doc_typename = GetDocTypeName (type);
819                 XmlElement typenode = (XmlElement) nsnode.SelectSingleNode ("Type[@Name='" + typename + "']");
820                 if (typenode == null) {
821                         typenode = index_types.OwnerDocument.CreateElement ("Type");
822                         typenode.SetAttribute ("Name", typename);
823                         nsnode.AppendChild (typenode);
824                 }
825                 if (typename != doc_typename)
826                         typenode.SetAttribute("DisplayName", doc_typename);
827                 else
828                         typenode.RemoveAttribute("DisplayName");
829
830                 typenode.SetAttribute ("Kind", GetTypeKind (type));
831         }
832
833         private void DoUpdateAssemblies (string source, string dest) 
834         {
835                 string indexfile = dest + "/index.xml";
836                 XmlDocument index;
837                 if (System.IO.File.Exists(indexfile)) {
838                         index = new XmlDocument();
839                         index.Load(indexfile);
840
841                         // Format change
842                         ClearElement(index.DocumentElement, "Assembly");
843                         ClearElement(index.DocumentElement, "Attributes");
844                 } else {
845                         index = CreateIndexStub();
846                 }
847                 
848                 string defaultTitle = "Untitled";
849                 if (assemblies.Count == 1)
850                         defaultTitle = assemblies[0].Name.Name;
851                 WriteElementInitialText(index.DocumentElement, "Title", defaultTitle);
852                 
853                 XmlElement index_types = WriteElement(index.DocumentElement, "Types");
854                 XmlElement index_assemblies = WriteElement(index.DocumentElement, "Assemblies");
855                 if (!multiassembly) 
856                         index_assemblies.RemoveAll ();
857
858
859                 HashSet<string> goodfiles = new HashSet<string> (StringComparer.OrdinalIgnoreCase);
860
861                 foreach (AssemblyDefinition assm in assemblies) {
862                         AddIndexAssembly (assm, index_assemblies);
863                         DoUpdateAssembly (assm, index_types, source, dest, goodfiles);
864                 }
865
866                 SortIndexEntries (index_types);
867                 
868                 CleanupFiles (dest, goodfiles);
869                 CleanupIndexTypes (index_types, goodfiles);
870                 CleanupExtensions (index_types);
871
872                 WriteFile (indexfile, FileMode.Create, 
873                                 writer => WriteXml(index.DocumentElement, writer));
874         }
875                 
876         private static char[] InvalidFilenameChars = {'\\', '/', ':', '*', '?', '"', '<', '>', '|'};
877
878         private void DoUpdateAssembly (AssemblyDefinition assembly, XmlElement index_types, string source, string dest, HashSet<string> goodfiles) 
879         {
880                 foreach (TypeDefinition type in docEnum.GetDocumentationTypes (assembly, null)) {
881                         string typename = GetTypeFileName(type);
882                         if (!IsPublic (type) || typename.IndexOfAny (InvalidFilenameChars) >= 0 || forwardedTypes.Contains (type.FullName))
883                                 continue;
884
885                         string reltypepath = DoUpdateType (type, source, dest);
886                         if (reltypepath == null)
887                                 continue;
888                         
889                         // Add namespace and type nodes into the index file as needed
890                         AddIndexType (type, index_types);
891
892                         // Ensure the namespace index file exists
893                         string namespaceToUse = type.Namespace;
894                         if (HasDroppedNamespace(assembly)) {
895                                 namespaceToUse = string.Format ("{0}.{1}", droppedNamespace, namespaceToUse);
896                         }
897                         string onsdoc = DocUtils.PathCombine (dest, namespaceToUse + ".xml");
898                         string nsdoc  = DocUtils.PathCombine (dest, "ns-" + namespaceToUse + ".xml");
899                         if (File.Exists (onsdoc)) {
900                                 File.Move (onsdoc, nsdoc);
901                         }
902
903                         if (!File.Exists (nsdoc)) {
904                                 Console.WriteLine("New Namespace File: " + type.Namespace);
905                                 WriteNamespaceStub(namespaceToUse, dest);
906                         }
907
908                         goodfiles.Add (reltypepath);
909                 }
910         }
911
912         private static void SortIndexEntries (XmlElement indexTypes)
913         {
914                 XmlNodeList namespaces = indexTypes.SelectNodes ("Namespace");
915                 XmlNodeComparer c = new AttributeNameComparer ();
916                 SortXmlNodes (indexTypes, namespaces, c);
917
918                 for (int i = 0; i < namespaces.Count; ++i)
919                         SortXmlNodes (namespaces [i], namespaces [i].SelectNodes ("Type"), c);
920         }
921
922         private static void SortXmlNodes (XmlNode parent, XmlNodeList children, XmlNodeComparer comparer)
923         {
924                 MyXmlNodeList l = new MyXmlNodeList (children.Count);
925                 for (int i = 0; i < children.Count; ++i)
926                         l.Add (children [i]);
927                 l.Sort (comparer);
928                 for (int i = l.Count - 1; i > 0; --i) {
929                         parent.InsertBefore (parent.RemoveChild ((XmlNode) l [i-1]), (XmlNode) l [i]);
930                 }
931         }
932
933         abstract class XmlNodeComparer : IComparer, IComparer<XmlNode>
934         {
935                 public abstract int Compare (XmlNode x, XmlNode y);
936
937                 public int Compare (object x, object y)
938                 {
939                         return Compare ((XmlNode) x, (XmlNode) y);
940                 }
941         }
942
943         class AttributeNameComparer : XmlNodeComparer {
944                 string attribute;
945
946                 public AttributeNameComparer ()
947                         : this ("Name")
948                 {
949                 }
950
951                 public AttributeNameComparer (string attribute)
952                 {
953                         this.attribute = attribute;
954                 }
955
956                 public override int Compare (XmlNode x, XmlNode y)
957                 {
958                         return x.Attributes [attribute].Value.CompareTo (y.Attributes [attribute].Value);
959                 }
960         }
961         
962         class VersionComparer : XmlNodeComparer {
963                 public override int Compare (XmlNode x, XmlNode y)
964                 {
965                         // Some of the existing docs use e.g. 1.0.x.x, which Version doesn't like.
966                         string a = GetVersion (x.InnerText);
967                         string b = GetVersion (y.InnerText);
968                         return new Version (a).CompareTo (new Version (b));
969                 }
970
971                 static string GetVersion (string v)
972                 {
973                         int n = v.IndexOf ("x");
974                         if (n < 0)
975                                 return v;
976                         return v.Substring (0, n-1);
977                 }
978         }
979
980         private static string GetTypeKind (TypeDefinition type)
981         {
982                 if (type.IsEnum)
983                         return "Enumeration";
984                 if (type.IsValueType)
985                         return "Structure";
986                 if (type.IsInterface)
987                         return "Interface";
988                 if (DocUtils.IsDelegate (type))
989                         return "Delegate";
990                 if (type.IsClass || type.FullName == "System.Enum") // FIXME
991                         return "Class";
992                 throw new ArgumentException ("Unknown kind for type: " + type.FullName);
993         }
994
995         public static bool IsPublic (TypeDefinition type)
996         {
997                 TypeDefinition decl = type;
998                 while (decl != null) {
999                         if (!(decl.IsPublic || decl.IsNestedPublic ||
1000                                                 decl.IsNestedFamily || decl.IsNestedFamily || decl.IsNestedFamilyOrAssembly)) {
1001                                 return false;
1002                         }
1003                         decl = (TypeDefinition) decl.DeclaringType;
1004                 }
1005                 return true;
1006         }
1007
1008         private void CleanupFiles (string dest, HashSet<string> goodfiles)
1009         {
1010                 // Look for files that no longer correspond to types
1011                 foreach (System.IO.DirectoryInfo nsdir in new System.IO.DirectoryInfo(dest).GetDirectories("*")) {
1012                         foreach (System.IO.FileInfo typefile in nsdir.GetFiles("*.xml")) {
1013                                 string relTypeFile = Path.Combine(nsdir.Name, typefile.Name);
1014                                 if (!goodfiles.Contains (relTypeFile)) {
1015                                         XmlDocument doc = new XmlDocument ();
1016                                         doc.Load (typefile.FullName);
1017                                         XmlElement e = doc.SelectSingleNode("/Type") as XmlElement;
1018                                         string assemblyName = doc.SelectSingleNode ("/Type/AssemblyInfo/AssemblyName").InnerText;
1019                                         AssemblyDefinition assembly = assemblies.FirstOrDefault (a => a.Name.Name == assemblyName);
1020
1021                                         Action saveDoc = () => {
1022                                                 using (TextWriter writer = OpenWrite (typefile.FullName, FileMode.Truncate))
1023                                                         WriteXml(doc.DocumentElement, writer);
1024                                         };
1025
1026                                         if (e != null && !no_assembly_versions && assembly != null && assemblyName != null && UpdateAssemblyVersions (e, assembly, GetAssemblyVersions(assemblyName), false)) {
1027                                                 saveDoc ();
1028                                                 goodfiles.Add (relTypeFile);
1029                                                 continue;
1030                                         }
1031
1032                                         Action actuallyDelete = () => {
1033                                                 string newname = typefile.FullName + ".remove";
1034                                                 try { System.IO.File.Delete (newname); } catch (Exception) { Warning ("Unable to delete existing file: {0}", newname); }
1035                                                 try { typefile.MoveTo (newname); } catch (Exception) { Warning ("Unable to rename to: {0}", newname); }
1036                                                 Console.WriteLine ("Class no longer present; file renamed: " + Path.Combine (nsdir.Name, typefile.Name));
1037                                         };
1038
1039                                         if (string.IsNullOrWhiteSpace (PreserveTag)) { // only do this if there was not a -preserve
1040                                                 saveDoc ();
1041
1042                                                 var unifiedAssemblyNode = doc.SelectSingleNode ("/Type/AssemblyInfo[@apistyle='unified']");
1043                                                 var classicAssemblyNode = doc.SelectSingleNode ("/Type/AssemblyInfo[@apistyle='classic']");
1044                                                 var unifiedMembers = doc.SelectNodes ("//Member[@apistyle='unified']|//Member/AssemblyInfo[@apistyle='unified']");
1045                                                 var classicMembers = doc.SelectNodes ("//Member[@apistyle='classic']|//Member/AssemblyInfo[@apistyle='classic']");
1046                                                 bool isUnifiedRun = HasDroppedAnyNamespace ();
1047                                                 bool isClassicOrNormalRun = !isUnifiedRun;
1048
1049                                                 Action<XmlNode, ApiStyle> removeStyles = (x, style) => {
1050                                                         var styledNodes = doc.SelectNodes("//*[@apistyle='"+ style.ToString ().ToLowerInvariant () +"']");
1051                                                         if (styledNodes != null && styledNodes.Count > 0) {
1052                                                                 foreach(var node in styledNodes.Cast<XmlNode> ()) {
1053                                                                         node.ParentNode.RemoveChild (node);
1054                                                                 }
1055                                                         }
1056                                                         saveDoc ();
1057                                                 };
1058                                                 if (isClassicOrNormalRun) {
1059                                                         if (unifiedAssemblyNode != null || unifiedMembers.Count > 0) {
1060                                                                 Warning ("*** this type is marked as unified, not deleting during this run: {0}", typefile.FullName);
1061                                                                 // if truly removed from both assemblies, it will be removed fully during the unified run
1062                                                                 removeStyles (doc, ApiStyle.Classic);
1063                                                                 continue;
1064                                                         } else {
1065                                                                 // we should be safe to delete here because it was not marked as a unified assembly
1066                                                                 actuallyDelete ();
1067                                                         }
1068                                                 }
1069                                                 if (isUnifiedRun) {
1070                                                         if (classicAssemblyNode != null || classicMembers.Count > 0) {
1071                                                                 Warning ("*** this type is marked as classic, not deleting {0}", typefile.FullName);
1072                                                                 continue; 
1073                                                         } else {
1074                                                                 // safe to delete because it wasn't marked as a classic assembly, so the type is gone in both.
1075                                                                 actuallyDelete ();
1076                                                         }
1077                                                 }
1078                                         }
1079                                 }
1080                         }
1081                 }
1082         }
1083
1084         private static TextWriter OpenWrite (string path, FileMode mode)
1085         {
1086                 var w = new StreamWriter (
1087                         new FileStream (path, mode),
1088                         new UTF8Encoding (false)
1089                 );
1090                 w.NewLine = "\n";
1091                 return w;
1092         }
1093
1094         private string[] GetAssemblyVersions (string assemblyName)
1095         {
1096                 return (from a in assemblies 
1097                         where a.Name.Name == assemblyName 
1098                         select GetAssemblyVersion (a)).ToArray ();
1099         }
1100
1101         private static void CleanupIndexTypes (XmlElement index_types, HashSet<string> goodfiles)
1102         {
1103                 // Look for type nodes that no longer correspond to types
1104                 MyXmlNodeList remove = new MyXmlNodeList ();
1105                 foreach (XmlElement typenode in index_types.SelectNodes("Namespace/Type")) {
1106                         string fulltypename = Path.Combine (((XmlElement)typenode.ParentNode).GetAttribute("Name"), typenode.GetAttribute("Name") + ".xml");
1107                         if (!goodfiles.Contains (fulltypename)) {
1108                                 remove.Add (typenode);
1109                         }
1110                 }
1111                 foreach (XmlNode n in remove)
1112                         n.ParentNode.RemoveChild (n);
1113         }
1114
1115         private void CleanupExtensions (XmlElement index_types)
1116         {
1117                 XmlNode e = index_types.SelectSingleNode ("/Overview/ExtensionMethods");
1118                 if (extensionMethods.Count == 0) {
1119                         if (e == null)
1120                                 return;
1121                         index_types.SelectSingleNode ("/Overview").RemoveChild (e);
1122                         return;
1123                 }
1124                 if (e == null) {
1125                         e = index_types.OwnerDocument.CreateElement ("ExtensionMethods");
1126                         index_types.SelectSingleNode ("/Overview").AppendChild (e);
1127                 }
1128                 else
1129                         e.RemoveAll ();
1130                 extensionMethods.Sort (DefaultExtensionMethodComparer);
1131                 foreach (XmlNode m in extensionMethods) {
1132                         e.AppendChild (index_types.OwnerDocument.ImportNode (m, true));
1133                 }
1134         }
1135
1136         class ExtensionMethodComparer : XmlNodeComparer {
1137                 public override int Compare (XmlNode x, XmlNode y)
1138                 {
1139                         XmlNode xLink = x.SelectSingleNode ("Member/Link");
1140                         XmlNode yLink = y.SelectSingleNode ("Member/Link");
1141
1142                         int n = xLink.Attributes ["Type"].Value.CompareTo (
1143                                         yLink.Attributes ["Type"].Value);
1144                         if (n != 0)
1145                                 return n;
1146                         n = xLink.Attributes ["Member"].Value.CompareTo (
1147                                         yLink.Attributes ["Member"].Value);
1148                         if (n == 0 && !object.ReferenceEquals (x, y))
1149                                 throw new InvalidOperationException ("Duplicate extension method found!");
1150                         return n;
1151                 }
1152         }
1153
1154         static readonly XmlNodeComparer DefaultExtensionMethodComparer = new ExtensionMethodComparer ();
1155                 
1156         public void DoUpdateType2 (string message, XmlDocument basefile, TypeDefinition type, string output, bool insertSince)
1157         {
1158                 Console.WriteLine(message + ": " + type.FullName);
1159                 
1160                 StringToXmlNodeMap seenmembers = new StringToXmlNodeMap ();
1161
1162                 // Update type metadata
1163                 UpdateType(basefile.DocumentElement, type);
1164
1165                 // Update existing members.  Delete member nodes that no longer should be there,
1166                 // and remember what members are already documented so we don't add them again.
1167                 
1168                 MyXmlNodeList todelete = new MyXmlNodeList ();
1169                 
1170                 foreach (DocsNodeInfo info in docEnum.GetDocumentationMembers (basefile, type)) {
1171                         XmlElement oldmember  = info.Node;
1172                         MemberReference oldmember2 = info.Member;
1173
1174                         if (info.Member != null &&  info.Node != null) {
1175                                 // Check for an error condition where the xml MemberName doesn't match the matched member
1176                                 var memberName = GetMemberName (info.Member);
1177                                 var memberAttribute = info.Node.Attributes ["MemberName"];
1178                                 if (memberAttribute == null || (memberAttribute.Value != memberName && memberAttribute.Value.Split (',').Length != memberName.Split (',').Length)) {
1179                                         oldmember.SetAttribute ("MemberName", memberName);
1180                                 }
1181                         }
1182
1183                         string sig = oldmember2 != null ? memberFormatters [0].GetDeclaration (oldmember2) : null;
1184                         
1185                         // Interface implementations and overrides are deleted from the docs
1186                         // unless the overrides option is given.
1187                         if (oldmember2 != null && sig == null)
1188                                 oldmember2 = null;
1189                         
1190                         // Deleted (or signature changed)
1191                         if (oldmember2 == null) {
1192                                 if (!no_assembly_versions && UpdateAssemblyVersions (oldmember, type.Module.Assembly, new string[]{ GetAssemblyVersion (type.Module.Assembly) }, false))
1193                                         continue;
1194
1195                                 DeleteMember ("Member Removed", output, oldmember, todelete, type);
1196                                 continue;
1197                         }
1198                         
1199                         // Duplicated
1200                         if (seenmembers.ContainsKey (sig)) {
1201                                 if (object.ReferenceEquals (oldmember, seenmembers [sig])) {
1202                                         // ignore, already seen
1203                                 }
1204                                 else if (DefaultMemberComparer.Compare (oldmember, seenmembers [sig]) == 0)
1205                                         DeleteMember ("Duplicate Member Found", output, oldmember, todelete, type);
1206                                 else
1207                                         Warning ("TODO: found a duplicate member '{0}', but it's not identical to the prior member found!", sig);
1208                                 continue;
1209                         }
1210                         
1211                         // Update signature information
1212                         UpdateMember(info);
1213
1214                         // get all apistyles of sig from info.Node
1215                         var styles = oldmember.GetElementsByTagName ("MemberSignature").Cast<XmlElement> ()
1216                                 .Where (x => x.GetAttribute ("Language") == "C#" && !seenmembers.ContainsKey(x.GetAttribute("Value")))
1217                                 .Select (x => x.GetAttribute ("Value"));
1218
1219                         foreach (var stylesig in styles) {
1220                                 seenmembers.Add (stylesig, oldmember);
1221                         }
1222                 }
1223                 foreach (XmlElement oldmember in todelete)
1224                         oldmember.ParentNode.RemoveChild (oldmember);
1225                 
1226                 
1227                 if (!DocUtils.IsDelegate (type)) {
1228                         XmlNode members = WriteElement (basefile.DocumentElement, "Members");
1229                         var typemembers = type.GetMembers()
1230                                         .Where(m => {
1231                                                 if (m is TypeDefinition) return false;
1232                                                 string sig = memberFormatters [0].GetDeclaration (m);
1233                                                 if (sig == null) return false;
1234                                                 if (seenmembers.ContainsKey(sig)) return false;
1235
1236                                                 // Verify that the member isn't an explicitly implemented 
1237                                                 // member of an internal interface, in which case we shouldn't return true.
1238                                                 MethodDefinition methdef = null;
1239                                                 if (m is MethodDefinition) 
1240                                                         methdef = m as MethodDefinition;
1241                                                 else if (m is PropertyDefinition) {
1242                                                         var prop = m as PropertyDefinition;
1243                                                         methdef = prop.GetMethod ?? prop.SetMethod;
1244                                                 }
1245
1246                                                 if (methdef != null) {
1247                                                         TypeReference iface;
1248                                                         MethodReference imethod;
1249
1250                                                         if (methdef.Overrides.Count == 1) {
1251                                                                 DocUtils.GetInfoForExplicitlyImplementedMethod (methdef, out iface, out imethod);
1252                                                                 if (!IsPublic (iface.Resolve ())) return false;
1253                                                         }
1254                                                 }
1255
1256                                                 return true;
1257                                         })
1258                                         .ToArray();
1259                         foreach (MemberReference m in typemembers) {
1260                                 XmlElement mm = MakeMember(basefile, new DocsNodeInfo (null, m));
1261                                 if (mm == null) continue;
1262
1263                                 if (MDocUpdater.SwitchingToMagicTypes) {
1264                                         // this is a unified style API that obviously doesn't exist in the classic API. Let's mark
1265                                         // it with apistyle="unified", so that it's not displayed for classic style APIs
1266                                         mm.SetAttribute ("apistyle", "unified");
1267                                 }
1268
1269                                 members.AppendChild( mm );
1270         
1271                                 Console.WriteLine("Member Added: " + mm.SelectSingleNode("MemberSignature/@Value").InnerText);
1272                                 additions++;
1273                         }
1274                 }
1275                 
1276                 // Import code snippets from files
1277                 foreach (XmlNode code in basefile.GetElementsByTagName("code")) {
1278                         if (!(code is XmlElement)) continue;
1279                         string file = ((XmlElement)code).GetAttribute("src");
1280                         string lang = ((XmlElement)code).GetAttribute("lang");
1281                         if (file != "") {
1282                                 string src = GetCodeSource (lang, Path.Combine (srcPath, file));
1283                                 if (src != null)
1284                                         code.InnerText = src;
1285                         }
1286                 }
1287
1288                 if (insertSince && since != null) {
1289                         XmlNode docs = basefile.DocumentElement.SelectSingleNode("Docs");
1290                         docs.AppendChild (CreateSinceNode (basefile));
1291                 }
1292
1293                 do {
1294                         XmlElement d = basefile.DocumentElement ["Docs"];
1295                         XmlElement m = basefile.DocumentElement ["Members"];
1296                         if (d != null && m != null)
1297                                 basefile.DocumentElement.InsertBefore (
1298                                                 basefile.DocumentElement.RemoveChild (d), m);
1299                         SortTypeMembers (m);
1300                 } while (false);
1301
1302                 if (output == null)
1303                         WriteXml(basefile.DocumentElement, Console.Out);
1304                 else {
1305                         FileInfo file = new FileInfo (output);
1306                         if (!file.Directory.Exists) {
1307                                 Console.WriteLine("Namespace Directory Created: " + type.Namespace);
1308                                 file.Directory.Create ();
1309                         }
1310                         WriteFile (output, FileMode.Create,
1311                                         writer => WriteXml(basefile.DocumentElement, writer));
1312                 }
1313         }
1314
1315         private string GetCodeSource (string lang, string file)
1316         {
1317                 int anchorStart;
1318                 if (lang == "C#" && (anchorStart = file.IndexOf (".cs#")) >= 0) {
1319                         // Grab the specified region
1320                         string region = "#region " + file.Substring (anchorStart + 4);
1321                         file          = file.Substring (0, anchorStart + 3);
1322                         try {
1323                                 using (StreamReader reader = new StreamReader (file)) {
1324                                         string line;
1325                                         StringBuilder src = new StringBuilder ();
1326                                         int indent = -1;
1327                                         while ((line = reader.ReadLine ()) != null) {
1328                                                 if (line.Trim() == region) {
1329                                                         indent = line.IndexOf (region);
1330                                                         continue;
1331                                                 }
1332                                                 if (indent >= 0 && line.Trim().StartsWith ("#endregion")) {
1333                                                         break;
1334                                                 }
1335                                                 if (indent >= 0)
1336                                                         src.Append (
1337                                                                         (line.Length > 0 ? line.Substring (indent) : string.Empty) +
1338                                                                         "\n");
1339                                         }
1340                                         return src.ToString ();
1341                                 }
1342                         } catch (Exception e) {
1343                                 Warning ("Could not load <code/> file '{0}' region '{1}': {2}",
1344                                                 file, region, show_exceptions ? e.ToString () : e.Message);
1345                                 return null;
1346                         }
1347                 }
1348                 try {
1349                         using (StreamReader reader = new StreamReader (file))
1350                                 return reader.ReadToEnd ();
1351                 } catch (Exception e) {
1352                         Warning ("Could not load <code/> file '" + file + "': " + e.Message);
1353                 }
1354                 return null;
1355         }
1356
1357         void DeleteMember (string reason, string output, XmlNode member, MyXmlNodeList todelete, TypeDefinition type)
1358         {
1359                 string format = output != null
1360                         ? "{0}: File='{1}'; Signature='{4}'"
1361                         : "{0}: XPath='/Type[@FullName=\"{2}\"]/Members/Member[@MemberName=\"{3}\"]'; Signature='{4}'";
1362                 string signature = member.SelectSingleNode ("MemberSignature[@Language='C#']/@Value").Value;
1363                 Warning (format,
1364                                 reason, 
1365                                 output,
1366                                 member.OwnerDocument.DocumentElement.GetAttribute ("FullName"),
1367                                 member.Attributes ["MemberName"].Value, 
1368                                 signature);
1369
1370                 // Identify all of the different states that could affect our decision to delete the member
1371                 bool shouldPreserve = !string.IsNullOrWhiteSpace (PreserveTag);
1372                 bool hasContent = MemberDocsHaveUserContent (member);
1373                 bool shouldDelete = !shouldPreserve && (delete || !hasContent);
1374
1375                 bool unifiedRun = HasDroppedNamespace (type);
1376
1377                 var classicAssemblyInfo = member.SelectSingleNode ("AssemblyInfo[@apistyle='classic']");
1378                 bool nodeIsClassic = classicAssemblyInfo != null || member.HasApiStyle (ApiStyle.Classic);
1379                 var unifiedAssemblyInfo = member.SelectSingleNode ("AssemblyInfo[@apistyle='unified']");
1380                 bool nodeIsUnified = unifiedAssemblyInfo != null || member.HasApiStyle (ApiStyle.Unified);
1381
1382                 Action actuallyDelete = () => {
1383                         todelete.Add (member);
1384                         deletions++;
1385                 };
1386
1387                 if (!shouldDelete) {
1388                         // explicitly not deleting
1389                         string message = shouldPreserve ? 
1390                                         "Not deleting '{0}' due to --preserve." :
1391                                         "Not deleting '{0}'; must be enabled with the --delete option";
1392                         Warning (message, signature);
1393                 } else if (unifiedRun && nodeIsClassic) {
1394                         // this is a unified run, and the member doesn't exist, but is marked as being in the classic assembly.
1395                         member.RemoveApiStyle (ApiStyle.Unified);
1396                         Warning ("Not removing '{0}' since it's still in the classic assembly.", signature);
1397                 } else if (unifiedRun && !nodeIsClassic) {
1398                         // unified run, and the node is not classic, which means it doesn't exist anywhere.
1399                         actuallyDelete ();
1400                 } else { 
1401                         if (!nodeIsClassic && !nodeIsUnified) { // regular codepath (ie. not classic/unified)
1402                                 actuallyDelete ();
1403                         } else { // this is a classic run
1404                                 Warning ("Removing classic from '{0}' ... will be removed in the unified run if not present there.", signature);
1405                                 member.RemoveApiStyle (ApiStyle.Classic);
1406                                 if (classicAssemblyInfo != null) {
1407                                         member.RemoveChild (classicAssemblyInfo);
1408                                 }
1409                         }
1410                 }
1411         }
1412
1413         class MemberComparer : XmlNodeComparer {
1414                 public override int Compare (XmlNode x, XmlNode y)
1415                 {
1416                         int r;
1417                         string xMemberName = x.Attributes ["MemberName"].Value;
1418                         string yMemberName = y.Attributes ["MemberName"].Value;
1419
1420                         // generic methods *end* with '>'
1421                         // it's possible for explicitly implemented generic interfaces to
1422                         // contain <...> without being a generic method
1423                         if ((!xMemberName.EndsWith (">") || !yMemberName.EndsWith (">")) &&
1424                                         (r = xMemberName.CompareTo (yMemberName)) != 0)
1425                                 return r;
1426
1427                         int lt;
1428                         if ((lt = xMemberName.IndexOf ("<")) >= 0)
1429                                 xMemberName = xMemberName.Substring (0, lt);
1430                         if ((lt = yMemberName.IndexOf ("<")) >= 0)
1431                                 yMemberName = yMemberName.Substring (0, lt);
1432                         if ((r = xMemberName.CompareTo (yMemberName)) != 0)
1433                                 return r;
1434
1435                         // if @MemberName matches, then it's either two different types of
1436                         // members sharing the same name, e.g. field & property, or it's an
1437                         // overloaded method.
1438                         // for different type, sort based on MemberType value.
1439                         r = x.SelectSingleNode ("MemberType").InnerText.CompareTo (
1440                                         y.SelectSingleNode ("MemberType").InnerText);
1441                         if (r != 0)
1442                                 return r;
1443
1444                         // same type -- must be an overloaded method.  Sort based on type 
1445                         // parameter count, then parameter count, then by the parameter 
1446                         // type names.
1447                         XmlNodeList xTypeParams = x.SelectNodes ("TypeParameters/TypeParameter");
1448                         XmlNodeList yTypeParams = y.SelectNodes ("TypeParameters/TypeParameter");
1449                         if (xTypeParams.Count != yTypeParams.Count)
1450                                 return xTypeParams.Count <= yTypeParams.Count ? -1 : 1;
1451                         for (int i = 0; i < xTypeParams.Count; ++i) {
1452                                 r = xTypeParams [i].Attributes ["Name"].Value.CompareTo (
1453                                                 yTypeParams [i].Attributes ["Name"].Value);
1454                                 if (r != 0)
1455                                         return r;
1456                         }
1457
1458                         XmlNodeList xParams = x.SelectNodes ("Parameters/Parameter");
1459                         XmlNodeList yParams = y.SelectNodes ("Parameters/Parameter");
1460                         if (xParams.Count != yParams.Count)
1461                                 return xParams.Count <= yParams.Count ? -1 : 1;
1462                         for (int i = 0; i < xParams.Count; ++i) {
1463                                 r = xParams [i].Attributes ["Type"].Value.CompareTo (
1464                                                 yParams [i].Attributes ["Type"].Value);
1465                                 if (r != 0)
1466                                         return r;
1467                         }
1468                         // all parameters match, but return value might not match if it was
1469                         // changed between one version and another.
1470                         XmlNode xReturn = x.SelectSingleNode ("ReturnValue/ReturnType");
1471                         XmlNode yReturn = y.SelectSingleNode ("ReturnValue/ReturnType");
1472                         if (xReturn != null && yReturn != null) {
1473                                 r = xReturn.InnerText.CompareTo (yReturn.InnerText);
1474                                 if (r != 0)
1475                                         return r;
1476                         }
1477
1478                         return 0;
1479                 }
1480         }
1481
1482         static readonly MemberComparer DefaultMemberComparer = new MemberComparer ();
1483
1484         private static void SortTypeMembers (XmlNode members)
1485         {
1486                 if (members == null)
1487                         return;
1488                 SortXmlNodes (members, members.SelectNodes ("Member"), DefaultMemberComparer);
1489         }
1490         
1491         private static bool MemberDocsHaveUserContent (XmlNode e)
1492         {
1493                 e = (XmlElement)e.SelectSingleNode("Docs");
1494                 if (e == null) return false;
1495                 foreach (XmlElement d in e.SelectNodes("*"))
1496                         if (d.InnerText != "" && !d.InnerText.StartsWith("To be added"))
1497                                 return true;
1498                 return false;
1499         }
1500         
1501         // UPDATE HELPER FUNCTIONS
1502         
1503         // CREATE A STUB DOCUMENTATION FILE     
1504
1505         public XmlElement StubType (TypeDefinition type, string output)
1506         {
1507                 string typesig = typeFormatters [0].GetDeclaration (type);
1508                 if (typesig == null) return null; // not publicly visible
1509                 
1510                 XmlDocument doc = new XmlDocument();
1511                 XmlElement root = doc.CreateElement("Type");
1512                 doc.AppendChild (root);
1513
1514                 DoUpdateType2 ("New Type", doc, type, output, true);
1515                 
1516                 return root;
1517         }
1518
1519         private XmlElement CreateSinceNode (XmlDocument doc)
1520         {
1521                 XmlElement s = doc.CreateElement ("since");
1522                 s.SetAttribute ("version", since);
1523                 return s;
1524         }
1525         
1526         // STUBBING/UPDATING FUNCTIONS
1527         
1528         public void UpdateType (XmlElement root, TypeDefinition type)
1529         {
1530                 root.SetAttribute("Name", GetDocTypeName (type));
1531                 root.SetAttribute("FullName", GetDocTypeFullName (type));
1532
1533                 foreach (MemberFormatter f in typeFormatters) {
1534                         string element = "TypeSignature[@Language='" + f.Language + "']";
1535                         string valueToUse = f.GetDeclaration (type);
1536
1537                         AddXmlNode (
1538                                 root.SelectNodes (element).Cast<XmlElement> ().ToArray (), 
1539                                 x => x.GetAttribute ("Value") == valueToUse, 
1540                                 x => x.SetAttribute ("Value", valueToUse), 
1541                                 () => {
1542                                         var node = WriteElementAttribute (root, element, "Language", f.Language, forceNewElement: true);
1543                                         var newnode = WriteElementAttribute (root, node, "Value", valueToUse);
1544                                         return newnode;
1545                                 },
1546                                 type);
1547                 }
1548                 
1549                 AddAssemblyNameToNode (root, type);
1550
1551                 string assemblyInfoNodeFilter = MDocUpdater.HasDroppedNamespace (type) ? "[@apistyle='unified']" : "[not(@apistyle) or @apistyle='classic']";
1552                 Func<XmlElement, bool> assemblyFilter = x => x.SelectSingleNode ("AssemblyName").InnerText == type.Module.Assembly.Name.Name;
1553                 foreach(var ass in root.SelectNodes ("AssemblyInfo" + assemblyInfoNodeFilter).Cast<XmlElement> ().Where (assemblyFilter))
1554                 {
1555                         WriteElementText(ass, "AssemblyName", type.Module.Assembly.Name.Name);
1556                         if (!no_assembly_versions) {
1557                                 UpdateAssemblyVersions (ass, type, true);
1558                         }
1559                         else {
1560                                 var versions = ass.SelectNodes ("AssemblyVersion").Cast<XmlNode> ().ToList ();
1561                                 foreach (var version in versions)
1562                                         ass.RemoveChild (version);
1563                         }
1564                         if (!string.IsNullOrEmpty (type.Module.Assembly.Name.Culture))
1565                                 WriteElementText(ass, "AssemblyCulture", type.Module.Assembly.Name.Culture);
1566                         else
1567                                 ClearElement(ass, "AssemblyCulture");
1568
1569
1570                         // Why-oh-why do we put assembly attributes in each type file?
1571                         // Neither monodoc nor monodocs2html use them, so I'm deleting them
1572                         // since they're outdated in current docs, and a waste of space.
1573                         //MakeAttributes(ass, type.Assembly, true);
1574                         XmlNode assattrs = ass.SelectSingleNode("Attributes");
1575                         if (assattrs != null)
1576                                 ass.RemoveChild(assattrs);
1577
1578                         NormalizeWhitespace(ass);
1579                 }
1580                 
1581                 if (type.IsGenericType ()) {
1582                                 MakeTypeParameters (root, type.GenericParameters, type, MDocUpdater.HasDroppedNamespace(type));
1583                 } else {
1584                         ClearElement(root, "TypeParameters");
1585                 }
1586                 
1587                 if (type.BaseType != null) {
1588                         XmlElement basenode = WriteElement(root, "Base");
1589                         
1590                         string basetypename = GetDocTypeFullName (type.BaseType);
1591                         if (basetypename == "System.MulticastDelegate") basetypename = "System.Delegate";
1592                         WriteElementText(root, "Base/BaseTypeName", basetypename);
1593                         
1594                         // Document how this type instantiates the generic parameters of its base type
1595                         TypeReference origBase = type.BaseType.GetElementType ();
1596                         if (origBase.IsGenericType ()) {
1597                                 ClearElement(basenode, "BaseTypeArguments");
1598                                 GenericInstanceType baseInst             = type.BaseType as GenericInstanceType;
1599                                 IList<TypeReference> baseGenArgs    = baseInst == null ? null : baseInst.GenericArguments;
1600                                 IList<GenericParameter> baseGenParams = origBase.GenericParameters;
1601                                 if (baseGenArgs.Count != baseGenParams.Count)
1602                                         throw new InvalidOperationException ("internal error: number of generic arguments doesn't match number of generic parameters.");
1603                                 for (int i = 0; baseGenArgs != null && i < baseGenArgs.Count; i++) {
1604                                         GenericParameter param = baseGenParams [i];
1605                                         TypeReference    value = baseGenArgs [i];
1606
1607                                         XmlElement bta = WriteElement(basenode, "BaseTypeArguments");
1608                                         XmlElement arg = bta.OwnerDocument.CreateElement("BaseTypeArgument");
1609                                         bta.AppendChild(arg);
1610                                         arg.SetAttribute ("TypeParamName", param.Name);
1611                                         arg.InnerText = GetDocTypeFullName (value);
1612                                 }
1613                         }
1614                 } else {
1615                         ClearElement(root, "Base");
1616                 }
1617
1618                 if (!DocUtils.IsDelegate (type) && !type.IsEnum) {
1619                         IEnumerable<TypeReference> userInterfaces = DocUtils.GetUserImplementedInterfaces (type);
1620                         List<string> interface_names = userInterfaces
1621                                         .Select (iface => GetDocTypeFullName (iface))
1622                                         .OrderBy (s => s)
1623                                         .ToList ();
1624
1625                         XmlElement interfaces = WriteElement(root, "Interfaces");
1626                         interfaces.RemoveAll();
1627                         foreach (string iname in interface_names) {
1628                                 XmlElement iface = root.OwnerDocument.CreateElement("Interface");
1629                                 interfaces.AppendChild(iface);
1630                                 WriteElementText(iface, "InterfaceName", iname);
1631                         }
1632                 } else {
1633                         ClearElement(root, "Interfaces");
1634                 }
1635
1636                         MakeAttributes (root, GetCustomAttributes (type), type);
1637                 
1638                 if (DocUtils.IsDelegate (type)) {
1639                         MakeTypeParameters (root, type.GenericParameters, type, MDocUpdater.HasDroppedNamespace(type));
1640                         var member = type.GetMethod ("Invoke");
1641                         MakeParameters(root, member, member.Parameters);
1642                         MakeReturnValue(root, member);
1643                 }
1644                 
1645                 DocsNodeInfo typeInfo = new DocsNodeInfo (WriteElement(root, "Docs"), type);
1646                 MakeDocNode (typeInfo);
1647                 
1648                 if (!DocUtils.IsDelegate (type))
1649                         WriteElement (root, "Members");
1650
1651                 OrderTypeNodes (root, root.ChildNodes);
1652                 NormalizeWhitespace(root);
1653         }
1654
1655         /// <summary>Adds an AssemblyInfo with AssemblyName node to an XmlElement.</summary>
1656         /// <returns>The assembly that was either added, or was already present</returns>
1657         static XmlElement AddAssemblyNameToNode (XmlElement root, TypeDefinition type)
1658         {
1659                 return AddAssemblyNameToNode (root, type.Module);
1660         }
1661
1662         /// <summary>Adds an AssemblyInfo with AssemblyName node to an XmlElement.</summary>
1663         /// <returns>The assembly that was either added, or was already present</returns>
1664         static XmlElement AddAssemblyNameToNode (XmlElement root, ModuleDefinition module)
1665         {
1666                 Func<XmlElement, bool> assemblyFilter = x => x.SelectSingleNode ("AssemblyName").InnerText == module.Assembly.Name.Name;
1667                 return AddAssemblyXmlNode (
1668                         root.SelectNodes ("AssemblyInfo").Cast<XmlElement> ().ToArray (), 
1669                         assemblyFilter, x => WriteElementText (x, "AssemblyName", module.Assembly.Name.Name), 
1670                         () =>  {
1671                                 XmlElement ass = WriteElement (root, "AssemblyInfo", forceNewElement: true);
1672                                 if (MDocUpdater.HasDroppedNamespace (module))
1673                                         ass.SetAttribute ("apistyle", "unified");
1674                                 return ass;
1675                         }, module);
1676         }
1677
1678         static readonly string[] TypeNodeOrder = {
1679                 "TypeSignature",
1680                 "MemberOfLibrary",
1681                 "AssemblyInfo",
1682                 "ThreadingSafetyStatement",
1683                 "ThreadSafetyStatement",
1684                 "TypeParameters",
1685                 "Base",
1686                 "Interfaces",
1687                 "Attributes",
1688                 "Parameters",
1689                 "ReturnValue",
1690                 "Docs",
1691                 "Members",
1692                 "TypeExcluded",
1693         };
1694
1695         static void OrderTypeNodes (XmlNode member, XmlNodeList children)
1696         {
1697                 ReorderNodes (member, children, TypeNodeOrder);
1698         }
1699
1700         internal static IEnumerable<T> Sort<T> (IEnumerable<T> list)
1701         {
1702                 List<T> l = new List<T> (list);
1703                 l.Sort ();
1704                 return l;
1705         }
1706
1707         private void UpdateMember (DocsNodeInfo info)
1708         {
1709                 XmlElement me = (XmlElement) info.Node;
1710                 MemberReference mi = info.Member;
1711
1712                 foreach (MemberFormatter f in memberFormatters) {
1713                         string element = "MemberSignature[@Language='" + f.Language + "']";
1714
1715                         var valueToUse = f.GetDeclaration (mi);
1716
1717                         AddXmlNode (
1718                                 me.SelectNodes (element).Cast<XmlElement> ().ToArray(), 
1719                                 x => x.GetAttribute("Value") == valueToUse, 
1720                                 x => x.SetAttribute ("Value", valueToUse), 
1721                                 () => {
1722                                         var node = WriteElementAttribute (me, element, "Language", f.Language, forceNewElement:true);
1723                                         var newNode = WriteElementAttribute (me, node, "Value", valueToUse);
1724                                         return newNode;
1725                                 },
1726                                 mi);
1727
1728                 }
1729
1730                 WriteElementText(me, "MemberType", GetMemberType(mi));
1731
1732                 if (!no_assembly_versions) {
1733                         if (!multiassembly)
1734                                 UpdateAssemblyVersions (me, mi, true);
1735                         else {
1736                                 var node = AddAssemblyNameToNode (me, mi.Module);
1737
1738                                 UpdateAssemblyVersionForAssemblyInfo (node, me, new[] { GetAssemblyVersion (mi.Module.Assembly) }, add: true);
1739                         }
1740                 }
1741                 else {
1742                         ClearElement (me, "AssemblyInfo");
1743                 }
1744
1745                 MakeAttributes (me, GetCustomAttributes (mi), mi.DeclaringType);
1746
1747                 MakeReturnValue(me, mi, MDocUpdater.HasDroppedNamespace(mi));
1748                 if (mi is MethodReference) {
1749                         MethodReference mb = (MethodReference) mi;
1750                         if (mb.IsGenericMethod ())
1751                                         MakeTypeParameters (me, mb.GenericParameters, mi, MDocUpdater.HasDroppedNamespace(mi));
1752                 }
1753                 MakeParameters(me, mi, MDocUpdater.HasDroppedNamespace(mi));
1754                 
1755                 string fieldValue;
1756                 if (mi is FieldDefinition && GetFieldConstValue ((FieldDefinition)mi, out fieldValue))
1757                         WriteElementText(me, "MemberValue", fieldValue);
1758                 
1759                 info.Node = WriteElement (me, "Docs");
1760                 MakeDocNode (info);
1761                 OrderMemberNodes (me, me.ChildNodes);
1762                 UpdateExtensionMethods (me, info);
1763         }
1764
1765         static void AddXmlNode (XmlElement[] relevant, Func<XmlElement, bool> valueMatches, Action<XmlElement> setValue, Func<XmlElement> makeNewNode, MemberReference member) {
1766                 AddXmlNode (relevant, valueMatches, setValue, makeNewNode, member.Module);
1767         }
1768
1769         static void AddXmlNode (XmlElement[] relevant, Func<XmlElement, bool> valueMatches, Action<XmlElement> setValue, Func<XmlElement> makeNewNode, TypeDefinition type) {
1770                 AddXmlNode (relevant, valueMatches, setValue, makeNewNode, type.Module);
1771         }
1772
1773         static XmlElement AddAssemblyXmlNode (XmlElement[] relevant, Func<XmlElement, bool> valueMatches, Action<XmlElement> setValue, Func<XmlElement> makeNewNode, ModuleDefinition module)
1774         {
1775                 bool isUnified = MDocUpdater.HasDroppedNamespace (module);
1776                 XmlElement thisAssemblyNode = relevant.FirstOrDefault (valueMatches);
1777                 if (thisAssemblyNode == null) {
1778                         thisAssemblyNode = makeNewNode ();
1779                         setValue (thisAssemblyNode);
1780                 }
1781
1782                 if (isUnified) {
1783                         thisAssemblyNode.AddApiStyle (ApiStyle.Unified);
1784
1785                         foreach (var otherNodes in relevant.Where (n => n != thisAssemblyNode && n.DoesNotHaveApiStyle (ApiStyle.Unified))) {
1786                                 otherNodes.AddApiStyle (ApiStyle.Classic);
1787                         }
1788                 }
1789                 return thisAssemblyNode;
1790         }
1791
1792         /// <summary>Adds an xml node, reusing the node if it's available</summary>
1793         /// <param name="relevant">The existing set of nodes</param>
1794         /// <param name="valueMatches">Checks to see if the node's value matches what you're trying to write.</param>
1795         /// <param name="setValue">Sets the node's value</param>
1796         /// <param name="makeNewNode">Creates a new node, if valueMatches returns false.</param>
1797         static void AddXmlNode (XmlElement[] relevant, Func<XmlElement, bool> valueMatches, Action<XmlElement> setValue, Func<XmlElement> makeNewNode, ModuleDefinition module)
1798         {
1799                 bool shouldDuplicate = MDocUpdater.HasDroppedNamespace (module);
1800                 var styleToUse = shouldDuplicate ? ApiStyle.Unified : ApiStyle.Classic;
1801                 var existing = relevant;
1802                 bool done = false;
1803                 bool addedOldApiStyle = false;
1804
1805                 if (shouldDuplicate) {
1806                         existing = existing.Where (n => n.HasApiStyle (styleToUse)).ToArray ();
1807                         foreach (var n in relevant.Where (n => n.DoesNotHaveApiStyle (styleToUse))) {
1808                                 if (valueMatches (n)) {
1809                                         done = true;
1810                                 }
1811                                 else {
1812                                         n.AddApiStyle (ApiStyle.Classic);
1813                                         addedOldApiStyle = true;
1814                                 }
1815                         }
1816                 }
1817                 if (!done) {
1818                         if (!existing.Any ()) {
1819                                 var newNode = makeNewNode ();
1820                                 if (shouldDuplicate && addedOldApiStyle) {
1821                                         newNode.AddApiStyle (ApiStyle.Unified);
1822                                 }
1823                         }
1824                         else {
1825                                 var itemToReuse = existing.First ();
1826                                 setValue (itemToReuse);
1827                                 
1828                                 if (shouldDuplicate && addedOldApiStyle) {
1829                                         itemToReuse.AddApiStyle (styleToUse);
1830                                 }
1831                         }
1832                 }
1833         }
1834
1835         static readonly string[] MemberNodeOrder = {
1836                 "MemberSignature",
1837                 "MemberType",
1838                 "AssemblyInfo",
1839                 "Attributes",
1840                 "ReturnValue",
1841                 "TypeParameters",
1842                 "Parameters",
1843                 "MemberValue",
1844                 "Docs",
1845                 "Excluded",
1846                 "ExcludedLibrary",
1847                 "Link",
1848         };
1849
1850         static void OrderMemberNodes (XmlNode member, XmlNodeList children)
1851         {
1852                 ReorderNodes (member, children, MemberNodeOrder);
1853         }
1854
1855         static void ReorderNodes (XmlNode node, XmlNodeList children, string[] ordering)
1856         {
1857                 MyXmlNodeList newChildren = new MyXmlNodeList (children.Count);
1858                 for (int i = 0; i < ordering.Length; ++i) {
1859                         for (int j = 0; j < children.Count; ++j) {
1860                                 XmlNode c = children [j];
1861                                 if (c.Name == ordering [i]) {
1862                                         newChildren.Add (c);
1863                                 }
1864                         }
1865                 }
1866                 if (newChildren.Count >= 0)
1867                         node.PrependChild ((XmlNode) newChildren [0]);
1868                 for (int i = 1; i < newChildren.Count; ++i) {
1869                         XmlNode prev = (XmlNode) newChildren [i-1];
1870                         XmlNode cur  = (XmlNode) newChildren [i];
1871                         node.RemoveChild (cur);
1872                         node.InsertAfter (cur, prev);
1873                 }
1874         }
1875
1876         IEnumerable<string> GetCustomAttributes (MemberReference mi)
1877         {
1878                 IEnumerable<string> attrs = Enumerable.Empty<string>();
1879
1880                 ICustomAttributeProvider p = mi as ICustomAttributeProvider;
1881                 if (p != null)
1882                         attrs = attrs.Concat (GetCustomAttributes (p.CustomAttributes, ""));
1883
1884                 PropertyDefinition pd = mi as PropertyDefinition;
1885                 if (pd != null) {
1886                         if (pd.GetMethod != null)
1887                                 attrs = attrs.Concat (GetCustomAttributes (pd.GetMethod.CustomAttributes, "get: "));
1888                         if (pd.SetMethod != null)
1889                                 attrs = attrs.Concat (GetCustomAttributes (pd.SetMethod.CustomAttributes, "set: "));
1890                 }
1891
1892                 EventDefinition ed = mi as EventDefinition;
1893                 if (ed != null) {
1894                         if (ed.AddMethod != null)
1895                                 attrs = attrs.Concat (GetCustomAttributes (ed.AddMethod.CustomAttributes, "add: "));
1896                         if (ed.RemoveMethod != null)
1897                                 attrs = attrs.Concat (GetCustomAttributes (ed.RemoveMethod.CustomAttributes, "remove: "));
1898                 }
1899
1900                 return attrs;
1901         }
1902
1903         IEnumerable<string> GetCustomAttributes (IList<CustomAttribute> attributes, string prefix)
1904         {
1905                 foreach (CustomAttribute attribute in attributes.OrderBy (ca => ca.AttributeType.FullName)) {
1906
1907                         TypeDefinition attrType = attribute.AttributeType as TypeDefinition;
1908                         if (attrType != null && !IsPublic (attrType))
1909                                 continue;
1910                         if (slashdocFormatter.GetName (attribute.AttributeType) == null)
1911                                 continue;
1912                         
1913                         if (Array.IndexOf (IgnorableAttributes, attribute.AttributeType.FullName) >= 0)
1914                                 continue;
1915                         
1916                         StringList fields = new StringList ();
1917
1918                         for (int i = 0; i < attribute.ConstructorArguments.Count; ++i) {
1919                                 CustomAttributeArgument argument = attribute.ConstructorArguments [i];
1920                                 fields.Add (MakeAttributesValueString (
1921                                                 argument.Value,
1922                                                 argument.Type));
1923                         }
1924                         var namedArgs =
1925                                 (from namedArg in attribute.Fields
1926                                  select new { Type=namedArg.Argument.Type, Name=namedArg.Name, Value=namedArg.Argument.Value })
1927                                 .Concat (
1928                                                 (from namedArg in attribute.Properties
1929                                                  select new { Type=namedArg.Argument.Type, Name=namedArg.Name, Value=namedArg.Argument.Value }))
1930                                 .OrderBy (v => v.Name);
1931                         foreach (var d in namedArgs)
1932                                 fields.Add (string.Format ("{0}={1}", d.Name, 
1933                                                 MakeAttributesValueString (d.Value, d.Type)));
1934
1935                         string a2 = String.Join(", ", fields.ToArray ());
1936                         if (a2 != "") a2 = "(" + a2 + ")";
1937
1938                         string name = attribute.GetDeclaringType();
1939                         if (name.EndsWith("Attribute")) name = name.Substring(0, name.Length-"Attribute".Length);
1940                         yield return prefix + name + a2;
1941                 }
1942         }
1943
1944         static readonly string[] ValidExtensionMembers = {
1945                 "Docs",
1946                 "MemberSignature",
1947                 "MemberType",
1948                 "Parameters",
1949                 "ReturnValue",
1950                 "TypeParameters",
1951         };
1952
1953         static readonly string[] ValidExtensionDocMembers = {
1954                 "param",
1955                 "summary",
1956                 "typeparam",
1957         };
1958
1959         private void UpdateExtensionMethods (XmlElement e, DocsNodeInfo info)
1960         {
1961                 MethodDefinition me = info.Member as MethodDefinition;
1962                 if (me == null)
1963                         return;
1964                 if (info.Parameters.Count < 1)
1965                         return;
1966                 if (!DocUtils.IsExtensionMethod (me))
1967                         return;
1968
1969                 XmlNode em = e.OwnerDocument.CreateElement ("ExtensionMethod");
1970                 XmlNode member = e.CloneNode (true);
1971                 em.AppendChild (member);
1972                 RemoveExcept (member, ValidExtensionMembers);
1973                 RemoveExcept (member.SelectSingleNode ("Docs"), ValidExtensionDocMembers);
1974                 WriteElementText (member, "MemberType", "ExtensionMethod");
1975                 XmlElement link = member.OwnerDocument.CreateElement ("Link");
1976                 link.SetAttribute ("Type", slashdocFormatter.GetName (me.DeclaringType));
1977                 link.SetAttribute ("Member", slashdocFormatter.GetDeclaration (me));
1978                 member.AppendChild (link);
1979                 AddTargets (em, info);
1980
1981                 extensionMethods.Add (em);
1982         }
1983
1984         private static void RemoveExcept (XmlNode node, string[] except)
1985         {
1986                 if (node == null)
1987                         return;
1988                 MyXmlNodeList remove = null;
1989                 foreach (XmlNode n in node.ChildNodes) {
1990                         if (Array.BinarySearch (except, n.Name) < 0) {
1991                                 if (remove == null)
1992                                         remove = new MyXmlNodeList ();
1993                                 remove.Add (n);
1994                         }
1995                 }
1996                 if (remove != null)
1997                         foreach (XmlNode n in remove)
1998                                 node.RemoveChild (n);
1999         }
2000
2001         private static void AddTargets (XmlNode member, DocsNodeInfo info)
2002         {
2003                 XmlElement targets = member.OwnerDocument.CreateElement ("Targets");
2004                 member.PrependChild (targets);
2005                 if (!(info.Parameters [0].ParameterType is GenericParameter)) {
2006                         AppendElementAttributeText (targets, "Target", "Type",
2007                                 slashdocFormatter.GetDeclaration (info.Parameters [0].ParameterType));
2008                 }
2009                 else {
2010                         GenericParameter gp = (GenericParameter) info.Parameters [0].ParameterType;
2011                         IList<TypeReference> constraints = gp.Constraints;
2012                         if (constraints.Count == 0)
2013                                 AppendElementAttributeText (targets, "Target", "Type", "System.Object");
2014                         else
2015                                 foreach (TypeReference c in constraints)
2016                                         AppendElementAttributeText(targets, "Target", "Type",
2017                                                 slashdocFormatter.GetDeclaration (c));
2018                 }
2019         }
2020         
2021         private static bool GetFieldConstValue (FieldDefinition field, out string value)
2022         {
2023                 value = null;
2024                 TypeDefinition type = field.DeclaringType.Resolve ();
2025                 if (type != null && type.IsEnum) return false;
2026                 
2027                 if (type != null && type.IsGenericType ()) return false;
2028                 if (!field.HasConstant)
2029                         return false;
2030                 if (field.IsLiteral) {
2031                         object val = field.Constant;
2032                         if (val == null) value = "null";
2033                         else if (val is Enum) value = val.ToString();
2034                         else if (val is IFormattable) {
2035                                 value = ((IFormattable)val).ToString();
2036                                 if (val is string)
2037                                         value = "\"" + value + "\"";
2038                         }
2039                         if (value != null && value != "")
2040                                 return true;
2041                 }
2042                 return false;
2043         }
2044         
2045         // XML HELPER FUNCTIONS
2046         
2047         internal static XmlElement WriteElement(XmlNode parent, string element, bool forceNewElement = false) {
2048                 XmlElement ret = (XmlElement)parent.SelectSingleNode(element);
2049                 if (ret == null || forceNewElement) {
2050                         string[] path = element.Split('/');
2051                         foreach (string p in path) {
2052                                 ret = (XmlElement)parent.SelectSingleNode(p);
2053                                 if (ret == null || forceNewElement) {
2054                                         string ename = p;
2055                                         if (ename.IndexOf('[') >= 0) // strip off XPath predicate
2056                                                 ename = ename.Substring(0, ename.IndexOf('['));
2057                                         ret = parent.OwnerDocument.CreateElement(ename);
2058                                         parent.AppendChild(ret);
2059                                         parent = ret;
2060                                 } else {
2061                                         parent = ret;
2062                                 }
2063                         }
2064                 }
2065                 return ret;
2066         }
2067         private static XmlElement WriteElementText(XmlNode parent, string element, string value, bool forceNewElement = false) {
2068                 XmlElement node = WriteElement(parent, element, forceNewElement: forceNewElement);
2069                 node.InnerText = value;
2070                 return node;
2071         }
2072
2073         static XmlElement AppendElementText (XmlNode parent, string element, string value)
2074         {
2075                 XmlElement n = parent.OwnerDocument.CreateElement (element);
2076                 parent.AppendChild (n);
2077                 n.InnerText = value;
2078                 return n;
2079         }
2080
2081         static XmlElement AppendElementAttributeText (XmlNode parent, string element, string attribute, string value)
2082         {
2083                 XmlElement n = parent.OwnerDocument.CreateElement (element);
2084                 parent.AppendChild (n);
2085                 n.SetAttribute (attribute, value);
2086                 return n;
2087         }
2088
2089         internal static XmlNode CopyNode (XmlNode source, XmlNode dest)
2090         {
2091                 XmlNode copy = dest.OwnerDocument.ImportNode (source, true);
2092                 dest.AppendChild (copy);
2093                 return copy;
2094         }
2095
2096         private static void WriteElementInitialText(XmlElement parent, string element, string value) {
2097                 XmlElement node = (XmlElement)parent.SelectSingleNode(element);
2098                 if (node != null)
2099                         return;
2100                 node = WriteElement(parent, element);
2101                 node.InnerText = value;
2102         }
2103         private static XmlElement WriteElementAttribute(XmlElement parent, string element, string attribute, string value, bool forceNewElement = false) {
2104                 XmlElement node = WriteElement(parent, element, forceNewElement:forceNewElement);
2105                 return WriteElementAttribute (parent, node, attribute, value);
2106         }
2107         private static XmlElement WriteElementAttribute(XmlElement parent, XmlElement node, string attribute, string value) {
2108                 if (node.GetAttribute (attribute) != value) {
2109                         node.SetAttribute (attribute, value);
2110                 }
2111                 return node;
2112         }
2113         internal static void ClearElement(XmlElement parent, string name) {
2114                 XmlElement node = (XmlElement)parent.SelectSingleNode(name);
2115                 if (node != null)
2116                         parent.RemoveChild(node);
2117         }
2118         
2119         // DOCUMENTATION HELPER FUNCTIONS
2120         
2121         private void MakeDocNode (DocsNodeInfo info)
2122         {
2123                 List<GenericParameter> genericParams      = info.GenericParameters;
2124                 IList<ParameterDefinition> parameters  = info.Parameters;
2125                 TypeReference returntype                  = info.ReturnType;
2126                 bool returnisreturn         = info.ReturnIsReturn;
2127                 XmlElement e                = info.Node;
2128                 bool addremarks             = info.AddRemarks;
2129
2130                 WriteElementInitialText(e, "summary", "To be added.");
2131                 
2132                 if (parameters != null) {
2133                         string[] values = new string [parameters.Count];
2134                         for (int i = 0; i < values.Length; ++i)
2135                                 values [i] = parameters [i].Name;
2136                         UpdateParameters (e, "param", values);
2137                 }
2138
2139                 if (genericParams != null) {
2140                         string[] values = new string [genericParams.Count];
2141                         for (int i = 0; i < values.Length; ++i)
2142                                 values [i] = genericParams [i].Name;
2143                         UpdateParameters (e, "typeparam", values);
2144                 }
2145
2146                 string retnodename = null;
2147                 if (returntype != null && returntype.FullName != "System.Void") { // FIXME
2148                         retnodename = returnisreturn ? "returns" : "value";
2149                         string retnodename_other = !returnisreturn ? "returns" : "value";
2150                         
2151                         // If it has a returns node instead of a value node, change its name.
2152                         XmlElement retother = (XmlElement)e.SelectSingleNode(retnodename_other);
2153                         if (retother != null) {
2154                                 XmlElement retnode = e.OwnerDocument.CreateElement(retnodename);
2155                                 foreach (XmlNode node in retother)
2156                                         retnode.AppendChild(node.CloneNode(true));
2157                                 e.ReplaceChild(retnode, retother);
2158                         } else {
2159                                 WriteElementInitialText(e, retnodename, "To be added.");
2160                         }
2161                 } else {
2162                         ClearElement(e, "returns");
2163                         ClearElement(e, "value");
2164                 }
2165
2166                 if (addremarks)
2167                         WriteElementInitialText(e, "remarks", "To be added.");
2168
2169                 if (exceptions.HasValue && info.Member != null &&
2170                                 (exceptions.Value & ExceptionLocations.AddedMembers) == 0) {
2171                         UpdateExceptions (e, info.Member);
2172                 }
2173
2174                 foreach (DocumentationImporter importer in importers)
2175                         importer.ImportDocumentation (info);
2176                 
2177                 OrderDocsNodes (e, e.ChildNodes);
2178                 NormalizeWhitespace(e);
2179         }
2180
2181         static readonly string[] DocsNodeOrder = {
2182                 "typeparam", "param", "summary", "returns", "value", "remarks",
2183         };
2184
2185         private static void OrderDocsNodes (XmlNode docs, XmlNodeList children)
2186         {
2187                 ReorderNodes (docs, children, DocsNodeOrder);
2188         }
2189         
2190
2191         private void UpdateParameters (XmlElement e, string element, string[] values)
2192         {       
2193                 if (values != null) {
2194                         XmlNode[] paramnodes = new XmlNode[values.Length];
2195                         
2196                         // Some documentation had param nodes with leading spaces.
2197                         foreach (XmlElement paramnode in e.SelectNodes(element)){
2198                                 paramnode.SetAttribute("name", paramnode.GetAttribute("name").Trim());
2199                         }
2200                         
2201                         // If a member has only one parameter, we can track changes to
2202                         // the name of the parameter easily.
2203                         if (values.Length == 1 && e.SelectNodes(element).Count == 1) {
2204                                 UpdateParameterName (e, (XmlElement) e.SelectSingleNode(element), values [0]);
2205                         }
2206
2207                         bool reinsert = false;
2208
2209                         // Pick out existing and still-valid param nodes, and
2210                         // create nodes for parameters not in the file.
2211                         Hashtable seenParams = new Hashtable();
2212                         for (int pi = 0; pi < values.Length; pi++) {
2213                                 string p = values [pi];
2214                                 seenParams[p] = pi;
2215                                 
2216                                 paramnodes[pi] = e.SelectSingleNode(element + "[@name='" + p + "']");
2217                                 if (paramnodes[pi] != null) continue;
2218                                 
2219                                 XmlElement pe = e.OwnerDocument.CreateElement(element);
2220                                 pe.SetAttribute("name", p);
2221                                 pe.InnerText = "To be added.";
2222                                 paramnodes[pi] = pe;
2223                                 reinsert = true;
2224                         }
2225
2226                         // Remove parameters that no longer exist and check all params are in the right order.
2227                         int idx = 0;
2228                         MyXmlNodeList todelete = new MyXmlNodeList ();
2229                         foreach (XmlElement paramnode in e.SelectNodes(element)) {
2230                                 string name = paramnode.GetAttribute("name");
2231                                 if (!seenParams.ContainsKey(name)) {
2232                                         if (!delete && !paramnode.InnerText.StartsWith("To be added")) {
2233                                                 Warning ("The following param node can only be deleted if the --delete option is given: ");
2234                                                 if (e.ParentNode == e.OwnerDocument.DocumentElement) {
2235                                                         // delegate type
2236                                                         Warning ("\tXPath=/Type[@FullName=\"{0}\"]/Docs/param[@name=\"{1}\"]",
2237                                                                         e.OwnerDocument.DocumentElement.GetAttribute ("FullName"),
2238                                                                         name);
2239                                                 }
2240                                                 else {
2241                                                         Warning ("\tXPath=/Type[@FullName=\"{0}\"]//Member[@MemberName=\"{1}\"]/Docs/param[@name=\"{2}\"]",
2242                                                                         e.OwnerDocument.DocumentElement.GetAttribute ("FullName"),
2243                                                                         e.ParentNode.Attributes ["MemberName"].Value, 
2244                                                                         name);
2245                                                 }
2246                                                 Warning ("\tValue={0}", paramnode.OuterXml);
2247                                         } else {
2248                                                 todelete.Add (paramnode);
2249                                         }
2250                                         continue;
2251                                 }
2252
2253                                 if ((int)seenParams[name] != idx)
2254                                         reinsert = true;
2255                                 
2256                                 idx++;
2257                         }
2258
2259                         foreach (XmlNode n in todelete) {
2260                                 n.ParentNode.RemoveChild (n);
2261                         }
2262                         
2263                         // Re-insert the parameter nodes at the top of the doc section.
2264                         if (reinsert)
2265                                 for (int pi = values.Length-1; pi >= 0; pi--)
2266                                         e.PrependChild(paramnodes[pi]);
2267                 } else {
2268                         // Clear all existing param nodes
2269                         foreach (XmlNode paramnode in e.SelectNodes(element)) {
2270                                 if (!delete && !paramnode.InnerText.StartsWith("To be added")) {
2271                                         Console.WriteLine("The following param node can only be deleted if the --delete option is given:");
2272                                         Console.WriteLine(paramnode.OuterXml);
2273                                 } else {
2274                                         paramnode.ParentNode.RemoveChild(paramnode);
2275                                 }
2276                         }
2277                 }
2278         }
2279
2280         private static void UpdateParameterName (XmlElement docs, XmlElement pe, string newName)
2281         {
2282                 string existingName = pe.GetAttribute ("name");
2283                 pe.SetAttribute ("name", newName);
2284                 if (existingName == newName)
2285                         return;
2286                 foreach (XmlElement paramref in docs.SelectNodes (".//paramref"))
2287                         if (paramref.GetAttribute ("name").Trim () == existingName)
2288                                 paramref.SetAttribute ("name", newName);
2289         }
2290
2291         class CrefComparer : XmlNodeComparer {
2292
2293                 public CrefComparer ()
2294                 {
2295                 }
2296
2297                 public override int Compare (XmlNode x, XmlNode y)
2298                 {
2299                         string xType = x.Attributes ["cref"].Value;
2300                         string yType = y.Attributes ["cref"].Value;
2301                         string xNamespace = GetNamespace (xType);
2302                         string yNamespace = GetNamespace (yType);
2303
2304                         int c = xNamespace.CompareTo (yNamespace);
2305                         if (c != 0)
2306                                 return c;
2307                         return xType.CompareTo (yType);
2308                 }
2309
2310                 static string GetNamespace (string type)
2311                 {
2312                         int n = type.LastIndexOf ('.');
2313                         if (n >= 0)
2314                                 return type.Substring (0, n);
2315                         return string.Empty;
2316                 }
2317         }
2318         
2319         private void UpdateExceptions (XmlNode docs, MemberReference member)
2320         {
2321                 string indent = new string (' ', 10);
2322                 foreach (var source in new ExceptionLookup (exceptions.Value)[member]) {
2323                         string cref = slashdocFormatter.GetDeclaration (source.Exception);
2324                         var node = docs.SelectSingleNode ("exception[@cref='" + cref + "']");
2325                         if (node != null)
2326                                 continue;
2327                         XmlElement e = docs.OwnerDocument.CreateElement ("exception");
2328                         e.SetAttribute ("cref", cref);
2329                         e.InnerXml = "To be added; from:\n" + indent + "<see cref=\"" +
2330                                 string.Join ("\" />,\n" + indent + "<see cref=\"",
2331                                                 source.Sources.Select (m => slashdocFormatter.GetDeclaration (m))
2332                                                 .OrderBy (s => s)) +
2333                                 "\" />";
2334                         docs.AppendChild (e);
2335                 }
2336                 SortXmlNodes (docs, docs.SelectNodes ("exception"), 
2337                                 new CrefComparer ());
2338         }
2339
2340         private static void NormalizeWhitespace(XmlElement e) {
2341                 // Remove all text and whitespace nodes from the element so it
2342                 // is outputted with nice indentation and no blank lines.
2343                 ArrayList deleteNodes = new ArrayList();
2344                 foreach (XmlNode n in e)
2345                         if (n is XmlText || n is XmlWhitespace || n is XmlSignificantWhitespace)
2346                                 deleteNodes.Add(n);
2347                 foreach (XmlNode n in deleteNodes)
2348                                 n.ParentNode.RemoveChild(n);
2349         }
2350         
2351         private static bool UpdateAssemblyVersions (XmlElement root, MemberReference member, bool add)
2352         {
2353                 TypeDefinition type = member as TypeDefinition;
2354                 if (type == null)
2355                         type = member.DeclaringType as TypeDefinition;
2356
2357                 var versions = new string[] { GetAssemblyVersion (type.Module.Assembly) };
2358
2359                 if (root.LocalName == "AssemblyInfo")
2360                         return UpdateAssemblyVersionForAssemblyInfo (root, root.ParentNode as XmlElement, versions, add: true);
2361                 else 
2362                         return UpdateAssemblyVersions (root, type.Module.Assembly, versions, add);
2363         }
2364         
2365         private static string GetAssemblyVersion (AssemblyDefinition assembly)
2366         {
2367                 return assembly.Name.Version.ToString();
2368         }
2369         
2370         private static bool UpdateAssemblyVersions(XmlElement root, AssemblyDefinition assembly, string[] assemblyVersions, bool add)
2371         {
2372                 XmlElement av = (XmlElement) root.SelectSingleNode ("AssemblyVersions");
2373                 if (av != null) {
2374                                 // AssemblyVersions is not part of the spec
2375                                 root.RemoveChild (av);
2376                 }
2377
2378                 string oldNodeFilter = "AssemblyInfo[not(@apistyle) or @apistyle='classic']";
2379                 string newNodeFilter = "AssemblyInfo[@apistyle='unified']";
2380                 string thisNodeFilter = MDocUpdater.HasDroppedNamespace (assembly) ? newNodeFilter : oldNodeFilter;
2381                 string thatNodeFilter = MDocUpdater.HasDroppedNamespace (assembly) ? oldNodeFilter : newNodeFilter;
2382
2383                 XmlElement e = (XmlElement) root.SelectSingleNode (thisNodeFilter);
2384                 if (e == null) {
2385                         e = root.OwnerDocument.CreateElement("AssemblyInfo");
2386
2387                         if (MDocUpdater.HasDroppedNamespace (assembly)) {
2388                                 e.SetAttribute ("apistyle", "unified");
2389                         }
2390
2391                         root.AppendChild(e);
2392                 }
2393
2394                 var thatNode = (XmlElement) root.SelectSingleNode (thatNodeFilter);
2395                 if (MDocUpdater.HasDroppedNamespace (assembly) && thatNode != null) {
2396                         // there's a classic node, we should add apistyles
2397                         e.SetAttribute ("apistyle", "unified");
2398                         thatNode.SetAttribute ("apistyle", "classic");
2399                 }
2400
2401                 return UpdateAssemblyVersionForAssemblyInfo (e, root, assemblyVersions, add);
2402         }
2403
2404         static bool UpdateAssemblyVersionForAssemblyInfo (XmlElement e, XmlElement root, string[] assemblyVersions, bool add)
2405         {
2406                 List<XmlNode> matches = e.SelectNodes ("AssemblyVersion").Cast<XmlNode> ().Where (v => Array.IndexOf (assemblyVersions, v.InnerText) >= 0).ToList ();
2407                 // matches.Count > 0 && add: ignore -- already present
2408                 if (matches.Count > 0 && !add) {
2409                         foreach (XmlNode c in matches)
2410                                 e.RemoveChild (c);
2411                 }
2412                 else if (matches.Count == 0 && add) {
2413                         foreach (string sv in assemblyVersions) {
2414                                 XmlElement c = root.OwnerDocument.CreateElement("AssemblyVersion");
2415                                 c.InnerText = sv;
2416                                 e.AppendChild(c);
2417                         }
2418                 }
2419
2420                 // matches.Count == 0 && !add: ignore -- already not present
2421                 XmlNodeList avs = e.SelectNodes ("AssemblyVersion");
2422                 SortXmlNodes (e, avs, new VersionComparer ());
2423
2424                 bool anyNodesLeft = avs.Count != 0;
2425                 if (!anyNodesLeft) {
2426                         e.ParentNode.RemoveChild (e);
2427                 }
2428                 return anyNodesLeft;
2429         }
2430
2431         // FIXME: get TypeReferences instead of string comparison?
2432         private static string[] IgnorableAttributes = {
2433                 // Security related attributes
2434                 "System.Reflection.AssemblyKeyFileAttribute",
2435                 "System.Reflection.AssemblyDelaySignAttribute",
2436                 // Present in @RefType
2437                 "System.Runtime.InteropServices.OutAttribute",
2438                 // For naming the indexer to use when not using indexers
2439                 "System.Reflection.DefaultMemberAttribute",
2440                 // for decimal constants
2441                 "System.Runtime.CompilerServices.DecimalConstantAttribute",
2442                 // compiler generated code
2443                 "System.Runtime.CompilerServices.CompilerGeneratedAttribute",
2444                 // more compiler generated code, e.g. iterator methods
2445                 "System.Diagnostics.DebuggerHiddenAttribute",
2446                 "System.Runtime.CompilerServices.FixedBufferAttribute",
2447                 "System.Runtime.CompilerServices.UnsafeValueTypeAttribute",
2448                 // extension methods
2449                 "System.Runtime.CompilerServices.ExtensionAttribute",
2450                 // Used to differentiate 'object' from C#4 'dynamic'
2451                 "System.Runtime.CompilerServices.DynamicAttribute",
2452         };
2453
2454         private void MakeAttributes (XmlElement root, IEnumerable<string> attributes, TypeReference t=null)
2455         {
2456                 if (!attributes.Any ()) {
2457                         ClearElement (root, "Attributes");
2458                         return;
2459                 }
2460
2461                 XmlElement e = (XmlElement)root.SelectSingleNode("Attributes");
2462                 if (e != null)
2463                         e.RemoveAll();
2464                 else if (e == null)
2465                         e = root.OwnerDocument.CreateElement("Attributes");
2466                 
2467                 foreach (string attribute in attributes) {
2468                         XmlElement ae = root.OwnerDocument.CreateElement("Attribute");
2469                         e.AppendChild(ae);
2470                         
2471                         WriteElementText(ae, "AttributeName", attribute);
2472                 }
2473                 
2474                 if (e.ParentNode == null)
2475                         root.AppendChild(e);
2476
2477                 NormalizeWhitespace(e);
2478         }
2479
2480         public static string MakeAttributesValueString (object v, TypeReference valueType)
2481         {
2482                 var formatters = new [] { 
2483                         new AttributeValueFormatter (), 
2484                         new ApplePlatformEnumFormatter (), 
2485                         new StandardFlagsEnumFormatter (), 
2486                         new DefaultAttributeValueFormatter (),
2487                 };
2488
2489                 ResolvedTypeInfo type = new ResolvedTypeInfo (valueType);
2490                 foreach (var formatter in formatters) {
2491                         string formattedValue;
2492                         if (formatter.TryFormatValue (v, type, out formattedValue)) {
2493                                 return formattedValue;
2494                         }
2495                 }
2496
2497                 // this should never occur because the DefaultAttributeValueFormatter will always
2498                 // successfully format the value ... but this is needed to satisfy the compiler :)
2499                 throw new InvalidDataException (string.Format ("Unable to format attribute value ({0})", v.ToString ()));
2500         }
2501
2502         internal static IDictionary<long, string> GetEnumerationValues (TypeDefinition type)
2503         {
2504                 var values = new Dictionary<long, string> ();
2505                 foreach (var f in 
2506                                 (from f in type.Fields
2507                                  where !(f.IsRuntimeSpecialName || f.IsSpecialName)
2508                                  select f)) {
2509                         values [ToInt64 (f.Constant)] = f.Name;
2510                 }
2511                 return values;
2512         }
2513
2514         internal static long ToInt64 (object value)
2515         {
2516                 if (value is ulong)
2517                         return (long) (ulong) value;
2518                 return Convert.ToInt64 (value);
2519         }
2520         
2521         private void MakeParameters (XmlElement root, MemberReference member, IList<ParameterDefinition> parameters, bool shouldDuplicateWithNew=false)
2522         {
2523                 XmlElement e = WriteElement(root, "Parameters");
2524
2525                 int i = 0;
2526                 foreach (ParameterDefinition p in parameters) {
2527                         XmlElement pe;
2528                         
2529                         // param info
2530                         var ptype = GetDocParameterType (p.ParameterType);
2531                         var newPType = ptype;
2532
2533                         if (MDocUpdater.SwitchingToMagicTypes) {
2534                                 newPType = NativeTypeManager.ConvertFromNativeType (ptype);
2535                         }
2536
2537                         // now find the existing node, if it's there so we can reuse it.
2538                         var nodes = root.SelectSingleNode ("Parameters").SelectNodes ("Parameter")
2539                                 .Cast<XmlElement> ().Where (x => x.GetAttribute ("Name") == p.Name)
2540                                 .ToArray();
2541
2542                         if (nodes.Count () == 0) {
2543                                 // wasn't found, let's make sure it wasn't just cause the param name was changed
2544                                 nodes = root.SelectSingleNode ("Parameters").SelectNodes ("Parameter")
2545                                         .Cast<XmlElement> ()
2546                                         .Skip (i) // this makes sure we don't inadvertently "reuse" nodes when adding new ones
2547                                         .Where (x => x.GetAttribute ("Name") != p.Name && (x.GetAttribute ("Type") == ptype || x.GetAttribute ("Type") == newPType))
2548                                         .Take(1) // there might be more than one that meets this parameter ... only take the first.
2549                                         .ToArray();
2550                         }
2551
2552                         AddXmlNode (nodes, 
2553                                 x => x.GetAttribute ("Type") == ptype,
2554                                 x => x.SetAttribute ("Type", ptype),
2555                                 () => {
2556                                         pe = root.OwnerDocument.CreateElement ("Parameter");
2557                                         e.AppendChild (pe);
2558
2559                                         pe.SetAttribute ("Name", p.Name);
2560                                         pe.SetAttribute ("Type", ptype);
2561                                         if (p.ParameterType is ByReferenceType) {
2562                                                 if (p.IsOut)
2563                                                         pe.SetAttribute ("RefType", "out");
2564                                                 else
2565                                                         pe.SetAttribute ("RefType", "ref");
2566                                         }
2567
2568                                         MakeAttributes (pe, GetCustomAttributes (p.CustomAttributes, ""));
2569                                         return pe;
2570                                 },
2571                                 member);
2572
2573                         i++;
2574                 }
2575         }
2576         
2577         private void MakeTypeParameters (XmlElement root, IList<GenericParameter> typeParams, MemberReference member, bool shouldDuplicateWithNew)
2578         {
2579                 if (typeParams == null || typeParams.Count == 0) {
2580                         XmlElement f = (XmlElement) root.SelectSingleNode ("TypeParameters");
2581                         if (f != null)
2582                                 root.RemoveChild (f);
2583                         return;
2584                 }
2585                 XmlElement e = WriteElement(root, "TypeParameters");
2586
2587                 var nodes = e.SelectNodes ("TypeParameter").Cast<XmlElement> ().ToArray ();
2588
2589                 foreach (GenericParameter t in typeParams) {
2590
2591                                 IList<TypeReference> constraints = t.Constraints;
2592                                 GenericParameterAttributes attrs = t.Attributes;
2593
2594
2595                                 AddXmlNode (
2596                                         nodes,
2597                                         x => { 
2598                                                 var baseType = e.SelectSingleNode("BaseTypeName");
2599                                                 // TODO: should this comparison take into account BaseTypeName?
2600                                                 return x.GetAttribute("Name") == t.Name;
2601                                         },
2602                                         x => {}, // no additional action required
2603                                         () => {
2604
2605                                                 XmlElement pe = root.OwnerDocument.CreateElement("TypeParameter");
2606                                                 e.AppendChild(pe);
2607                                                 pe.SetAttribute("Name", t.Name);
2608                                                         MakeAttributes (pe, GetCustomAttributes (t.CustomAttributes, ""), t.DeclaringType);
2609                                                 XmlElement ce = (XmlElement) e.SelectSingleNode ("Constraints");
2610                                                 if (attrs == GenericParameterAttributes.NonVariant && constraints.Count == 0) {
2611                                                         if (ce != null)
2612                                                                 e.RemoveChild (ce);
2613                                                         return pe;
2614                                                 }
2615                                                 if (ce != null)
2616                                                         ce.RemoveAll();
2617                                                 else {
2618                                                         ce = root.OwnerDocument.CreateElement ("Constraints");
2619                                                 }
2620                                                 pe.AppendChild (ce);
2621                                                 if ((attrs & GenericParameterAttributes.Contravariant) != 0)
2622                                                         AppendElementText (ce, "ParameterAttribute", "Contravariant");
2623                                                 if ((attrs & GenericParameterAttributes.Covariant) != 0)
2624                                                         AppendElementText (ce, "ParameterAttribute", "Covariant");
2625                                                 if ((attrs & GenericParameterAttributes.DefaultConstructorConstraint) != 0)
2626                                                         AppendElementText (ce, "ParameterAttribute", "DefaultConstructorConstraint");
2627                                                 if ((attrs & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0)
2628                                                         AppendElementText (ce, "ParameterAttribute", "NotNullableValueTypeConstraint");
2629                                                 if ((attrs & GenericParameterAttributes.ReferenceTypeConstraint) != 0)
2630                                                         AppendElementText (ce, "ParameterAttribute", "ReferenceTypeConstraint");
2631                                                 foreach (TypeReference c in constraints) {
2632                                                         TypeDefinition cd = c.Resolve ();
2633                                                         AppendElementText (ce,
2634                                                                         (cd != null && cd.IsInterface) ? "InterfaceName" : "BaseTypeName",
2635                                                                         GetDocTypeFullName (c));
2636                                                 }
2637                                         
2638                                                 return pe;
2639                                         },
2640                                 member);
2641                 }
2642         }
2643
2644         private void MakeParameters (XmlElement root, MemberReference mi, bool shouldDuplicateWithNew)
2645         {
2646                 if (mi is MethodDefinition && ((MethodDefinition) mi).IsConstructor)
2647                                 MakeParameters (root, mi, ((MethodDefinition)mi).Parameters, shouldDuplicateWithNew);
2648                 else if (mi is MethodDefinition) {
2649                         MethodDefinition mb = (MethodDefinition) mi;
2650                         IList<ParameterDefinition> parameters = mb.Parameters;
2651                                 MakeParameters(root, mi, parameters, shouldDuplicateWithNew);
2652                         if (parameters.Count > 0 && DocUtils.IsExtensionMethod (mb)) {
2653                                 XmlElement p = (XmlElement) root.SelectSingleNode ("Parameters/Parameter[position()=1]");
2654                                 p.SetAttribute ("RefType", "this");
2655                         }
2656                 }
2657                 else if (mi is PropertyDefinition) {
2658                         IList<ParameterDefinition> parameters = ((PropertyDefinition)mi).Parameters;
2659                         if (parameters.Count > 0)
2660                                         MakeParameters(root, mi, parameters, shouldDuplicateWithNew);
2661                         else
2662                                 return;
2663                 }
2664                 else if (mi is FieldDefinition) return;
2665                 else if (mi is EventDefinition) return;
2666                 else throw new ArgumentException();
2667         }
2668
2669         internal static string GetDocParameterType (TypeReference type)
2670         {
2671                 return GetDocTypeFullName (type).Replace ("@", "&");
2672         }
2673
2674         private void MakeReturnValue (XmlElement root, TypeReference type, IList<CustomAttribute> attributes, bool shouldDuplicateWithNew=false) 
2675                 {
2676                         XmlElement e = WriteElement(root, "ReturnValue");
2677                         var valueToUse = GetDocTypeFullName (type);
2678
2679                         AddXmlNode (e.SelectNodes("ReturnType").Cast<XmlElement> ().ToArray (),
2680                                 x => x.InnerText == valueToUse,
2681                                 x => x.InnerText = valueToUse,
2682                                 () => {
2683                                         var newNode = WriteElementText(e, "ReturnType", valueToUse, forceNewElement: true);
2684                                         if (attributes != null)
2685                                                 MakeAttributes(e, GetCustomAttributes (attributes, ""), type);
2686
2687                                         return newNode;
2688                                 },
2689                         type);
2690         }
2691         
2692         private void MakeReturnValue (XmlElement root, MemberReference mi, bool shouldDuplicateWithNew=false)
2693         {
2694                 if (mi is MethodDefinition && ((MethodDefinition) mi).IsConstructor)
2695                         return;
2696                 else if (mi is MethodDefinition)
2697                         MakeReturnValue (root, ((MethodDefinition)mi).ReturnType, ((MethodDefinition)mi).MethodReturnType.CustomAttributes, shouldDuplicateWithNew);
2698                 else if (mi is PropertyDefinition)
2699                         MakeReturnValue (root, ((PropertyDefinition)mi).PropertyType, null, shouldDuplicateWithNew);
2700                 else if (mi is FieldDefinition)
2701                         MakeReturnValue (root, ((FieldDefinition)mi).FieldType, null, shouldDuplicateWithNew);
2702                 else if (mi is EventDefinition)
2703                         MakeReturnValue (root, ((EventDefinition)mi).EventType, null, shouldDuplicateWithNew);
2704                 else
2705                         throw new ArgumentException(mi + " is a " + mi.GetType().FullName);
2706         }
2707         
2708         private XmlElement MakeMember(XmlDocument doc, DocsNodeInfo info)
2709         {
2710                 MemberReference mi = info.Member;
2711                 if (mi is TypeDefinition) return null;
2712
2713                 string sigs = memberFormatters [0].GetDeclaration (mi);
2714                 if (sigs == null) return null; // not publicly visible
2715                 
2716                 // no documentation for property/event accessors.  Is there a better way of doing this?
2717                 if (mi.Name.StartsWith("get_")) return null;
2718                 if (mi.Name.StartsWith("set_")) return null;
2719                 if (mi.Name.StartsWith("add_")) return null;
2720                 if (mi.Name.StartsWith("remove_")) return null;
2721                 if (mi.Name.StartsWith("raise_")) return null;
2722                 
2723                 XmlElement me = doc.CreateElement("Member");
2724                 me.SetAttribute("MemberName", GetMemberName (mi));
2725
2726                 info.Node = me;
2727                 UpdateMember(info);
2728                 if (exceptions.HasValue && 
2729                                 (exceptions.Value & ExceptionLocations.AddedMembers) != 0)
2730                         UpdateExceptions (info.Node, info.Member);
2731
2732                 if (since != null) {
2733                         XmlNode docs = me.SelectSingleNode("Docs");
2734                         docs.AppendChild (CreateSinceNode (doc));
2735                 }
2736                 
2737                 return me;
2738         }
2739
2740         internal static string GetMemberName (MemberReference mi)
2741         {
2742                 MethodDefinition mb = mi as MethodDefinition;
2743                 if (mb == null) {
2744                         PropertyDefinition pi = mi as PropertyDefinition;
2745                         if (pi == null)
2746                                 return mi.Name;
2747                         return DocUtils.GetPropertyName (pi);
2748                 }
2749                 StringBuilder sb = new StringBuilder (mi.Name.Length);
2750                 if (!DocUtils.IsExplicitlyImplemented (mb))
2751                         sb.Append (mi.Name);
2752                 else {
2753                         TypeReference iface;
2754                         MethodReference ifaceMethod;
2755                         DocUtils.GetInfoForExplicitlyImplementedMethod (mb, out iface, out ifaceMethod);
2756                         sb.Append (GetDocTypeFullName (iface));
2757                         sb.Append ('.');
2758                         sb.Append (ifaceMethod.Name);
2759                 }
2760                 if (mb.IsGenericMethod ()) {
2761                         IList<GenericParameter> typeParams = mb.GenericParameters;
2762                         if (typeParams.Count > 0) {
2763                                 sb.Append ("<");
2764                                 sb.Append (typeParams [0].Name);
2765                                 for (int i = 1; i < typeParams.Count; ++i)
2766                                         sb.Append (",").Append (typeParams [i].Name);
2767                                 sb.Append (">");
2768                         }
2769                 }
2770                 return sb.ToString ();
2771         }
2772         
2773         /// SIGNATURE GENERATION FUNCTIONS
2774         internal static bool IsPrivate (MemberReference mi)
2775         {
2776                 return memberFormatters [0].GetDeclaration (mi) == null;
2777         }
2778
2779         internal static string GetMemberType (MemberReference mi)
2780         {
2781                 if (mi is MethodDefinition && ((MethodDefinition) mi).IsConstructor)
2782                         return "Constructor";
2783                 if (mi is MethodDefinition)
2784                         return "Method";
2785                 if (mi is PropertyDefinition)
2786                         return "Property";
2787                 if (mi is FieldDefinition)
2788                         return "Field";
2789                 if (mi is EventDefinition)
2790                         return "Event";
2791                 throw new ArgumentException();
2792         }
2793
2794         private static string GetDocTypeName (TypeReference type)
2795         {
2796                 return docTypeFormatter.GetName (type);
2797         }
2798
2799         internal static string GetDocTypeFullName (TypeReference type)
2800         {
2801                 return DocTypeFullMemberFormatter.Default.GetName (type);
2802         }
2803
2804         internal static string GetXPathForMember (DocumentationMember member)
2805         {
2806                 StringBuilder xpath = new StringBuilder ();
2807                 xpath.Append ("//Members/Member[@MemberName=\"")
2808                         .Append (member.MemberName)
2809                         .Append ("\"]");
2810                 if (member.Parameters != null && member.Parameters.Count > 0) {
2811                         xpath.Append ("/Parameters[count(Parameter) = ")
2812                                 .Append (member.Parameters.Count);
2813                         for (int i = 0; i < member.Parameters.Count; ++i) {
2814                                 xpath.Append (" and Parameter [").Append (i+1).Append ("]/@Type=\"");
2815                                 xpath.Append (member.Parameters [i]);
2816                                 xpath.Append ("\"");
2817                         }
2818                         xpath.Append ("]/..");
2819                 }
2820                 return xpath.ToString ();
2821         }
2822
2823         public static string GetXPathForMember (XPathNavigator member)
2824         {
2825                 StringBuilder xpath = new StringBuilder ();
2826                 xpath.Append ("//Type[@FullName=\"")
2827                         .Append (member.SelectSingleNode ("../../@FullName").Value)
2828                         .Append ("\"]/");
2829                 xpath.Append ("Members/Member[@MemberName=\"")
2830                         .Append (member.SelectSingleNode ("@MemberName").Value)
2831                         .Append ("\"]");
2832                 XPathNodeIterator parameters = member.Select ("Parameters/Parameter");
2833                 if (parameters.Count > 0) {
2834                         xpath.Append ("/Parameters[count(Parameter) = ")
2835                                 .Append (parameters.Count);
2836                         int i = 0;
2837                         while (parameters.MoveNext ()) {
2838                                 ++i;
2839                                 xpath.Append (" and Parameter [").Append (i).Append ("]/@Type=\"");
2840                                 xpath.Append (parameters.Current.Value);
2841                                 xpath.Append ("\"");
2842                         }
2843                         xpath.Append ("]/..");
2844                 }
2845                 return xpath.ToString ();
2846         }
2847
2848         public static string GetXPathForMember (MemberReference member)
2849         {
2850                 StringBuilder xpath = new StringBuilder ();
2851                 xpath.Append ("//Type[@FullName=\"")
2852                         .Append (member.DeclaringType.FullName)
2853                         .Append ("\"]/");
2854                 xpath.Append ("Members/Member[@MemberName=\"")
2855                         .Append (GetMemberName (member))
2856                         .Append ("\"]");
2857
2858                 IList<ParameterDefinition> parameters = null;
2859                 if (member is MethodDefinition)
2860                         parameters = ((MethodDefinition) member).Parameters;
2861                 else if (member is PropertyDefinition) {
2862                         parameters = ((PropertyDefinition) member).Parameters;
2863                 }
2864                 if (parameters != null && parameters.Count > 0) {
2865                         xpath.Append ("/Parameters[count(Parameter) = ")
2866                                 .Append (parameters.Count);
2867                         for (int i = 0; i < parameters.Count; ++i) {
2868                                 xpath.Append (" and Parameter [").Append (i+1).Append ("]/@Type=\"");
2869                                 xpath.Append (GetDocParameterType (parameters [i].ParameterType));
2870                                 xpath.Append ("\"");
2871                         }
2872                         xpath.Append ("]/..");
2873                 }
2874                 return xpath.ToString ();
2875         }
2876 }
2877
2878 static class CecilExtensions {
2879         public static string GetDeclaringType(this CustomAttribute attribute)
2880         {
2881                         var type = attribute.Constructor.DeclaringType;
2882                         var typeName = type.FullName;
2883
2884                         string translatedType = NativeTypeManager.GetTranslatedName (type);
2885                         return translatedType;
2886         }
2887
2888         public static IEnumerable<MemberReference> GetMembers (this TypeDefinition type)
2889         {
2890                 foreach (var c in type.Methods.Where (m => m.IsConstructor))
2891                         yield return (MemberReference) c;
2892                 foreach (var e in type.Events)
2893                         yield return (MemberReference) e;
2894                 foreach (var f in type.Fields)
2895                         yield return (MemberReference) f;
2896                 foreach (var m in type.Methods.Where (m => !m.IsConstructor))
2897                         yield return (MemberReference) m;
2898                 foreach (var t in type.NestedTypes)
2899                         yield return (MemberReference) t;
2900                 foreach (var p in type.Properties)
2901                         yield return (MemberReference) p;
2902         }
2903
2904         public static IEnumerable<MemberReference> GetMembers (this TypeDefinition type, string member)
2905         {
2906                 return GetMembers (type).Where (m => m.Name == member);
2907         }
2908
2909         public static MemberReference GetMember (this TypeDefinition type, string member)
2910         {
2911                 return GetMembers (type, member).EnsureZeroOrOne ();
2912         }
2913
2914         static T EnsureZeroOrOne<T> (this IEnumerable<T> source)
2915         {
2916                 if (source.Count () > 1)
2917                         throw new InvalidOperationException ("too many matches");
2918                 return source.FirstOrDefault ();
2919         }
2920
2921         public static MethodDefinition GetMethod (this TypeDefinition type, string method)
2922         {
2923                 return type.Methods
2924                         .Where (m => m.Name == method)
2925                         .EnsureZeroOrOne ();
2926         }
2927
2928         public static IEnumerable<MemberReference> GetDefaultMembers (this TypeReference type)
2929         {
2930                 TypeDefinition def = type as TypeDefinition;
2931                 if (def == null)
2932                         return new MemberReference [0];
2933                 CustomAttribute defMemberAttr = def.CustomAttributes
2934                                 .FirstOrDefault (c => c.AttributeType.FullName == "System.Reflection.DefaultMemberAttribute");
2935                 if (defMemberAttr == null)
2936                         return new MemberReference [0];
2937                 string name = (string) defMemberAttr.ConstructorArguments [0].Value;
2938                 return def.Properties
2939                                 .Where (p => p.Name == name)
2940                                 .Select (p => (MemberReference) p);
2941         }
2942
2943         public static IEnumerable<TypeDefinition> GetTypes (this AssemblyDefinition assembly)
2944         {
2945                 return assembly.Modules.SelectMany (md => md.GetAllTypes ());
2946         }
2947
2948         public static TypeDefinition GetType (this AssemblyDefinition assembly, string type)
2949         {
2950                 return GetTypes (assembly)
2951                         .Where (td => td.FullName == type)
2952                         .EnsureZeroOrOne ();
2953         }
2954
2955         public static bool IsGenericType (this TypeReference type)
2956         {
2957                 return type.GenericParameters.Count > 0;
2958         }
2959
2960         public static bool IsGenericMethod (this MethodReference method)
2961         {
2962                 return method.GenericParameters.Count > 0;
2963         }
2964
2965         public static MemberReference Resolve (this MemberReference member)
2966         {
2967                 FieldReference fr = member as FieldReference;
2968                 if (fr != null)
2969                         return fr.Resolve ();
2970                 MethodReference mr = member as MethodReference;
2971                 if (mr != null)
2972                         return mr.Resolve ();
2973                 TypeReference tr = member as TypeReference;
2974                 if (tr != null)
2975                         return tr.Resolve ();
2976                 PropertyReference pr = member as PropertyReference;
2977                 if (pr != null)
2978                         return pr;
2979                 EventReference er = member as EventReference;
2980                 if (er != null)
2981                         return er;
2982                 throw new NotSupportedException ("Cannot find definition for " + member.ToString ());
2983         }
2984
2985         public static TypeReference GetUnderlyingType (this TypeDefinition type)
2986         {
2987                 if (!type.IsEnum)
2988                         return type;
2989                 return type.Fields.First (f => f.Name == "value__").FieldType;
2990         }
2991
2992         public static IEnumerable<TypeDefinition> GetAllTypes (this ModuleDefinition self)
2993         {
2994                 return self.Types.SelectMany (t => t.GetAllTypes ());
2995         }
2996
2997         static IEnumerable<TypeDefinition> GetAllTypes (this TypeDefinition self)
2998         {
2999                 yield return self;
3000
3001                 if (!self.HasNestedTypes)
3002                         yield break;
3003
3004                 foreach (var type in self.NestedTypes.SelectMany (t => t.GetAllTypes ()))
3005                         yield return type;
3006         }
3007 }
3008
3009 enum ApiStyle {
3010         Classic,
3011         Unified
3012 }
3013
3014 static class DocUtils {
3015
3016         public static bool DoesNotHaveApiStyle(this XmlElement element, ApiStyle style) {
3017                 string styleString = style.ToString ().ToLowerInvariant ();
3018                         string apistylevalue = element.GetAttribute ("apistyle");
3019                         return apistylevalue != styleString || string.IsNullOrWhiteSpace(apistylevalue);
3020         }
3021         public static bool HasApiStyle(this XmlElement element, ApiStyle style) {
3022                 string styleString = style.ToString ().ToLowerInvariant ();
3023                 return element.GetAttribute ("apistyle") == styleString;
3024         }
3025         public static bool HasApiStyle(this XmlNode node, ApiStyle style) 
3026         {
3027                 var attribute = node.Attributes ["apistyle"];
3028                 return attribute != null && attribute.Value == style.ToString ().ToLowerInvariant ();
3029         }
3030         public static void AddApiStyle(this XmlElement element, ApiStyle style) {
3031                 string styleString = style.ToString ().ToLowerInvariant ();
3032                 var existingValue = element.GetAttribute ("apistyle");
3033                 if (string.IsNullOrWhiteSpace (existingValue) || existingValue != styleString) {
3034                         element.SetAttribute ("apistyle", styleString);
3035                 }
3036         }
3037         public static void RemoveApiStyle (this XmlElement element, ApiStyle style) 
3038         {
3039                 string styleString = style.ToString ().ToLowerInvariant ();
3040                 string existingValue = element.GetAttribute ("apistyle");
3041                 if (string.IsNullOrWhiteSpace (existingValue) || existingValue == styleString) {
3042                         element.RemoveAttribute ("apistyle");
3043                 }
3044         }
3045         public static void RemoveApiStyle (this XmlNode node, ApiStyle style) 
3046         {
3047                 var styleAttribute = node.Attributes ["apistyle"];
3048                 if (styleAttribute != null && styleAttribute.Value == style.ToString ().ToLowerInvariant ()) {
3049                         node.Attributes.Remove (styleAttribute);
3050                 }
3051         }
3052
3053         public static bool IsExplicitlyImplemented (MethodDefinition method)
3054         {
3055                 return method.IsPrivate && method.IsFinal && method.IsVirtual;
3056         }
3057
3058         public static string GetTypeDotMember (string name)
3059         {
3060                 int startType, startMethod;
3061                 startType = startMethod = -1;
3062                 for (int i = 0; i < name.Length; ++i) {
3063                         if (name [i] == '.') {
3064                                 startType = startMethod;
3065                                 startMethod = i;
3066                         }
3067                 }
3068                 return name.Substring (startType+1);
3069         }
3070
3071         public static string GetMember (string name)
3072         {
3073                 int i = name.LastIndexOf ('.');
3074                 if (i == -1)
3075                         return name;
3076                 return name.Substring (i+1);
3077         }
3078
3079         public static void GetInfoForExplicitlyImplementedMethod (
3080                         MethodDefinition method, out TypeReference iface, out MethodReference ifaceMethod)
3081         {
3082                 iface = null;
3083                 ifaceMethod = null;
3084                 if (method.Overrides.Count != 1)
3085                         throw new InvalidOperationException ("Could not determine interface type for explicitly-implemented interface member " + method.Name);
3086                 iface = method.Overrides [0].DeclaringType;
3087                 ifaceMethod = method.Overrides [0];
3088         }
3089
3090         public static string GetPropertyName (PropertyDefinition pi)
3091         {
3092                 // Issue: (g)mcs-generated assemblies that explicitly implement
3093                 // properties don't specify the full namespace, just the 
3094                 // TypeName.Property; .NET uses Full.Namespace.TypeName.Property.
3095                 MethodDefinition method = pi.GetMethod;
3096                 if (method == null)
3097                         method = pi.SetMethod;
3098                 if (!IsExplicitlyImplemented (method))
3099                         return pi.Name;
3100
3101                 // Need to determine appropriate namespace for this member.
3102                 TypeReference iface;
3103                 MethodReference ifaceMethod;
3104                 GetInfoForExplicitlyImplementedMethod (method, out iface, out ifaceMethod);
3105                 return string.Join (".", new string[]{
3106                                 DocTypeFullMemberFormatter.Default.GetName (iface),
3107                                 GetMember (pi.Name)});
3108         }
3109
3110         public static string GetNamespace (TypeReference type)
3111         {
3112                 if (type.GetElementType ().IsNested)
3113                         type = type.GetElementType ();
3114                 while (type != null && type.IsNested)
3115                         type = type.DeclaringType;
3116                 if (type == null)
3117                         return string.Empty;
3118
3119                         string typeNS = type.Namespace;
3120
3121                         // first, make sure this isn't a type reference to another assembly/module
3122
3123                         bool isInAssembly = MDocUpdater.IsInAssemblies(type.Module.Name);
3124                         if (isInAssembly && !typeNS.StartsWith ("System") && MDocUpdater.HasDroppedNamespace (type)) {
3125                                 typeNS = string.Format ("{0}.{1}", MDocUpdater.droppedNamespace, typeNS);
3126                         }
3127                         return typeNS;
3128         }
3129
3130         public static string PathCombine (string dir, string path)
3131         {
3132                 if (dir == null)
3133                         dir = "";
3134                 if (path == null)
3135                         path = "";
3136                 return Path.Combine (dir, path);
3137         }
3138
3139         public static bool IsExtensionMethod (MethodDefinition method)
3140         {
3141                 return
3142                         method.CustomAttributes
3143                                         .Any (m => m.AttributeType.FullName == "System.Runtime.CompilerServices.ExtensionAttribute")
3144                         && method.DeclaringType.CustomAttributes
3145                                         .Any (m => m.AttributeType.FullName == "System.Runtime.CompilerServices.ExtensionAttribute");
3146         }
3147
3148         public static bool IsDelegate (TypeDefinition type)
3149         {
3150                 TypeReference baseRef = type.BaseType;
3151                 if (baseRef == null)
3152                         return false;
3153                 return !type.IsAbstract && baseRef.FullName == "System.Delegate" || // FIXME
3154                                 baseRef.FullName == "System.MulticastDelegate";
3155         }
3156
3157         public static List<TypeReference> GetDeclaringTypes (TypeReference type)
3158         {
3159                 List<TypeReference> decls = new List<TypeReference> ();
3160                 decls.Add (type);
3161                 while (type.DeclaringType != null) {
3162                         decls.Add (type.DeclaringType);
3163                         type = type.DeclaringType;
3164                 }
3165                 decls.Reverse ();
3166                 return decls;
3167         }
3168
3169         public static int GetGenericArgumentCount (TypeReference type)
3170         {
3171                 GenericInstanceType inst = type as GenericInstanceType;
3172                 return inst != null
3173                                 ? inst.GenericArguments.Count
3174                                 : type.GenericParameters.Count;
3175         }
3176
3177         public static IEnumerable<TypeReference> GetUserImplementedInterfaces (TypeDefinition type)
3178         {
3179                 HashSet<string> inheritedInterfaces = GetInheritedInterfaces (type);
3180                 List<TypeReference> userInterfaces = new List<TypeReference> ();
3181                 foreach (TypeReference iface in type.Interfaces) {
3182                         TypeReference lookup = iface.Resolve () ?? iface;
3183                         if (!inheritedInterfaces.Contains (GetQualifiedTypeName (lookup)))
3184                                 userInterfaces.Add (iface);
3185                 }
3186                 return userInterfaces.Where (i => MDocUpdater.IsPublic (i.Resolve ()));
3187         }
3188
3189         private static string GetQualifiedTypeName (TypeReference type)
3190         {
3191                 return "[" + type.Scope.Name + "]" + type.FullName;
3192         }
3193
3194         private static HashSet<string> GetInheritedInterfaces (TypeDefinition type)
3195         {
3196                 HashSet<string> inheritedInterfaces = new HashSet<string> ();
3197                 Action<TypeDefinition> a = null;
3198                 a = t => {
3199                         if (t == null) return;
3200                         foreach (TypeReference r in t.Interfaces) {
3201                                 inheritedInterfaces.Add (GetQualifiedTypeName (r));
3202                                 a (r.Resolve ());
3203                         }
3204                 };
3205                 TypeReference baseRef = type.BaseType;
3206                 while (baseRef != null) {
3207                         TypeDefinition baseDef = baseRef.Resolve ();
3208                         if (baseDef != null) {
3209                                 a (baseDef);
3210                                 baseRef = baseDef.BaseType;
3211                         }
3212                         else
3213                                 baseRef = null;
3214                 }
3215                 foreach (TypeReference r in type.Interfaces)
3216                         a (r.Resolve ());
3217                 return inheritedInterfaces;
3218         }
3219 }
3220
3221 class DocsNodeInfo {
3222         public DocsNodeInfo (XmlElement node)
3223         {
3224                 this.Node = node;
3225         }
3226
3227         public DocsNodeInfo (XmlElement node, TypeDefinition type)
3228                 : this (node)
3229         {
3230                 SetType (type);
3231         }
3232
3233         public DocsNodeInfo (XmlElement node, MemberReference member)
3234                 : this (node)
3235         {
3236                 SetMemberInfo (member);
3237         }
3238
3239         void SetType (TypeDefinition type)
3240         {
3241                 if (type == null)
3242                         throw new ArgumentNullException ("type");
3243                 Type = type;
3244                 GenericParameters = new List<GenericParameter> (type.GenericParameters);
3245                 List<TypeReference> declTypes = DocUtils.GetDeclaringTypes (type);
3246                 int maxGenArgs = DocUtils.GetGenericArgumentCount (type);
3247                 for (int i = 0; i < declTypes.Count - 1; ++i) {
3248                         int remove = System.Math.Min (maxGenArgs, 
3249                                         DocUtils.GetGenericArgumentCount (declTypes [i]));
3250                         maxGenArgs -= remove;
3251                         while (remove-- > 0)
3252                                 GenericParameters.RemoveAt (0);
3253                 }
3254                 if (DocUtils.IsDelegate (type)) {
3255                         Parameters = type.GetMethod("Invoke").Parameters;
3256                         ReturnType = type.GetMethod("Invoke").ReturnType;
3257                         ReturnIsReturn = true;
3258                 }
3259         }
3260
3261         void SetMemberInfo (MemberReference member)
3262         {
3263                 if (member == null)
3264                         throw new ArgumentNullException ("member");
3265                 ReturnIsReturn = true;
3266                 AddRemarks = true;
3267                 Member = member;
3268                 
3269                 if (member is MethodReference ) {
3270                         MethodReference mr = (MethodReference) member;
3271                         Parameters = mr.Parameters;
3272                         if (mr.IsGenericMethod ()) {
3273                                 GenericParameters = new List<GenericParameter> (mr.GenericParameters);
3274                         }
3275                 }
3276                 else if (member is PropertyDefinition) {
3277                         Parameters = ((PropertyDefinition) member).Parameters;
3278                 }
3279                         
3280                 if (member is MethodDefinition) {
3281                         ReturnType = ((MethodDefinition) member).ReturnType;
3282                 } else if (member is PropertyDefinition) {
3283                         ReturnType = ((PropertyDefinition) member).PropertyType;
3284                         ReturnIsReturn = false;
3285                 }
3286
3287                 // no remarks section for enum members
3288                 if (member.DeclaringType != null && ((TypeDefinition) member.DeclaringType).IsEnum)
3289                         AddRemarks = false;
3290         }
3291
3292         public TypeReference ReturnType;
3293         public List<GenericParameter> GenericParameters;
3294         public IList<ParameterDefinition> Parameters;
3295         public bool ReturnIsReturn;
3296         public XmlElement Node;
3297         public bool AddRemarks = true;
3298         public MemberReference Member;
3299         public TypeDefinition Type;
3300
3301         public override string ToString ()
3302         {
3303                 return string.Format ("{0} - {1} - {2}", Type, Member, Node == null ? "no xml" : "with xml");
3304         }
3305 }
3306
3307 class DocumentationEnumerator {
3308         
3309         public virtual IEnumerable<TypeDefinition> GetDocumentationTypes (AssemblyDefinition assembly, List<string> forTypes)
3310         {
3311                 return GetDocumentationTypes (assembly, forTypes, null);
3312         }
3313
3314         protected IEnumerable<TypeDefinition> GetDocumentationTypes (AssemblyDefinition assembly, List<string> forTypes, HashSet<string> seen)
3315         {
3316                 foreach (TypeDefinition type in assembly.GetTypes()) {
3317                         if (forTypes != null && forTypes.BinarySearch (type.FullName) < 0)
3318                                 continue;
3319                         if (seen != null && seen.Contains (type.FullName))
3320                                 continue;
3321                         yield return type;
3322                         foreach (TypeDefinition nested in type.NestedTypes)
3323                                 yield return nested;
3324                 }
3325         }
3326
3327         public virtual IEnumerable<DocsNodeInfo> GetDocumentationMembers (XmlDocument basefile, TypeDefinition type)
3328         {
3329                 foreach (XmlElement oldmember in basefile.SelectNodes("Type/Members/Member")) {
3330                         if (oldmember.GetAttribute ("__monodocer-seen__") == "true") {
3331                                 oldmember.RemoveAttribute ("__monodocer-seen__");
3332                                 continue;
3333                         }
3334                         MemberReference m = GetMember (type, new DocumentationMember (oldmember));
3335                         if (m == null) {
3336                                 yield return new DocsNodeInfo (oldmember);
3337                         }
3338                         else {
3339                                 yield return new DocsNodeInfo (oldmember, m);
3340                         }
3341                 }
3342         }
3343
3344         protected static MemberReference GetMember (TypeDefinition type, DocumentationMember member)
3345         {
3346                 string membertype = member.MemberType;
3347                 
3348                 string returntype = member.ReturnType;
3349                 
3350                 string docName = member.MemberName;
3351
3352                 string[] docTypeParams = GetTypeParameters (docName, member.TypeParameters);
3353
3354                 // If we're using 'magic types', then we might get false positives ... in those cases, we keep searching
3355                 MemberReference likelyCandidate = null;
3356                 
3357                 // Loop through all members in this type with the same name
3358                 var reflectedMembers = GetReflectionMembers (type, docName).ToArray ();
3359                 foreach (MemberReference mi in reflectedMembers) {
3360                         bool matchedMagicType = false;
3361                         if (mi is TypeDefinition) continue;
3362                         if (MDocUpdater.GetMemberType(mi) != membertype) continue;
3363
3364                         if (MDocUpdater.IsPrivate (mi))
3365                                 continue;
3366
3367                         IList<ParameterDefinition> pis = null;
3368                         string[] typeParams = null;
3369                         if (mi is MethodDefinition) {
3370                                 MethodDefinition mb = (MethodDefinition) mi;
3371                                 pis = mb.Parameters;
3372                                 if (mb.IsGenericMethod ()) {
3373                                         IList<GenericParameter> args = mb.GenericParameters;
3374                                         typeParams = args.Select (p => p.Name).ToArray ();
3375                                 }
3376                         }
3377                         else if (mi is PropertyDefinition)
3378                                 pis = ((PropertyDefinition)mi).Parameters;
3379                                 
3380                         // check type parameters
3381                         int methodTcount = member.TypeParameters == null ? 0 : member.TypeParameters.Count;
3382                         int reflectionTcount = typeParams == null ? 0 : typeParams.Length;
3383                         if (methodTcount != reflectionTcount) 
3384                                 continue;
3385
3386                         // check member parameters
3387                         int mcount = member.Parameters == null ? 0 : member.Parameters.Count;
3388                         int pcount = pis == null ? 0 : pis.Count;
3389                         if (mcount != pcount)
3390                                 continue;
3391
3392                         MethodDefinition mDef = mi as MethodDefinition;
3393                         if (mDef != null && !mDef.IsConstructor) {
3394                                 // Casting operators can overload based on return type.
3395                                 string rtype = GetReplacedString (
3396                                                        MDocUpdater.GetDocTypeFullName (((MethodDefinition)mi).ReturnType), 
3397                                                        typeParams, docTypeParams);
3398                                 string originalRType = rtype;
3399                                 if (MDocUpdater.SwitchingToMagicTypes) {
3400                                         rtype = NativeTypeManager.ConvertFromNativeType (rtype);
3401                                         
3402                                 }
3403                                 if ((returntype != rtype && originalRType == rtype) ||
3404                                         (MDocUpdater.SwitchingToMagicTypes && returntype != originalRType && returntype != rtype && originalRType != rtype)) {
3405                                         continue;
3406                                 }
3407
3408                                 if (originalRType != rtype)
3409                                         matchedMagicType = true;
3410                         }
3411
3412                         if (pcount == 0)
3413                                 return mi;
3414                         bool good = true;
3415                         for (int i = 0; i < pis.Count; i++) {
3416                                 string paramType = GetReplacedString (
3417                                         MDocUpdater.GetDocParameterType (pis [i].ParameterType),
3418                                         typeParams, docTypeParams);
3419
3420                                 // if magictypes, replace paramType to "classic value" ... so the comparison works
3421                                 string originalParamType = paramType;
3422                                 if (MDocUpdater.SwitchingToMagicTypes) {
3423                                         paramType = NativeTypeManager.ConvertFromNativeType (paramType);
3424                                 }
3425
3426                                 string xmlMemberType = member.Parameters [i];
3427                                 if ((!paramType.Equals(xmlMemberType) && paramType.Equals(originalParamType)) || 
3428                                         (MDocUpdater.SwitchingToMagicTypes && !originalParamType.Equals(xmlMemberType) && !paramType.Equals(xmlMemberType) && !paramType.Equals(originalParamType))) {
3429
3430                                         // did not match ... if we're dropping the namespace, and the paramType has the dropped
3431                                         // namespace, we should see if it matches when added
3432                                         bool stillDoesntMatch = true;
3433                                         if (MDocUpdater.HasDroppedNamespace(type) && paramType.StartsWith (MDocUpdater.droppedNamespace)) {
3434                                                 string withDroppedNs = string.Format ("{0}.{1}", MDocUpdater.droppedNamespace, xmlMemberType);
3435
3436                                                 stillDoesntMatch = withDroppedNs != paramType;
3437                                         }
3438
3439                                         if (stillDoesntMatch) {
3440                                                 good = false;
3441                                                 break;
3442                                         }
3443                                 }
3444
3445                                 if (originalParamType != paramType)
3446                                         matchedMagicType = true;
3447                         }
3448                         if (!good) continue;
3449
3450                         if (MDocUpdater.SwitchingToMagicTypes && likelyCandidate == null && matchedMagicType) {
3451                                 // we matched this on a magic type conversion ... let's keep going to see if there's another one we should look at that matches more closely
3452                                 likelyCandidate = mi;
3453                                 continue;
3454                         }
3455
3456                         return mi;
3457                 }
3458                 
3459                 return likelyCandidate;
3460         }
3461
3462         static string[] GetTypeParameters (string docName, IEnumerable<string> knownParameters)
3463         {
3464                 if (docName [docName.Length-1] != '>')
3465                         return null;
3466                 StringList types = new StringList ();
3467                 int endToken = docName.Length-2;
3468                 int i = docName.Length-2;
3469                 do {
3470                         if (docName [i] == ',' || docName [i] == '<') {
3471                                 types.Add (docName.Substring (i + 1, endToken - i));
3472                                 endToken = i-1;
3473                         }
3474                         if (docName [i] == '<')
3475                                 break;
3476                 } while (--i >= 0);
3477
3478                 types.Reverse ();
3479                 var arrayTypes = types.ToArray ();
3480
3481                 if (knownParameters != null && knownParameters.Any () && arrayTypes.Length != knownParameters.Count ())
3482                         return knownParameters.ToArray ();
3483                 else
3484                         return arrayTypes;
3485         }
3486
3487         protected static IEnumerable<MemberReference> GetReflectionMembers (TypeDefinition type, string docName)
3488         {
3489                 // In case of dropping the namespace, we have to remove the dropped NS
3490                 // so that docName will match what's in the assembly/type
3491                 if (MDocUpdater.HasDroppedNamespace (type) && docName.StartsWith(MDocUpdater.droppedNamespace + ".")) {
3492                         int droppedNsLength = MDocUpdater.droppedNamespace.Length;
3493                         docName = docName.Substring (droppedNsLength + 1, docName.Length - droppedNsLength - 1);
3494                 }
3495
3496                 // need to worry about 4 forms of //@MemberName values:
3497                 //  1. "Normal" (non-generic) member names: GetEnumerator
3498                 //    - Lookup as-is.
3499                 //  2. Explicitly-implemented interface member names: System.Collections.IEnumerable.Current
3500                 //    - try as-is, and try type.member (due to "kludge" for property
3501                 //      support.
3502                 //  3. "Normal" Generic member names: Sort<T> (CSC)
3503                 //    - need to remove generic parameters --> "Sort"
3504                 //  4. Explicitly-implemented interface members for generic interfaces: 
3505                 //    -- System.Collections.Generic.IEnumerable<T>.Current
3506                 //    - Try as-is, and try type.member, *keeping* the generic parameters.
3507                 //     --> System.Collections.Generic.IEnumerable<T>.Current, IEnumerable<T>.Current
3508                 //  5. As of 2008-01-02, gmcs will do e.g. 'IFoo`1[A].Method' instead of
3509                 //    'IFoo<A>.Method' for explicitly implemented methods; don't interpret
3510                 //    this as (1) or (2).
3511                 if (docName.IndexOf ('<') == -1 && docName.IndexOf ('[') == -1) {
3512                         // Cases 1 & 2
3513                         foreach (MemberReference mi in type.GetMembers (docName))
3514                                 yield return mi;
3515                         if (CountChars (docName, '.') > 0)
3516                                 // might be a property; try only type.member instead of
3517                                 // namespace.type.member.
3518                                 foreach (MemberReference mi in 
3519                                                 type.GetMembers (DocUtils.GetTypeDotMember (docName)))
3520                                         yield return mi;
3521                         yield break;
3522                 }
3523                 // cases 3 & 4
3524                 int numLt = 0;
3525                 int numDot = 0;
3526                 int startLt, startType, startMethod;
3527                 startLt = startType = startMethod = -1;
3528                 for (int i = 0; i < docName.Length; ++i) {
3529                         switch (docName [i]) {
3530                                 case '<':
3531                                         if (numLt == 0) {
3532                                                 startLt = i;
3533                                         }
3534                                         ++numLt;
3535                                         break;
3536                                 case '>':
3537                                         --numLt;
3538                                         if (numLt == 0 && (i + 1) < docName.Length)
3539                                                 // there's another character in docName, so this <...> sequence is
3540                                                 // probably part of a generic type -- case 4.
3541                                                 startLt = -1;
3542                                         break;
3543                                 case '.':
3544                                         startType = startMethod;
3545                                         startMethod = i;
3546                                         ++numDot;
3547                                         break;
3548                         }
3549                 }
3550                 string refName = startLt == -1 ? docName : docName.Substring (0, startLt);
3551                 // case 3
3552                 foreach (MemberReference mi in type.GetMembers (refName))
3553                         yield return mi;
3554
3555                 // case 4
3556                 foreach (MemberReference mi in type.GetMembers (refName.Substring (startType + 1)))
3557                         yield return mi;
3558
3559                 // If we _still_ haven't found it, we've hit another generic naming issue:
3560                 // post Mono 1.1.18, gmcs generates [[FQTN]] instead of <TypeName> for
3561                 // explicitly-implemented METHOD names (not properties), e.g. 
3562                 // "System.Collections.Generic.IEnumerable`1[[Foo, test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]].GetEnumerator"
3563                 // instead of "System.Collections.Generic.IEnumerable<Foo>.GetEnumerator",
3564                 // which the XML docs will contain.
3565                 //
3566                 // Alas, we can't derive the Mono name from docName, so we need to iterate
3567                 // over all member names, convert them into CSC format, and compare... :-(
3568                 if (numDot == 0)
3569                         yield break;
3570                 foreach (MemberReference mi in type.GetMembers ()) {
3571                         if (MDocUpdater.GetMemberName (mi) == docName)
3572                                 yield return mi;
3573                 }
3574         }
3575
3576         static string GetReplacedString (string typeName, string[] from, string[] to)
3577         {
3578                 if (from == null)
3579                         return typeName;
3580                 for (int i = 0; i < from.Length; ++i)
3581                         typeName = typeName.Replace (from [i], to [i]);
3582                 return typeName;
3583         }
3584
3585         private static int CountChars (string s, char c)
3586         {
3587                 int count = 0;
3588                 for (int i = 0; i < s.Length; ++i) {
3589                         if (s [i] == c)
3590                                 ++count;
3591                 }
3592                 return count;
3593         }
3594 }
3595
3596 class EcmaDocumentationEnumerator : DocumentationEnumerator {
3597
3598         XmlReader ecmadocs;
3599         MDocUpdater app;
3600
3601         public EcmaDocumentationEnumerator (MDocUpdater app, XmlReader ecmaDocs)
3602         {
3603                 this.app      = app;
3604                 this.ecmadocs = ecmaDocs;
3605         }
3606
3607         public override IEnumerable<TypeDefinition> GetDocumentationTypes (AssemblyDefinition assembly, List<string> forTypes)
3608         {
3609                 HashSet<string> seen = new HashSet<string> ();
3610                 return GetDocumentationTypes (assembly, forTypes, seen)
3611                         .Concat (base.GetDocumentationTypes (assembly, forTypes, seen));
3612         }
3613
3614         new IEnumerable<TypeDefinition> GetDocumentationTypes (AssemblyDefinition assembly, List<string> forTypes, HashSet<string> seen)
3615         {
3616                 int typeDepth = -1;
3617                 while (ecmadocs.Read ()) {
3618                         switch (ecmadocs.Name) {
3619                                 case "Type": {
3620                                         if (typeDepth == -1)
3621                                                 typeDepth = ecmadocs.Depth;
3622                                         if (ecmadocs.NodeType != XmlNodeType.Element)
3623                                                 continue;
3624                                         if (typeDepth != ecmadocs.Depth) // nested <TypeDefinition/> element?
3625                                                 continue;
3626                                         string typename = ecmadocs.GetAttribute ("FullName");
3627                                         string typename2 = MDocUpdater.GetTypeFileName (typename);
3628                                         if (forTypes != null && 
3629                                                         forTypes.BinarySearch (typename) < 0 &&
3630                                                         typename != typename2 &&
3631                                                         forTypes.BinarySearch (typename2) < 0)
3632                                                 continue;
3633                                         TypeDefinition t;
3634                                         if ((t = assembly.GetType (typename)) == null && 
3635                                                         (t = assembly.GetType (typename2)) == null)
3636                                                 continue;
3637                                         seen.Add (typename);
3638                                         if (typename != typename2)
3639                                                 seen.Add (typename2);
3640                                         Console.WriteLine ("  Import: {0}", t.FullName);
3641                                         if (ecmadocs.Name != "Docs") {
3642                                                 int depth = ecmadocs.Depth;
3643                                                 while (ecmadocs.Read ()) {
3644                                                         if (ecmadocs.Name == "Docs" && ecmadocs.Depth == depth + 1)
3645                                                                 break;
3646                                                 }
3647                                         }
3648                                         if (!ecmadocs.IsStartElement ("Docs"))
3649                                                 throw new InvalidOperationException ("Found " + ecmadocs.Name + "; expecting <Docs/>!");
3650                                         yield return t;
3651                                         break;
3652                                 }
3653                                 default:
3654                                         break;
3655                         }
3656                 }
3657         }
3658
3659         public override IEnumerable<DocsNodeInfo> GetDocumentationMembers (XmlDocument basefile, TypeDefinition type)
3660         {
3661                 return GetMembers (basefile, type)
3662                         .Concat (base.GetDocumentationMembers (basefile, type));
3663         }
3664
3665         private IEnumerable<DocsNodeInfo> GetMembers (XmlDocument basefile, TypeDefinition type)
3666         {
3667                 while (ecmadocs.Name != "Members" && ecmadocs.Read ()) {
3668                         // do nothing
3669                 }
3670                 if (ecmadocs.IsEmptyElement)
3671                         yield break;
3672
3673                 int membersDepth = ecmadocs.Depth;
3674                 bool go = true;
3675                 while (go && ecmadocs.Read ()) {
3676                         switch (ecmadocs.Name) {
3677                                 case "Member": {
3678                                         if (membersDepth != ecmadocs.Depth - 1 || ecmadocs.NodeType != XmlNodeType.Element)
3679                                                 continue;
3680                                         DocumentationMember dm = new DocumentationMember (ecmadocs);
3681                                         
3682                                         string xp = MDocUpdater.GetXPathForMember (dm);
3683                                         XmlElement oldmember = (XmlElement) basefile.SelectSingleNode (xp);
3684                                         MemberReference m;
3685                                         if (oldmember == null) {
3686                                                 m = GetMember (type, dm);
3687                                                 if (m == null) {
3688                                                         app.Warning ("Could not import ECMA docs for `{0}'s `{1}': Member not found.",
3689                                                                         type.FullName, dm.MemberSignatures ["C#"]);
3690                                                                         // SelectSingleNode (ecmaDocsMember, "MemberSignature[@Language=\"C#\"]/@Value").Value);
3691                                                         continue;
3692                                                 }
3693                                                 // oldmember lookup may have failed due to type parameter renames.
3694                                                 // Try again.
3695                                                 oldmember = (XmlElement) basefile.SelectSingleNode (MDocUpdater.GetXPathForMember (m));
3696                                                 if (oldmember == null) {
3697                                                         XmlElement members = MDocUpdater.WriteElement (basefile.DocumentElement, "Members");
3698                                                         oldmember = basefile.CreateElement ("Member");
3699                                                         oldmember.SetAttribute ("MemberName", dm.MemberName);
3700                                                         members.AppendChild (oldmember);
3701                                                         foreach (string key in MDocUpdater.Sort (dm.MemberSignatures.Keys)) {
3702                                                                 XmlElement ms = basefile.CreateElement ("MemberSignature");
3703                                                                 ms.SetAttribute ("Language", key);
3704                                                                 ms.SetAttribute ("Value", (string) dm.MemberSignatures [key]);
3705                                                                 oldmember.AppendChild (ms);
3706                                                         }
3707                                                         oldmember.SetAttribute ("__monodocer-seen__", "true");
3708                                                         Console.WriteLine ("Member Added: {0}", oldmember.SelectSingleNode("MemberSignature[@Language='C#']/@Value").InnerText);
3709                                                         app.additions++;
3710                                                 }
3711                                         }
3712                                         else {
3713                                                 m = GetMember (type, new DocumentationMember (oldmember));
3714                                                 if (m == null) {
3715                                                         app.Warning ("Could not import ECMA docs for `{0}'s `{1}': Member not found.",
3716                                                                         type.FullName, dm.MemberSignatures ["C#"]);
3717                                                         continue;
3718                                                 }
3719                                                 oldmember.SetAttribute ("__monodocer-seen__", "true");
3720                                         }
3721                                         DocsNodeInfo node = new DocsNodeInfo (oldmember, m);
3722                                         if (ecmadocs.Name != "Docs")
3723                                                 throw new InvalidOperationException ("Found " + ecmadocs.Name + "; expected <Docs/>!");
3724                                         yield return node;
3725                                         break;
3726                                 }
3727                                 case "Members":
3728                                         if (membersDepth == ecmadocs.Depth && ecmadocs.NodeType == XmlNodeType.EndElement) {
3729                                                 go = false;
3730                                         }
3731                                         break;
3732                         }
3733                 }
3734         }
3735 }
3736
3737 abstract class DocumentationImporter {
3738
3739         public abstract void ImportDocumentation (DocsNodeInfo info);
3740 }
3741
3742 class MsxdocDocumentationImporter : DocumentationImporter {
3743
3744         XmlDocument slashdocs;
3745
3746         public MsxdocDocumentationImporter (string file)
3747         {
3748                 var xml = File.ReadAllText (file);
3749
3750                 // Ensure Unix line endings
3751                 xml = xml.Replace ("\r", "");
3752
3753                 slashdocs = new XmlDocument();
3754                 slashdocs.LoadXml (xml);
3755         }
3756
3757         public override void ImportDocumentation (DocsNodeInfo info)
3758         {
3759                 XmlNode elem = GetDocs (info.Member ?? info.Type);
3760
3761                 if (elem == null)
3762                         return;
3763
3764                 XmlElement e = info.Node;
3765
3766                 if (elem.SelectSingleNode("summary") != null)
3767                         MDocUpdater.ClearElement(e, "summary");
3768                 if (elem.SelectSingleNode("remarks") != null)
3769                         MDocUpdater.ClearElement(e, "remarks");
3770                 if (elem.SelectSingleNode ("value") != null || elem.SelectSingleNode ("returns") != null) {
3771                         MDocUpdater.ClearElement(e, "value");
3772                         MDocUpdater.ClearElement(e, "returns");
3773                 }
3774
3775                 foreach (XmlNode child in elem.ChildNodes) {
3776                         switch (child.Name) {
3777                                 case "param":
3778                                 case "typeparam": {
3779                                         XmlAttribute name = child.Attributes ["name"];
3780                                         if (name == null)
3781                                                 break;
3782                                         XmlElement p2 = (XmlElement) e.SelectSingleNode (child.Name + "[@name='" + name.Value + "']");
3783                                         if (p2 != null)
3784                                                 p2.InnerXml = child.InnerXml;
3785                                         break;
3786                                 }
3787                                 // Occasionally XML documentation will use <returns/> on
3788                                 // properties, so let's try to normalize things.
3789                                 case "value":
3790                                 case "returns": {
3791                                         XmlElement v = e.OwnerDocument.CreateElement (info.ReturnIsReturn ? "returns" : "value");
3792                                         v.InnerXml = child.InnerXml;
3793                                         e.AppendChild (v);
3794                                         break;
3795                                 }
3796                                 case "altmember":
3797                                 case "exception":
3798                                 case "permission": {
3799                                         XmlAttribute cref = child.Attributes ["cref"] ?? child.Attributes ["name"];
3800                                         if (cref == null)
3801                                                 break;
3802                                         XmlElement a = (XmlElement) e.SelectSingleNode (child.Name + "[@cref='" + cref.Value + "']");
3803                                         if (a == null) {
3804                                                 a = e.OwnerDocument.CreateElement (child.Name);
3805                                                 a.SetAttribute ("cref", cref.Value);
3806                                                 e.AppendChild (a);
3807                                         }
3808                                         a.InnerXml = child.InnerXml;
3809                                         break;
3810                                 }
3811                                 case "seealso": {
3812                                         XmlAttribute cref = child.Attributes ["cref"];
3813                                         if (cref == null)
3814                                                 break;
3815                                         XmlElement a = (XmlElement) e.SelectSingleNode ("altmember[@cref='" + cref.Value + "']");
3816                                         if (a == null) {
3817                                                 a = e.OwnerDocument.CreateElement ("altmember");
3818                                                 a.SetAttribute ("cref", cref.Value);
3819                                                 e.AppendChild (a);
3820                                         }
3821                                         break;
3822                                 }
3823                                 default: {
3824                                         bool add = true;
3825                                         if (child.NodeType == XmlNodeType.Element && 
3826                                                         e.SelectNodes (child.Name).Cast<XmlElement>().Any (n => n.OuterXml == child.OuterXml))
3827                                                 add = false;
3828                                         if (add)
3829                                                 MDocUpdater.CopyNode (child, e);
3830                                         break;
3831                                 }
3832                         }
3833                 }
3834         }
3835
3836         private XmlNode GetDocs (MemberReference member)
3837         {
3838                 string slashdocsig = MDocUpdater.slashdocFormatter.GetDeclaration (member);
3839                 if (slashdocsig != null)
3840                         return slashdocs.SelectSingleNode ("doc/members/member[@name='" + slashdocsig + "']");
3841                 return null;
3842         }
3843 }
3844
3845 class EcmaDocumentationImporter : DocumentationImporter {
3846
3847         XmlReader ecmadocs;
3848
3849         public EcmaDocumentationImporter (XmlReader ecmaDocs)
3850         {
3851                 this.ecmadocs = ecmaDocs;
3852         }
3853
3854         public override void ImportDocumentation (DocsNodeInfo info)
3855         {
3856                 if (!ecmadocs.IsStartElement ("Docs")) {
3857                         return;
3858                 }
3859
3860                 XmlElement e = info.Node;
3861
3862                 int depth = ecmadocs.Depth;
3863                 ecmadocs.ReadStartElement ("Docs");
3864                 while (ecmadocs.Read ()) {
3865                         if (ecmadocs.Name == "Docs") {
3866                                 if (ecmadocs.Depth == depth && ecmadocs.NodeType == XmlNodeType.EndElement)
3867                                         break;
3868                                 else
3869                                         throw new InvalidOperationException ("Skipped past current <Docs/> element!");
3870                         }
3871                         if (!ecmadocs.IsStartElement ())
3872                                 continue;
3873                         switch (ecmadocs.Name) {
3874                                 case "param":
3875                                 case "typeparam": {
3876                                         string name = ecmadocs.GetAttribute ("name");
3877                                         if (name == null)
3878                                                 break;
3879                                         XmlNode doc = e.SelectSingleNode (
3880                                                         ecmadocs.Name + "[@name='" + name + "']");
3881                                         string value = ecmadocs.ReadInnerXml ();
3882                                         if (doc != null)
3883                                                 doc.InnerXml = value.Replace ("\r", "");
3884                                         break;
3885                                 }
3886                                 case "altmember":
3887                                 case "exception":
3888                                 case "permission":
3889                                 case "seealso": {
3890                                         string name = ecmadocs.Name;
3891                                         string cref = ecmadocs.GetAttribute ("cref");
3892                                         if (cref == null)
3893                                                 break;
3894                                         XmlNode doc = e.SelectSingleNode (
3895                                                         ecmadocs.Name + "[@cref='" + cref + "']");
3896                                         string value = ecmadocs.ReadInnerXml ().Replace ("\r", "");
3897                                         if (doc != null)
3898                                                 doc.InnerXml = value;
3899                                         else {
3900                                                 XmlElement n = e.OwnerDocument.CreateElement (name);
3901                                                 n.SetAttribute ("cref", cref);
3902                                                 n.InnerXml = value;
3903                                                 e.AppendChild (n);
3904                                         }
3905                                         break;
3906                                 }
3907                                 default: {
3908                                         string name = ecmadocs.Name;
3909                                         string xpath = ecmadocs.Name;
3910                                         StringList attributes = new StringList (ecmadocs.AttributeCount);
3911                                         if (ecmadocs.MoveToFirstAttribute ()) {
3912                                                 do {
3913                                                         attributes.Add ("@" + ecmadocs.Name + "=\"" + ecmadocs.Value + "\"");
3914                                                 } while (ecmadocs.MoveToNextAttribute ());
3915                                                 ecmadocs.MoveToContent ();
3916                                         }
3917                                         if (attributes.Count > 0) {
3918                                                 xpath += "[" + string.Join (" and ", attributes.ToArray ()) + "]";
3919                                         }
3920                                         XmlNode doc = e.SelectSingleNode (xpath);
3921                                         string value = ecmadocs.ReadInnerXml ().Replace ("\r", "");
3922                                         if (doc != null) {
3923                                                 doc.InnerXml = value;
3924                                         }
3925                                         else {
3926                                                 XmlElement n = e.OwnerDocument.CreateElement (name);
3927                                                 n.InnerXml = value;
3928                                                 foreach (string a in attributes) {
3929                                                         int eq = a.IndexOf ('=');
3930                                                         n.SetAttribute (a.Substring (1, eq-1), a.Substring (eq+2, a.Length-eq-3));
3931                                                 }
3932                                                 e.AppendChild (n);
3933                                         }
3934                                         break;
3935                                 }
3936                         }
3937                 }
3938         }
3939 }
3940
3941 class DocumentationMember {
3942         public StringToStringMap MemberSignatures = new StringToStringMap ();
3943         public string ReturnType;
3944         public StringList Parameters;
3945         public StringList TypeParameters;
3946         public string MemberName;
3947         public string MemberType;
3948
3949         public DocumentationMember (XmlReader reader)
3950         {
3951                 MemberName = reader.GetAttribute ("MemberName");
3952                 int depth = reader.Depth;
3953                 bool go = true;
3954                 StringList p = new StringList ();
3955                 StringList tp = new StringList ();
3956                 do {
3957                         if (reader.NodeType != XmlNodeType.Element)
3958                                 continue;
3959
3960                         bool shouldUse = true;
3961                         try {
3962                                 string apistyle = reader.GetAttribute ("apistyle");
3963                                 shouldUse = string.IsNullOrWhiteSpace(apistyle) || apistyle == "classic"; // only use this tag if it's an 'classic' style node
3964                         }
3965                         catch (Exception ex) {}
3966                         switch (reader.Name) {
3967                                 case "MemberSignature":
3968                                         if (shouldUse) {
3969                                                 MemberSignatures [reader.GetAttribute ("Language")] = reader.GetAttribute ("Value");
3970                                         }
3971                                         break;
3972                                 case "MemberType":
3973                                         MemberType = reader.ReadElementString ();
3974                                         break;
3975                                 case "ReturnType":
3976                                         if (reader.Depth == depth + 2 && shouldUse)
3977                                                 ReturnType = reader.ReadElementString ();
3978                                         break;
3979                                 case "Parameter":
3980                                         if (reader.Depth == depth + 2 && shouldUse)
3981                                                 p.Add (reader.GetAttribute ("Type"));
3982                                         break;
3983                                 case "TypeParameter":
3984                                         if (reader.Depth == depth + 2 && shouldUse)
3985                                                 tp.Add (reader.GetAttribute ("Name"));
3986                                         break;
3987                                 case "Docs":
3988                                         if (reader.Depth == depth + 1)
3989                                                 go = false;
3990                                         break;
3991                         }
3992                 } while (go && reader.Read () && reader.Depth >= depth);
3993                 if (p.Count > 0) {
3994                         Parameters = p;
3995                 }
3996                 if (tp.Count > 0) {
3997                         TypeParameters = tp;
3998                 } else {
3999                         DiscernTypeParameters ();
4000                 }
4001         }
4002
4003         public DocumentationMember (XmlNode node)
4004         {
4005                 MemberName = node.Attributes ["MemberName"].Value;
4006                 foreach (XmlNode n in node.SelectNodes ("MemberSignature")) {
4007                         XmlAttribute l = n.Attributes ["Language"];
4008                         XmlAttribute v = n.Attributes ["Value"];
4009                         XmlAttribute apistyle = n.Attributes ["apistyle"];
4010                         bool shouldUse = apistyle == null || apistyle.Value == "classic";
4011                         if (l != null && v != null && shouldUse)
4012                                 MemberSignatures [l.Value] = v.Value;
4013                 }
4014                 MemberType = node.SelectSingleNode ("MemberType").InnerText;
4015                 XmlNode rt = node.SelectSingleNode ("ReturnValue/ReturnType[not(@apistyle) or @apistyle='classic']");
4016                 if (rt != null)
4017                         ReturnType = rt.InnerText;
4018                 XmlNodeList p = node.SelectNodes ("Parameters/Parameter[not(@apistyle) or @apistyle='classic']");
4019                 if (p.Count > 0) {
4020                         Parameters = new StringList (p.Count);
4021                         for (int i = 0; i < p.Count; ++i)
4022                                 Parameters.Add (p [i].Attributes ["Type"].Value);
4023                 }
4024                 XmlNodeList tp = node.SelectNodes ("TypeParameters/TypeParameter[not(@apistyle) or @apistyle='classic']");
4025                 if (tp.Count > 0) {
4026                         TypeParameters = new StringList (tp.Count);
4027                         for (int i = 0; i < tp.Count; ++i)
4028                                 TypeParameters.Add (tp [i].Attributes ["Name"].Value);
4029                 }
4030                 else {
4031                         DiscernTypeParameters ();
4032                 }
4033         }
4034
4035         void DiscernTypeParameters ()
4036         {
4037                 // see if we can discern the param list from the name
4038                 if (MemberName.Contains ("<") && MemberName.EndsWith (">")) {
4039                         var starti = MemberName.IndexOf ("<") + 1;
4040                         var endi = MemberName.LastIndexOf (">");
4041                         var paramlist = MemberName.Substring (starti, endi - starti);
4042                         var tparams = paramlist.Split (new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
4043                         TypeParameters = new StringList (tparams);
4044                 }
4045         }
4046 }
4047
4048 public class DynamicParserContext {
4049         public ReadOnlyCollection<bool> TransformFlags;
4050         public int TransformIndex;
4051
4052         public DynamicParserContext (ICustomAttributeProvider provider)
4053         {
4054                 CustomAttribute da;
4055                 if (provider.HasCustomAttributes &&
4056                                 (da = (provider.CustomAttributes.Cast<CustomAttribute>()
4057                                         .SingleOrDefault (ca => ca.GetDeclaringType() == "System.Runtime.CompilerServices.DynamicAttribute"))) != null) {
4058                         CustomAttributeArgument[] values = da.ConstructorArguments.Count == 0
4059                                 ? new CustomAttributeArgument [0]
4060                                 : (CustomAttributeArgument[]) da.ConstructorArguments [0].Value;
4061
4062                         TransformFlags = new ReadOnlyCollection<bool> (values.Select (t => (bool) t.Value).ToArray());
4063                 }
4064         }
4065 }
4066
4067 public enum MemberFormatterState {
4068         None,
4069         WithinGenericTypeParameters,
4070 }
4071
4072 public abstract class MemberFormatter {
4073
4074         public virtual string Language {
4075                 get {return "";}
4076         }
4077
4078         public string GetName (MemberReference member)
4079         {
4080                 return GetName (member, null);
4081         }
4082
4083         public virtual string GetName (MemberReference member, DynamicParserContext context)
4084         {
4085                 TypeReference type = member as TypeReference;
4086                 if (type != null)
4087                         return GetTypeName (type, context);
4088                 MethodReference method  = member as MethodReference;
4089                 if (method != null && method.Name == ".ctor") // method.IsConstructor
4090                         return GetConstructorName (method);
4091                 if (method != null)
4092                         return GetMethodName (method);
4093                 PropertyReference prop = member as PropertyReference;
4094                 if (prop != null)
4095                         return GetPropertyName (prop);
4096                 FieldReference field = member as FieldReference;
4097                 if (field != null)
4098                         return GetFieldName (field);
4099                 EventReference e = member as EventReference;
4100                 if (e != null)
4101                         return GetEventName (e);
4102                 throw new NotSupportedException ("Can't handle: " +
4103                                         (member == null ? "null" : member.GetType().ToString()));
4104         }
4105
4106         protected virtual string GetTypeName (TypeReference type)
4107         {
4108                 return GetTypeName (type, null);
4109         }
4110
4111         protected virtual string GetTypeName (TypeReference type, DynamicParserContext context)
4112         {
4113                 if (type == null)
4114                         throw new ArgumentNullException ("type");
4115                 return _AppendTypeName (new StringBuilder (type.Name.Length), type, context).ToString ();
4116         }
4117
4118         protected virtual char[] ArrayDelimeters {
4119                 get {return new char[]{'[', ']'};}
4120         }
4121
4122         protected virtual MemberFormatterState MemberFormatterState { get; set; }
4123
4124         protected StringBuilder _AppendTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context)
4125         {
4126                 if (type is ArrayType) {
4127                         TypeSpecification spec = type as TypeSpecification;
4128                         _AppendTypeName (buf, spec != null ? spec.ElementType : type.GetElementType (), context);
4129                         return AppendArrayModifiers (buf, (ArrayType) type);
4130                 }
4131                 if (type is ByReferenceType) {
4132                         return AppendRefTypeName (buf, type, context);
4133                 }
4134                 if (type is PointerType) {
4135                         return AppendPointerTypeName (buf, type, context);
4136                 }
4137                 if (type is GenericParameter) {
4138                         return AppendTypeName (buf, type, context);
4139                 }
4140                 AppendNamespace (buf, type);
4141                 GenericInstanceType genInst = type as GenericInstanceType;
4142                 if (type.GenericParameters.Count == 0 &&
4143                                 (genInst == null ? true : genInst.GenericArguments.Count == 0)) {
4144                         return AppendFullTypeName (buf, type, context);
4145                 }
4146                 return AppendGenericType (buf, type, context);
4147         }
4148
4149         protected virtual StringBuilder AppendNamespace (StringBuilder buf, TypeReference type)
4150         {
4151                 string ns = DocUtils.GetNamespace (type);
4152                 if (ns != null && ns.Length > 0)
4153                         buf.Append (ns).Append ('.');
4154                 return buf;
4155         }
4156
4157         protected virtual StringBuilder AppendFullTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context)
4158         {
4159                 if (type.DeclaringType != null)
4160                         AppendFullTypeName (buf, type.DeclaringType, context).Append (NestedTypeSeparator);
4161                 return AppendTypeName (buf, type, context);
4162         }
4163
4164         protected virtual StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context)
4165         {
4166                 if (context != null)
4167                         context.TransformIndex++;
4168                 return AppendTypeName (buf, type.Name);
4169         }
4170
4171         protected virtual StringBuilder AppendTypeName (StringBuilder buf, string typename)
4172         {
4173                 int n = typename.IndexOf ("`");
4174                 if (n >= 0)
4175                         return buf.Append (typename.Substring (0, n));
4176                 return buf.Append (typename);
4177         }
4178
4179         protected virtual StringBuilder AppendArrayModifiers (StringBuilder buf, ArrayType array)
4180         {
4181                 buf.Append (ArrayDelimeters [0]);
4182                 int rank = array.Rank;
4183                 if (rank > 1)
4184                         buf.Append (new string (',', rank-1));
4185                 return buf.Append (ArrayDelimeters [1]);
4186         }
4187
4188         protected virtual string RefTypeModifier {
4189                 get {return "@";}
4190         }
4191
4192         protected virtual StringBuilder AppendRefTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context)
4193         {
4194                 TypeSpecification spec = type as TypeSpecification;
4195                 return _AppendTypeName (buf, spec != null ? spec.ElementType : type.GetElementType (), context)
4196                                 .Append (RefTypeModifier);
4197         }
4198
4199         protected virtual string PointerModifier {
4200                 get {return "*";}
4201         }
4202
4203         protected virtual StringBuilder AppendPointerTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context)
4204         {
4205                 TypeSpecification spec = type as TypeSpecification;
4206                 return _AppendTypeName (buf, spec != null ? spec.ElementType : type.GetElementType (), context)
4207                                 .Append (PointerModifier);
4208         }
4209
4210         protected virtual char[] GenericTypeContainer {
4211                 get {return new char[]{'<', '>'};}
4212         }
4213
4214         protected virtual char NestedTypeSeparator {
4215                 get {return '.';}
4216         }
4217
4218         protected virtual StringBuilder AppendGenericType (StringBuilder buf, TypeReference type, DynamicParserContext context)
4219         {
4220                 List<TypeReference> decls = DocUtils.GetDeclaringTypes (
4221                                 type is GenericInstanceType ? type.GetElementType () : type);
4222                 List<TypeReference> genArgs = GetGenericArguments (type);
4223                 int argIdx = 0;
4224                 int prev = 0;
4225                 bool insertNested = false;
4226                 foreach (var decl in decls) {
4227                         TypeReference declDef = decl.Resolve () ?? decl;
4228                         if (insertNested) {
4229                                 buf.Append (NestedTypeSeparator);
4230                         }
4231                         insertNested = true;
4232                         AppendTypeName (buf, declDef, context);
4233                         int ac = DocUtils.GetGenericArgumentCount (declDef);
4234                         int c = ac - prev;
4235                         prev = ac;
4236                         if (c > 0) {
4237                                 buf.Append (GenericTypeContainer [0]);
4238                                 var origState = MemberFormatterState;
4239                                 MemberFormatterState = MemberFormatterState.WithinGenericTypeParameters;
4240                                 _AppendTypeName (buf, genArgs [argIdx++], context);
4241                                 for (int i = 1; i < c; ++i) {
4242                                         _AppendTypeName (buf.Append (","), genArgs [argIdx++], context);
4243                                 }
4244                                 MemberFormatterState = origState;
4245                                 buf.Append (GenericTypeContainer [1]);
4246                         }
4247                 }
4248                 return buf;
4249         }
4250
4251         protected List<TypeReference> GetGenericArguments (TypeReference type)
4252         {
4253                 var args = new List<TypeReference> ();
4254                 GenericInstanceType inst = type as GenericInstanceType;
4255                 if (inst != null)
4256                         args.AddRange (inst.GenericArguments.Cast<TypeReference> ());
4257                 else
4258                         args.AddRange (type.GenericParameters.Cast<TypeReference> ());
4259                 return args;
4260         }
4261
4262         protected virtual StringBuilder AppendGenericTypeConstraints (StringBuilder buf, TypeReference type)
4263         {
4264                 return buf;
4265         }
4266
4267         protected virtual string GetConstructorName (MethodReference constructor)
4268         {
4269                 return constructor.Name;
4270         }
4271
4272         protected virtual string GetMethodName (MethodReference method)
4273         {
4274                 return method.Name;
4275         }
4276
4277         protected virtual string GetPropertyName (PropertyReference property)
4278         {
4279                 return property.Name;
4280         }
4281
4282         protected virtual string GetFieldName (FieldReference field)
4283         {
4284                 return field.Name;
4285         }
4286
4287         protected virtual string GetEventName (EventReference e)
4288         {
4289                 return e.Name;
4290         }
4291
4292         public virtual string GetDeclaration (MemberReference member)
4293         {
4294                 if (member == null)
4295                         throw new ArgumentNullException ("member");
4296                 TypeDefinition type = member as TypeDefinition;
4297                 if (type != null)
4298                         return GetTypeDeclaration (type);
4299                 MethodDefinition method = member as MethodDefinition;
4300                 if (method != null && method.IsConstructor)
4301                         return GetConstructorDeclaration (method);
4302                 if (method != null)
4303                         return GetMethodDeclaration (method);
4304                 PropertyDefinition prop = member as PropertyDefinition;
4305                 if (prop != null)
4306                         return GetPropertyDeclaration (prop);
4307                 FieldDefinition field = member as FieldDefinition;
4308                 if (field != null)
4309                         return GetFieldDeclaration (field);
4310                 EventDefinition e = member as EventDefinition;
4311                 if (e != null)
4312                         return GetEventDeclaration (e);
4313                 throw new NotSupportedException ("Can't handle: " + member.GetType().ToString());
4314         }
4315
4316         protected virtual string GetTypeDeclaration (TypeDefinition type)
4317         {
4318                 if (type == null)
4319                         throw new ArgumentNullException ("type");
4320                 StringBuilder buf = new StringBuilder (type.Name.Length);
4321                 _AppendTypeName (buf, type, null);
4322                 AppendGenericTypeConstraints (buf, type);
4323                 return buf.ToString ();
4324         }
4325
4326         protected virtual string GetConstructorDeclaration (MethodDefinition constructor)
4327         {
4328                 return GetConstructorName (constructor);
4329         }
4330
4331         protected virtual string GetMethodDeclaration (MethodDefinition method)
4332         {
4333                 if (method.HasCustomAttributes && method.CustomAttributes.Cast<CustomAttribute>().Any(
4334                                         ca => ca.GetDeclaringType() == "System.Diagnostics.Contracts.ContractInvariantMethodAttribute"))
4335                         return null;
4336
4337                 // Special signature for destructors.
4338                 if (method.Name == "Finalize" && method.Parameters.Count == 0)
4339                         return GetFinalizerName (method);
4340
4341                 StringBuilder buf = new StringBuilder ();
4342
4343                 AppendVisibility (buf, method);
4344                 if (buf.Length == 0 && 
4345                                 !(DocUtils.IsExplicitlyImplemented (method) && !method.IsSpecialName))
4346                         return null;
4347
4348                 AppendModifiers (buf, method);
4349
4350                 if (buf.Length != 0)
4351                         buf.Append (" ");
4352                 buf.Append (GetTypeName (method.ReturnType, new DynamicParserContext (method.MethodReturnType))).Append (" ");
4353
4354                 AppendMethodName (buf, method);
4355                 AppendGenericMethod (buf, method).Append (" ");
4356                 AppendParameters (buf, method, method.Parameters);
4357                 AppendGenericMethodConstraints (buf, method);
4358                 return buf.ToString ();
4359         }
4360
4361         protected virtual StringBuilder AppendMethodName (StringBuilder buf, MethodDefinition method)
4362         {
4363                 return buf.Append (method.Name);
4364         }
4365
4366         protected virtual string GetFinalizerName (MethodDefinition method)
4367         {
4368                 return "Finalize";
4369         }
4370
4371         protected virtual StringBuilder AppendVisibility (StringBuilder buf, MethodDefinition method)
4372         {
4373                 return buf;
4374         }
4375
4376         protected virtual StringBuilder AppendModifiers (StringBuilder buf, MethodDefinition method)
4377         {
4378                 return buf;
4379         }
4380
4381         protected virtual StringBuilder AppendGenericMethod (StringBuilder buf, MethodDefinition method)
4382         {
4383                 return buf;
4384         }
4385
4386         protected virtual StringBuilder AppendParameters (StringBuilder buf, MethodDefinition method, IList<ParameterDefinition> parameters)
4387         {
4388                 return buf;
4389         }
4390
4391         protected virtual StringBuilder AppendGenericMethodConstraints (StringBuilder buf, MethodDefinition method)
4392         {
4393                 return buf;
4394         }
4395
4396         protected virtual string GetPropertyDeclaration (PropertyDefinition property)
4397         {
4398                 return GetPropertyName (property);
4399         }
4400
4401         protected virtual string GetFieldDeclaration (FieldDefinition field)
4402         {
4403                 return GetFieldName (field);
4404         }
4405
4406         protected virtual string GetEventDeclaration (EventDefinition e)
4407         {
4408                 return GetEventName (e);
4409         }
4410 }
4411
4412 class ILFullMemberFormatter : MemberFormatter {
4413
4414         public override string Language {
4415                 get {return "ILAsm";}
4416         }
4417
4418         protected override char NestedTypeSeparator {
4419                 get {
4420                         return '/';
4421                 }
4422         }
4423
4424         protected override StringBuilder AppendNamespace (StringBuilder buf, TypeReference type)
4425         {
4426                 if (GetBuiltinType (type.FullName) != null)
4427                         return buf;
4428                 string ns = DocUtils.GetNamespace (type);
4429                 if (ns != null && ns.Length > 0) {
4430                         if (type.IsValueType)
4431                                 buf.Append ("valuetype ");
4432                         else
4433                                 buf.Append ("class ");
4434                         buf.Append (ns).Append ('.');
4435                 }
4436                 return buf;
4437         }
4438
4439         protected static string GetBuiltinType (string t)
4440         {
4441                 switch (t) {
4442                 case "System.Byte":    return "unsigned int8";
4443                 case "System.SByte":   return "int8";
4444                 case "System.Int16":   return "int16";
4445                 case "System.Int32":   return "int32";
4446                 case "System.Int64":   return "int64";
4447                 case "System.IntPtr":  return "native int";
4448
4449                 case "System.UInt16":  return "unsigned int16";
4450                 case "System.UInt32":  return "unsigned int32";
4451                 case "System.UInt64":  return "unsigned int64";
4452                 case "System.UIntPtr": return "native unsigned int";
4453
4454                 case "System.Single":  return "float32";
4455                 case "System.Double":  return "float64";
4456                 case "System.Boolean": return "bool";
4457                 case "System.Char":    return "char";
4458                 case "System.Void":    return "void";
4459                 case "System.String":  return "string";
4460                 case "System.Object":  return "object";
4461                 }
4462                 return null;
4463         }
4464
4465         protected override StringBuilder AppendTypeName (StringBuilder buf, string typename)
4466         {
4467                 return buf.Append (typename);
4468         }
4469
4470         protected override StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context)
4471         {
4472                 if (type is GenericParameter) {
4473                         AppendGenericParameterConstraints (buf, (GenericParameter) type).Append (type.Name);
4474                         return buf;
4475                 }
4476
4477                 string s = GetBuiltinType (type.FullName);
4478                 if (s != null) {
4479                         return buf.Append (s);
4480                 }
4481                 return base.AppendTypeName (buf, type, context);
4482         }
4483
4484         private StringBuilder AppendGenericParameterConstraints (StringBuilder buf, GenericParameter type)
4485         {
4486                 if (MemberFormatterState != MemberFormatterState.WithinGenericTypeParameters) {
4487                         return buf.Append (type.Owner is TypeReference ? "!" : "!!");
4488                 }
4489                 GenericParameterAttributes attrs = type.Attributes;
4490                 if ((attrs & GenericParameterAttributes.ReferenceTypeConstraint) != 0)
4491                         buf.Append ("class ");
4492                 if ((attrs & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0)
4493                         buf.Append ("struct ");
4494                 if ((attrs & GenericParameterAttributes.DefaultConstructorConstraint) != 0)
4495                         buf.Append (".ctor ");
4496                 IList<TypeReference> constraints = type.Constraints;
4497                 MemberFormatterState = 0;
4498                 if (constraints.Count > 0) {
4499                         var full = new ILFullMemberFormatter ();
4500                         buf.Append ("(").Append (full.GetName (constraints [0]));
4501                         for (int i = 1; i < constraints.Count; ++i) {
4502                                 buf.Append (", ").Append (full.GetName (constraints [i]));
4503                         }
4504                         buf.Append (") ");
4505                 }
4506                 MemberFormatterState = MemberFormatterState.WithinGenericTypeParameters;
4507
4508                 if ((attrs & GenericParameterAttributes.Covariant) != 0)
4509                         buf.Append ("+ ");
4510                 if ((attrs & GenericParameterAttributes.Contravariant) != 0)
4511                         buf.Append ("- ");
4512                 return buf;
4513         }
4514
4515         protected override string GetTypeDeclaration (TypeDefinition type)
4516         {
4517                 string visibility = GetTypeVisibility (type.Attributes);
4518                 if (visibility == null)
4519                         return null;
4520
4521                 StringBuilder buf = new StringBuilder ();
4522
4523                 buf.Append (".class ");
4524                 if (type.IsNested)
4525                         buf.Append ("nested ");
4526                 buf.Append (visibility).Append (" ");
4527                 if (type.IsInterface)
4528                         buf.Append ("interface ");
4529                 if (type.IsSequentialLayout)
4530                         buf.Append ("sequential ");
4531                 if (type.IsAutoLayout)
4532                         buf.Append ("auto ");
4533                 if (type.IsAnsiClass)
4534                         buf.Append ("ansi ");
4535                 if (type.IsAbstract)
4536                         buf.Append ("abstract ");
4537                 if (type.IsSerializable)
4538                         buf.Append ("serializable ");
4539                 if (type.IsSealed)
4540                         buf.Append ("sealed ");
4541                 if (type.IsBeforeFieldInit)
4542                         buf.Append ("beforefieldinit ");
4543                 var state = MemberFormatterState;
4544                 MemberFormatterState = MemberFormatterState.WithinGenericTypeParameters;
4545                 buf.Append (GetName (type));
4546                 MemberFormatterState = state;
4547                 var full = new ILFullMemberFormatter ();
4548                 if (type.BaseType != null) {
4549                         buf.Append (" extends ");
4550                         if (type.BaseType.FullName == "System.Object")
4551                                 buf.Append ("System.Object");
4552                         else
4553                                 buf.Append (full.GetName (type.BaseType).Substring ("class ".Length));
4554                 }
4555                 bool first = true;
4556                 foreach (var name in type.Interfaces.Where (i => MDocUpdater.IsPublic (i.Resolve ()))
4557                                 .Select (i => full.GetName (i))
4558                                 .OrderBy (n => n)) {
4559                         if (first) {
4560                                 buf.Append (" implements ");
4561                                 first = false;
4562                         }
4563                         else {
4564                                 buf.Append (", ");
4565                         }
4566                         buf.Append (name);
4567                 }
4568
4569                 return buf.ToString ();
4570         }
4571
4572         protected override StringBuilder AppendGenericType (StringBuilder buf, TypeReference type, DynamicParserContext context)
4573         {
4574                 List<TypeReference> decls = DocUtils.GetDeclaringTypes (
4575                                 type is GenericInstanceType ? type.GetElementType () : type);
4576                 bool first = true;
4577                 foreach (var decl in decls) {
4578                         TypeReference declDef = decl.Resolve () ?? decl;
4579                         if (!first) {
4580                                 buf.Append (NestedTypeSeparator);
4581                         }
4582                         first = false;
4583                         AppendTypeName (buf, declDef, context);
4584                 }
4585                 buf.Append ('<');
4586                 first = true;
4587                 foreach (TypeReference arg in GetGenericArguments (type)) {
4588                         if (!first)
4589                                 buf.Append (", ");
4590                         first = false;
4591                         _AppendTypeName (buf, arg, context);
4592                 }
4593                 buf.Append ('>');
4594                 return buf;
4595         }
4596
4597         static string GetTypeVisibility (TypeAttributes ta)
4598         {
4599                 switch (ta & TypeAttributes.VisibilityMask) {
4600                 case TypeAttributes.Public:
4601                 case TypeAttributes.NestedPublic:
4602                         return "public";
4603
4604                 case TypeAttributes.NestedFamily:
4605                 case TypeAttributes.NestedFamORAssem:
4606                         return "protected";
4607
4608                 default:
4609                         return null;
4610                 }
4611         }
4612
4613         protected override string GetConstructorDeclaration (MethodDefinition constructor)
4614         {
4615                 return GetMethodDeclaration (constructor);
4616         }
4617
4618         protected override string GetMethodDeclaration (MethodDefinition method)
4619         {
4620                 if (method.IsPrivate && !DocUtils.IsExplicitlyImplemented (method))
4621                         return null;
4622
4623                 var buf = new StringBuilder ();
4624                 buf.Append (".method ");
4625                 AppendVisibility (buf, method);
4626                 if (method.IsStatic)
4627                         buf.Append ("static ");
4628                 if (method.IsHideBySig)
4629                         buf.Append ("hidebysig ");
4630                 if (method.IsPInvokeImpl) {
4631                         var info = method.PInvokeInfo;
4632                         buf.Append ("pinvokeimpl (\"")
4633                                 .Append (info.Module.Name)
4634                                 .Append ("\" as \"")
4635                                 .Append (info.EntryPoint)
4636                                 .Append ("\"");
4637                         if (info.IsCharSetAuto)
4638                                 buf.Append (" auto");
4639                         if (info.IsCharSetUnicode)
4640                                 buf.Append (" unicode");
4641                         if (info.IsCharSetAnsi)
4642                                 buf.Append (" ansi");
4643                         if (info.IsCallConvCdecl)
4644                                 buf.Append (" cdecl");
4645                         if (info.IsCallConvStdCall)
4646                                 buf.Append (" stdcall");
4647                         if (info.IsCallConvWinapi)
4648                                 buf.Append (" winapi");
4649                         if (info.IsCallConvThiscall)
4650                                 buf.Append (" thiscall");
4651                         if (info.SupportsLastError)
4652                                 buf.Append (" lasterr");
4653                         buf.Append (")");
4654                 }
4655                 if (method.IsSpecialName)
4656                         buf.Append ("specialname ");
4657                 if (method.IsRuntimeSpecialName)
4658                         buf.Append ("rtspecialname ");
4659                 if (method.IsNewSlot)
4660                         buf.Append ("newslot ");
4661                 if (method.IsVirtual)
4662                         buf.Append ("virtual ");
4663                 if (!method.IsStatic)
4664                         buf.Append ("instance ");
4665                 _AppendTypeName (buf, method.ReturnType, new DynamicParserContext (method.MethodReturnType));
4666                 buf.Append (' ')
4667                         .Append (method.Name);
4668                 if (method.IsGenericMethod ()) {
4669                         var state = MemberFormatterState;
4670                         MemberFormatterState = MemberFormatterState.WithinGenericTypeParameters;
4671                         IList<GenericParameter> args = method.GenericParameters;
4672                         if (args.Count > 0) {
4673                                 buf.Append ("<");
4674                                 _AppendTypeName (buf, args [0], null);
4675                                 for (int i = 1; i < args.Count; ++i)
4676                                         _AppendTypeName (buf.Append (", "), args [i], null);
4677                                 buf.Append (">");
4678                         }
4679                         MemberFormatterState = state;
4680                 }
4681
4682                 buf.Append ('(');
4683                 bool first = true;
4684                 for (int i = 0; i < method.Parameters.Count; ++i) {
4685                         if (!first)
4686                                 buf.Append (", ");
4687                         first = false;
4688                         _AppendTypeName (buf, method.Parameters [i].ParameterType, new DynamicParserContext (method.Parameters [i]));
4689                         buf.Append (' ');
4690                         buf.Append (method.Parameters [i].Name);
4691                 }
4692                 buf.Append (')');
4693                 if (method.IsIL)
4694                         buf.Append (" cil");
4695                 if (method.IsRuntime)
4696                         buf.Append (" runtime");
4697                 if (method.IsManaged)
4698                         buf.Append (" managed");
4699
4700                 return buf.ToString ();
4701         }
4702
4703         protected override StringBuilder AppendMethodName (StringBuilder buf, MethodDefinition method)
4704         {
4705                 if (DocUtils.IsExplicitlyImplemented (method)) {
4706                         TypeReference iface;
4707                         MethodReference ifaceMethod;
4708                         DocUtils.GetInfoForExplicitlyImplementedMethod (method, out iface, out ifaceMethod);
4709                         return buf.Append (new CSharpMemberFormatter ().GetName (iface))
4710                                 .Append ('.')
4711                                 .Append (ifaceMethod.Name);
4712                 }
4713                 return base.AppendMethodName (buf, method);
4714         }
4715
4716         protected override string RefTypeModifier {
4717                 get {return "";}
4718         }
4719
4720         protected override StringBuilder AppendVisibility (StringBuilder buf, MethodDefinition method)
4721         {
4722                 if (method.IsPublic)
4723                         return buf.Append ("public ");
4724                 if (method.IsFamilyAndAssembly)
4725                         return buf.Append ("familyandassembly");
4726                 if (method.IsFamilyOrAssembly)
4727                         return buf.Append ("familyorassembly");
4728                 if (method.IsFamily)
4729                         return buf.Append ("family");
4730                 return buf;
4731         }
4732
4733         protected override StringBuilder AppendModifiers (StringBuilder buf, MethodDefinition method)
4734         {
4735                 string modifiers = String.Empty;
4736                 if (method.IsStatic) modifiers += " static";
4737                 if (method.IsVirtual && !method.IsAbstract) {
4738                         if ((method.Attributes & MethodAttributes.NewSlot) != 0) modifiers += " virtual";
4739                         else modifiers += " override";
4740                 }
4741                 TypeDefinition declType = (TypeDefinition) method.DeclaringType;
4742                 if (method.IsAbstract && !declType.IsInterface) modifiers += " abstract";
4743                 if (method.IsFinal) modifiers += " sealed";
4744                 if (modifiers == " virtual sealed") modifiers = "";
4745
4746                 return buf.Append (modifiers);
4747         }
4748
4749         protected override StringBuilder AppendGenericMethod (StringBuilder buf, MethodDefinition method)
4750         {
4751                 if (method.IsGenericMethod ()) {
4752                         IList<GenericParameter> args = method.GenericParameters;
4753                         if (args.Count > 0) {
4754                                 buf.Append ("<");
4755                                 buf.Append (args [0].Name);
4756                                 for (int i = 1; i < args.Count; ++i)
4757                                         buf.Append (",").Append (args [i].Name);
4758                                 buf.Append (">");
4759                         }
4760                 }
4761                 return buf;
4762         }
4763
4764         protected override StringBuilder AppendParameters (StringBuilder buf, MethodDefinition method, IList<ParameterDefinition> parameters)
4765         {
4766                 return AppendParameters (buf, method, parameters, '(', ')');
4767         }
4768
4769         private StringBuilder AppendParameters (StringBuilder buf, MethodDefinition method, IList<ParameterDefinition> parameters, char begin, char end)
4770         {
4771                 buf.Append (begin);
4772
4773                 if (parameters.Count > 0) {
4774                         if (DocUtils.IsExtensionMethod (method))
4775                                 buf.Append ("this ");
4776                         AppendParameter (buf, parameters [0]);
4777                         for (int i = 1; i < parameters.Count; ++i) {
4778                                 buf.Append (", ");
4779                                 AppendParameter (buf, parameters [i]);
4780                         }
4781                 }
4782
4783                 return buf.Append (end);
4784         }
4785
4786         private StringBuilder AppendParameter (StringBuilder buf, ParameterDefinition parameter)
4787         {
4788                 if (parameter.ParameterType is ByReferenceType) {
4789                         if (parameter.IsOut)
4790                                 buf.Append ("out ");
4791                         else
4792                                 buf.Append ("ref ");
4793                 }
4794                 buf.Append (GetName (parameter.ParameterType)).Append (" ");
4795                 return buf.Append (parameter.Name);
4796         }
4797
4798         protected override string GetPropertyDeclaration (PropertyDefinition property)
4799         {
4800                 MethodDefinition gm = null, sm = null;
4801
4802                 string get_visible = null;
4803                 if ((gm = property.GetMethod) != null &&
4804                                 (DocUtils.IsExplicitlyImplemented (gm) ||
4805                                  (!gm.IsPrivate && !gm.IsAssembly && !gm.IsFamilyAndAssembly)))
4806                         get_visible = AppendVisibility (new StringBuilder (), gm).ToString ();
4807                 string set_visible = null;
4808                 if ((sm = property.SetMethod) != null &&
4809                                 (DocUtils.IsExplicitlyImplemented (sm) ||
4810                                  (!sm.IsPrivate && !sm.IsAssembly && !sm.IsFamilyAndAssembly)))
4811                         set_visible = AppendVisibility (new StringBuilder (), sm).ToString ();
4812
4813                 if ((set_visible == null) && (get_visible == null))
4814                         return null;
4815
4816                 StringBuilder buf = new StringBuilder ()
4817                         .Append (".property ");
4818                 if (!(gm ?? sm).IsStatic)
4819                         buf.Append ("instance ");
4820                 _AppendTypeName (buf, property.PropertyType, new DynamicParserContext (property));
4821                 buf.Append (' ').Append (property.Name);
4822                 if (!property.HasParameters || property.Parameters.Count == 0)
4823                         return buf.ToString ();
4824
4825                 buf.Append ('(');
4826                 bool first = true;
4827                 foreach (ParameterDefinition p in property.Parameters) {
4828                         if (!first)
4829                                 buf.Append (", ");
4830                         first = false;
4831                         _AppendTypeName (buf, p.ParameterType, new DynamicParserContext (p));
4832                 }
4833                 buf.Append (')');
4834
4835                 return buf.ToString ();
4836         }
4837
4838         protected override string GetFieldDeclaration (FieldDefinition field)
4839         {
4840                 TypeDefinition declType = (TypeDefinition) field.DeclaringType;
4841                 if (declType.IsEnum && field.Name == "value__")
4842                         return null; // This member of enums aren't documented.
4843
4844                 StringBuilder buf = new StringBuilder ();
4845                 AppendFieldVisibility (buf, field);
4846                 if (buf.Length == 0)
4847                         return null;
4848
4849                 buf.Insert (0, ".field ");
4850
4851                 if (field.IsStatic)
4852                         buf.Append ("static ");
4853                 if (field.IsInitOnly)
4854                         buf.Append ("initonly ");
4855                 if (field.IsLiteral)
4856                         buf.Append ("literal ");
4857                 _AppendTypeName (buf, field.FieldType, new DynamicParserContext (field));
4858                 buf.Append (' ').Append (field.Name);
4859                 AppendFieldValue (buf, field);
4860
4861                 return buf.ToString ();
4862         }
4863
4864         static StringBuilder AppendFieldVisibility (StringBuilder buf, FieldDefinition field)
4865         {
4866                 if (field.IsPublic)
4867                         return buf.Append ("public ");
4868                 if (field.IsFamilyAndAssembly)
4869                         return buf.Append ("familyandassembly ");
4870                 if (field.IsFamilyOrAssembly)
4871                         return buf.Append ("familyorassembly ");
4872                 if (field.IsFamily)
4873                         return buf.Append ("family ");
4874                 return buf;
4875         }
4876
4877         static StringBuilder AppendFieldValue (StringBuilder buf, FieldDefinition field)
4878         {
4879                 // enums have a value__ field, which we ignore
4880                 if (field.DeclaringType.IsGenericType ())
4881                         return buf;
4882                 if (field.HasConstant && field.IsLiteral) {
4883                         object val = null;
4884                         try {
4885                                 val   = field.Constant;
4886                         } catch {
4887                                 return buf;
4888                         }
4889                         if (val == null)
4890                                 buf.Append (" = ").Append ("null");
4891                         else if (val is Enum)
4892                                 buf.Append (" = ")
4893                                         .Append (GetBuiltinType (field.DeclaringType.GetUnderlyingType ().FullName))
4894                                         .Append ('(')
4895                                         .Append (val.ToString ())
4896                                         .Append (')');
4897                         else if (val is IFormattable) {
4898                                 string value = ((IFormattable)val).ToString();
4899                                 buf.Append (" = ");
4900                                 if (val is string)
4901                                         buf.Append ("\"" + value + "\"");
4902                                 else
4903                                         buf.Append (GetBuiltinType (field.DeclaringType.GetUnderlyingType ().FullName))
4904                                                 .Append ('(')
4905                                                 .Append (value)
4906                                                 .Append (')');
4907                         }
4908                 }
4909                 return buf;
4910         }
4911
4912         protected override string GetEventDeclaration (EventDefinition e)
4913         {
4914                 StringBuilder buf = new StringBuilder ();
4915                 if (AppendVisibility (buf, e.AddMethod).Length == 0) {
4916                         return null;
4917                 }
4918
4919                 buf.Length = 0;
4920                 buf.Append (".event ")
4921                         .Append (GetName (e.EventType))
4922                         .Append (' ')
4923                         .Append (e.Name);
4924
4925                 return buf.ToString ();
4926         }
4927 }
4928
4929 class ILMemberFormatter : ILFullMemberFormatter {
4930         protected override StringBuilder AppendNamespace (StringBuilder buf, TypeReference type)
4931         {
4932                 return buf;
4933         }
4934 }
4935
4936         class ILNativeTypeMemberFormatter : ILFullMemberFormatter {
4937                 protected static string _GetBuiltinType (string t)
4938                 {
4939                         //string moddedType = base.GetBuiltinType (t);
4940                         return null;
4941                         //return moddedType;
4942                 }
4943         }
4944
4945         class CSharpNativeTypeMemberFormatter : CSharpFullMemberFormatter {
4946                 protected override string GetCSharpType (string t) {
4947                         string moddedType = base.GetCSharpType (t);
4948
4949                         switch (moddedType) {
4950                         case "int":             return "nint";
4951                         case "uint":
4952                                 return "nuint";
4953                         case "float":
4954                                 return "nfloat";
4955                         case "System.Drawing.SizeF":
4956                                 return "CoreGraphics.CGSize";
4957                         case "System.Drawing.PointF":
4958                                 return "CoreGraphics.CGPoint";
4959                         case "System.Drawing.RectangleF":
4960                                 return "CoreGraphics.CGPoint";
4961                         }
4962                         return null;
4963                 }
4964         }
4965
4966 class CSharpFullMemberFormatter : MemberFormatter {
4967
4968         public override string Language {
4969                 get {return "C#";}
4970         }
4971
4972         protected override StringBuilder AppendNamespace (StringBuilder buf, TypeReference type)
4973         {
4974
4975                 string ns = DocUtils.GetNamespace (type);
4976                 if (GetCSharpType (type.FullName) == null && ns != null && ns.Length > 0 && ns != "System")
4977                         buf.Append (ns).Append ('.');
4978                 return buf;
4979         }
4980
4981         protected virtual string GetCSharpType (string t)
4982         {
4983                 switch (t) {
4984                 case "System.Byte":    return "byte";
4985                 case "System.SByte":   return "sbyte";
4986                 case "System.Int16":   return "short";
4987                 case "System.Int32":   return "int";
4988                 case "System.Int64":   return "long";
4989
4990                 case "System.UInt16":  return "ushort";
4991                 case "System.UInt32":  return "uint";
4992                 case "System.UInt64":  return "ulong";
4993
4994                 case "System.Single":  return "float";
4995                 case "System.Double":  return "double";
4996                 case "System.Decimal": return "decimal";
4997                 case "System.Boolean": return "bool";
4998                 case "System.Char":    return "char";
4999                 case "System.Void":    return "void";
5000                 case "System.String":  return "string";
5001                 case "System.Object":  return "object";
5002                 }
5003                 return null;
5004         }
5005
5006         protected override StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context)
5007         {
5008                 if (context != null && context.TransformFlags != null &&
5009                                 (context.TransformFlags.Count == 0 || context.TransformFlags [context.TransformIndex])) {
5010                         context.TransformIndex++;
5011                         return buf.Append ("dynamic");
5012                 }
5013
5014                 if (type is GenericParameter)
5015                         return AppendGenericParameterConstraints (buf, (GenericParameter) type, context).Append (type.Name);
5016                 string t = type.FullName;
5017                 if (!t.StartsWith ("System.")) {
5018                         return base.AppendTypeName (buf, type, context);
5019                 }
5020
5021                 string s = GetCSharpType (t);
5022                 if (s != null) {
5023                         if (context != null)
5024                                 context.TransformIndex++;
5025                         return buf.Append (s);
5026                 }
5027                 
5028                 return base.AppendTypeName (buf, type, context);
5029         }
5030
5031         private StringBuilder AppendGenericParameterConstraints (StringBuilder buf, GenericParameter type, DynamicParserContext context)
5032         {
5033                 if (MemberFormatterState != MemberFormatterState.WithinGenericTypeParameters)
5034                         return buf;
5035                 GenericParameterAttributes attrs = type.Attributes;
5036                 bool isout = (attrs & GenericParameterAttributes.Covariant) != 0;
5037                 bool isin  = (attrs & GenericParameterAttributes.Contravariant) != 0;
5038                 if (isin)
5039                         buf.Append ("in ");
5040                 else if (isout)
5041                         buf.Append ("out ");
5042                 return buf;
5043         }
5044
5045         protected override string GetTypeDeclaration (TypeDefinition type)
5046         {
5047                 string visibility = GetTypeVisibility (type.Attributes);
5048                 if (visibility == null)
5049                         return null;
5050
5051                 StringBuilder buf = new StringBuilder ();
5052                 
5053                 buf.Append (visibility);
5054                 buf.Append (" ");
5055
5056                 MemberFormatter full = new CSharpFullMemberFormatter ();
5057
5058                 if (DocUtils.IsDelegate (type)) {
5059                         buf.Append("delegate ");
5060                         MethodDefinition invoke = type.GetMethod ("Invoke");
5061                         buf.Append (full.GetName (invoke.ReturnType, new DynamicParserContext (invoke.MethodReturnType))).Append (" ");
5062                         buf.Append (GetName (type));
5063                         AppendParameters (buf, invoke, invoke.Parameters);
5064                         AppendGenericTypeConstraints (buf, type);
5065                         buf.Append (";");
5066
5067                         return buf.ToString();
5068                 }
5069                 
5070                 if (type.IsAbstract && !type.IsInterface)
5071                         buf.Append("abstract ");
5072                 if (type.IsSealed && !DocUtils.IsDelegate (type) && !type.IsValueType)
5073                         buf.Append("sealed ");
5074                 buf.Replace ("abstract sealed", "static");
5075
5076                 buf.Append (GetTypeKind (type));
5077                 buf.Append (" ");
5078                 buf.Append (GetCSharpType (type.FullName) == null 
5079                                 ? GetName (type) 
5080                                 : type.Name);
5081
5082                 if (!type.IsEnum) {
5083                         TypeReference basetype = type.BaseType;
5084                         if (basetype != null && basetype.FullName == "System.Object" || type.IsValueType)       // FIXME
5085                                 basetype = null;
5086
5087                         List<string> interface_names = DocUtils.GetUserImplementedInterfaces (type)
5088                                         .Select (iface => full.GetName (iface))
5089                                         .OrderBy (s => s)
5090                                         .ToList ();
5091
5092                         if (basetype != null || interface_names.Count > 0)
5093                                 buf.Append (" : ");
5094                         
5095                         if (basetype != null) {
5096                                 buf.Append (full.GetName (basetype));
5097                                 if (interface_names.Count > 0)
5098                                         buf.Append (", ");
5099                         }
5100                         
5101                         for (int i = 0; i < interface_names.Count; i++){
5102                                 if (i != 0)
5103                                         buf.Append (", ");
5104                                 buf.Append (interface_names [i]);
5105                         }
5106                         AppendGenericTypeConstraints (buf, type);
5107                 }
5108
5109                 return buf.ToString ();
5110         }
5111
5112         static string GetTypeKind (TypeDefinition t)
5113         {
5114                 if (t.IsEnum)
5115                         return "enum";
5116                 if (t.IsValueType)
5117                         return "struct";
5118                 if (t.IsClass || t.FullName == "System.Enum")
5119                         return "class";
5120                 if (t.IsInterface)
5121                         return "interface";
5122                 throw new ArgumentException(t.FullName);
5123         }
5124
5125         static string GetTypeVisibility (TypeAttributes ta)
5126         {
5127                 switch (ta & TypeAttributes.VisibilityMask) {
5128                 case TypeAttributes.Public:
5129                 case TypeAttributes.NestedPublic:
5130                         return "public";
5131
5132                 case TypeAttributes.NestedFamily:
5133                 case TypeAttributes.NestedFamORAssem:
5134                         return "protected";
5135
5136                 default:
5137                         return null;
5138                 }
5139         }
5140
5141         protected override StringBuilder AppendGenericTypeConstraints (StringBuilder buf, TypeReference type)
5142         {
5143                 if (type.GenericParameters.Count == 0)
5144                         return buf;
5145                 return AppendConstraints (buf, type.GenericParameters);
5146         }
5147
5148         private StringBuilder AppendConstraints (StringBuilder buf, IList<GenericParameter> genArgs)
5149         {
5150                 foreach (GenericParameter genArg in genArgs) {
5151                         GenericParameterAttributes attrs = genArg.Attributes;
5152                         IList<TypeReference> constraints = genArg.Constraints;
5153                         if (attrs == GenericParameterAttributes.NonVariant && constraints.Count == 0)
5154                                 continue;
5155
5156                         bool isref = (attrs & GenericParameterAttributes.ReferenceTypeConstraint) != 0;
5157                         bool isvt  = (attrs & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0;
5158                         bool isnew = (attrs & GenericParameterAttributes.DefaultConstructorConstraint) != 0;
5159                         bool comma = false;
5160
5161                         if (!isref && !isvt && !isnew && constraints.Count == 0)
5162                                 continue;
5163                         buf.Append (" where ").Append (genArg.Name).Append (" : ");
5164                         if (isref) {
5165                                 buf.Append ("class");
5166                                 comma = true;
5167                         }
5168                         else if (isvt) {
5169                                 buf.Append ("struct");
5170                                 comma = true;
5171                         }
5172                         if (constraints.Count > 0 && !isvt) {
5173                                 if (comma)
5174                                         buf.Append (", ");
5175                                 buf.Append (GetTypeName (constraints [0]));
5176                                 for (int i = 1; i < constraints.Count; ++i)
5177                                         buf.Append (", ").Append (GetTypeName (constraints [i]));
5178                         }
5179                         if (isnew && !isvt) {
5180                                 if (comma)
5181                                         buf.Append (", ");
5182                                 buf.Append ("new()");
5183                         }
5184                 }
5185                 return buf;
5186         }
5187
5188         protected override string GetConstructorDeclaration (MethodDefinition constructor)
5189         {
5190                 StringBuilder buf = new StringBuilder ();
5191                 AppendVisibility (buf, constructor);
5192                 if (buf.Length == 0)
5193                         return null;
5194
5195                 buf.Append (' ');
5196                 base.AppendTypeName (buf, constructor.DeclaringType.Name).Append (' ');
5197                 AppendParameters (buf, constructor, constructor.Parameters);
5198                 buf.Append (';');
5199
5200                 return buf.ToString ();
5201         }
5202         
5203         protected override string GetMethodDeclaration (MethodDefinition method)
5204         {
5205                 string decl = base.GetMethodDeclaration (method);
5206                 if (decl != null)
5207                         return decl + ";";
5208                 return null;
5209         }
5210
5211         protected override StringBuilder AppendMethodName (StringBuilder buf, MethodDefinition method)
5212         {
5213                 if (DocUtils.IsExplicitlyImplemented (method)) {
5214                         TypeReference iface;
5215                         MethodReference ifaceMethod;
5216                         DocUtils.GetInfoForExplicitlyImplementedMethod (method, out iface, out ifaceMethod);
5217                         return buf.Append (new CSharpMemberFormatter ().GetName (iface))
5218                                 .Append ('.')
5219                                 .Append (ifaceMethod.Name);
5220                 }
5221                 return base.AppendMethodName (buf, method);
5222         }
5223
5224         protected override StringBuilder AppendGenericMethodConstraints (StringBuilder buf, MethodDefinition method)
5225         {
5226                 if (method.GenericParameters.Count == 0)
5227                         return buf;
5228                 return AppendConstraints (buf, method.GenericParameters);
5229         }
5230
5231         protected override string RefTypeModifier {
5232                 get {return "";}
5233         }
5234
5235         protected override string GetFinalizerName (MethodDefinition method)
5236         {
5237                 return "~" + method.DeclaringType.Name + " ()"; 
5238         }
5239
5240         protected override StringBuilder AppendVisibility (StringBuilder buf, MethodDefinition method)
5241         {
5242                 if (method == null)
5243                         return buf;
5244                 if (method.IsPublic)
5245                         return buf.Append ("public");
5246                 if (method.IsFamily || method.IsFamilyOrAssembly)
5247                         return buf.Append ("protected");
5248                 return buf;
5249         }
5250
5251         protected override StringBuilder AppendModifiers (StringBuilder buf, MethodDefinition method)
5252         {
5253                 string modifiers = String.Empty;
5254                 if (method.IsStatic) modifiers += " static";
5255                 if (method.IsVirtual && !method.IsAbstract) {
5256                         if ((method.Attributes & MethodAttributes.NewSlot) != 0) modifiers += " virtual";
5257                         else modifiers += " override";
5258                 }
5259                 TypeDefinition declType = (TypeDefinition) method.DeclaringType;
5260                 if (method.IsAbstract && !declType.IsInterface) modifiers += " abstract";
5261                 if (method.IsFinal) modifiers += " sealed";
5262                 if (modifiers == " virtual sealed") modifiers = "";
5263
5264                 return buf.Append (modifiers);
5265         }
5266
5267         protected override StringBuilder AppendGenericMethod (StringBuilder buf, MethodDefinition method)
5268         {
5269                 if (method.IsGenericMethod ()) {
5270                         IList<GenericParameter> args = method.GenericParameters;
5271                         if (args.Count > 0) {
5272                                 buf.Append ("<");
5273                                 buf.Append (args [0].Name);
5274                                 for (int i = 1; i < args.Count; ++i)
5275                                         buf.Append (",").Append (args [i].Name);
5276                                 buf.Append (">");
5277                         }
5278                 }
5279                 return buf;
5280         }
5281
5282         protected override StringBuilder AppendParameters (StringBuilder buf, MethodDefinition method, IList<ParameterDefinition> parameters)
5283         {
5284                 return AppendParameters (buf, method, parameters, '(', ')');
5285         }
5286
5287         private StringBuilder AppendParameters (StringBuilder buf, MethodDefinition method, IList<ParameterDefinition> parameters, char begin, char end)
5288         {
5289                 buf.Append (begin);
5290
5291                 if (parameters.Count > 0) {
5292                         if (DocUtils.IsExtensionMethod (method))
5293                                 buf.Append ("this ");
5294                         AppendParameter (buf, parameters [0]);
5295                         for (int i = 1; i < parameters.Count; ++i) {
5296                                 buf.Append (", ");
5297                                 AppendParameter (buf, parameters [i]);
5298                         }
5299                 }
5300
5301                 return buf.Append (end);
5302         }
5303
5304         private StringBuilder AppendParameter (StringBuilder buf, ParameterDefinition parameter)
5305         {
5306                 if (parameter.ParameterType is ByReferenceType) {
5307                         if (parameter.IsOut)
5308                                 buf.Append ("out ");
5309                         else
5310                                 buf.Append ("ref ");
5311                 }
5312                 buf.Append (GetTypeName (parameter.ParameterType, new DynamicParserContext (parameter))).Append (" ");
5313                 buf.Append (parameter.Name);
5314                 if (parameter.HasDefault && parameter.IsOptional && parameter.HasConstant) {
5315                         buf.AppendFormat (" = {0}", MDocUpdater.MakeAttributesValueString (parameter.Constant, parameter.ParameterType));
5316                 }
5317                 return buf;
5318         }
5319
5320         protected override string GetPropertyDeclaration (PropertyDefinition property)
5321         {
5322                 MethodDefinition method;
5323
5324                 string get_visible = null;
5325                 if ((method = property.GetMethod) != null && 
5326                                 (DocUtils.IsExplicitlyImplemented (method) || 
5327                                  (!method.IsPrivate && !method.IsAssembly && !method.IsFamilyAndAssembly)))
5328                         get_visible = AppendVisibility (new StringBuilder (), method).ToString ();
5329                 string set_visible = null;
5330                 if ((method = property.SetMethod) != null &&
5331                                 (DocUtils.IsExplicitlyImplemented (method) || 
5332                                  (!method.IsPrivate && !method.IsAssembly && !method.IsFamilyAndAssembly)))
5333                         set_visible = AppendVisibility (new StringBuilder (), method).ToString ();
5334
5335                 if ((set_visible == null) && (get_visible == null))
5336                         return null;
5337
5338                 string visibility;
5339                 StringBuilder buf = new StringBuilder ();
5340                 if (get_visible != null && (set_visible == null || (set_visible != null && get_visible == set_visible)))
5341                         buf.Append (visibility = get_visible);
5342                 else if (set_visible != null && get_visible == null)
5343                         buf.Append (visibility = set_visible);
5344                 else
5345                         buf.Append (visibility = "public");
5346
5347                 // Pick an accessor to use for static/virtual/override/etc. checks.
5348                 method = property.SetMethod;
5349                 if (method == null)
5350                         method = property.GetMethod;
5351         
5352                 string modifiers = String.Empty;
5353                 if (method.IsStatic) modifiers += " static";
5354                 if (method.IsVirtual && !method.IsAbstract) {
5355                                 if ((method.Attributes & MethodAttributes.NewSlot) != 0)
5356                                         modifiers += " virtual";
5357                                 else
5358                                         modifiers += " override";
5359                 }
5360                 TypeDefinition declDef = (TypeDefinition) method.DeclaringType;
5361                 if (method.IsAbstract && !declDef.IsInterface)
5362                         modifiers += " abstract";
5363                 if (method.IsFinal)
5364                         modifiers += " sealed";
5365                 if (modifiers == " virtual sealed")
5366                         modifiers = "";
5367                 buf.Append (modifiers).Append (' ');
5368
5369                 buf.Append (GetTypeName (property.PropertyType, new DynamicParserContext (property))).Append (' ');
5370
5371                 IEnumerable<MemberReference> defs = property.DeclaringType.GetDefaultMembers ();
5372                 string name = property.Name;
5373                 foreach (MemberReference mi in defs) {
5374                         if (mi == property) {
5375                                 name = "this";
5376                                 break;
5377                         }
5378                 }
5379                 buf.Append (name == "this" ? name : DocUtils.GetPropertyName (property));
5380         
5381                 if (property.Parameters.Count != 0) {
5382                         AppendParameters (buf, method, property.Parameters, '[', ']');
5383                 }
5384
5385                 buf.Append (" {");
5386                 if (get_visible != null) {
5387                         if (get_visible != visibility)
5388                                 buf.Append (' ').Append (get_visible);
5389                         buf.Append (" get;");
5390                 }
5391                 if (set_visible != null) {
5392                         if (set_visible != visibility)
5393                                 buf.Append (' ').Append (set_visible);
5394                         buf.Append (" set;");
5395                 }
5396                 buf.Append (" }");
5397         
5398                 return buf [0] != ' ' ? buf.ToString () : buf.ToString (1, buf.Length-1);
5399         }
5400
5401         protected override string GetFieldDeclaration (FieldDefinition field)
5402         {
5403                 TypeDefinition declType = (TypeDefinition) field.DeclaringType;
5404                 if (declType.IsEnum && field.Name == "value__")
5405                         return null; // This member of enums aren't documented.
5406
5407                 StringBuilder buf = new StringBuilder ();
5408                 AppendFieldVisibility (buf, field);
5409                 if (buf.Length == 0)
5410                         return null;
5411
5412                 if (declType.IsEnum)
5413                         return field.Name;
5414
5415                 if (field.IsStatic && !field.IsLiteral)
5416                         buf.Append (" static");
5417                 if (field.IsInitOnly)
5418                         buf.Append (" readonly");
5419                 if (field.IsLiteral)
5420                         buf.Append (" const");
5421
5422                 buf.Append (' ').Append (GetTypeName (field.FieldType, new DynamicParserContext (field))).Append (' ');
5423                 buf.Append (field.Name);
5424                 AppendFieldValue (buf, field);
5425                 buf.Append (';');
5426
5427                 return buf.ToString ();
5428         }
5429
5430         static StringBuilder AppendFieldVisibility (StringBuilder buf, FieldDefinition field)
5431         {
5432                 if (field.IsPublic)
5433                         return buf.Append ("public");
5434                 if (field.IsFamily || field.IsFamilyOrAssembly)
5435                         return buf.Append ("protected");
5436                 return buf;
5437         }
5438
5439         static StringBuilder AppendFieldValue (StringBuilder buf, FieldDefinition field)
5440         {
5441                 // enums have a value__ field, which we ignore
5442                 if (((TypeDefinition ) field.DeclaringType).IsEnum || 
5443                                 field.DeclaringType.IsGenericType ())
5444                         return buf;
5445                 if (field.HasConstant && field.IsLiteral) {
5446                         object val = null;
5447                         try {
5448                                 val   = field.Constant;
5449                         } catch {
5450                                 return buf;
5451                         }
5452                         if (val == null)
5453                                 buf.Append (" = ").Append ("null");
5454                         else if (val is Enum)
5455                                 buf.Append (" = ").Append (val.ToString ());
5456                         else if (val is IFormattable) {
5457                                 string value = ((IFormattable)val).ToString();
5458                                 if (val is string)
5459                                         value = "\"" + value + "\"";
5460                                 buf.Append (" = ").Append (value);
5461                         }
5462                 }
5463                 return buf;
5464         }
5465
5466         protected override string GetEventDeclaration (EventDefinition e)
5467         {
5468                 StringBuilder buf = new StringBuilder ();
5469                 if (AppendVisibility (buf, e.AddMethod).Length == 0) {
5470                         return null;
5471                 }
5472
5473                 AppendModifiers (buf, e.AddMethod);
5474
5475                 buf.Append (" event ");
5476                 buf.Append (GetTypeName (e.EventType, new DynamicParserContext (e.AddMethod.Parameters [0]))).Append (' ');
5477                 buf.Append (e.Name).Append (';');
5478
5479                 return buf.ToString ();
5480         }
5481 }
5482
5483 class CSharpMemberFormatter : CSharpFullMemberFormatter {
5484         protected override StringBuilder AppendNamespace (StringBuilder buf, TypeReference type)
5485         {
5486                 return buf;
5487         }
5488 }
5489
5490 class DocTypeFullMemberFormatter : MemberFormatter {
5491         public static readonly MemberFormatter Default = new DocTypeFullMemberFormatter ();
5492
5493         protected override char NestedTypeSeparator {
5494                 get {return '+';}
5495         }
5496 }
5497
5498 class DocTypeMemberFormatter : DocTypeFullMemberFormatter {
5499         protected override StringBuilder AppendNamespace (StringBuilder buf, TypeReference type)
5500         {
5501                 return buf;
5502         }
5503 }
5504
5505 class SlashDocMemberFormatter : MemberFormatter {
5506
5507         protected override char[] GenericTypeContainer {
5508                 get {return new char[]{'{', '}'};}
5509         }
5510
5511         private bool AddTypeCount = true;
5512
5513         private TypeReference genDeclType;
5514         private MethodReference genDeclMethod;
5515
5516         protected override StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context)
5517         {
5518                 if (type is GenericParameter) {
5519                         int l = buf.Length;
5520                         if (genDeclType != null) {
5521                                 IList<GenericParameter> genArgs = genDeclType.GenericParameters;
5522                                 for (int i = 0; i < genArgs.Count; ++i) {
5523                                         if (genArgs [i].Name == type.Name) {
5524                                                 buf.Append ('`').Append (i);
5525                                                 break;
5526                                         }
5527                                 }
5528                         }
5529                         if (genDeclMethod != null) {
5530                                 IList<GenericParameter> genArgs = null;
5531                                 if (genDeclMethod.IsGenericMethod ()) {
5532                                         genArgs = genDeclMethod.GenericParameters;
5533                                         for (int i = 0; i < genArgs.Count; ++i) {
5534                                                 if (genArgs [i].Name == type.Name) {
5535                                                         buf.Append ("``").Append (i);
5536                                                         break;
5537                                                 }
5538                                         }
5539                                 }
5540                         }
5541                         if (genDeclType == null && genDeclMethod == null) {
5542                                 // Probably from within an explicitly implemented interface member,
5543                                 // where CSC uses parameter names instead of indices (why?), e.g.
5544                                 // MyList`2.Mono#DocTest#Generic#IFoo{A}#Method``1(`0,``0) instead of
5545                                 // MyList`2.Mono#DocTest#Generic#IFoo{`0}#Method``1(`0,``0).
5546                                 buf.Append (type.Name);
5547                         }
5548                         if (buf.Length == l) {
5549                                 throw new Exception (string.Format (
5550                                                 "Unable to translate generic parameter {0}; genDeclType={1}, genDeclMethod={2}", 
5551                                                 type.Name, genDeclType, genDeclMethod));
5552                         }
5553                 }
5554                 else {
5555                         base.AppendTypeName (buf, type, context);
5556                         if (AddTypeCount) {
5557                                 int numArgs = type.GenericParameters.Count;
5558                                 if (type.DeclaringType != null)
5559                                         numArgs -= type.GenericParameters.Count;
5560                                 if (numArgs > 0) {
5561                                         buf.Append ('`').Append (numArgs);
5562                                 }
5563                         }
5564                 }
5565                 return buf;
5566         }
5567
5568         protected override StringBuilder AppendArrayModifiers (StringBuilder buf, ArrayType array)
5569         {
5570                 buf.Append (ArrayDelimeters [0]);
5571                 int rank = array.Rank;
5572                 if (rank > 1) {
5573                         buf.Append ("0:");
5574                         for (int i = 1; i < rank; ++i) {
5575                                 buf.Append (",0:");
5576                         }
5577                 }
5578                 return buf.Append (ArrayDelimeters [1]);
5579         }
5580
5581         protected override StringBuilder AppendGenericType (StringBuilder buf, TypeReference type, DynamicParserContext context)
5582         {
5583                 if (!AddTypeCount)
5584                         base.AppendGenericType (buf, type, context);
5585                 else
5586                         AppendType (buf, type, context);
5587                 return buf;
5588         }
5589
5590         private StringBuilder AppendType (StringBuilder buf, TypeReference type, DynamicParserContext context)
5591         {
5592                 List<TypeReference> decls = DocUtils.GetDeclaringTypes (type);
5593                 bool insertNested = false;
5594                 int prevParamCount = 0;
5595                 foreach (var decl in decls) {
5596                         if (insertNested)
5597                                 buf.Append (NestedTypeSeparator);
5598                         insertNested = true;
5599                         base.AppendTypeName (buf, decl, context);
5600                         int argCount = DocUtils.GetGenericArgumentCount (decl);
5601                         int numArgs = argCount - prevParamCount;
5602                         prevParamCount = argCount;
5603                         if (numArgs > 0)
5604                                 buf.Append ('`').Append (numArgs);
5605                 }
5606                 return buf;
5607         }
5608
5609         public override string GetDeclaration (MemberReference member)
5610         {
5611                 TypeReference r = member as TypeReference;
5612                 if (r != null) {
5613                         return "T:" + GetTypeName (r);
5614                 }
5615                 return base.GetDeclaration (member);
5616         }
5617
5618         protected override string GetConstructorName (MethodReference constructor)
5619         {
5620                 return GetMethodDefinitionName (constructor, "#ctor");
5621         }
5622
5623         protected override string GetMethodName (MethodReference method)
5624         {
5625                 string name = null;
5626                 MethodDefinition methodDef = method as MethodDefinition;
5627                 if (methodDef == null || !DocUtils.IsExplicitlyImplemented (methodDef))
5628                         name = method.Name;
5629                 else {
5630                         TypeReference iface;
5631                         MethodReference ifaceMethod;
5632                         DocUtils.GetInfoForExplicitlyImplementedMethod (methodDef, out iface, out ifaceMethod);
5633                         AddTypeCount = false;
5634                         name = GetTypeName (iface) + "." + ifaceMethod.Name;
5635                         AddTypeCount = true;
5636                 }
5637                 return GetMethodDefinitionName (method, name);
5638         }
5639
5640         private string GetMethodDefinitionName (MethodReference method, string name)
5641         {
5642                 StringBuilder buf = new StringBuilder ();
5643                 buf.Append (GetTypeName (method.DeclaringType));
5644                 buf.Append ('.');
5645                 buf.Append (name.Replace (".", "#"));
5646                 if (method.IsGenericMethod ()) {
5647                         IList<GenericParameter> genArgs = method.GenericParameters;
5648                         if (genArgs.Count > 0)
5649                                 buf.Append ("``").Append (genArgs.Count);
5650                 }
5651                 IList<ParameterDefinition> parameters = method.Parameters;
5652                 try {
5653                         genDeclType   = method.DeclaringType;
5654                         genDeclMethod = method;
5655                         AppendParameters (buf, method.DeclaringType.GenericParameters, parameters);
5656                 }
5657                 finally {
5658                         genDeclType   = null;
5659                         genDeclMethod = null;
5660                 }
5661                 return buf.ToString ();
5662         }
5663
5664         private StringBuilder AppendParameters (StringBuilder buf, IList<GenericParameter> genArgs, IList<ParameterDefinition> parameters)
5665         {
5666                 if (parameters.Count == 0)
5667                         return buf;
5668
5669                 buf.Append ('(');
5670
5671                 AppendParameter (buf, genArgs, parameters [0]);
5672                 for (int i = 1; i < parameters.Count; ++i) {
5673                         buf.Append (',');
5674                         AppendParameter (buf, genArgs, parameters [i]);
5675                 }
5676
5677                 return buf.Append (')');
5678         }
5679
5680         private StringBuilder AppendParameter (StringBuilder buf, IList<GenericParameter> genArgs, ParameterDefinition parameter)
5681         {
5682                 AddTypeCount = false;
5683                 buf.Append (GetTypeName (parameter.ParameterType));
5684                 AddTypeCount = true;
5685                 return buf;
5686         }
5687
5688         protected override string GetPropertyName (PropertyReference property)
5689         {
5690                 string name = null;
5691
5692                 PropertyDefinition propertyDef = property as PropertyDefinition;
5693                 MethodDefinition method = null;
5694                 if (propertyDef != null)
5695                         method = propertyDef.GetMethod ?? propertyDef.SetMethod;
5696                 if (method != null && !DocUtils.IsExplicitlyImplemented (method))
5697                         name = property.Name;
5698                 else {
5699                         TypeReference iface;
5700                         MethodReference ifaceMethod;
5701                         DocUtils.GetInfoForExplicitlyImplementedMethod (method, out iface, out ifaceMethod);
5702                         AddTypeCount = false;
5703                         name = string.Join ("#", new string[]{
5704                                         GetTypeName (iface).Replace (".", "#"),
5705                                         DocUtils.GetMember (property.Name)
5706                         });
5707                         AddTypeCount = true;
5708                 }
5709
5710                 StringBuilder buf = new StringBuilder ();
5711                 buf.Append (GetName (property.DeclaringType));
5712                 buf.Append ('.');
5713                 buf.Append (name);
5714                 IList<ParameterDefinition> parameters = property.Parameters;
5715                 if (parameters.Count > 0) {
5716                         genDeclType = property.DeclaringType;
5717                         buf.Append ('(');
5718                         IList<GenericParameter> genArgs = property.DeclaringType.GenericParameters;
5719                         AppendParameter (buf, genArgs, parameters [0]);
5720                         for (int i = 1; i < parameters.Count; ++i) {
5721                                  buf.Append (',');
5722                                  AppendParameter (buf, genArgs, parameters [i]);
5723                         }
5724                         buf.Append (')');
5725                         genDeclType = null;
5726                 }
5727                 return buf.ToString ();
5728         }
5729
5730         protected override string GetFieldName (FieldReference field)
5731         {
5732                 return string.Format ("{0}.{1}",
5733                         GetName (field.DeclaringType), field.Name);
5734         }
5735
5736         protected override string GetEventName (EventReference e)
5737         {
5738                 return string.Format ("{0}.{1}",
5739                         GetName (e.DeclaringType), e.Name);
5740         }
5741
5742         protected override string GetTypeDeclaration (TypeDefinition type)
5743         {
5744                 string name = GetName (type);
5745                 if (type == null)
5746                         return null;
5747                 return "T:" + name;
5748         }
5749
5750         protected override string GetConstructorDeclaration (MethodDefinition constructor)
5751         {
5752                 string name = GetName (constructor);
5753                 if (name == null)
5754                         return null;
5755                 return "M:" + name;
5756         }
5757
5758         protected override string GetMethodDeclaration (MethodDefinition method)
5759         {
5760                 string name = GetName (method);
5761                 if (name == null)
5762                         return null;
5763                 if (method.Name == "op_Implicit" || method.Name == "op_Explicit") {
5764                         genDeclType = method.DeclaringType;
5765                         genDeclMethod = method;
5766                         name += "~" + GetName (method.ReturnType);
5767                         genDeclType = null;
5768                         genDeclMethod = null;
5769                 }
5770                 return "M:" + name;
5771         }
5772
5773         protected override string GetPropertyDeclaration (PropertyDefinition property)
5774         {
5775                 string name = GetName (property);
5776                 if (name == null)
5777                         return null;
5778                 return "P:" + name;
5779         }
5780
5781         protected override string GetFieldDeclaration (FieldDefinition field)
5782         {
5783                 string name = GetName (field);
5784                 if (name == null)
5785                         return null;
5786                 return "F:" + name;
5787         }
5788
5789         protected override string GetEventDeclaration (EventDefinition e)
5790         {
5791                 string name = GetName (e);
5792                 if (name == null)
5793                         return null;
5794                 return "E:" + name;
5795         }
5796 }
5797
5798 class FileNameMemberFormatter : SlashDocMemberFormatter {
5799         protected override StringBuilder AppendNamespace (StringBuilder buf, TypeReference type)
5800         {
5801                 return buf;
5802         }
5803
5804         protected override char NestedTypeSeparator {
5805                 get {return '+';}
5806         }
5807 }
5808
5809 class ResolvedTypeInfo {
5810         TypeDefinition typeDef;
5811
5812         public ResolvedTypeInfo (TypeReference value) {
5813                 Reference = value;
5814         }
5815
5816         public TypeReference Reference { get; private set; }
5817
5818         public TypeDefinition Definition {
5819                 get {
5820                         if (typeDef == null) {
5821                                 typeDef = Reference.Resolve ();
5822                         }
5823                         return typeDef;
5824                 }
5825         }
5826 }
5827
5828 /// <summary>Formats attribute values. Should return true if it is able to format the value.</summary>
5829 class AttributeValueFormatter {
5830         public virtual bool TryFormatValue (object v, ResolvedTypeInfo type, out string returnvalue)
5831         {
5832                 TypeReference valueType = type.Reference;
5833                 if (v == null) {
5834                         returnvalue = "null";
5835                         return true;
5836                 }
5837                 if (valueType.FullName == "System.Type") {
5838                         var vTypeRef = v as TypeReference;
5839                         if (vTypeRef != null) 
5840                                 returnvalue = "typeof(" + NativeTypeManager.GetTranslatedName (vTypeRef) + ")"; // TODO: drop NS handling
5841                         else
5842                                 returnvalue = "typeof(" + v.ToString () + ")";
5843                         
5844                         return true;
5845                 }
5846                 if (valueType.FullName == "System.String") {
5847                         returnvalue = "\"" + v.ToString () + "\"";
5848                         return true;
5849                 }
5850                 if (valueType.FullName == "System.Char") {
5851                         returnvalue = "'" + v.ToString () + "'";
5852                         return true;
5853                 }
5854                 if (v is Boolean) {
5855                         returnvalue = (bool)v ? "true" : "false";
5856                         return true;
5857                 }
5858
5859                 TypeDefinition valueDef = type.Definition;
5860                 if (valueDef == null || !valueDef.IsEnum) {
5861                         returnvalue = v.ToString ();
5862                         return true;
5863                 }
5864
5865                 string typename = MDocUpdater.GetDocTypeFullName (valueType);
5866                 var values = MDocUpdater.GetEnumerationValues (valueDef);
5867                 long c = MDocUpdater.ToInt64 (v);
5868                 if (values.ContainsKey (c)) {
5869                         returnvalue = typename + "." + values [c];
5870                         return true;
5871                 }
5872
5873                 returnvalue = null;
5874                 return false;
5875         }
5876 }
5877
5878 /// <summary>The final value formatter in the pipeline ... if no other formatter formats the value,
5879 /// then this one will serve as the default implementation.</summary>
5880 class DefaultAttributeValueFormatter : AttributeValueFormatter {
5881         public override bool TryFormatValue (object v, ResolvedTypeInfo type, out string returnvalue)
5882         {
5883                 returnvalue = "(" + MDocUpdater.GetDocTypeFullName (type.Reference) + ") " + v.ToString ();
5884                 return true;
5885         }
5886 }
5887
5888 /// <summary>Flags enum formatter that assumes powers of two values.</summary>
5889 /// <remarks>As described here: https://msdn.microsoft.com/en-us/library/vstudio/ms229062(v=vs.100).aspx</remarks>
5890 class StandardFlagsEnumFormatter : AttributeValueFormatter {
5891         public override bool TryFormatValue (object v, ResolvedTypeInfo type, out string returnvalue)
5892         {
5893                 TypeReference valueType = type.Reference;
5894                 TypeDefinition valueDef = type.Definition;
5895                 if (valueDef.CustomAttributes.Any (ca => ca.AttributeType.FullName == "System.FlagsAttribute")) {
5896
5897                         string typename = MDocUpdater.GetDocTypeFullName (valueType);
5898                         var values = MDocUpdater.GetEnumerationValues (valueDef);
5899                         long c = MDocUpdater.ToInt64 (v);
5900                         returnvalue = string.Join (" | ",
5901                                 (from i in values.Keys
5902                                  where (c & i) == i && i != 0
5903                                  select typename + "." + values [i])
5904                                 .DefaultIfEmpty (c.ToString ()).ToArray ());
5905                         
5906                         return true;
5907                 }
5908
5909                 returnvalue = null;
5910                 return false;
5911         }
5912 }
5913
5914 /// <summary>A custom formatter for the ObjCRuntime.Platform enumeration.</summary>
5915 class ApplePlatformEnumFormatter : AttributeValueFormatter {
5916         public override bool TryFormatValue (object v, ResolvedTypeInfo type, out string returnvalue)
5917         {
5918                 TypeReference valueType = type.Reference;
5919                 string typename = MDocUpdater.GetDocTypeFullName (valueType);
5920                 TypeDefinition valueDef = type.Definition;
5921                 if (typename.Contains ("ObjCRuntime.Platform") && valueDef.CustomAttributes.Any (ca => ca.AttributeType.FullName == "System.FlagsAttribute")) {
5922
5923                         var values = MDocUpdater.GetEnumerationValues (valueDef);
5924                         long c = MDocUpdater.ToInt64 (v);
5925
5926                         returnvalue = Format (c, values, typename);
5927                         return true;
5928                 }
5929
5930                 returnvalue = null;
5931                 return false;
5932         }
5933
5934         string Format (long c, IDictionary<long, string> values, string typename)
5935         {
5936                 int iosarch, iosmajor, iosminor, iossubminor;
5937                 int macarch, macmajor, macminor, macsubminor;
5938                 GetEncodingiOS (c, out iosarch, out iosmajor, out iosminor, out iossubminor);
5939                 GetEncodingMac ((ulong)c, out macarch, out macmajor, out macminor, out macsubminor);
5940
5941                 if (iosmajor == 0 & iosminor == 0 && iossubminor == 0) {
5942                         return FormatValues ("Mac", macarch, macmajor, macminor, macsubminor);
5943                 }
5944
5945                 if (macmajor == 0 & macminor == 0 && macsubminor == 0) {
5946                         return FormatValues ("iOS", iosarch, iosmajor, iosminor, iossubminor);
5947                 }
5948
5949                 return string.Format ("(Platform){0}", c);
5950         }
5951
5952         string FormatValues (string plat, int arch, int major, int minor, int subminor) 
5953         {
5954                 string archstring = "";
5955                 switch (arch) {
5956                 case 1:
5957                         archstring = "32";
5958                         break;
5959                 case 2:
5960                         archstring = "64";
5961                         break;
5962                 }
5963                 return string.Format ("Platform.{4}_{0}_{1}{2} | Platform.{4}_Arch{3}",
5964                         major,
5965                         minor,
5966                         subminor == 0 ? "" : "_" + subminor.ToString (),
5967                         archstring,
5968                         plat
5969                 );
5970         }
5971
5972         void GetEncodingiOS (long entireLong, out int archindex, out int major, out int minor, out int subminor)
5973         {
5974                 long lowerBits = entireLong & 0xffffffff; 
5975                 int lowerBitsAsInt = (int) lowerBits;
5976                 GetEncoding (lowerBitsAsInt, out archindex, out major, out minor, out subminor);
5977         }
5978
5979         void GetEncodingMac (ulong entireLong, out int archindex, out int major, out int minor, out int subminor)
5980         {
5981                 ulong higherBits = entireLong & 0xffffffff00000000; 
5982                 int higherBitsAsInt = (int) ((higherBits) >> 32);
5983                 GetEncoding (higherBitsAsInt, out archindex, out major, out minor, out subminor);
5984         }
5985
5986         void GetEncoding (Int32 encodedBits, out int archindex, out int major, out int minor, out int subminor)
5987         {
5988                 // format is AAJJNNSS
5989                 archindex = (int)((encodedBits & 0xFF000000) >> 24);
5990                 major = (int)((encodedBits & 0x00FF0000) >> 16);
5991                 minor = (int)((encodedBits & 0x0000FF00) >> 8);
5992                 subminor = (int)((encodedBits & 0x000000FF) >> 0);
5993         }
5994 }
5995 }