File: EcmaDesc.cs

package info (click to toggle)
mono 6.12.0.199%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,192 kB
  • sloc: cs: 11,181,844; xml: 2,850,076; ansic: 689,413; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,845; makefile: 19,951; sh: 15,030; python: 4,771; pascal: 925; sql: 859; sed: 16; php: 1
file content (419 lines) | stat: -rw-r--r-- 11,700 bytes parent folder | download | duplicates (9)
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
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;

namespace Monodoc.Ecma
{
	/* Some properties might not be filled/meaningful depending on kind
	 * like a namespace EcmaUrl won't have a valid TypeName
	 */
	public class EcmaDesc : IEquatable<EcmaDesc>
	{
		public enum Kind
		{
			Type,
			Constructor,
			Method,
			Namespace,
			Field,
			Property,
			Event,
			Operator
		}

		public enum Mod
		{
			Normal,
			Pointer,
			Ref,
			Out
		}

		public enum Format
		{
			WithArgs,
			WithoutArgs
		}

		public Kind DescKind {
			get;
			set;
		}

		public Mod DescModifier {
			get;
			set;
		}

		public string Namespace {
			get;
			set;
		}

		public string TypeName {
			get;
			set;
		}

		public string MemberName {
			get;
			set;
		}

		public EcmaDesc NestedType {
			get;
			set;
		}

		/* A list of the array dimensions attached to this type.
		 * The list count corresponds to the number of recursive
		 * array definition (jagged arrays) the value of the
		 * corresponding list item is the number of dimension
		 * attached to that array definition instance
		 */
		public IList<int> ArrayDimensions {
			get;
			set;
		}

		/* Depending on the form of the url, we might not have the type
		 * of the argument but only how many the type/member has i.e.
		 * when such number is specified with a backtick
		 */
		public IList<EcmaDesc> GenericTypeArguments {
			get;
			set;
		}

		/* The GenericTypeArguments list may be null, in which case, this
		 * is an easier/safer way to check the count.
		 */
		public int GenericTypeArgumentsCount {
			get { return GenericTypeArguments != null ? GenericTypeArguments.Count : 0; }
		}

		/* This property tells if the above collections only correct value
		 * is the number of item in it to represent generic arguments
		 */
		public bool GenericTypeArgumentsIsNumeric {
			get {
				return GenericTypeArguments != null && GenericTypeArguments.FirstOrDefault () == null;
			}
		}

		public IList<EcmaDesc> GenericMemberArguments {
			get;
			set;
		}

		/* The GenericMemberArguments list may be null, in which case, this
		 * is an easier/safer way to check the count.
		 */
		public int GenericMemberArgumentsCount {
			get { return GenericMemberArguments != null ? GenericMemberArguments.Count : 0; }
		}

		public bool GenericMemberArgumentsIsNumeric {
			get {
				return GenericMemberArguments != null && GenericMemberArguments.FirstOrDefault () == null;
			}
		}

		public IList<EcmaDesc> MemberArguments {
			get;
			set;
		}

		/* The GenericTypeArguments list may be null, in which case, this
		 * is an easier/safer way to check the count.
		 */
		public int MemberArgumentsCount {
			get { return MemberArguments != null ? MemberArguments.Count : 0; }
		}

		/* This indicates that we actually want an inner part of the ecmadesc
		 * i.e. in case of T: we could want the members (*), ctor (C), methods (M), ...
		 */
		public char Etc {
			get;
			set;
		}

		public bool IsEtc {
			get {
				return Etc != (char)0;
			}
		}

		/* EtcFilter is only valid in some case of IsEtc when the inner part needs
		 * to be further filtered e.g. in case we want a listing of the type overloads
		 * Equals
		 */
		public string EtcFilter {
			get;
			set;
		}

		/* When a member is an explicit implementation of an interface member, we register
		 * the member EcmaDesc with its interface parent here
		 */
		public EcmaDesc ExplicitImplMember {
			get;
			set;
		}

		// Returns the TypeName and the generic/inner type information if existing
		public string ToCompleteTypeName (char innerTypeSeparator = '.')
		{
			var result = TypeName;
			if (GenericTypeArguments != null)
				result += FormatGenericArgs (GenericTypeArguments);
			if (NestedType != null)
				result += innerTypeSeparator + NestedType.ToCompleteTypeName ();
			if (ArrayDimensions != null && ArrayDimensions.Count > 0)
				result += ArrayDimensions.Select (dim => "[" + new string (',', dim - 1) + "]").Aggregate (string.Concat);

			return result;
		}

		// Returns the member name with its generic types if existing
		public string ToCompleteMemberName (Format format)
		{
			/* We special process two cases:
			 *   - Explicit member implementation which append a full type specification
			 *   - Conversion operator which are exposed as normal method but have specific captioning in the end
			 */
			if (ExplicitImplMember != null) {
				var impl = ExplicitImplMember;
				return impl.FormattedNamespace + impl.ToCompleteTypeName () + "." + impl.ToCompleteMemberName (format);
			} else if (format == Format.WithArgs && DescKind == Kind.Operator && MemberName.EndsWith ("Conversion")) {
				var type1 = MemberArguments[0].FormattedNamespace + MemberArguments[0].ToCompleteTypeName () + ModToString (MemberArguments[0]);
				var type2 = MemberArguments[1].FormattedNamespace + MemberArguments[1].ToCompleteTypeName () + ModToString (MemberArguments[1]);
				return type1 + " to " + type2;
			}

			var result = IsEtc && !string.IsNullOrEmpty (EtcFilter) ? EtcFilter : MemberName;

			// Temporary hack for monodoc produced inner type ctor
			//if (DescKind == Kind.Constructor && NestedType != null)
				//result = ToCompleteTypeName ();

			if (GenericMemberArguments != null)
				result += FormatGenericArgs (GenericMemberArguments);

			if (format == Format.WithArgs) {
				result += '(';
				if (MemberArguments != null && MemberArguments.Count > 0) {
					var args = MemberArguments.Select (a => FormatNamespace (a) + a.ToCompleteTypeName ('+') + ModToString (a));
					result += string.Join (",", args);
				}
				result += ')';
			}

			return result;
		}

		public string ToEcmaCref ()
		{
			var sb = new StringBuilder ();
			// Cref type
			sb.Append (DescKind.ToString ()[0]);
			sb.Append (":");
			// Create the rest
			ConstructCRef (sb);

			return sb.ToString ();
		}

		void ConstructCRef (StringBuilder sb, bool skipLeadingDot = false)
		{
			if (string.IsNullOrEmpty (Namespace))
				skipLeadingDot = true;

			sb.Append (Namespace);
			if (DescKind == Kind.Namespace)
				return;

			if (!skipLeadingDot)
				sb.Append ('.');

			sb.Append (TypeName);
			AppendGenericArguments (sb, GenericTypeArguments, GenericTypeArgumentsIsNumeric, GenericTypeArgumentsCount);

			if (NestedType != null) {
				sb.Append ('+');
				NestedType.ConstructCRef (sb, skipLeadingDot: true);
			}
			if (ArrayDimensions != null && ArrayDimensions.Count > 0) {
				for (int i = 0; i < ArrayDimensions.Count; i++) {
					sb.Append ('[');
					sb.Append (new string (',', ArrayDimensions[i] - 1));
					sb.Append (']');
				}
			}
			if (DescKind == Kind.Type)
				return;

			if (ExplicitImplMember != null) {
				sb.Append ('$');
				ExplicitImplMember.DescKind = this.DescKind;
				ExplicitImplMember.ConstructCRef (sb, skipLeadingDot: false);
				return;
			}

			sb.Append (".");
			sb.Append (MemberName);

			AppendGenericArguments (sb, GenericMemberArguments, GenericMemberArgumentsIsNumeric, GenericMemberArgumentsCount);

			if (MemberArguments != null && MemberArgumentsCount > 0) {
				sb.Append ("(");
				int i=0;
				foreach (var a in MemberArguments) {
					if (i > 0) {
						sb.Append(",");
					}
					a.ConstructCRef (sb);
					i++;
				}
				sb.Append (")");
			}
		}

		void AppendGenericArguments (StringBuilder sb, IEnumerable<EcmaDesc> arguments, bool isNumeric, int argumentsCount)
		{
			if (arguments != null && isNumeric) {
				sb.AppendFormat ("`{0}", argumentsCount);
			} else if (arguments != null) {
				sb.Append ('<');
				int i=0;
				foreach (var t in arguments) {
					if (i > 0) {
						sb.Append (",");
					}
					t.ConstructCRef (sb);

					i++;
				}
				sb.Append ('>');
			}
		}

		public override string ToString ()
		{
			return string.Format ("({8}) {0}::{1}{2}{3}{7} {4}{5}{6} {9} {10}",
			                      Namespace,
			                      TypeName,
			                      FormatGenericArgsFull (GenericTypeArguments),
			                      NestedType != null ? "+" + NestedType.ToString () : string.Empty,
			                      MemberName ?? string.Empty,
			                      FormatGenericArgsFull (GenericMemberArguments),
			                      MemberArguments != null ? "(" + string.Join (",", MemberArguments.Select (m => m.ToString ())) + ")" : string.Empty,
			                      ArrayDimensions != null && ArrayDimensions.Count > 0 ? ArrayDimensions.Select (dim => "[" + new string (',', dim - 1) + "]").Aggregate (string.Concat) : string.Empty,
			                      DescKind.ToString ()[0],
			                      Etc != 0 ? '(' + Etc.ToString () + ')' : string.Empty,
			                      ExplicitImplMember != null ? "$" + ExplicitImplMember.ToString () : string.Empty);
			                      
		}

		public override bool Equals (object other)
		{
			var otherDesc = other as EcmaDesc;
			return otherDesc != null && Equals (otherDesc);
		}

		public bool Equals (EcmaDesc other)
		{
			if (other == null)
				return false;

			if (NestedType == null ^ other.NestedType == null
			    || ArrayDimensions == null ^ other.ArrayDimensions == null
			    || GenericTypeArguments == null ^ other.GenericTypeArguments == null
			    || GenericMemberArguments == null ^ other.GenericMemberArguments == null
			    || MemberArguments == null ^ other.MemberArguments == null
			    || ExplicitImplMember == null ^ other.ExplicitImplMember == null)
				return false;

			return other != null
				&& DescKind == other.DescKind
				&& TypeName == other.TypeName
				&& Namespace == other.Namespace
				&& MemberName == other.MemberName
				&& (NestedType == null || NestedType.Equals (other.NestedType))
				&& (ArrayDimensions == null || ArrayDimensions.SequenceEqual (other.ArrayDimensions))
				&& (GenericTypeArguments == null || GenericTypeArguments.SequenceEqual (other.GenericTypeArguments))
				&& (GenericMemberArguments == null || GenericMemberArguments.SequenceEqual (other.GenericMemberArguments))
				&& (MemberArguments == null || MemberArguments.SequenceEqual (other.MemberArguments))
				&& Etc == other.Etc
				&& EtcFilter == other.EtcFilter
				&& (ExplicitImplMember == null || ExplicitImplMember.Equals (other.ExplicitImplMember));
		}

		public override int GetHashCode ()
		{
			return DescKind.GetHashCode ()
				^ TypeName.GetHashCode ()
				^ Namespace.GetHashCode ()
				^ MemberName.GetHashCode ();
		}

		bool What (bool input)
		{
			if (!input)
				throw new Exception ("Not equal");
			return input;
		}

		bool WhatT (bool input)
		{
			if (input)
				throw new Exception ("Not equal");
			return input;
		}

		string FormatNamespace (EcmaDesc desc)
		{
			return string.IsNullOrEmpty (desc.Namespace) ? string.Empty : desc.Namespace + ".";
		}

		string FormatGenericArgs (IEnumerable<EcmaDesc> args)
		{
			if (args == null || !args.Any ())
				return string.Empty;
			// If we only have the number of generic arguments, use ` notation
			if (args.First () == null)
				return "`" + args.Count ();

			IEnumerable<string> argsList = args.Select (t => FormatNamespace (t) + t.ToCompleteTypeName ());

			return "<" + string.Join (",", argsList) + ">";
		}

		string FormatGenericArgsFull (IEnumerable<EcmaDesc> genericArgs)
		{
			return genericArgs != null ? "<" + string.Join (",", genericArgs.Select (t => t.ToString ())) + ">" : string.Empty;
		}

		string ModToString (EcmaDesc desc)
		{
			switch (desc.DescModifier) {
			case Mod.Pointer:
				return "*";
			case Mod.Ref:
				return "&";
			case Mod.Out:
				return "@";
			default:
				return string.Empty;
			}
		}

		string FormattedNamespace {
			get {
				return !string.IsNullOrEmpty (Namespace) ? Namespace + "." : string.Empty;
			}
		}
	}
}