Debian Codegraph @

external/api-doc-tools/mdoc/Mono.Documentation/Updater/Formatters/SlashDocMemberFormatter.cs

raw
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 using Mono.Cecil;
6
7 using Mono.Documentation.Util;
8
9 namespace Mono.Documentation.Updater
10 {
11 class SlashDocMemberFormatter : MemberFormatter
12 {
13 protected override string[] GenericTypeContainer
14 {
15
16 get { return new string[] { "{", "}" }; }
17 }
18
19 protected bool AddTypeCount = true;
20
21 protected TypeReference genDeclType;
22 protected MethodReference genDeclMethod;
23
24 protected override StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context)
25 {
26 if (type is GenericParameter)
27 {
28 int l = buf.Length;
29 if (genDeclType != null)
30 {
31 IList<GenericParameter> genArgs = genDeclType.GenericParameters;
32 for (int i = 0; i < genArgs.Count; ++i)
33 {
34 if (genArgs[i].Name == type.Name)
35 {
36 buf.Append ('`').Append (i);
37 break;
38 }
39 }
40 }
41 if (genDeclMethod != null)
42 {
43 IList<GenericParameter> genArgs = null;
44 if (genDeclMethod.IsGenericMethod ())
45 {
46 genArgs = genDeclMethod.GenericParameters;
47 for (int i = 0; i < genArgs.Count; ++i)
48 {
49 if (genArgs[i].Name == type.Name)
50 {
51 buf.Append ("``").Append (i);
52 break;
53 }
54 }
55 }
56 }
57 if ((genDeclType == null && genDeclMethod == null) || buf.Length == l)
58 {
59 // Probably from within an explicitly implemented interface member,
60 // where CSC uses parameter names instead of indices (why?), e.g.
61 // MyList`2.Mono#DocTest#Generic#IFoo{A}#Method``1(`0,``0) instead of
62 // MyList`2.Mono#DocTest#Generic#IFoo{`0}#Method``1(`0,``0).
63 buf.Append (type.Name);
64 }
65 }
66 else
67 {
68 base.AppendTypeName (buf, type, context);
69 if (AddTypeCount)
70 {
71 int numArgs = type.GenericParameters.Count;
72 if (type.DeclaringType != null)
73 numArgs -= type.GenericParameters.Count;
74 if (numArgs > 0)
75 {
76 buf.Append ('`').Append (numArgs);
77 }
78 }
79 }
80 return buf;
81 }
82
83 protected override StringBuilder AppendRefTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context)
84 {
85 return base.AppendRefTypeName (buf, type, context).Append (RefTypeModifier);
86 }
87
88 protected override StringBuilder AppendArrayModifiers (StringBuilder buf, ArrayType array)
89 {
90 buf.Append (ArrayDelimeters[0]);
91 int rank = array.Rank;
92 if (rank > 1)
93 {
94 buf.Append ("0:");
95 for (int i = 1; i < rank; ++i)
96 {
97 buf.Append (",0:");
98 }
99 }
100 return buf.Append (ArrayDelimeters[1]);
101 }
102
103 protected override StringBuilder AppendGenericType (StringBuilder buf, TypeReference type, DynamicParserContext context, bool appendGeneric = true)
104 {
105 if (!AddTypeCount)
106 base.AppendGenericType (buf, type, context);
107 else
108 AppendType (buf, type, context);
109 return buf;
110 }
111
112 private StringBuilder AppendType (StringBuilder buf, TypeReference type, DynamicParserContext context)
113 {
114 List<TypeReference> decls = DocUtils.GetDeclaringTypes (type);
115 bool insertNested = false;
116 int prevParamCount = 0;
117 foreach (var decl in decls)
118 {
119 if (insertNested)
120 buf.Append (NestedTypeSeparator);
121 insertNested = true;
122 base.AppendTypeName (buf, decl, context);
123 int argCount = DocUtils.GetGenericArgumentCount (decl);
124 int numArgs = argCount - prevParamCount;
125 prevParamCount = argCount;
126 if (numArgs > 0)
127 buf.Append ('`').Append (numArgs);
128 }
129 return buf;
130 }
131
132 protected override string GetConstructorName (MethodReference constructor)
133 {
134 return GetMethodDefinitionName (constructor, "#ctor");
135 }
136
137 protected override string GetMethodName (MethodReference method)
138 {
139 string name = null;
140 MethodDefinition methodDef = method as MethodDefinition;
141 if (methodDef == null || !DocUtils.IsExplicitlyImplemented (methodDef))
142 name = method.Name;
143 else
144 {
145 TypeReference iface;
146 MethodReference ifaceMethod;
147 DocUtils.GetInfoForExplicitlyImplementedMethod (methodDef, out iface, out ifaceMethod);
148 AddTypeCount = false;
149 name = GetTypeName (iface) + "." + ifaceMethod.Name;
150 AddTypeCount = true;
151 }
152 return GetMethodDefinitionName (method, name);
153 }
154
155 private string GetMethodDefinitionName (MethodReference method, string name)
156 {
157 StringBuilder buf = new StringBuilder ();
158 buf.Append (GetTypeName (method.DeclaringType));
159 buf.Append ('.');
160 buf.Append (name.Replace (".", "#"));
161 if (method.IsGenericMethod ())
162 {
163 IList<GenericParameter> genArgs = method.GenericParameters;
164 if (genArgs.Count > 0)
165 buf.Append ("``").Append (genArgs.Count);
166 }
167 IList<ParameterDefinition> parameters = method.Parameters;
168 try
169 {
170 genDeclType = method.DeclaringType;
171 genDeclMethod = method;
172 AppendParameters (buf, method.DeclaringType.GenericParameters, parameters);
173 }
174 finally
175 {
176 genDeclType = null;
177 genDeclMethod = null;
178 }
179 return buf.ToString ();
180 }
181
182 private StringBuilder AppendParameters (StringBuilder buf, IList<GenericParameter> genArgs, IList<ParameterDefinition> parameters)
183 {
184 if (parameters.Count == 0)
185 return buf;
186
187 buf.Append ('(');
188
189 AppendParameter (buf, genArgs, parameters[0]);
190 for (int i = 1; i < parameters.Count; ++i)
191 {
192 buf.Append (',');
193 AppendParameter (buf, genArgs, parameters[i]);
194 }
195
196 return buf.Append (')');
197 }
198
199 private StringBuilder AppendParameter (StringBuilder buf, IList<GenericParameter> genArgs, ParameterDefinition parameter)
200 {
201 AddTypeCount = false;
202 buf.Append (GetTypeName (parameter.ParameterType));
203 AddTypeCount = true;
204 return buf;
205 }
206
207 protected override string GetPropertyName (PropertyReference property)
208 {
209 string name = EiiPropertyProcessing(property);
210
211 StringBuilder buf = new StringBuilder ();
212 buf.Append (GetName (property.DeclaringType));
213 buf.Append ('.');
214 buf.Append (name);
215 IList<ParameterDefinition> parameters = property.Parameters;
216 if (parameters.Count > 0)
217 {
218 genDeclType = property.DeclaringType;
219 buf.Append ('(');
220 IList<GenericParameter> genArgs = property.DeclaringType.GenericParameters;
221 AppendParameter (buf, genArgs, parameters[0]);
222 for (int i = 1; i < parameters.Count; ++i)
223 {
224 buf.Append (',');
225 AppendParameter (buf, genArgs, parameters[i]);
226 }
227 buf.Append (')');
228 genDeclType = null;
229 }
230 return buf.ToString ();
231 }
232
233 /// <summary>
234 /// Vb, Cpp and F# compilers uses simple logic to generate docIds for Eii properties. No need to have special logic here.
235 /// </summary>
236 /// <param name="property">Value to process</param>
237 /// <returns>DocId for property member wchich needs to be equal to docId generated by compiler's /doc flag </returns>
238 protected virtual string EiiPropertyProcessing(PropertyReference property)
239 {
240 return property.Name;
241 }
242
243 protected override string GetFieldName (FieldReference field)
244 {
245 return string.Format ("{0}.{1}",
246 GetName (field.DeclaringType), field.Name);
247 }
248
249 protected override string GetEventName (EventReference e)
250 {
251 return string.Format ("{0}.{1}",
252 GetName (e.DeclaringType), e.Name);
253 }
254
255 protected override string GetTypeDeclaration (TypeDefinition type)
256 {
257 string name = GetName (type);
258 if (type == null)
259 return null;
260 return "T:" + name;
261 }
262
263 protected override string GetConstructorDeclaration (MethodDefinition constructor)
264 {
265 string name = GetName (constructor);
266 if (name == null)
267 return null;
268 return "M:" + name;
269 }
270
271 protected override string GetMethodDeclaration (MethodDefinition method)
272 {
273 string name = GetName (method);
274 if (name == null)
275 return null;
276 if (method.Name == "op_Implicit" || method.Name == "op_Explicit")
277 {
278 genDeclType = method.DeclaringType;
279 genDeclMethod = method;
280 name += "~" + GetName (method.ReturnType);
281 genDeclType = null;
282 genDeclMethod = null;
283 }
284 return "M:" + name;
285 }
286
287 protected override string GetPropertyDeclaration (PropertyDefinition property)
288 {
289 string name = GetName (property);
290 if (name == null)
291 return null;
292 return "P:" + name;
293 }
294
295 protected override string GetFieldDeclaration (FieldDefinition field)
296 {
297 string name = GetName (field);
298 if (name == null)
299 return null;
300 return "F:" + name;
301 }
302
303 protected override string GetEventDeclaration (EventDefinition e)
304 {
305 string name = GetName (e);
306 if (name == null)
307 return null;
308 return "E:" + name;
309 }
310
311 protected override string GetAttachedEventDeclaration(AttachedEventDefinition e)
312 {
313 string name = GetName(e);
314 if (name == null)
315 return null;
316 return "E:" + name;
317 }
318
319 protected override string GetAttachedPropertyDeclaration(AttachedPropertyDefinition a)
320 {
321 string name = GetName(a);
322 if (name == null)
323 return null;
324 return "P:" + name;
325 }
326 }
327 }