Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XmlDocIdGenerator.cs « XmlDocIdLib « MonoDevelop.AssemblyBrowser « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2bc932d588e23f4f64a9ea9f4a3410c2a906f522 (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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
using System;
using System.Collections.Generic;
using System.Text;

using Mono.Cecil;

namespace XmlDocIdLib
{
    #region XmlDocIdGenerator
	class XmlDocIdGenerator
    {
        #region Constructors
        public XmlDocIdGenerator()
        {
            this.m_compat = CompatibilityType.Net35;
        }
        #endregion

        #region Public methods
        public string GetXmlDocPath(
            MemberReference Member)
        {
            if (Member == null)
                throw new ArgumentNullException("Member");

            StringBuilder stbBuilder = new StringBuilder();
            List<string> Path = new List<string>();

            // get path
            GetXmlDocPathRecursive(Member, Path);

            // generate string
            if (Path.Count == 0)
                return string.Empty;

            foreach (string strTemp in Path)
                stbBuilder.Append(strTemp);

            return stbBuilder.ToString();
        }

        public void SetCompatibilityType(
            CompatibilityType Compatibility)
        {
            if (Compatibility == CompatibilityType.None)
                throw new ArgumentException("Invalid parameter value.");

            this.m_compat = Compatibility;
        }

        public CompatibilityType GetCompatibilityType()
        {
            return this.m_compat;
        }
        #endregion

        #region Private methods
        private string GetXmlDocExplicitIfaceImplPath(
            MemberReference Member)
        {
            TypeReference declaringTypeRef = null;
            TypeDefinition declaringTypeDef = null;
            string strPath = string.Empty;

            if (Member.DeclaringType is GenericInstanceType)
                declaringTypeRef = (Member.DeclaringType as GenericInstanceType).ElementType;
            else
                declaringTypeRef = Member.DeclaringType;

            // lookup TypeDefinition for TypeReference
            declaringTypeDef = TryLookUpTypeDefinition(declaringTypeRef);

            if (declaringTypeDef == null || declaringTypeDef.IsInterface)
                return string.Empty;

            foreach (InterfaceImplementation tempIface in declaringTypeDef.Interfaces)
            {
                var tempIfaceRef = tempIface.InterfaceType;
                // check whether this member name begins with interface name (plus generic arguments)
                if (Member.Name.StartsWith(this.StripInterfaceName(tempIfaceRef.FullName)))
                {
                    // element begins with interface name, this is explicit interface implementation,
                    // get explicit interface implementation path

                    // add member's name to path, at this point
                    // name contains interface name (with generic arguments) plus member name
                    strPath = Member.Name;

                    // remove text between "<" and ">" and put interface parameters 
                    // (in explicit mode of course)
                    int LeftBrace = strPath.IndexOf("<");
                    int RightBrace = strPath.LastIndexOf(">");

                    if (LeftBrace != -1 && RightBrace != -1)
                    {
                        bool firstAppend = true;
                        GenericInstanceType tempGenericIfaceDef = null;
                        StringBuilder stbParameters = new StringBuilder();

                        // convert to definition
                        tempGenericIfaceDef = tempIfaceRef as GenericInstanceType;

                        if (tempGenericIfaceDef == null)
                            break;

                        strPath = strPath.Remove(LeftBrace, (RightBrace - LeftBrace) + 1);
                        stbParameters.Append("{");
                        foreach (TypeReference tempParam in tempGenericIfaceDef.GenericArguments)
                        {
                            // in "explicit" mode "@" is used as a separator instead of ","
                            // in "normal" mode
                            if (!firstAppend)
                                stbParameters.Append(CanAppendSpecialExplicitChar() ? "@" : ",");

                            GetXmlDocParameterPathRecursive(tempParam, true, stbParameters);
                            firstAppend = false;
                        }
                        stbParameters.Append("}");

                        // insert                             
                        strPath = strPath.Insert(LeftBrace, stbParameters.ToString());
                    }

                    // replace "." with "#"
                    if (CanAppendSpecialExplicitChar())
                        strPath = strPath.Replace(".", "#");

                    return strPath;
                }
            }

            return string.Empty;
        }

        private TypeDefinition TryLookUpTypeDefinition(
            TypeReference Reference)
        {
            // try find in the current assembly
            foreach (TypeDefinition tempTypeDef in Reference.Module.Types)
                if (tempTypeDef.ToString() == Reference.ToString())
                    return tempTypeDef;

            return null;
        }

        private string StripInterfaceName(
            string OrginalName)
        {
            StringBuilder builderStrippedName = new StringBuilder();

            // split name 
            string[] strSlices = OrginalName.Split(new char[] { '`' });

            // remove numbers at the begining of each string to "<" charter
            if (strSlices.Length > 1)
                for (int i = 0; i < strSlices.Length; i++)
                    if (strSlices[i].Contains("<"))
                        strSlices[i] = strSlices[i].Remove(0, strSlices[i].IndexOf("<"));

            // build string
            foreach (string tempString in strSlices)
                builderStrippedName.Append(tempString);

            return builderStrippedName.ToString();
        }

        private void GetXmlDocPathRecursive(
            MemberReference Member,
            List<string> CurrPath)
        {
            /*
             * determine type of the current member, if current path is empty
             * we have also to insert to path element type:
             * - "N:" - for namespace (not used here)
             * - "T:" - for a type (class, structure, delegate)
             * - "M:" - for a method (or constructor)
             * - "F:" - for a field
             * - "P:" - for a property or indexer
             * - "E:" - for an event
             */

            StringBuilder stbTempPath = new StringBuilder();
            string strExplicitPath = string.Empty;

            if (Member is TypeReference)
            {
                TypeReference thisTypeRef = null;
                GenericInstanceType thisGenericTypeDef = null;
                GenericParameter thisGenericParam = null;
                string strTempTypeName = string.Empty;

                if (Member is GenericInstanceType)
                {
                    thisGenericTypeDef = Member as GenericInstanceType;
                    thisTypeRef = thisGenericTypeDef.ElementType;
                }
                else if (Member is GenericParameter)
                {
                    thisGenericParam = Member as GenericParameter;
                    CurrPath.Add("`" + thisGenericParam.Position.ToString());

                    // return immediatelly, because there is nothing to do.
                    return;
                }
                else
                {
                    // cast to TypeReference
                    thisTypeRef = Member as TypeReference;
                }

                // if nested, scan enclosing type 
                if (this.IsNested(thisTypeRef))
                    GetXmlDocPathRecursive(Member.DeclaringType, CurrPath);

                // determine namespace
                string strNamespace = string.Empty;
                if ((thisTypeRef.Namespace != null && thisTypeRef.Namespace.Length > 0) || thisTypeRef.DeclaringType != null)
                    strNamespace = thisTypeRef.Namespace + ".";

                // remove "`" char or not
                string strTempShortTypeName = thisTypeRef.Name;
                if (thisTypeRef.Name.Contains("`") && thisGenericTypeDef != null)
                    strTempShortTypeName = thisTypeRef.Name.Remove(thisTypeRef.Name.IndexOf("`"));

                // class, interface, structure or delegate
                if (CurrPath.Count == 0)
                    strTempTypeName = "T:" + strNamespace + strTempShortTypeName;
                else if (CurrPath.Count > 0 && !this.IsNested(thisTypeRef))
                    strTempTypeName = strNamespace + strTempShortTypeName;
                else
                    strTempTypeName = "." + strTempShortTypeName;

                CurrPath.Add(strTempTypeName);

                // add generic _arguments_ (not parameters !)
                if (thisTypeRef.Name.Contains("`") && thisGenericTypeDef != null)
                {
                    bool firstAppend = true;

                    // open bracket
                    CurrPath.Add("{");

                    foreach (TypeReference tempGenArgument in thisGenericTypeDef.GenericArguments)
                    {
                        // add comma
                        if (!firstAppend)
                            CurrPath.Add(",");

                        // add argument's xmlDocPath
                        GetXmlDocPathRecursive(tempGenArgument as MemberReference, CurrPath);

                        // first append done
                        firstAppend = false;
                    }

                    // close bracket
                    CurrPath.Add("}");
                }
            }
			else if (Member is MethodReference)
            {
				var thisMethodDef = Member as MethodReference;

                // method, get type's path firstAppend
                CurrPath.Add("M:");
                if (Member.DeclaringType != null)
                    GetXmlDocPathRecursive(Member.DeclaringType, CurrPath);

                // method's path
                // check whether this is constructor method, or explicitly implemented method
                strExplicitPath = GetXmlDocExplicitIfaceImplPath(Member);

				//if (thisMethodDef.IsStatic && thisMethodDef.IsConstructor)
				//    stbTempPath.Append(".#cctor");
				//if (!thisMethodDef.IsStatic && thisMethodDef.IsConstructor)
				//    stbTempPath.Append(".#ctor");
				stbTempPath.Append (".");
				if (strExplicitPath.Length > 0)
					stbTempPath.Append (strExplicitPath);
                else
					stbTempPath.Append (thisMethodDef.Name);

                // check whether this method is generic
                if (thisMethodDef.GenericParameters.Count > 0)
					stbTempPath.Append("``").Append (thisMethodDef.GenericParameters.Count);

                if (thisMethodDef.Parameters.Count > 0)
                    stbTempPath.Append("(");
                bool firstAppend = true;
                foreach (ParameterDefinition TempParam in thisMethodDef.Parameters)
                {
                    if (!firstAppend)
                        stbTempPath.Append(",");

                    stbTempPath.Append(GetXmlDocParameterPath(TempParam.ParameterType, false));
                    firstAppend = false;
                }

                if (thisMethodDef.Parameters.Count > 0)
                    stbTempPath.Append(")");

                // check whether this is a conversion operator (implicit or explicit)
                // if so, we have to read return type and add "~" char.
                //if (IsOperator(thisMethodDef))
                //{
                //    OperatorType OpType = GetOperatorType(thisMethodDef);

                //    if (OpType == OperatorType.op_Implicit || OpType == OperatorType.op_Explicit)
                //    {
                //        // add return type parameter path
                //        stbTempPath.Append("~");
                //        stbTempPath.Append(GetXmlDocParameterPath(thisMethodDef.ReturnType, false));
                //    }
                //}

                // add to path
                CurrPath.Add(stbTempPath.ToString());
            }
			else if (Member is FieldReference)
            {
                // field, get type's path name
                CurrPath.Add("F:");
                if (Member.DeclaringType != null)
                    GetXmlDocPathRecursive(Member.DeclaringType, CurrPath);

                // field's path
                CurrPath.Add("." + Member.Name);
            }
			else if (Member is PropertyReference)
            {
                // property or indexer, get declaring type's path 
                CurrPath.Add("P:");
                if (Member.DeclaringType != null)
                    GetXmlDocPathRecursive(Member.DeclaringType, CurrPath);

                // property's path
                // check whether this is explicitly implemented property
                strExplicitPath = GetXmlDocExplicitIfaceImplPath(Member);
				stbTempPath.Append (".");
                if (strExplicitPath.Length > 0)
                    stbTempPath.Append (strExplicitPath);
                else
                    stbTempPath.Append (Member.Name);

                // is it an indexer ?
                bool firstAppend = true;
                PropertyDefinition piProperty = Member as PropertyDefinition;
                if (piProperty.Parameters.Count > 0)
                    stbTempPath.Append("(");

                foreach (ParameterDefinition TempParam in piProperty.Parameters)
                {
                    if (!firstAppend)
                        stbTempPath.Append(",");

                    stbTempPath.Append(GetXmlDocParameterPath(TempParam.ParameterType, false));
                    firstAppend = false;
                }

                if (piProperty.Parameters.Count > 0)
                    stbTempPath.Append(")");

                CurrPath.Add(stbTempPath.ToString());
            }
			else if (Member is EventReference)
            {
                // event, get type's path firstAppend
                CurrPath.Add("E:");
                if (Member.DeclaringType != null)
                    GetXmlDocPathRecursive(Member.DeclaringType, CurrPath);

                // event's path
                CurrPath.Add("." + Member.Name);
            }
        }

        private string GetXmlDocParameterPath(
            TypeReference Type,
            bool ExplicitMode)
        {
            StringBuilder stbCurrPath = new StringBuilder();

            GetXmlDocParameterPathRecursive(Type, ExplicitMode, stbCurrPath);

            return stbCurrPath.ToString();
        }

        private void GetXmlDocParameterPathRecursive(
            TypeReference tpType,
            bool ExplicitMode,
            StringBuilder CurrPath)
        {
            if (tpType == null)
                return;

            if (tpType.GenericParameters.Count > 0)
            {
                CurrPath.Append(tpType.Namespace)
					.Append ((CanAppendSpecialExplicitChar () && ExplicitMode) ? "#" : ".")
					.Append(StripGenericName(tpType.Name));

                // list parameters or types
                bool firstAppend = true;
                CurrPath.Append("{");
                foreach (GenericParameter TempType in tpType.GenericParameters)
                {
                    if (!firstAppend)
                        CurrPath.Append(",");

                    CurrPath.Append(GetXmlDocParameterPath(TempType, ExplicitMode));
                    firstAppend = false;
                }
                CurrPath.Append("}");
            }
            else if (tpType is GenericInstanceType)
            {
                GenericInstanceType thisGenericType = tpType as GenericInstanceType;

                // if nested, scan enclosing type
                if (tpType.DeclaringType != null)
                    CurrPath.Append(GetXmlDocParameterPath(tpType.DeclaringType, ExplicitMode));

                // determine namespace
                string strNamespace = string.Empty;
                if ((tpType.Namespace != null && tpType.Namespace.Length > 0) || tpType.DeclaringType != null)
                {
                    strNamespace = tpType.Namespace +
                        ((CanAppendSpecialExplicitChar() && ExplicitMode) ? "#" : ".");
                }

				CurrPath.Append(strNamespace).Append (StripGenericName(thisGenericType.Name));

                // list parameters or types
                bool firstAppend = true;
                CurrPath.Append("{");
                foreach (TypeReference tempTypeRef in thisGenericType.GenericArguments)
                {
                    if (!firstAppend)
                        CurrPath.Append(",");

                    CurrPath.Append(GetXmlDocParameterPath(tempTypeRef, ExplicitMode));
                    firstAppend = false;
                }
                CurrPath.Append("}");
            }
            else if (tpType is GenericParameter)
            {
                GenericParameter thisGenParam = tpType as GenericParameter;

                if (ExplicitMode)
                {
                    // in explicit mode we print parameter name
                    CurrPath.Append(thisGenParam.Name);
                }
                else
                {
                    // in non-explicit mode we print parameter order
                    int paramOrder = 0;

                    // find
                    for (int i = 0; i < thisGenParam.Owner.GenericParameters.Count; i++)
                    {
                        if (thisGenParam.Owner.GenericParameters[i].Name == tpType.Name)
                        {
                            paramOrder = i;
                            break;
                        }
                    }
                    if (thisGenParam.Owner is MethodReference)
						CurrPath.Append("``").Append (paramOrder);
                    else
						CurrPath.Append("`").Append (paramOrder);
                }
            }
            else if (tpType is PointerType)
            {
                // parameter is pointer type
                CurrPath.Append(GetXmlDocParameterPath((tpType as PointerType).ElementType, ExplicitMode));
                CurrPath.Append("*");
            }
            else if (tpType is ArrayType)
            {
                ArrayType thisArrayType = tpType as ArrayType;
                if (thisArrayType.ElementType != null)
                    CurrPath.Append(GetXmlDocParameterPath(thisArrayType.ElementType, ExplicitMode));

                int iRank = thisArrayType.Rank;
                if (iRank == 1)
                {
                    CurrPath.Append("[]");
                }
                else
                {
                    bool firstAppend = true;
                    CurrPath.Append("[");

                    for (int i = 0; i < (ExplicitMode ? iRank - 1 : iRank); i++)
                    {
                        // in explicit mode for .NET3.5/VS2008, 
                        // there is no separator char "," used for multi-dimensional array,
                        // so there are three cases when comma shall be added:
                        // firstAppend = false; ExplicitMode = false; CanAppendSpecialExplicitChar() = true;
                        // firstAppend = false; ExplicitMode = false; CanAppendSpecialExplicitChar() = false;
                        // firstAppend = false; ExplicitMode = true; CanAppendSpecialExplicitChar() = false;
                        // below this is stored in decent manner
                        if (!firstAppend && (!ExplicitMode || !CanAppendSpecialExplicitChar()))
                            CurrPath.Append(",");

                        CurrPath.Append(((CanAppendSpecialExplicitChar() && ExplicitMode) ? "@" : "0:"));
                        if (thisArrayType.Dimensions[i].UpperBound > 0)
                            CurrPath.Append(thisArrayType.Dimensions[i].UpperBound.ToString());
                        firstAppend = false;
                    }

                    CurrPath.Append("]");
                }
            }
//            else if (!tpType.IsValueType)
//            {
//                // parameter is passed by reference
//                CurrPath.Append(GetXmlDocParameterPath((tpType as ReferenceType).ElementType, false));
//                CurrPath.Append("@");
//            }
//            else if (tpType is ModifierOptional)
//            {
//                // parameter has optional modifier
//                ModifierOptional thisModOpt = tpType as ModifierOptional;
//
//                CurrPath.Append(GetXmlDocParameterPath(thisModOpt.ElementType, ExplicitMode));
//                CurrPath.Append("!");
//                CurrPath.Append(GetXmlDocParameterPath(thisModOpt.ModifierType, ExplicitMode));
//            }
//            else if (tpType is ModifierRequired)
//            {
//                // parameter has required modifier
//                ModifierRequired thisModReq = tpType as ModifierRequired;
//
//                CurrPath.Append(GetXmlDocParameterPath(thisModReq.ElementType, ExplicitMode));
//                CurrPath.Append("|");
//                CurrPath.Append(GetXmlDocParameterPath(thisModReq.ModifierType, ExplicitMode));
//            }
            else if (tpType is FunctionPointerType)
            {
                // type is function pointer
                FunctionPointerType thisFuncPtr = tpType as FunctionPointerType;
//                string tempString = string.Empty;

                // return type
                CurrPath.Append("=FUNC:");
                CurrPath.Append(GetXmlDocParameterPath(thisFuncPtr.ReturnType, ExplicitMode));

                // method's parameters
                if (thisFuncPtr.Parameters.Count > 0)
                {
                    bool firstAppend = true;
                    CurrPath.Append("(");

                    foreach (ParameterDefinition tempParam in thisFuncPtr.Parameters)
                    {
                        if (!firstAppend)
                            CurrPath.Append(",");

                        CurrPath.Append(GetXmlDocParameterPath(tempParam.ParameterType, ExplicitMode));
                        firstAppend = false;
                    }

                    CurrPath.Append(")");
                }
                else
                {
                    CurrPath.Append("(System.Void)");
                }
            }
            else if (tpType is PinnedType)
            {
                // type is pinned type
                CurrPath.Append(GetXmlDocParameterPath((tpType as PinnedType).ElementType, ExplicitMode));
                CurrPath.Append("^");
            }
            else if (tpType is TypeReference)
            {
                // if nested, scan enclosing type
                if (tpType.DeclaringType != null)
                    CurrPath.Append(GetXmlDocParameterPath(tpType.DeclaringType, ExplicitMode));

                // determine namespace
                string strNamespace = string.Empty;
                if ((tpType.Namespace != null && tpType.Namespace.Length > 0) || tpType.DeclaringType != null)
                {
                    strNamespace = tpType.Namespace +
                        ((CanAppendSpecialExplicitChar() && ExplicitMode) ? "#" : ".");
                }

                // concrete type
				CurrPath.Append(strNamespace).Append (
                    ((CanAppendSpecialExplicitChar() && ExplicitMode) ? tpType.Name.Replace(".", "#") : tpType.Name));
            }
        }

        private OperatorType GetOperatorType(MethodDefinition OperatorMethod)
        {
            try
            {
                return (OperatorType)Enum.Parse(typeof(OperatorType), OperatorMethod.Name.Trim());
            }
            catch
            {
                return OperatorType.None;
            }
        }

        public bool IsNested(
            TypeReference Type)
        {
            if (Type.IsNested)
                return true;

            return false;
        }

        private bool IsOperator(MethodDefinition Method)
        {
            if (Method.IsSpecialName && Method.Name.StartsWith("op_"))
                return true;

            return false;
        }

        private bool CanAppendSpecialExplicitChar()
        {
            if (m_compat == CompatibilityType.Net35)
                return true;

            return false;
        }

        private string StripGenericName(string OrginalClassName)
        {
            if (OrginalClassName.IndexOf("`") != -1)
                return OrginalClassName.Remove(OrginalClassName.IndexOf("`"));
            else
                return OrginalClassName;
        }
        #endregion

        #region Private members
        private CompatibilityType m_compat;
        #endregion
    }
    #endregion
}