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

SlashDocMemberFormatter.cs « Formatters « Updater « Mono.Documentation « mdoc - github.com/mono/api-doc-tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 26b2024daf45fb1c68c501bce262fc396dfa01a0 (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
using System;
using System.Collections.Generic;
using System.Text;

using Mono.Cecil;

using Mono.Documentation.Util;

namespace Mono.Documentation.Updater
{
    class SlashDocMemberFormatter : MemberFormatter
    {
        protected override string[] GenericTypeContainer
        {

            get { return new string[] { "{", "}" }; }
        }

        protected bool AddTypeCount = true;

        protected TypeReference genDeclType;
        protected MethodReference genDeclMethod;

        public SlashDocMemberFormatter(TypeMap map) : base(map) { }

        protected override StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, IAttributeParserContext context)
        {
            if (type is GenericParameter)
            {
                int l = buf.Length;
                if (genDeclType != null)
                {
                    IList<GenericParameter> genArgs = genDeclType.GenericParameters;
                    for (int i = 0; i < genArgs.Count; ++i)
                    {
                        if (genArgs[i].Name == type.Name)
                        {
                            buf.Append ('`').Append (i);
                            break;
                        }
                    }
                }
                if (genDeclMethod != null)
                {
                    IList<GenericParameter> genArgs = null;
                    if (genDeclMethod.IsGenericMethod ())
                    {
                        genArgs = genDeclMethod.GenericParameters;
                        for (int i = 0; i < genArgs.Count; ++i)
                        {
                            if (genArgs[i].Name == type.Name)
                            {
                                buf.Append ("``").Append (i);
                                break;
                            }
                        }
                    }
                }
                if ((genDeclType == null && genDeclMethod == null) || buf.Length == l)
                {
                    // Probably from within an explicitly implemented interface member,
                    // where CSC uses parameter names instead of indices (why?), e.g.
                    // MyList`2.Mono#DocTest#Generic#IFoo{A}#Method``1(`0,``0) instead of
                    // MyList`2.Mono#DocTest#Generic#IFoo{`0}#Method``1(`0,``0).
                    buf.Append (type.Name);
                }
            }
            else
            {
                base.AppendTypeName (buf, type, context);
                if (AddTypeCount)
                {
                    int numArgs = type.GenericParameters.Count;
                    if (type.DeclaringType != null)
                        numArgs -= type.GenericParameters.Count;
                    if (numArgs > 0)
                    {
                        buf.Append ('`').Append (numArgs);
                    }
                }
            }
            return buf;
        }

        protected override StringBuilder AppendRefTypeName (StringBuilder buf, ByReferenceType type, IAttributeParserContext context)
        {
            return base.AppendRefTypeName (buf, type, context).Append (RefTypeModifier);
        }

        protected override StringBuilder AppendArrayModifiers (StringBuilder buf, ArrayType array)
        {
            buf.Append (ArrayDelimeters[0]);
            int rank = array.Rank;
            if (rank > 1)
            {
                buf.Append ("0:");
                for (int i = 1; i < rank; ++i)
                {
                    buf.Append (",0:");
                }
            }
            return buf.Append (ArrayDelimeters[1]);
        }

        protected override StringBuilder AppendGenericType (StringBuilder buf, TypeReference type, IAttributeParserContext context, bool appendGeneric = true, bool useTypeProjection = false, bool isTypeofOperator = false)
        {
            if (!AddTypeCount)
                base.AppendGenericType (buf, type, context);
            else
                AppendType (buf, type, context);
            return buf;
        }

        private StringBuilder AppendType (StringBuilder buf, TypeReference type, IAttributeParserContext context)
        {
            List<TypeReference> decls = DocUtils.GetDeclaringTypes (type);
            bool insertNested = false;
            int prevParamCount = 0;
            foreach (var decl in decls)
            {
                if (insertNested)
                    buf.Append (NestedTypeSeparator);
                insertNested = true;
                base.AppendTypeName (buf, decl, context);
                int argCount = DocUtils.GetGenericArgumentCount (decl);
                int numArgs = argCount - prevParamCount;
                prevParamCount = argCount;
                if (numArgs > 0)
                    buf.Append ('`').Append (numArgs);
            }
            return buf;
        }

        protected override string GetConstructorName (MethodReference constructor)
        {
            return GetMethodDefinitionName (constructor, "#ctor");
        }

        protected override string GetMethodName (MethodReference method)
        {
            string name = null;
            MethodDefinition methodDef = method as MethodDefinition;
            if (methodDef == null || !DocUtils.IsExplicitlyImplemented (methodDef))
                name = method.Name;
            else
            {
                TypeReference iface;
                MethodReference ifaceMethod;
                DocUtils.GetInfoForExplicitlyImplementedMethod (methodDef, out iface, out ifaceMethod);
                AddTypeCount = false;
                name = GetTypeName (iface) + "." + ifaceMethod.Name;
                AddTypeCount = true;
            }
            return GetMethodDefinitionName (method, name);
        }

        private string GetMethodDefinitionName (MethodReference method, string name)
        {
            StringBuilder buf = new StringBuilder ();
            buf.Append (GetTypeName (method.DeclaringType));
            buf.Append ('.');
            buf.Append (name.Replace (".", "#"));
            if (method.IsGenericMethod ())
            {
                IList<GenericParameter> genArgs = method.GenericParameters;
                if (genArgs.Count > 0)
                    buf.Append ("``").Append (genArgs.Count);
            }
            IList<ParameterDefinition> parameters = method.Parameters;
            try
            {
                genDeclType = method.DeclaringType;
                genDeclMethod = method;
                AppendParameters (buf, method.DeclaringType.GenericParameters, parameters);
            }
            finally
            {
                genDeclType = null;
                genDeclMethod = null;
            }
            return buf.ToString ();
        }

        private StringBuilder AppendParameters (StringBuilder buf, IList<GenericParameter> genArgs, IList<ParameterDefinition> parameters)
        {
            if (parameters.Count == 0)
                return buf;

            buf.Append ('(');

            AppendParameter (buf, genArgs, parameters[0]);
            for (int i = 1; i < parameters.Count; ++i)
            {
                buf.Append (',');
                AppendParameter (buf, genArgs, parameters[i]);
            }

            return buf.Append (')');
        }

        private StringBuilder AppendParameter (StringBuilder buf, IList<GenericParameter> genArgs, ParameterDefinition parameter)
        {
            AddTypeCount = false;
            buf.Append (GetTypeName (parameter.ParameterType));
            AddTypeCount = true;
            return buf;
        }

        protected override string GetPropertyName (PropertyReference property)
        {
            string name = EiiPropertyProcessing(property);

            StringBuilder buf = new StringBuilder ();
            buf.Append (GetName (property.DeclaringType));
            buf.Append ('.');
            buf.Append (name);
            IList<ParameterDefinition> parameters = property.Parameters;
            if (parameters.Count > 0)
            {
                genDeclType = property.DeclaringType;
                buf.Append ('(');
                IList<GenericParameter> genArgs = property.DeclaringType.GenericParameters;
                AppendParameter (buf, genArgs, parameters[0]);
                for (int i = 1; i < parameters.Count; ++i)
                {
                    buf.Append (',');
                    AppendParameter (buf, genArgs, parameters[i]);
                }
                buf.Append (')');
                genDeclType = null;
            }
            return buf.ToString ();
        }

        /// <summary>
        /// Vb, Cpp and F# compilers uses simple logic to generate docIds for Eii properties. No need to have special logic here.
        /// </summary>
        /// <param name="property">Value to process</param>
        /// <returns>DocId for property member wchich needs to be equal to docId generated by compiler's /doc flag </returns>
        protected virtual string EiiPropertyProcessing(PropertyReference property)
        {
            return property.Name;
        }

        protected override string GetFieldName (FieldReference field)
        {
            return string.Format ("{0}.{1}",
                GetName (field.DeclaringType), field.Name);
        }

        protected override string GetEventName (EventReference e)
        {
            return string.Format ("{0}.{1}",
                GetName (e.DeclaringType), e.Name);
        }

        protected override string GetTypeDeclaration (TypeDefinition type)
        {
            string name = GetName (type);
            if (type == null)
                return null;
            return "T:" + name;
        }

        protected override string GetConstructorDeclaration (MethodDefinition constructor)
        {
            string name = GetName (constructor);
            if (name == null)
                return null;
            return "M:" + name;
        }

        protected override string GetMethodDeclaration (MethodDefinition method)
        {
            string name = GetName (method);
            if (name == null)
                return null;
            if (method.Name == "op_Implicit" || method.Name == "op_Explicit")
            {
                genDeclType = method.DeclaringType;
                genDeclMethod = method;
                name += "~" + GetName (method.ReturnType);
                genDeclType = null;
                genDeclMethod = null;
            }
            return "M:" + name;
        }

        protected override string GetPropertyDeclaration (PropertyDefinition property)
        {
            string name = GetName (property);
            if (name == null)
                return null;
            return "P:" + name;
        }

        protected override string GetFieldDeclaration (FieldDefinition field)
        {
            string name = GetName (field);
            if (name == null)
                return null;
            return "F:" + name;
        }

        protected override string GetEventDeclaration (EventDefinition e)
        {
            string name = GetName (e);
            if (name == null)
                return null;
            return "E:" + name;
        }

        protected override string GetAttachedEventDeclaration(AttachedEventDefinition e)
        {
            string name = GetName(e);
            if (name == null)
                return null;
            return "E:" + name;
        }

        protected override string GetAttachedPropertyDeclaration(AttachedPropertyDefinition a)
        {
            string name = GetName(a);
            if (name == null)
                return null;
            return "P:" + name;
        }
    }
}