summaryrefslogtreecommitdiff
path: root/mcs/class/monodoc/Monodoc/providers/EcmaDoc.cs
blob: efdeedf63a623c36c620cf40d09b86c715ebe79c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Collections.Generic;

using Monodoc.Ecma;

namespace Monodoc.Providers
{
	public enum EcmaNodeType {
		Invalid,
		Namespace,
		Type,
		Member,
		Meta, // A node that's here to serve as a header for other node
	}

	// Common functionality between ecma-provider and ecmauncompiled-provider
	internal class EcmaDoc
	{
		static EcmaUrlParser parser = new EcmaUrlParser ();

		public static void PopulateTreeFromIndexFile (string indexFilePath,
		                                              string idPrefix,
		                                              Tree tree,
		                                              IDocStorage storage,
		                                              Dictionary<string, XElement> nsSummaries,
		                                              Func<XElement, string> indexGenerator = null)
		{
			var root = tree.RootNode;
			int resID = 0;
			var asm = Path.GetDirectoryName (indexFilePath);

			storage = storage ?? new Storage.NullStorage ();
			// nsSummaries is allowed to be null if the user doesn't care about it
			nsSummaries = nsSummaries ?? new Dictionary<string, XElement> ();
			// default index generator uses a counter
			indexGenerator = indexGenerator ?? (_ => resID++.ToString ());

			using (var reader = XmlReader.Create (File.OpenRead (indexFilePath))) {
				reader.ReadToFollowing ("Types");
				var types = XElement.Load (reader.ReadSubtree ());

				foreach (var ns in types.Elements ("Namespace")) {
					var nsName = (string)ns.Attribute ("Name");
					nsName = !string.IsNullOrEmpty (nsName) ? nsName : "global";
					var nsNode = root.GetOrCreateNode (nsName, "N:" + nsName);

					XElement nsElements;
					if (!nsSummaries.TryGetValue (nsName, out nsElements))
						nsSummaries[nsName] = nsElements = new XElement ("elements",
						                                                 new XElement ("summary"),
						                                                 new XElement ("remarks"));
			        //Add namespace summary and remarks data from file, if available
					var nsFileName = Path.Combine(asm, String.Format("ns-{0}.xml", nsName));
					if(File.Exists(nsFileName)){
						var nsEl = XElement.Load (nsFileName);

						nsElements.Element ("summary").ReplaceWith (nsEl.Descendants ("summary").First ());
						nsElements.Element ("remarks").ReplaceWith (nsEl.Descendants ("remarks").First ());
					}else{
						Console.WriteLine ("Error reading namespace XML for " + nsName);
					}
			       
					foreach (var type in ns.Elements ("Type")) {
						// Add the XML file corresponding to the type to our storage
						var id = indexGenerator (type);
						string typeFilePath;
						var typeDocument = EcmaDoc.LoadTypeDocument (asm, nsName, type.Attribute ("Name").Value, out typeFilePath);
						if (typeDocument == null)
							continue;
						using (var file = File.OpenRead (typeFilePath))
							storage.Store (id, file);
						nsElements.Add (ExtractClassSummary (typeFilePath));

						var typeCaption = EcmaDoc.GetTypeCaptionFromIndex (type);
						var url = idPrefix + id + '#' + typeCaption + '/';
						typeCaption = EcmaDoc.GetTypeCaptionFromIndex (type, true);
						var typeNode = nsNode.CreateNode (typeCaption, url);

						// Add meta "Members" node
						typeNode.CreateNode ("Members", "*");
						var membersNode = typeDocument.Root.Element ("Members");
						if (membersNode == null || !membersNode.Elements ().Any ())
							continue;
						var members = membersNode
							.Elements ("Member")
							.ToLookup (EcmaDoc.GetMemberType);

						foreach (var memberType in members) {
							// We pluralize the member type to get the caption and take the first letter as URL
							var node = typeNode.CreateNode (EcmaDoc.PluralizeMemberType (memberType.Key), memberType.Key[0].ToString ());
							var memberIndex = 0;

							var isCtors = memberType.Key[0] == 'C';

							// We do not escape much member name here
							foreach (var memberGroup in memberType.GroupBy (m => MakeMemberCaption (m, isCtors))) {
								if (memberGroup.Count () > 1) {
									// Generate overload
									var overloadCaption = MakeMemberCaption (memberGroup.First (), false);
									var overloadNode = node.CreateNode (overloadCaption, overloadCaption);
									foreach (var member in memberGroup)
										overloadNode.CreateNode (MakeMemberCaption (member, true), (memberIndex++).ToString ());
									overloadNode.Sort ();
								} else {
									// We treat constructor differently by showing their argument list in all cases
									node.CreateNode (MakeMemberCaption (memberGroup.First (), isCtors), (memberIndex++).ToString ());
								}
							}
							node.Sort ();
						}
					}

					nsNode.Sort ();
				}
				root.Sort ();
			}
		}

		// Utility methods

		public static XDocument LoadTypeDocument (string basePath, string nsName, string typeName)
		{
			string dummy;
			return LoadTypeDocument (basePath, nsName, typeName, out dummy);
		}

		public static XDocument LoadTypeDocument (string basePath, string nsName, string typeName, out string finalPath)
		{
			finalPath = Path.Combine (basePath, nsName, Path.ChangeExtension (typeName, ".xml"));
			if (!File.Exists (finalPath)) {
				Console.Error.WriteLine ("Warning: couldn't process type file `{0}' as it doesn't exist", finalPath);
				return null;
			}

			XDocument doc = null;
			try {
				doc = XDocument.Load (finalPath);
			} catch (Exception e) {
				Console.WriteLine ("Document `{0}' is unparsable, {1}", finalPath, e.ToString ());
			}

			return doc;
		}

		public static string GetTypeCaptionFromIndex (XElement typeNodeFromIndex, bool full = false)
		{
			var t = typeNodeFromIndex;
			var c = ((string)(t.Attribute ("DisplayName") ?? t.Attribute ("Name"))).Replace ('+', '.');
			if (full)
				c += " " + (string)t.Attribute ("Kind");
			return c;
		}

		public static string PluralizeMemberType (string memberType)
		{
			switch (memberType) {
			case "Property":
				return "Properties";
			default:
				return memberType + "s";
			}
		}

		public static string GetMemberType (XElement m)
		{
			return m.Attribute ("MemberName").Value.StartsWith ("op_") ? "Operator" : m.Element ("MemberType").Value;
		}

		public static string MakeMemberCaption (XElement member, bool withArguments)
		{
			var caption = (string)member.Attribute ("MemberName");
			// Use type name instead of .ctor for cosmetic sake
			if (caption == ".ctor") {
				caption = (string)member.Ancestors ("Type").First ().Attribute ("Name");
				// If this is an inner type ctor, strip the parent type reference
				var plusIndex = caption.LastIndexOf ('+');
				if (plusIndex != -1)
					caption = caption.Substring (plusIndex + 1);
			}
			if (caption.StartsWith ("op_")) {
				string sig;
				caption = MakeOperatorSignature (member, out sig);
				caption = withArguments ? sig : caption;
				return caption;
			}
			if (withArguments) {
				var args = member.Element ("Parameters");
				caption += '(';
				if (args != null && args.Elements ("Parameter").Any ()) {
					caption += args.Elements ("Parameter")
						.Select (p => (string)p.Attribute ("Type"))
						.Aggregate ((p1, p2) => p1 + "," + p2);
				}
				caption += ')';
			}
			
			return caption;
		}

		public static Node MatchNodeWithEcmaUrl (string url, Tree tree)
		{
			Node result = null;
			EcmaDesc desc;
			if (!parser.TryParse (url, out desc))
				return null;

			// Namespace search
			Node currentNode = tree.RootNode;
			Node searchNode = new Node () { Caption = desc.Namespace };
			int index = currentNode.ChildNodes.BinarySearch (searchNode, EcmaGenericNodeComparer.Instance);
			if (index >= 0)
				result = currentNode.ChildNodes[index];
			if (desc.DescKind == EcmaDesc.Kind.Namespace || index < 0)
				return result;

			// Type search
			currentNode = result;
			result = null;
			searchNode.Caption = desc.ToCompleteTypeName ();
			if (!desc.GenericTypeArgumentsIsNumeric)
				index = currentNode.ChildNodes.BinarySearch (searchNode, EcmaTypeNodeComparer.Instance);
			else
				index = GenericTypeBacktickSearch (currentNode.ChildNodes, desc);
			if (index >= 0)
				result = currentNode.ChildNodes[index];
			if ((desc.DescKind == EcmaDesc.Kind.Type && !desc.IsEtc) || index < 0)
				return result;

			// Member selection
			currentNode = result;
			result = null;
			var caption = desc.IsEtc ? EtcKindToCaption (desc.Etc) : MemberKindToCaption (desc.DescKind);
			currentNode = FindNodeForCaption (currentNode.ChildNodes, caption);
			if (currentNode == null
			    || (desc.IsEtc && desc.DescKind == EcmaDesc.Kind.Type && string.IsNullOrEmpty (desc.EtcFilter)))
				return currentNode;

			// Member search
			result = null;
			var format = desc.DescKind == EcmaDesc.Kind.Constructor ? EcmaDesc.Format.WithArgs : EcmaDesc.Format.WithoutArgs;
			searchNode.Caption = desc.ToCompleteMemberName (format);
			index = currentNode.ChildNodes.BinarySearch (searchNode, EcmaGenericNodeComparer.Instance);
			if (index < 0)
				return null;
			result = currentNode.ChildNodes[index];
			if (result.ChildNodes.Count == 0 || desc.IsEtc)
				return result;

			// Overloads search
			currentNode = result;
			searchNode.Caption = desc.ToCompleteMemberName (EcmaDesc.Format.WithArgs);
			index = currentNode.ChildNodes.BinarySearch (searchNode, EcmaGenericNodeComparer.Instance);
			if (index < 0)
				return result;
			result = result.ChildNodes[index];

			return result;
		}

		static int GenericTypeBacktickSearch (IList<Node> childNodes, EcmaDesc desc)
		{
			/* Our strategy is to search for the non-generic variant of the type
			 * (which in most case should fail) and then use the closest index
			 * to linearily search for the generic variant with the right generic arg number
			 */
			var searchNode = new Node () { Caption = desc.TypeName };
			int index = childNodes.BinarySearch (searchNode, EcmaTypeNodeComparer.Instance);
			// Place the index in the right start position
			if (index < 0)
				index = ~index;

			for (int i = index; i < childNodes.Count; i++) {
				var currentNode = childNodes[i];
				// Find the index of the generic argument list
				int genericIndex = currentNode.Caption.IndexOf ('<');
				// If we are not on the same base type name anymore, there is no point
				int captionSlice = genericIndex != -1 ? genericIndex : currentNode.Caption.LastIndexOf (' ');
				if (string.Compare (searchNode.Caption, 0,
				                    currentNode.Caption, 0,
				                    Math.Max (captionSlice, searchNode.Caption.Length),
				                    StringComparison.Ordinal) != 0)
					break;

				var numGenerics = CountTypeGenericArguments (currentNode.Caption, genericIndex);
				if (numGenerics == desc.GenericTypeArguments.Count) {
					// Simple comparison if we are not looking for an inner type
					if (desc.NestedType == null)
						return i;
					// If more complicated, we fallback to using EcmaUrlParser
					var caption = currentNode.Caption;
					caption = "T:" + caption.Substring (0, caption.LastIndexOf (' ')).Replace ('.', '+');
					EcmaDesc otherDesc;
					var parser = new EcmaUrlParser ();
					if (parser.TryParse (caption, out otherDesc) && desc.NestedType.Equals (otherDesc.NestedType))
						return i;
				}
			}

			return -1;
		}

		// This comparer returns the answer straight from caption comparison
		class EcmaGenericNodeComparer : IComparer<Node>
		{
			public static readonly EcmaGenericNodeComparer Instance = new EcmaGenericNodeComparer ();

			public int Compare (Node n1, Node n2)
			{
				return string.Compare (n1.Caption, n2.Caption, StringComparison.Ordinal);
			}
		}

		// This comparer take into account the space in the caption
		class EcmaTypeNodeComparer : IComparer<Node>
		{
			public static readonly EcmaTypeNodeComparer Instance = new EcmaTypeNodeComparer ();

			public int Compare (Node n1, Node n2)
			{
				int length1 = CaptionLength (n1.Caption);
				int length2 = CaptionLength (n2.Caption);

				return string.Compare (n1.Caption, 0, n2.Caption, 0, Math.Max (length1, length2), StringComparison.Ordinal);
			}

			int CaptionLength (string caption)
			{
				var length = caption.LastIndexOf (' ');
				return length == -1 ? caption.Length : length;
			}
		}

		public static Dictionary<string, string> GetContextForEcmaNode (string hash, string sourceID, Node node)
		{
			var args = new Dictionary<string, string> ();

			args["source-id"] = sourceID;

			if (node != null) {
				var nodeType = GetNodeType (node);
				switch (nodeType) {
				case EcmaNodeType.Namespace:
					args["show"] = "namespace";
					args["namespace"] = node.Element.Substring ("N:".Length);
					break;
				case EcmaNodeType.Type:
					args["show"] = "typeoverview";
					break;
				case EcmaNodeType.Member:
				case EcmaNodeType.Meta:
					switch (GetNodeMemberTypeChar (node)){
					case 'C':
						args["membertype"] = "Constructor";
						break;
					case 'M':
						args["membertype"] = "Method";
						break;
					case 'P':
						args["membertype"] = "Property";
						break;
					case 'F':
						args["membertype"] = "Field";
						break;
					case 'E':
						args["membertype"] = "Event";
						break;
					case 'O':
						args["membertype"] = "Operator";
						break;
					case 'X':
						args["membertype"] = "ExtensionMethod";
						break;
					case '*':
						args["membertype"] = "All";
						break;
					}

					if (nodeType == EcmaNodeType.Meta) {
						args["show"] = "members";
						args["index"] = "all";
					} else {
						args["show"] = "member";
						args["index"] = node.Element;
					}
					break;
				}
			}

			if (!string.IsNullOrEmpty (hash))
				args["hash"] = hash;

			return args;
		}

		public static EcmaNodeType GetNodeType (Node node)
		{
			// We guess the node type by checking the depth level it's at in the tree
			int level = GetNodeLevel (node);
			switch (level) {
			case 0:
				return EcmaNodeType.Namespace;
			case 1:
				return EcmaNodeType.Type;
			case 2:
				return EcmaNodeType.Meta;
			case 3: // Here it's either a member or, in case of overload, a meta
				return node.IsLeaf ? EcmaNodeType.Member : EcmaNodeType.Meta;
			case 4: // At this level, everything is necessarily a member
				return EcmaNodeType.Member;
			default:
				return EcmaNodeType.Invalid;
			}
		}

		public static char GetNodeMemberTypeChar (Node node)
		{
			int level = GetNodeLevel (node);
			// We try to reach the member group node depending on node nested level
			switch (level) {
			case 2:
				return node.Element[0];
			case 3:
				return node.Parent.Element[0];
			case 4:
				return node.Parent.Parent.Element[0];
			default:
				throw new ArgumentException ("node", "Couldn't determine member type of node `" + node.Caption + "'");
			}
		}

		public static int GetNodeLevel (Node node)
		{
			int i = 0;
			for (; !node.Element.StartsWith ("root:/", StringComparison.OrdinalIgnoreCase); i++) {
				node = node.Parent;
				if (node == null)
					return i - 1;
			}
			return i - 1;
		}

		public static string EtcKindToCaption (char etc)
		{
			switch (etc) {
			case 'M':
				return "Methods";
			case 'P':
				return "Properties";
			case 'C':
				return "Constructors";
			case 'F':
				return "Fields";
			case 'E':
				return "Events";
			case 'O':
				return "Operators";
			case '*':
				return "Members";
			default:
				return null;
			}
		}

		public static string MemberKindToCaption (EcmaDesc.Kind kind)
		{
			switch (kind) {
			case EcmaDesc.Kind.Method:
				return "Methods";
			case EcmaDesc.Kind.Property:
				return "Properties";
			case EcmaDesc.Kind.Constructor:
				return "Constructors";
			case EcmaDesc.Kind.Field:
				return "Fields";
			case EcmaDesc.Kind.Event:
				return "Events";
			case EcmaDesc.Kind.Operator:
				return "Operators";
			default:
				return null;
			}
		}

		public static Node FindNodeForCaption (IList<Node> nodes, string caption)
		{
			foreach (var node in nodes)
				if (node.Caption.Equals (caption, StringComparison.OrdinalIgnoreCase))
					return node;
			return null;
		}

		public static int CountTypeGenericArguments (string typeDefinition, int startIndex = 0)
		{
			int nestedLevel = 0;
			int count = 0;
			bool started = false;

			foreach (char c in typeDefinition.Skip (startIndex)) {
				switch (c) {
				case '<':
					if (!started)
						count = 1;
					started = true;
					nestedLevel++;
					break;
				case ',':
					if (started && nestedLevel == 1)
						count++;
					break;
				case '>':
					nestedLevel--;
					break;
				}
			}

			return count;
		}

		internal static string MakeOperatorSignature (XElement member, out string memberSignature)
		{
			string name = (string)member.Attribute ("MemberName");
			var nicename = name.Substring(3);
			memberSignature = null;

			switch (name) {
			// unary operators: no overloading possible	[ECMA-335 §10.3.1]
			case "op_UnaryPlus":                    // static     R operator+       (T)
			case "op_UnaryNegation":                // static     R operator-       (T)
			case "op_LogicalNot":                   // static     R operator!       (T)
			case "op_OnesComplement":               // static     R operator~       (T)
			case "op_Increment":                    // static     R operator++      (T)
			case "op_Decrement":                    // static     R operator--      (T)
			case "op_True":                         // static  bool operator true   (T)
			case "op_False":                        // static  bool operator false  (T)
			case "op_AddressOf":                    // static     R operator&       (T)
			case "op_PointerDereference":           // static     R operator*       (T)
				memberSignature = nicename;
				break;
			// conversion operators: overloading based on parameter and return type [ECMA-335 §10.3.3]
			case "op_Implicit":                    // static implicit operator R (T)
			case "op_Explicit":                    // static explicit operator R (T)
				nicename = name.EndsWith ("Implicit") ? "ImplicitConversion" : "ExplicitConversion";
				string arg = (string)member.Element ("Parameters").Element ("Parameter").Attribute ("Type");
				string ret = (string)member.Element ("ReturnValue").Element ("ReturnType");
				memberSignature = arg + " to " + ret;
				break;
			// binary operators: overloading is possible [ECMA-335 §10.3.2]
			default:
				if (member.Element ("Parameters") != null)
					memberSignature =
						nicename + "("
						+ string.Join (",", member.Element ("Parameters").Elements ("Parameter").Select (p => (string)p.Attribute ("Type")))
						+ ")";
				break;
			}

			return nicename;
		}

		static XElement ExtractClassSummary (string typeFilePath)
		{
			using (var reader = XmlReader.Create (typeFilePath)) {
				reader.ReadToFollowing ("Type");
				var name = reader.GetAttribute ("Name");
				var fullName = reader.GetAttribute ("FullName");
				reader.ReadToFollowing ("AssemblyName");
				var assemblyName = reader.ReadElementString ();
				var summary = reader.ReadToFollowing ("summary") ? XElement.Load (reader.ReadSubtree ()) : new XElement ("summary");
				var remarks = reader.ReadToFollowing ("remarks") ? XElement.Load (reader.ReadSubtree ()) : new XElement ("remarks");

				return new XElement ("class",
				                     new XAttribute ("name", name ?? string.Empty),
				                     new XAttribute ("fullname", fullName ?? string.Empty),
				                     new XAttribute ("assembly", assemblyName ?? string.Empty),
				                     summary,
				                     remarks);
			}
		}
	}
}