Debian Codegraph @

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

raw
1 using System;
2 using System.Collections.Generic;
3 using System.Globalization;
4 using System.Linq;
5 using System.Text;
6 using Mono.Cecil;
7 using Mono.Documentation.Util;
8
9 namespace Mono.Documentation.Updater
10 {
11 public class VBFullMemberFormatter : MemberFormatter
12 {
13 public override string Language => Consts.VbNet;
14
15 protected override StringBuilder AppendNamespace(StringBuilder buf, TypeReference type)
16 {
17 string ns = DocUtils.GetNamespace(type);
18 if (GetVBType(type.FullName) == null && !string.IsNullOrEmpty(ns) && ns != "System")
19 buf.Append(ns).Append('.');
20 return buf;
21 }
22
23 protected virtual string GetVBType(string t)
24 {
25 // make sure there are no modifiers in the type string (add them back before returning)
26 string typeToCompare = t;
27 string[] splitType = null;
28 if (t.Contains(' '))
29 {
30 splitType = t.Split(' ');
31 typeToCompare = splitType[0];
32 }
33
34 switch (typeToCompare)
35 {
36 case "System.Byte": typeToCompare = "Byte"; break;
37 case "System.SByte": typeToCompare = "SByte"; break;
38 case "System.Int16": typeToCompare = "Short"; break;
39 case "System.Int32": typeToCompare = "Integer"; break;
40 case "System.Int64": typeToCompare = "Long"; break;
41
42 case "System.UInt16": typeToCompare = "UShort"; break;
43 case "System.UInt32": typeToCompare = "UInteger"; break;
44 case "System.UInt64": typeToCompare = "ULong"; break;
45
46 case "System.Single": typeToCompare = "Single"; break;
47 case "System.Double": typeToCompare = "Double"; break;
48 case "System.Decimal": typeToCompare = "Decimal"; break;
49 case "System.Boolean": typeToCompare = "Boolean"; break;
50 case "System.Char": typeToCompare = "Char"; break;
51 case "System.String": typeToCompare = "String"; break;
52 case "System.Object": typeToCompare = "Object"; break;
53 }
54
55 if (splitType != null)
56 {
57 // re-add modreq/modopt if it was there
58 splitType[0] = typeToCompare;
59 typeToCompare = string.Join(" ", splitType);
60 }
61 return typeToCompare == t ? null : typeToCompare;
62 }
63
64 protected override StringBuilder AppendTypeName(StringBuilder buf, TypeReference type, DynamicParserContext context)
65 {
66 if (type is GenericParameter)
67 return AppendGenericParameterConstraints(buf, (GenericParameter)type, context).Append(type.Name);
68 string t = type.FullName;
69 if (!t.StartsWith("System."))
70 {
71 return base.AppendTypeName(buf, type, context);
72 }
73
74 string s = GetVBType(t);
75 if (s != null)
76 {
77 if (context != null)
78 context.TransformIndex++;
79 return buf.Append(s);
80 }
81
82 return base.AppendTypeName(buf, type, context);
83 }
84
85 private StringBuilder AppendGenericParameterConstraints(StringBuilder buf, GenericParameter type, DynamicParserContext context)
86 {
87 if (MemberFormatterState != MemberFormatterState.WithinGenericTypeParameters)
88 return buf;
89 GenericParameterAttributes attrs = type.Attributes;
90 bool isout = (attrs & GenericParameterAttributes.Covariant) != 0;
91 bool isin = (attrs & GenericParameterAttributes.Contravariant) != 0;
92 if (isin)
93 buf.Append("In ");
94 else if (isout)
95 buf.Append("Out ");
96 return buf;
97 }
98
99 protected override string GetTypeDeclaration(TypeDefinition type)
100 {
101 string visibility = GetTypeVisibility(type.Attributes);
102 if (visibility == null)
103 return null;
104
105 StringBuilder buf = new StringBuilder();
106
107 buf.Append(visibility);
108 buf.Append(" ");
109
110 MemberFormatter full = new VBMemberFormatter();
111 if (DocUtils.IsDelegate(type))
112 {
113 buf.Append("Delegate ");
114 MethodDefinition invoke = type.GetMethod("Invoke");
115 bool isFunction = invoke.ReturnType.FullName != "System.Void";
116 if (isFunction)
117 buf.Append("Function ");
118 else
119 buf.Append("Sub ");
120 buf.Append(GetName(type));
121 AppendParameters(buf, invoke, invoke.Parameters);
122 if (isFunction)
123 {
124 buf.Append(" As ");
125 buf.Append(full.GetName(invoke.ReturnType, new DynamicParserContext(invoke.MethodReturnType))).Append(" ");
126 }
127
128 return buf.ToString();
129 }
130
131 if (type.IsAbstract && !type.IsInterface && !IsModule(type))
132 buf.Append("MustInherit ");
133 if (type.IsSealed && !DocUtils.IsDelegate(type) && !type.IsValueType && !IsModule(type))
134 buf.Append("NotInheritable ");
135 buf.Replace(" MustInherit NotInheritable", "");
136
137 buf.Append(GetTypeKind(type));
138 buf.Append(" ");
139 buf.Append(GetVBType(type.FullName) == null
140 ? GetName(type)
141 : type.Name);
142
143 if (!type.IsEnum)
144 {
145 TypeReference basetype = type.BaseType;
146 if (basetype != null && basetype.FullName == "System.Object" || type.IsValueType)
147 basetype = null;
148
149 if (basetype != null)
150 {
151 buf.Append(GetLineEnding()).Append("Inherits ");
152 buf.Append(full.GetName(basetype));
153 }
154
155 List<string> interfaceNames = DocUtils.GetUserImplementedInterfaces(type)
156 .Select(iface => full.GetName(iface))
157 .OrderBy(s => s)
158 .ToList();
159 if (interfaceNames.Count > 0)
160 {
161 buf.Append(GetLineEnding()).Append("Implements ");
162 buf.Append(string.Join(", ", interfaceNames));
163 }
164 }
165
166 return buf.ToString();
167 }
168
169 protected override string[] GenericTypeContainer
170 {
171 get { return new[] {"(Of ", ")"}; }
172 }
173
174 static string GetTypeKind(TypeDefinition t)
175 {
176 if (IsModule(t))
177 return "Module";
178 if (t.IsEnum)
179 return "Enum";
180 if (t.IsValueType)
181 return "Structure";
182 if (t.IsClass || t.FullName == "System.Enum")
183 return "Class";
184 if (t.IsInterface)
185 return "Interface";
186 throw new ArgumentException(t.FullName);
187 }
188
189 static string GetTypeVisibility(TypeAttributes ta)
190 {
191 switch (ta & TypeAttributes.VisibilityMask)
192 {
193 case TypeAttributes.Public:
194 case TypeAttributes.NestedPublic:
195 return "Public";
196
197 case TypeAttributes.NestedFamily:
198 return "Protected";
199 case TypeAttributes.NestedFamORAssem:
200 return "Protected Friend";
201
202 default:
203 return null;
204 }
205 }
206
207 protected override StringBuilder AppendGenericType(StringBuilder buf, TypeReference type, DynamicParserContext context, bool appendGeneric = true)
208 {
209 List<TypeReference> decls = DocUtils.GetDeclaringTypes(
210 type is GenericInstanceType ? type.GetElementType() : type);
211 List<TypeReference> genArgs = GetGenericArguments(type);
212 int argIdx = 0;
213 int displayedInParentArguments = 0;
214 bool insertNested = false;
215 foreach (var decl in decls)
216 {
217 TypeReference declDef = decl.Resolve() ?? decl;
218 if (insertNested)
219 {
220 buf.Append(NestedTypeSeparator);
221 }
222 insertNested = true;
223
224 AppendTypeName(buf, declDef, context);
225
226 int argumentCount = DocUtils.GetGenericArgumentCount(declDef);
227 int notYetDisplayedArguments = argumentCount - displayedInParentArguments;
228 displayedInParentArguments = argumentCount;// nested TypeReferences have parents' generic arguments, but we shouldn't display them
229 if (notYetDisplayedArguments > 0)
230 {
231 buf.Append(GenericTypeContainer[0]);
232 var origState = MemberFormatterState;
233 MemberFormatterState = MemberFormatterState.WithinGenericTypeParameters;
234 for (int i = 0; i < notYetDisplayedArguments; ++i)
235 {
236 if (i > 0)
237 buf.Append(", ");
238 var genArg = genArgs[argIdx++];
239 _AppendTypeName(buf, genArg, context);
240 var genericParameter = genArg as GenericParameter;
241 if (genericParameter != null)
242 AppendConstraints(buf, genericParameter);
243 }
244 MemberFormatterState = origState;
245 buf.Append(GenericTypeContainer[1]);
246 }
247 }
248 return buf;
249 }
250
251 protected override StringBuilder AppendGenericTypeConstraints(StringBuilder buf, TypeReference type)
252 {
253 return buf;
254 }
255
256 private void AppendConstraints(StringBuilder buf, GenericParameter genArg)
257 {
258 if (MemberFormatterState == MemberFormatterState.WithinGenericTypeParameters)
259 {
260 // check to avoid such a code:
261 // Public Class MyList(Of A As {Class, Generic.IList(Of B --->As {Class, A}<----), New}, B As {Class, A})
262 return;
263 }
264
265 GenericParameterAttributes attrs = genArg.Attributes;
266 #if NEW_CECIL
267 Mono.Collections.Generic.Collection<GenericParameterConstraint> constraints = genArg.Constraints;
268 #else
269 IList<TypeReference> constraints = genArg.Constraints;
270 #endif
271 if (attrs == GenericParameterAttributes.NonVariant && constraints.Count == 0)
272 return;
273
274 bool isref = (attrs & GenericParameterAttributes.ReferenceTypeConstraint) != 0;
275 bool isvt = (attrs & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0;
276 bool isnew = (attrs & GenericParameterAttributes.DefaultConstructorConstraint) != 0;
277 bool comma = false;
278
279 if (!isref && !isvt && !isnew && constraints.Count == 0)
280 return;
281 int constraintsCount = Convert.ToInt32(isref) + Convert.ToInt32(isvt) + Convert.ToInt32(isnew) + constraints.Count;
282 buf.Append(" As ");
283
284 if (constraintsCount > 1 && !isvt)
285 buf.Append("{");
286 if (isref)
287 {
288 buf.Append("Class");
289 comma = true;
290 }
291 else if (isvt)
292 {
293 buf.Append("Structure");
294 comma = true;
295 }
296 if (constraints.Count > 0 && !isvt)
297 {
298 if (comma)
299 buf.Append(", ");
300
301 #if NEW_CECIL
302 buf.Append(GetTypeName(constraints[0].ConstraintType));
303 for (int i = 1; i < constraints.Count; ++i)
304 buf.Append(", ").Append(GetTypeName(constraints[i].ConstraintType));
305 #else
306 buf.Append(GetTypeName(constraints[0]));
307 for (int i = 1; i < constraints.Count; ++i)
308 buf.Append(", ").Append(GetTypeName(constraints[i]));
309 #endif
310 }
311 if (isnew && !isvt)
312 {
313 if (comma)
314 buf.Append(", ");
315 buf.Append("New");
316 }
317 if (constraintsCount > 1 && !isvt)
318 buf.Append("}");
319 }
320
321 protected override string GetConstructorDeclaration(MethodDefinition constructor)
322 {
323 StringBuilder buf = new StringBuilder();
324 AppendVisibility(buf, constructor);
325 if (buf.Length == 0)
326 return null;
327
328 buf.Append(" Sub New ");
329 AppendParameters(buf, constructor, constructor.Parameters);
330
331 return buf.ToString();
332 }
333
334 protected override string GetMethodDeclaration(MethodDefinition method)
335 {
336 if (method.HasCustomAttributes && method.CustomAttributes.Cast<CustomAttribute>().Any(
337 ca => ca.GetDeclaringType() == "System.Diagnostics.Contracts.ContractInvariantMethodAttribute"))
338 return null;
339
340
341 // Special signature for destructors.
342 if (method.Name == "Finalize" && method.Parameters.Count == 0)
343 return GetFinalizerName(method);
344
345 StringBuilder buf = new StringBuilder();
346 if (DocUtils.IsExtensionMethod(method))
347 buf.Append("<Extension()>" + GetLineEnding());
348
349 AppendVisibility(buf, method);
350 if (buf.Length == 0 &&
351 !(DocUtils.IsExplicitlyImplemented(method) && !method.IsSpecialName))
352 return null;
353
354 AppendModifiers(buf, method);
355 if (buf.Length != 0)
356 buf.Append(" ");
357 bool isFunction = method.MethodReturnType.ReturnType.FullName != "System.Void";
358 if (!DocUtils.IsOperator(method))
359 {
360 if (isFunction)
361 buf.Append("Function ");
362 else
363 buf.Append("Sub ");
364 }
365 AppendMethodName(buf, method);
366
367 AppendGenericMethod(buf, method).Append(" ");
368 AppendParameters(buf, method, method.Parameters);
369 AppendGenericMethodConstraints(buf, method);
370 if (isFunction)
371 buf.Append(" As ").Append(GetTypeName(method.ReturnType, new DynamicParserContext(method.MethodReturnType)));
372
373 if (DocUtils.IsExplicitlyImplemented(method))
374 {
375 TypeReference iface;
376 MethodReference ifaceMethod;
377 DocUtils.GetInfoForExplicitlyImplementedMethod(method, out iface, out ifaceMethod);
378 buf.Append(" Implements ")
379 .Append(new VBMemberFormatter().GetName(iface))
380 .Append('.')
381 .Append(ifaceMethod.Name);
382 }
383
384 return buf.ToString();
385 }
386
387 protected override StringBuilder AppendMethodName(StringBuilder buf, MethodDefinition method)
388 {
389 if (DocUtils.IsExplicitlyImplemented(method))
390 {
391 return buf.Append(method.Name.Split('.').Last());
392 }
393
394 if (DocUtils.IsOperator(method))
395 {
396 // this is an operator
397 switch (method.Name)
398 {
399 case "op_Implicit":
400 case "op_Explicit":
401 buf.Length--; // remove the last space, which assumes a member name is coming
402 return buf;
403 case "op_Addition":
404 case "op_UnaryPlus":
405 return buf.Append("Operator +");
406 case "op_Subtraction":
407 case "op_UnaryNegation":
408 return buf.Append("Operator -");
409 case "op_IntegerDivision":
410 return buf.Append("Operator \\");
411 case "op_Division":
412 return buf.Append("Operator /");
413 case "op_Multiply":
414 return buf.Append("Operator *");
415 case "op_Modulus":
416 return buf.Append("Operator Mod");
417 case "op_BitwiseAnd":
418 return buf.Append("Operator And");
419 case "op_BitwiseOr":
420 return buf.Append("Operator Or");
421 case "op_ExclusiveOr":
422 return buf.Append("Operator Xor");
423 case "op_LeftShift":
424 return buf.Append("Operator <<");
425 case "op_RightShift":
426 return buf.Append("Operator >>");
427 case "op_LogicalNot":
428 return buf.Append("Operator Not");
429 case "op_OnesComplement":
430 return buf.Append("Operator Not");
431 case "op_True":
432 return buf.Append("Operator IsTrue");
433 case "op_False":
434 return buf.Append("Operator IsFalse");
435 case "op_Equality":
436 return buf.Append("Operator ==");
437 case "op_Inequality":
438 return buf.Append("Operator !=");
439 case "op_LessThan":
440 return buf.Append("Operator <");
441 case "op_LessThanOrEqual":
442 return buf.Append("Operator <=");
443 case "op_GreaterThan":
444 return buf.Append("Operator >");
445 case "op_GreaterThanOrEqual":
446 return buf.Append("Operator >=");
447 case "op_Like":
448 return buf.Append("Operator Like");
449 default:
450 return base.AppendMethodName(buf, method);
451 }
452 }
453 else
454 return base.AppendMethodName(buf, method);
455 }
456
457 protected override StringBuilder AppendGenericMethodConstraints(StringBuilder buf, MethodDefinition method)
458 {
459 return buf;
460 }
461
462 protected override string RefTypeModifier
463 {
464 get { return ""; }
465 }
466
467 protected override string GetFinalizerName(MethodDefinition method)
468 {
469 return method.Name + " ()";
470 }
471
472 protected override StringBuilder AppendVisibility(StringBuilder buf, MethodDefinition method)
473 {
474 if (method == null)
475 return buf;
476 if (method.IsPublic)
477 return buf.Append("Public");
478 if (method.IsFamily)
479 return buf.Append("Protected");
480 if (method.IsFamilyOrAssembly)
481 return buf.Append("Protected Friend");
482 return buf;
483 }
484
485 protected override StringBuilder AppendModifiers(StringBuilder buf, MethodDefinition method)
486 {
487 string modifiers = String.Empty;
488 if (method.IsStatic && !IsModule(method.DeclaringType)) modifiers += " Shared";
489 if (IsIteratorMethod(method)) modifiers += " Iterator";
490 if (method.IsVirtual && !method.IsAbstract)
491 {
492 if ((method.Attributes & MethodAttributes.NewSlot) != 0) modifiers += " Overridable";
493 else modifiers += " Overrides";
494 }
495 TypeDefinition declType = (TypeDefinition)method.DeclaringType;
496 if (method.IsAbstract && !declType.IsInterface) modifiers += " MustOverride";
497 if (method.IsFinal) modifiers += " NotOverridable";
498 if (modifiers == " MustOverride NotOverridable") modifiers = "";
499 if (modifiers == " Overridable NotOverridable") modifiers = "";
500
501 switch (method.Name)
502 {
503 case "op_Implicit":
504 modifiers += " Widening Operator CType";
505 break;
506 case "op_Explicit":
507 modifiers += " Narrowing Operator CType";
508 break;
509 }
510
511 return buf.Append(modifiers);
512 }
513
514 protected override StringBuilder AppendGenericMethod(StringBuilder buf, MethodDefinition method)
515 {
516 if (method.IsGenericMethod())
517 {
518 IList<GenericParameter> args = method.GenericParameters;
519 if (args.Count > 0)
520 {
521 buf.Append(GenericTypeContainer[0]);
522 buf.Append(args[0].Name);
523 AppendConstraints(buf, args[0]);
524 for (int i = 1; i < args.Count; ++i)
525 {
526 buf.Append(", ").Append(args[i].Name);
527 AppendConstraints(buf, args[0]);
528 }
529 buf.Append(GenericTypeContainer[1]);
530 }
531 }
532 return buf;
533 }
534
535 protected override StringBuilder AppendParameters(StringBuilder buf, MethodDefinition method, IList<ParameterDefinition> parameters)
536 {
537 return AppendParameters(buf, method, parameters, '(', ')');
538 }
539
540 private StringBuilder AppendParameters(StringBuilder buf, MethodDefinition method, IList<ParameterDefinition> parameters, char begin, char end)
541 {
542 buf.Append(begin);
543
544 if (parameters.Count > 0)
545 {
546 AppendParameter(buf, parameters[0]);
547 for (int i = 1; i < parameters.Count; ++i)
548 {
549 buf.Append(", ");
550 AppendParameter(buf, parameters[i]);
551 }
552 }
553
554 return buf.Append(end);
555 }
556
557 private StringBuilder AppendParameter(StringBuilder buf, ParameterDefinition parameter)
558 {
559 if (parameter.IsOptional)
560 {
561 buf.Append("Optional ");
562 }
563 if (parameter.ParameterType is ByReferenceType)
564 {
565 buf.Append("ByRef ");
566 }
567 if (parameter.HasCustomAttributes)
568 {
569 var isParams = parameter.CustomAttributes.Any(ca => ca.AttributeType.Name == "ParamArrayAttribute");
570 if (isParams)
571 buf.AppendFormat("ParamArray ");
572 }
573 buf.Append(parameter.Name);
574 buf.Append(" As ");
575 buf.Append(GetTypeName(parameter.ParameterType, new DynamicParserContext(parameter)));
576 if (parameter.HasDefault && parameter.IsOptional && parameter.HasConstant)
577 {
578 buf.AppendFormat(" = {0}", MDocUpdater.MakeAttributesValueString(parameter.Constant, parameter.ParameterType));
579 }
580 return buf;
581 }
582
583 protected override string GetPropertyDeclaration(PropertyDefinition property)
584 {
585 string getVisible = null;
586 if (DocUtils.IsAvailablePropertyMethod(property.GetMethod))
587 getVisible = AppendVisibility(new StringBuilder(), property.GetMethod).ToString();
588 string setVisible = null;
589 if (DocUtils.IsAvailablePropertyMethod(property.SetMethod))
590 setVisible = AppendVisibility(new StringBuilder(), property.SetMethod).ToString();
591
592 if (setVisible == null && getVisible == null)
593 return null;
594
595 StringBuilder buf = new StringBuilder();
596 IEnumerable<MemberReference> defs = property.DeclaringType.GetDefaultMembers();
597 bool indexer = false;
598 foreach (MemberReference mi in defs)
599 {
600 if (mi == property)
601 {
602 indexer = true;
603 break;
604 }
605 }
606 if (indexer)
607 buf.Append("Default ");
608 if (getVisible != null && (setVisible == null || (setVisible != null && getVisible == setVisible)))
609 buf.Append(getVisible);
610 else if (setVisible != null && getVisible == null)
611 buf.Append(setVisible);
612 else
613 buf.Append("Public");
614
615 // Pick an accessor to use for static/virtual/override/etc. checks.
616 var method = property.SetMethod;
617 if (method == null)
618 method = property.GetMethod;
619
620 string modifiers = String.Empty;
621 if (method.IsStatic && !IsModule(method.DeclaringType)) modifiers += " Shared";
622 if (method.IsVirtual && !method.IsAbstract)
623 {
624 if ((method.Attributes & MethodAttributes.NewSlot) != 0)
625 modifiers += " Overridable";
626 else
627 modifiers += " Overrides";
628 }
629 TypeDefinition declDef = (TypeDefinition)method.DeclaringType;
630 if (method.IsAbstract && !declDef.IsInterface)
631 modifiers += " MustOverride";
632 if (method.IsFinal)
633 modifiers += " NotOverridable";
634 if (modifiers == " MustOverride NotOverridable")
635 modifiers = "";
636 if (modifiers == " Overridable NotOverridable")
637 modifiers = "";
638 buf.Append(modifiers).Append(' ');
639
640 if (getVisible != null && setVisible == null)
641 buf.Append("ReadOnly ");
642
643 buf.Append("Property ");
644 buf.Append(property.Name.Split('.').Last());
645
646 if (property.Parameters.Count != 0)
647 {
648 AppendParameters(buf, method, property.Parameters, '(', ')');
649 }
650 buf.Append(" As ");
651 buf.Append(GetTypeName(property.PropertyType, new DynamicParserContext(property)));
652 if (DocUtils.IsExplicitlyImplemented(property.GetMethod))
653 {
654 TypeReference iface;
655 MethodReference ifaceMethod;
656 DocUtils.GetInfoForExplicitlyImplementedMethod(method, out iface, out ifaceMethod);
657 buf.Append(" Implements ")
658 .Append(new VBMemberFormatter().GetName(iface))
659 .Append('.')
660 .Append(DocUtils.GetPropertyName(property, NestedTypeSeparator).Split('.').Last());
661 }
662 return buf.ToString();
663 }
664
665 protected override string GetFieldDeclaration(FieldDefinition field)
666 {
667 TypeDefinition declType = (TypeDefinition)field.DeclaringType;
668 if (declType.IsEnum && field.Name == "value__")
669 return null; // This member of enums aren't documented.
670
671 StringBuilder buf = new StringBuilder();
672 AppendFieldVisibility(buf, field);
673 if (buf.Length == 0)
674 return null;
675
676 if (declType.IsEnum)
677 return field.Name;
678
679 if (field.IsStatic && !field.IsLiteral && !IsModule(field.DeclaringType))
680 buf.Append(" Shared");
681 if (field.IsInitOnly)
682 buf.Append(" ReadOnly");
683 if (field.IsLiteral)
684 buf.Append(" Const");
685
686 buf.Append(' ').Append(field.Name);
687 buf.Append(" As ").Append(GetTypeName(field.FieldType, new DynamicParserContext(field))).Append(' ');
688 DocUtils.AppendFieldValue(buf, field);
689
690 return buf.ToString();
691 }
692
693 static void AppendFieldVisibility(StringBuilder buf, FieldDefinition field)
694 {
695 if (field.IsPublic)
696 {
697 buf.Append("Public");
698 return;
699 }
700 if (field.IsFamily)
701 {
702 buf.Append("Protected");
703 }
704 if (field.IsFamilyOrAssembly)
705 {
706 buf.Append("Protected Friend");
707 }
708 }
709
710 protected override string GetEventDeclaration(EventDefinition e)
711 {
712 StringBuilder buf = new StringBuilder();
713 bool isPublicEII = IsPublicEII(e);
714
715 if (AppendVisibility(buf, e.AddMethod).Length == 0 && !isPublicEII)
716 {
717 return null;
718 }
719 if (e.DeclaringType.IsInterface) // There is no access modifiers in interfaces
720 {
721 buf.Clear();
722 }
723 AppendModifiers(buf, e.AddMethod);
724 if (e.AddMethod.CustomAttributes.All(
725 i => i.AttributeType.FullName != Consts.CompilerGeneratedAttribute)
726 && !e.DeclaringType.IsInterface)// There is no 'Custom' modifier in interfaces
727 {
728 if (buf.Length > 0)
729 buf.Append(' ');
730 buf.Append("Custom");
731 }
732
733 if (buf.Length > 0)
734 buf.Append(' ');
735 buf.Append("Event ");
736 if (isPublicEII)
737 buf.Append(e.Name.Split('.').Last());
738 else
739 buf.Append(e.Name);
740 buf.Append(" As ").Append(GetTypeName(e.EventType, new DynamicParserContext(e.AddMethod.Parameters[0]))).Append(' ');
741 if (isPublicEII) {
742 var dotIndex = e.Name.LastIndexOf ('.');
743 dotIndex = dotIndex > -1 ? dotIndex : e.Name.Length;
744 buf.Append($"Implements {e.Name.Substring(0, dotIndex)}");
745 }
746
747 return buf.ToString();
748 }
749
750 protected override char[] ArrayDelimeters
751 {
752 get { return new[] {'(', ')'}; }
753 }
754
755 public override bool IsSupported(TypeReference tref)
756 {
757 return !tref.Name.Contains('*');
758 }
759
760 public override bool IsSupported(MemberReference mref)
761 {
762 var field = mref as FieldDefinition;
763 if (field != null)
764 {
765 return IsSupported(field.FieldType)
766 && IsSupportedNaming(field);
767 }
768
769 var method = mref as MethodDefinition;
770 if (method != null)
771 {
772 return IsSupported(method.ReturnType)
773 && method.Parameters.All(i => IsSupported(i.ParameterType))
774 && IsSupportedNaming(method);
775 }
776
777 var property = mref as PropertyDefinition;
778 if (property != null)
779 {
780 return IsSupported(property.PropertyType)
781 && IsSupportedNaming(property);
782 }
783
784 var @event = mref as EventDefinition;
785 if (@event != null)
786 {
787 return IsSupported(@event.EventType)
788 && IsSupportedNaming(@event);
789 }
790
791 var @event2 = mref as AttachedEventReference;
792 if (@event2 != null)
793 return true;
794
795 var prop = mref as AttachedPropertyReference;
796 if (prop != null)
797 return true;
798
799 throw new NotSupportedException("Unsupported member type: " + mref.GetType().FullName);
800 }
801
802 private bool IsSupportedNaming(EventDefinition @event)
803 {
804 return [email protected](@event.EventType.Name, StringComparison.InvariantCultureIgnoreCase);
805 }
806
807 private bool IsSupportedNaming(PropertyDefinition property)
808 {
809 return !property.Name.Equals(property.DeclaringType.Name, StringComparison.InvariantCultureIgnoreCase);
810 }
811
812 private bool IsSupportedNaming(MethodDefinition method)
813 {
814 var allTypes = method.Parameters.Select(i => i.ParameterType)
815 .Concat(method.GenericParameters)
816 .Concat(new[] { method.ReturnType })
817 .ToList();
818 foreach (var typeReference in allTypes)
819 {
820 foreach (var typeReference2 in allTypes)
821 {
822 // if there are types which differ only in letter case
823 if (typeReference.Name.Equals(typeReference2.Name, StringComparison.InvariantCultureIgnoreCase)
824 && typeReference.Name != typeReference2.Name)
825 return false;
826 }
827 foreach (var parameterDefinition in method.Parameters)
828 {
829 // it there's a parameter which name is case-insensitively equal to typename
830 if (parameterDefinition.Name.Equals(typeReference.Name, StringComparison.InvariantCultureIgnoreCase))
831 return false;
832 }
833 }
834
835 foreach (var parameterDefinition in method.Parameters)
836 {
837 foreach (var parameterDefinition2 in method.Parameters)
838 {
839 // if there're parameters which names are case-insensitively equal
840 if (parameterDefinition.Name.Equals(parameterDefinition2.Name, StringComparison.InvariantCultureIgnoreCase)
841 && parameterDefinition.Name != parameterDefinition2.Name)
842 return false;
843 }
844 }
845 return true;
846 }
847
848 private bool IsSupportedNaming(FieldDefinition field)
849 {
850 return !field.Name.Equals(field.DeclaringType.Name, StringComparison.InvariantCultureIgnoreCase);
851 }
852
853 private static bool IsModule(TypeDefinition typeDefinition)
854 {
855 return typeDefinition.CustomAttributes.Any(i => i.AttributeType.FullName == "Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute")
856 || typeDefinition.Methods.Any(DocUtils.IsExtensionMethod);
857 }
858
859 private bool IsIteratorMethod(MethodDefinition method)
860 {
861 return method.CustomAttributes.Any(i => i.AttributeType.FullName == "System.Runtime.CompilerServices.IteratorStateMachineAttribute");
862 }
863 }
864 }