diff options
30 files changed, 894 insertions, 155 deletions
diff --git a/external/Test/mdoc.Test.FSharp.dll b/external/Test/mdoc.Test.FSharp.dll Binary files differnew file mode 100644 index 00000000..e7de8b4a --- /dev/null +++ b/external/Test/mdoc.Test.FSharp.dll diff --git a/external/Test/mdoc.Test.NullableReferenceTypes.dll b/external/Test/mdoc.Test.NullableReferenceTypes.dll Binary files differnew file mode 100644 index 00000000..e72d594e --- /dev/null +++ b/external/Test/mdoc.Test.NullableReferenceTypes.dll diff --git a/external/Test/readme.md b/external/Test/readme.md index 95ec2dd3..4c56b81b 100644 --- a/external/Test/readme.md +++ b/external/Test/readme.md @@ -4,6 +4,7 @@ The files in this directory are used as test inputs for explicit assembly testin |---|---| |CSharpExample.dll|TBD| |mdoc.Test.Cplusplus.dll|../../mdoc/mdoc.Test.Cplusplus| +|mdoc.Test.NullableReferenceTypes|../../mdoc/mdoc.Test/NullableReferenceTypesTests| |UWPTestComponentCSharp.winmd|../../mdoc/mdoc.Test/UWPTestComponentCSharp| |UwpTestWinRtComponentCpp.dll|../../mdoc/mdoc.Test/UwpTestWinRtComponentCpp| |UwpTestWinRtComponentCpp.winmd|../../mdoc/mdoc.Test/UwpTestWinRtComponentCpp| diff --git a/mdoc/Mono.Documentation/Updater/AttributeParserContext.cs b/mdoc/Mono.Documentation/Updater/AttributeParserContext.cs new file mode 100644 index 00000000..de9ee35d --- /dev/null +++ b/mdoc/Mono.Documentation/Updater/AttributeParserContext.cs @@ -0,0 +1,92 @@ +using mdoc.Mono.Documentation.Updater; +using Mono.Cecil; +using Mono.Documentation.Util; +using System; +using System.Collections.ObjectModel; + +namespace Mono.Documentation.Updater +{ + public class AttributeParserContext : IAttributeParserContext + { + private int nullableAttributeIndex; + private int dynamicAttributeIndex; + private ICustomAttributeProvider provider; + private ReadOnlyCollection<bool?> nullableAttributeFlags; + private ReadOnlyCollection<bool> dynamicAttributeFlags; + + private AttributeParserContext(ICustomAttributeProvider provider) + { + this.provider = provider; + + ReadDynamicAttribute(); + ReadNullableAttribute(); + } + + private bool ExistsNullableAttribute + { + get + { + return nullableAttributeFlags.Count > 0; + } + } + + private bool HasSameNullableValue + { + get + { + return nullableAttributeFlags.Count == 1; + } + } + + public static IAttributeParserContext Create(ICustomAttributeProvider provider) + { + return new AttributeParserContext(provider); + } + + public void NextDynamicFlag() + { + dynamicAttributeIndex++; + } + + public bool IsDynamic() + { + return dynamicAttributeFlags != null && (dynamicAttributeFlags.Count == 0 || dynamicAttributeFlags[dynamicAttributeIndex]); + } + + public bool IsNullable() + { + if (ExistsNullableAttribute) + { + if (HasSameNullableValue) + { + return nullableAttributeFlags[0].IsTrue(); + } + + if (nullableAttributeIndex < nullableAttributeFlags.Count) + { + return nullableAttributeFlags[nullableAttributeIndex++].IsTrue(); + } + + throw new IndexOutOfRangeException("You are out of range in the nullable attribute values, please call the method for each nullable checking only once."); + } + + return false; + } + + private void ReadDynamicAttribute() + { + DynamicTypeProvider dynamicTypeProvider = new DynamicTypeProvider(provider); + var dynamicTypeFlags = dynamicTypeProvider.GetDynamicTypeFlags(); + if (dynamicTypeFlags != null) + { + dynamicAttributeFlags = new ReadOnlyCollection<bool>(dynamicTypeFlags); + } + } + + private void ReadNullableAttribute() + { + NullableReferenceTypeProvider nullableReferenceTypeProvider = new NullableReferenceTypeProvider(provider); + nullableAttributeFlags = new ReadOnlyCollection<bool?>(nullableReferenceTypeProvider.GetNullableReferenceTypeFlags()); + } + } +}
\ No newline at end of file diff --git a/mdoc/Mono.Documentation/Updater/DynamicParserContext.cs b/mdoc/Mono.Documentation/Updater/DynamicParserContext.cs deleted file mode 100644 index 53e36935..00000000 --- a/mdoc/Mono.Documentation/Updater/DynamicParserContext.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Collections.ObjectModel; -using System.Linq; - -using Mono.Cecil; - -using Mono.Documentation.Util; -
-namespace Mono.Documentation.Updater -{
- public class DynamicParserContext - { - public ReadOnlyCollection<bool> TransformFlags; - public int TransformIndex;
- public bool IsNullableAttribute; - public DynamicParserContext (ICustomAttributeProvider provider) - { - CustomAttribute da; - if (provider.HasCustomAttributes && - (da = (provider.CustomAttributes.SafeCast<CustomAttribute> () - .SingleOrDefault (ca => ca.GetDeclaringType () == "System.Runtime.CompilerServices.DynamicAttribute"))) != null) - { - CustomAttributeArgument[] values = da.ConstructorArguments.Count == 0 - ? new CustomAttributeArgument[0] - : (CustomAttributeArgument[])da.ConstructorArguments[0].Value; - - TransformFlags = new ReadOnlyCollection<bool> (values.Select (t => (bool)t.Value).ToArray ()); - } - - IsNullableAttribute = provider.HasCustomAttributes &&
- provider.CustomAttributes.Any(ca => ca.AttributeType.FullName == "System.Runtime.CompilerServices.NullableAttribute"); - } - } -}
\ No newline at end of file diff --git a/mdoc/Mono.Documentation/Updater/DynamicTypeProvider.cs b/mdoc/Mono.Documentation/Updater/DynamicTypeProvider.cs new file mode 100644 index 00000000..1d370d58 --- /dev/null +++ b/mdoc/Mono.Documentation/Updater/DynamicTypeProvider.cs @@ -0,0 +1,47 @@ +using Mono.Cecil; +using Mono.Documentation.Updater; +using Mono.Documentation.Util; +using System.Collections.Generic; +using System.Linq; + +namespace mdoc.Mono.Documentation.Updater +{ + public class DynamicTypeProvider + { + private const string DynamicAttributeFulleName = "System.Runtime.CompilerServices.DynamicAttribute"; + + private ICustomAttributeProvider provider; + + public DynamicTypeProvider(ICustomAttributeProvider provider) + { + this.provider = provider; + } + + public IList<bool> GetDynamicTypeFlags() + { + CustomAttribute dynamicAttribute = FindDynamicAttribute(); + if (dynamicAttribute != null) + { + CustomAttributeArgument[] attributeValues = new CustomAttributeArgument[0]; + if (dynamicAttribute.ConstructorArguments.Count > 0) + { + attributeValues = (CustomAttributeArgument[])dynamicAttribute.ConstructorArguments[0].Value; + } + + return attributeValues.Select(t => (bool)t.Value).ToList(); + } + + return null; + } + + private CustomAttribute FindDynamicAttribute() + { + if (provider.HasCustomAttributes) + { + return provider.CustomAttributes.SafeCast<CustomAttribute>().SingleOrDefault(ca => ca.GetDeclaringType() == DynamicAttributeFulleName); + } + + return null; + } + } +} diff --git a/mdoc/Mono.Documentation/Updater/EmptyAttributeParserContext.cs b/mdoc/Mono.Documentation/Updater/EmptyAttributeParserContext.cs new file mode 100644 index 00000000..87aecaa4 --- /dev/null +++ b/mdoc/Mono.Documentation/Updater/EmptyAttributeParserContext.cs @@ -0,0 +1,30 @@ +namespace Mono.Documentation.Updater +{ + public class EmptyAttributeParserContext : IAttributeParserContext + { + private static readonly EmptyAttributeParserContext singletonInstance = new EmptyAttributeParserContext(); + + private EmptyAttributeParserContext() + { + } + + public static IAttributeParserContext Empty() + { + return singletonInstance; + } + + public void NextDynamicFlag() + { + } + + public bool IsDynamic() + { + return false; + } + + public bool IsNullable() + { + return false; + } + } +} diff --git a/mdoc/Mono.Documentation/Updater/Formatters/CSharpFullMemberFormatter.cs b/mdoc/Mono.Documentation/Updater/Formatters/CSharpFullMemberFormatter.cs index 4dee7151..f00684db 100644 --- a/mdoc/Mono.Documentation/Updater/Formatters/CSharpFullMemberFormatter.cs +++ b/mdoc/Mono.Documentation/Updater/Formatters/CSharpFullMemberFormatter.cs @@ -1,12 +1,10 @@ -using System; +using Mono.Cecil; +using Mono.Documentation.Util; +using System; using System.Collections.Generic; -using System.Globalization; using System.Linq; using System.Text; -using Mono.Cecil; -using Mono.Documentation.Util; - namespace Mono.Documentation.Updater.Formatters { public class CSharpFullMemberFormatter : MemberFormatter @@ -21,7 +19,6 @@ namespace Mono.Documentation.Updater.Formatters protected override StringBuilder AppendNamespace (StringBuilder buf, TypeReference type) { - string ns = DocUtils.GetNamespace (type); if (GetCSharpType (type.FullName) == null && ns != null && ns.Length > 0 && ns != "System") buf.Append (ns).Append ('.'); @@ -70,12 +67,11 @@ namespace Mono.Documentation.Updater.Formatters return typeToCompare == t ? null : typeToCompare; } - protected override StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context) + protected override StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, IAttributeParserContext context) { - if (context != null && context.TransformFlags != null && - (context.TransformFlags.Count == 0 || context.TransformFlags[context.TransformIndex])) + if (context.IsDynamic()) { - context.TransformIndex++; + context.NextDynamicFlag(); return buf.Append ("dynamic"); } @@ -90,15 +86,14 @@ namespace Mono.Documentation.Updater.Formatters string s = GetCSharpType (t); if (s != null) { - if (context != null) - context.TransformIndex++; + context.NextDynamicFlag(); return buf.Append (s); } return base.AppendTypeName (buf, type, context); } - private StringBuilder AppendGenericParameterConstraints (StringBuilder buf, GenericParameter type, DynamicParserContext context) + private StringBuilder AppendGenericParameterConstraints (StringBuilder buf, GenericParameter type, IAttributeParserContext context) { if (MemberFormatterState != MemberFormatterState.WithinGenericTypeParameters) return buf; @@ -112,40 +107,55 @@ namespace Mono.Documentation.Updater.Formatters return buf; } - protected override string GetTypeName(TypeReference type, DynamicParserContext context, bool appendGeneric = true, bool useTypeProjection = true) + protected override string GetTypeName (TypeReference type, IAttributeParserContext context, bool appendGeneric = true, bool useTypeProjection = true) { GenericInstanceType genType = type as GenericInstanceType; - if (genType != null) + if (IsSpecialGenericNullableValueType (genType)) { - if (genType.Name.StartsWith("Nullable`") && genType.HasGenericArguments) - { - - var underlyingTypeName = base.GetTypeName(genType.GenericArguments.First(), context, appendGeneric, useTypeProjection); - return underlyingTypeName + "?"; - } - - if (genType.Name.StartsWith("Tuple`") || genType.Name.StartsWith("ValueTuple`")) - { - StringBuilder sb = new StringBuilder(); - sb.Append("("); + return AppendSpecialGenericNullableValueTypeName (new StringBuilder (), genType, context, appendGeneric, useTypeProjection).ToString (); + } - var genArgList = genType.GenericArguments.Select(genArg => base.GetTypeName(genArg, context, appendGeneric, useTypeProjection)).ToArray(); + return base.GetTypeName (type, context, appendGeneric, useTypeProjection); + } - sb.Append(string.Join(",", genArgList)); + protected override bool IsSpecialGenericNullableValueType (GenericInstanceType genInst) + { + return genInst != null && (genInst.Name.StartsWith("ValueTuple`") || + (genInst.Name.StartsWith("Nullable`") && genInst.HasGenericArguments)); + } - sb.Append(")"); + protected override StringBuilder AppendSpecialGenericNullableValueTypeName (StringBuilder buf, GenericInstanceType genInst, IAttributeParserContext context, bool appendGeneric = true, bool useTypeProjection = true) + { + if (genInst.Name.StartsWith ("Nullable`") && genInst.HasGenericArguments) + { + var underlyingTypeName = GetTypeName (genInst.GenericArguments.First (), context, appendGeneric, useTypeProjection); + buf.Append (underlyingTypeName + "?"); - return sb.ToString(); - } + return buf; } - if (context != null && context.IsNullableAttribute) + if (genInst.Name.StartsWith ("ValueTuple`")) { - var TypeName = base.GetTypeName(type, context, appendGeneric, useTypeProjection); - return TypeName + "?"; + buf.Append ("("); + var genArgList = new List<string> (); + foreach (var item in genInst.GenericArguments) + { + var isNullableType = false; + if (!item.IsValueType) + { + isNullableType = context.IsNullable (); + } + + var underlyingTypeName = GetTypeName (item, context, appendGeneric, useTypeProjection) + GetTypeNullableSymbol (item, isNullableType); + genArgList.Add (underlyingTypeName); + } + buf.Append (string.Join (",", genArgList)); + buf.Append (")"); + + return buf; } - return base.GetTypeName(type, context, appendGeneric, useTypeProjection); + return buf; } protected override string GetTypeDeclaration (TypeDefinition type) @@ -165,7 +175,11 @@ namespace Mono.Documentation.Updater.Formatters { buf.Append ("delegate "); MethodDefinition invoke = type.GetMethod ("Invoke"); - buf.Append (full.GetName (invoke.ReturnType, new DynamicParserContext (invoke.MethodReturnType))).Append (" "); + var context = AttributeParserContext.Create (invoke.MethodReturnType); + var isNullableType = context.IsNullable (); + buf.Append (full.GetName (invoke.ReturnType, context)); + buf.Append (GetTypeNullableSymbol (invoke.ReturnType, isNullableType)); + buf.Append (" "); buf.Append (GetName (type)); AppendParameters (buf, invoke, invoke.Parameters); AppendGenericTypeConstraints (buf, type); @@ -414,6 +428,18 @@ namespace Mono.Documentation.Updater.Formatters return base.AppendMethodName (buf, method); } + protected override string GetTypeNullableSymbol(TypeReference type, bool? isNullableType) + { + if (isNullableType.IsTrue() && + !type.IsValueType && + !type.FullName.Equals("System.Void")) + { + return "?"; + } + + return string.Empty; + } + protected override StringBuilder AppendGenericMethodConstraints (StringBuilder buf, MethodDefinition method) { if (method.GenericParameters.Count == 0) @@ -543,8 +569,14 @@ namespace Mono.Documentation.Updater.Formatters if (isParams) buf.AppendFormat ("params "); } - buf.Append (GetTypeName (parameter.ParameterType, new DynamicParserContext (parameter))).Append (" "); + + var context = AttributeParserContext.Create (parameter); + var isNullableType = context.IsNullable (); + buf.Append (GetTypeName (parameter.ParameterType, context)); + buf.Append (GetTypeNullableSymbol (parameter.ParameterType, isNullableType)); + buf.Append (" "); buf.Append (parameter.Name); + if (parameter.HasDefault && parameter.IsOptional && parameter.HasConstant) { var ReturnVal = new AttributeFormatter().MakeAttributesValueString(parameter.Constant, parameter.ParameterType); @@ -606,7 +638,12 @@ namespace Mono.Documentation.Updater.Formatters if (property.PropertyType.IsByReference) buf.Append("ref "); - buf.Append (GetTypeName (property.PropertyType, new DynamicParserContext (property))).Append (' '); + var context = AttributeParserContext.Create (property); + var isNullableType = context.IsNullable (); + var propertyReturnTypeName = GetTypeName (property.PropertyType, context); + buf.Append (propertyReturnTypeName); + buf.Append (GetTypeNullableSymbol (property.PropertyType, isNullableType)); + buf.Append (' '); IEnumerable<MemberReference> defs = property.DeclaringType.GetDefaultMembers (); string name = property.Name; @@ -664,7 +701,12 @@ namespace Mono.Documentation.Updater.Formatters if (field.IsLiteral) buf.Append (" const"); - buf.Append (' ').Append (GetTypeName (field.FieldType, new DynamicParserContext (field))).Append (' '); + buf.Append (' '); + var context = AttributeParserContext.Create (field); + var isNullableType = context.IsNullable (); + buf.Append (GetTypeName (field.FieldType, context)); + buf.Append (GetTypeNullableSymbol (field.FieldType, isNullableType)); + buf.Append (' '); buf.Append (field.Name); DocUtils.AppendFieldValue (buf, field); buf.Append (';'); @@ -702,8 +744,12 @@ namespace Mono.Documentation.Updater.Formatters AppendModifiers (buf, e.AddMethod); + var context = AttributeParserContext.Create (e.AddMethod.Parameters[0]); + var isNullableType = context.IsNullable (); buf.Append (buf.Length == 0 ? "event " : " event "); - buf.Append (GetTypeName (e.EventType, new DynamicParserContext (e.AddMethod.Parameters[0]))).Append (' '); + buf.Append (GetTypeName(e.EventType, context)); + buf.Append (GetTypeNullableSymbol (e.EventType, isNullableType)); + buf.Append (' '); buf.Append (e.Name).Append (';'); return buf.ToString (); diff --git a/mdoc/Mono.Documentation/Updater/Formatters/CppFormatters/CppCxFullMemberFormatter.cs b/mdoc/Mono.Documentation/Updater/Formatters/CppFormatters/CppCxFullMemberFormatter.cs index 7f8d1145..efba11be 100644 --- a/mdoc/Mono.Documentation/Updater/Formatters/CppFormatters/CppCxFullMemberFormatter.cs +++ b/mdoc/Mono.Documentation/Updater/Formatters/CppFormatters/CppCxFullMemberFormatter.cs @@ -141,7 +141,7 @@ namespace Mono.Documentation.Updater.Formatters.CppFormatters } protected override StringBuilder AppendArrayTypeName(StringBuilder buf, TypeReference type, - DynamicParserContext context) + IAttributeParserContext context) { buf.Append("Platform::Array <"); diff --git a/mdoc/Mono.Documentation/Updater/Formatters/CppFormatters/CppFullMemberFormatter.cs b/mdoc/Mono.Documentation/Updater/Formatters/CppFormatters/CppFullMemberFormatter.cs index d5bcceaa..757b6431 100644 --- a/mdoc/Mono.Documentation/Updater/Formatters/CppFormatters/CppFullMemberFormatter.cs +++ b/mdoc/Mono.Documentation/Updater/Formatters/CppFormatters/CppFullMemberFormatter.cs @@ -107,7 +107,7 @@ namespace Mono.Documentation.Updater.Formatters.CppFormatters return typeToCompare == t ? null : typeToCompare; } - protected override StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context) + protected override StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, IAttributeParserContext context) { string typeFullName = type.FullName; if (string.IsNullOrWhiteSpace (typeFullName)) @@ -275,7 +275,7 @@ namespace Mono.Documentation.Updater.Formatters.CppFormatters protected virtual string GetTypeNameWithOptions(TypeReference type, bool appendHat, bool appendGeneric = true) { - var typeName = GetTypeName(type, null, appendGeneric); + var typeName = GetTypeName(type, EmptyAttributeParserContext.Empty(), appendGeneric); var hatTypeName = !type.IsByReference && !type.IsPointer ? AppendHat(typeName, type, appendHat) @@ -553,7 +553,7 @@ namespace Mono.Documentation.Updater.Formatters.CppFormatters return AppendConstraints (buf, method.GenericParameters); } - protected override StringBuilder AppendGenericType(StringBuilder buf, TypeReference type, DynamicParserContext context, bool appendGeneric = true, bool useTypeProjection = false) + protected override StringBuilder AppendGenericType(StringBuilder buf, TypeReference type, IAttributeParserContext context, bool appendGeneric = true, bool useTypeProjection = false) { List<TypeReference> decls = DocUtils.GetDeclaringTypes( type is GenericInstanceType ? type.GetElementType() : type); @@ -755,7 +755,7 @@ namespace Mono.Documentation.Updater.Formatters.CppFormatters return buf; } - protected override StringBuilder AppendArrayTypeName(StringBuilder buf, TypeReference type, DynamicParserContext context) + protected override StringBuilder AppendArrayTypeName(StringBuilder buf, TypeReference type, IAttributeParserContext context) { buf.Append("cli::array <"); @@ -777,7 +777,7 @@ namespace Mono.Documentation.Updater.Formatters.CppFormatters return buf; } - protected override StringBuilder AppendRefTypeName(StringBuilder buf, TypeReference type, DynamicParserContext context) + protected override StringBuilder AppendRefTypeName(StringBuilder buf, TypeReference type, IAttributeParserContext context) { TypeSpecification spec = type as TypeSpecification; _AppendTypeName(buf, spec != null ? spec.ElementType : type.GetElementType(), context); @@ -787,7 +787,7 @@ namespace Mono.Documentation.Updater.Formatters.CppFormatters return buf; } - protected override StringBuilder AppendPointerTypeName(StringBuilder buf, TypeReference type, DynamicParserContext context) + protected override StringBuilder AppendPointerTypeName(StringBuilder buf, TypeReference type, IAttributeParserContext context) { TypeSpecification spec = type as TypeSpecification; _AppendTypeName(buf, spec != null ? spec.ElementType : type.GetElementType(), context); diff --git a/mdoc/Mono.Documentation/Updater/Formatters/CppFormatters/CppWinRtFullMemberFormatter.cs b/mdoc/Mono.Documentation/Updater/Formatters/CppFormatters/CppWinRtFullMemberFormatter.cs index c35a1e99..a7f7d3ec 100644 --- a/mdoc/Mono.Documentation/Updater/Formatters/CppFormatters/CppWinRtFullMemberFormatter.cs +++ b/mdoc/Mono.Documentation/Updater/Formatters/CppFormatters/CppWinRtFullMemberFormatter.cs @@ -123,7 +123,7 @@ namespace Mono.Documentation.Updater.Formatters.CppFormatters } protected override StringBuilder AppendArrayTypeName(StringBuilder buf, TypeReference type, - DynamicParserContext context) + IAttributeParserContext context) { buf.Append("std::Array <"); diff --git a/mdoc/Mono.Documentation/Updater/Formatters/FSharpFormatter.cs b/mdoc/Mono.Documentation/Updater/Formatters/FSharpFormatter.cs index 51ec7533..0589c7f9 100644 --- a/mdoc/Mono.Documentation/Updater/Formatters/FSharpFormatter.cs +++ b/mdoc/Mono.Documentation/Updater/Formatters/FSharpFormatter.cs @@ -162,7 +162,7 @@ namespace Mono.Documentation.Updater return typeToCompare == type.FullName ? null : typeToCompare; } - protected override StringBuilder AppendTypeName(StringBuilder buf, TypeReference type, DynamicParserContext context) + protected override StringBuilder AppendTypeName(StringBuilder buf, TypeReference type, IAttributeParserContext context) { if (type == null) return buf; @@ -308,7 +308,7 @@ namespace Mono.Documentation.Updater return "class"; } - protected override StringBuilder AppendGenericType(StringBuilder buf, TypeReference type, DynamicParserContext context, bool appendGeneric = true, bool useTypeProjection = false) + protected override StringBuilder AppendGenericType(StringBuilder buf, TypeReference type, IAttributeParserContext context, bool appendGeneric = true, bool useTypeProjection = false) { List<TypeReference> decls = DocUtils.GetDeclaringTypes( type is GenericInstanceType ? type.GetElementType() : type); @@ -613,7 +613,7 @@ namespace Mono.Documentation.Updater return buf; } - protected override StringBuilder AppendRefTypeName(StringBuilder buf, TypeReference type, DynamicParserContext context) + protected override StringBuilder AppendRefTypeName(StringBuilder buf, TypeReference type, IAttributeParserContext context) { ByReferenceType reftype = type as ByReferenceType; return AppendTypeName(buf, reftype?.ElementType, context); @@ -656,7 +656,7 @@ namespace Mono.Documentation.Updater if (property.PropertyType.FullName != "System.Object") { - var typeName = GetTypeName(property.PropertyType, new DynamicParserContext(property)); + var typeName = GetTypeName(property.PropertyType, AttributeParserContext.Create(property)); if (hasParameterName) buf.Append(" : "); buf.Append(typeName); @@ -698,7 +698,7 @@ namespace Mono.Documentation.Updater bool isFSharpFunction = IsFSharpFunction(parameter.ParameterType); if (isFSharpFunction) buf.Append("("); - var typeName = GetTypeName(parameter.ParameterType, new DynamicParserContext(parameter)); + var typeName = GetTypeName(parameter.ParameterType, AttributeParserContext.Create(parameter)); buf.Append(typeName); if (isFSharpFunction) buf.Append(")"); @@ -784,7 +784,7 @@ namespace Mono.Documentation.Updater buf.Append(" "); buf.Append(field.Name); buf.Append(" : "); - buf.Append(GetTypeName(field.FieldType, new DynamicParserContext(field))); + buf.Append(GetTypeName(field.FieldType, AttributeParserContext.Create(field))); return buf.ToString(); } @@ -802,7 +802,7 @@ namespace Mono.Documentation.Updater if (visibilityBuf.Length > 0) buf.Append(visibilityBuf).Append(' '); buf.Append(e.Name).Append(" : "); - buf.Append(GetTypeName(e.EventType, new DynamicParserContext(e.AddMethod.Parameters[0]))).Append(' '); + buf.Append(GetTypeName(e.EventType, AttributeParserContext.Create(e.AddMethod.Parameters[0]))).Append(' '); return buf.ToString(); } @@ -906,7 +906,7 @@ namespace Mono.Documentation.Updater return false; } - protected override StringBuilder AppendPointerTypeName(StringBuilder buf, TypeReference type, DynamicParserContext context) + protected override StringBuilder AppendPointerTypeName(StringBuilder buf, TypeReference type, IAttributeParserContext context) { TypeSpecification spec = type as TypeSpecification; buf.Append("nativeptr<"); diff --git a/mdoc/Mono.Documentation/Updater/Formatters/FSharpUsageFormatter.cs b/mdoc/Mono.Documentation/Updater/Formatters/FSharpUsageFormatter.cs index c0297ffb..cad07d31 100644 --- a/mdoc/Mono.Documentation/Updater/Formatters/FSharpUsageFormatter.cs +++ b/mdoc/Mono.Documentation/Updater/Formatters/FSharpUsageFormatter.cs @@ -32,7 +32,7 @@ namespace Mono.Documentation.Updater } else { - var typeName = AppendTypeName(new StringBuilder(), method.DeclaringType, null).ToString(); + var typeName = AppendTypeName(new StringBuilder(), method.DeclaringType, EmptyAttributeParserContext.Empty()).ToString(); buf.Append($"{CamelCase(typeName)}.{method.Name} "); } diff --git a/mdoc/Mono.Documentation/Updater/Formatters/ILFullMemberFormatter.cs b/mdoc/Mono.Documentation/Updater/Formatters/ILFullMemberFormatter.cs index 4718f560..1ad73548 100644 --- a/mdoc/Mono.Documentation/Updater/Formatters/ILFullMemberFormatter.cs +++ b/mdoc/Mono.Documentation/Updater/Formatters/ILFullMemberFormatter.cs @@ -77,7 +77,7 @@ namespace Mono.Documentation.Updater.Formatters return buf.Append (typename); } - protected override StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context) + protected override StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, IAttributeParserContext context) { if (type is GenericParameter) { @@ -203,7 +203,7 @@ namespace Mono.Documentation.Updater.Formatters return buf.ToString (); } - protected override StringBuilder AppendGenericType (StringBuilder buf, TypeReference type, DynamicParserContext context, bool appendGeneric = true, bool useTypeProjection = false) + protected override StringBuilder AppendGenericType (StringBuilder buf, TypeReference type, IAttributeParserContext context, bool appendGeneric = true, bool useTypeProjection = false) { List<TypeReference> decls = DocUtils.GetDeclaringTypes ( type is GenericInstanceType ? type.GetElementType () : type); @@ -303,7 +303,7 @@ namespace Mono.Documentation.Updater.Formatters buf.Append ("virtual "); if (!method.IsStatic) buf.Append ("instance "); - _AppendTypeName (buf, method.ReturnType, new DynamicParserContext (method.MethodReturnType)); + _AppendTypeName (buf, method.ReturnType, AttributeParserContext.Create (method.MethodReturnType)); buf.Append (' ') .Append (method.Name); if (method.IsGenericMethod ()) @@ -334,7 +334,7 @@ namespace Mono.Documentation.Updater.Formatters if (param.IsOut) buf.Append ("[out] "); else if (param.IsIn) buf.Append ("[in]"); - _AppendTypeName (buf, param.ParameterType, new DynamicParserContext (param)); + _AppendTypeName (buf, param.ParameterType, AttributeParserContext.Create (param)); if (param.ParameterType.IsByReference) buf.Append ("&"); buf.Append (' '); buf.Append (param.Name); @@ -475,7 +475,7 @@ namespace Mono.Documentation.Updater.Formatters .Append (".property "); if (!(gm ?? sm).IsStatic) buf.Append ("instance "); - _AppendTypeName (buf, property.PropertyType, new DynamicParserContext (property)); + _AppendTypeName (buf, property.PropertyType, AttributeParserContext.Create (property)); buf.Append (' ').Append (property.Name); if (!property.HasParameters || property.Parameters.Count == 0) return buf.ToString (); @@ -487,7 +487,7 @@ namespace Mono.Documentation.Updater.Formatters if (!first) buf.Append (", "); first = false; - _AppendTypeName (buf, p.ParameterType, new DynamicParserContext (p)); + _AppendTypeName (buf, p.ParameterType, AttributeParserContext.Create (p)); } buf.Append (')'); @@ -513,7 +513,7 @@ namespace Mono.Documentation.Updater.Formatters buf.Append ("initonly "); if (field.IsLiteral) buf.Append ("literal "); - _AppendTypeName (buf, field.FieldType, new DynamicParserContext (field)); + _AppendTypeName (buf, field.FieldType, AttributeParserContext.Create (field)); buf.Append (' ').Append (field.Name); AppendFieldValue (buf, field); diff --git a/mdoc/Mono.Documentation/Updater/Formatters/JsFormatter.cs b/mdoc/Mono.Documentation/Updater/Formatters/JsFormatter.cs index 0198463c..8664d1ba 100644 --- a/mdoc/Mono.Documentation/Updater/Formatters/JsFormatter.cs +++ b/mdoc/Mono.Documentation/Updater/Formatters/JsFormatter.cs @@ -151,7 +151,7 @@ namespace Mono.Documentation.Updater.Formatters return CamelCase(method.Name); } - protected override string GetTypeName(TypeReference type, DynamicParserContext context, bool appendGeneric = true, bool useTypeProjection = false) + protected override string GetTypeName(TypeReference type, IAttributeParserContext context, bool appendGeneric = true, bool useTypeProjection = false) { int n = type.Name.IndexOf("`"); if (n >= 0) diff --git a/mdoc/Mono.Documentation/Updater/Formatters/MemberFormatter.cs b/mdoc/Mono.Documentation/Updater/Formatters/MemberFormatter.cs index e4eb22d0..3fa389b8 100644 --- a/mdoc/Mono.Documentation/Updater/Formatters/MemberFormatter.cs +++ b/mdoc/Mono.Documentation/Updater/Formatters/MemberFormatter.cs @@ -27,10 +27,10 @@ namespace Mono.Documentation.Updater public string GetName (MemberReference member, bool appendGeneric = true, bool useTypeProjection = true) { - return GetName (member, null, appendGeneric, useTypeProjection: useTypeProjection); + return GetName (member, EmptyAttributeParserContext.Empty(), appendGeneric, useTypeProjection: useTypeProjection); } - public virtual string GetName (MemberReference member, DynamicParserContext context, bool appendGeneric = true, bool useTypeProjection = true) + public virtual string GetName (MemberReference member, IAttributeParserContext context, bool appendGeneric = true, bool useTypeProjection = true) { TypeReference type = member as TypeReference; if (type != null) @@ -55,21 +55,17 @@ namespace Mono.Documentation.Updater protected virtual string GetTypeName (TypeReference type, bool appendGeneric = true, bool useTypeProjection = true) { - return GetTypeName (type, null, appendGeneric, useTypeProjection: useTypeProjection); + return GetTypeName (type, EmptyAttributeParserContext.Empty(), appendGeneric, useTypeProjection: useTypeProjection); } - protected virtual string GetTypeName (TypeReference type, DynamicParserContext context, bool appendGeneric=true, bool useTypeProjection = true) + protected virtual string GetTypeName (TypeReference type, IAttributeParserContext context, bool appendGeneric=true, bool useTypeProjection = true) { if (type == null) throw new ArgumentNullException (nameof (type)); var typeName = _AppendTypeName (new StringBuilder (type.Name.Length), type, context, appendGeneric, useTypeProjection: useTypeProjection).ToString (); - typeName = RemoveMod (typeName); - - - - return typeName; + return RemoveMod (typeName); } public static string RemoveMod (string typeName) @@ -107,21 +103,34 @@ namespace Mono.Documentation.Updater protected virtual MemberFormatterState MemberFormatterState { get; set; } - protected virtual StringBuilder AppendRequiredModifierTypeName(StringBuilder buf, RequiredModifierType type, DynamicParserContext context) + protected virtual StringBuilder AppendRequiredModifierTypeName(StringBuilder buf, RequiredModifierType type, IAttributeParserContext context) { return _AppendTypeName (buf, type.ElementType, context); } - protected virtual StringBuilder AppendOptionalModifierTypeName(StringBuilder buf, OptionalModifierType type, DynamicParserContext context) + protected virtual StringBuilder AppendOptionalModifierTypeName(StringBuilder buf, OptionalModifierType type, IAttributeParserContext context) { return _AppendTypeName(buf, type.ElementType, context); } - protected virtual StringBuilder AppendArrayTypeName(StringBuilder buf, TypeReference type, DynamicParserContext context) + protected virtual StringBuilder AppendArrayTypeName(StringBuilder buf, TypeReference type, IAttributeParserContext context) { - TypeSpecification spec = type as TypeSpecification; - _AppendTypeName(buf, spec != null ? spec.ElementType : type.GetElementType(), context); - return AppendArrayModifiers(buf, (ArrayType)type); + var elementType = type.GetElementType (); + if (type is TypeSpecification typeSpecification) + { + elementType = typeSpecification.ElementType; + } + + var isNullableType = false; + if (!elementType.IsValueType) + { + isNullableType = context.IsNullable (); + } + + _AppendTypeName (buf, elementType, context); + AppendNullableSymbol (buf, elementType, isNullableType); + + return AppendArrayModifiers (buf, (ArrayType)type); } protected virtual bool ShouldStripModFromTypeName @@ -129,7 +138,7 @@ namespace Mono.Documentation.Updater get => true; } - protected StringBuilder _AppendTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context, bool appendGeneric = true, bool useTypeProjection =false) + protected StringBuilder _AppendTypeName (StringBuilder buf, TypeReference type, IAttributeParserContext context, bool appendGeneric = true, bool useTypeProjection =false) { if (type == null) return buf; @@ -167,9 +176,7 @@ namespace Mono.Documentation.Updater return SetBuffer(buf, interimBuilder, useTypeProjection: useTypeProjection); } - AppendNamespace (interimBuilder, type); - GenericInstanceType genInst = type as GenericInstanceType; if (type.IsRequiredModifier) { @@ -186,6 +193,12 @@ namespace Mono.Documentation.Updater } } + GenericInstanceType genInst = type as GenericInstanceType; + if (IsSpecialGenericNullableValueType (genInst)) // For special C# nullable value type only. + { + AppendSpecialGenericNullableValueTypeName (buf, genInst, context); + return SetBuffer (buf, interimBuilder, useTypeProjection: useTypeProjection); + } if (type.GenericParameters.Count == 0 && (genInst == null ? true : genInst.GenericArguments.Count == 0)) @@ -197,6 +210,18 @@ namespace Mono.Documentation.Updater return SetBuffer(buf, interimBuilder, useTypeProjection: useTypeProjection); } + protected virtual bool IsSpecialGenericNullableValueType(GenericInstanceType genInst) + { + // For special C# nullable value type only, the CSharpFullMemberFormatter subclass will override the method. + return false; + } + + protected virtual StringBuilder AppendSpecialGenericNullableValueTypeName(StringBuilder buf, GenericInstanceType genInst, IAttributeParserContext context, bool appendGeneric = true, bool useTypeProjection = true) + { + // For special C# nullable value type only, the CSharpFullMemberFormatter subclass will override the method. + return buf; + } + private StringBuilder SetBuffer(StringBuilder buf, StringBuilder interimBuilder, bool useTypeProjection) { var interimResult = interimBuilder.ToString(); @@ -232,17 +257,16 @@ namespace Mono.Documentation.Updater return buf; } - protected virtual StringBuilder AppendFullTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context) + protected virtual StringBuilder AppendFullTypeName (StringBuilder buf, TypeReference type, IAttributeParserContext context) { if (type.DeclaringType != null) AppendFullTypeName (buf, type.DeclaringType, context).Append (NestedTypeSeparator); return AppendTypeName (buf, type, context); } - protected virtual StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context) + protected virtual StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, IAttributeParserContext context) { - if (context != null) - context.TransformIndex++; + context.NextDynamicFlag(); return AppendTypeName (buf, type.Name); } @@ -268,7 +292,7 @@ namespace Mono.Documentation.Updater get { return "@"; } } - protected virtual StringBuilder AppendRefTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context) + protected virtual StringBuilder AppendRefTypeName (StringBuilder buf, TypeReference type, IAttributeParserContext context) { TypeSpecification spec = type as TypeSpecification; return _AppendTypeName(buf, spec != null ? spec.ElementType : type.GetElementType(), context); @@ -279,7 +303,7 @@ namespace Mono.Documentation.Updater get { return "*"; } } - protected virtual StringBuilder AppendPointerTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context) + protected virtual StringBuilder AppendPointerTypeName (StringBuilder buf, TypeReference type, IAttributeParserContext context) { TypeSpecification spec = type as TypeSpecification; return _AppendTypeName (buf, spec != null ? spec.ElementType : type.GetElementType (), context) @@ -296,17 +320,25 @@ namespace Mono.Documentation.Updater get { return "."; } } - protected virtual StringBuilder AppendGenericType (StringBuilder buf, TypeReference type, DynamicParserContext context, bool appendGeneric = true, bool useTypeProjection = true) + protected virtual StringBuilder AppendGenericType (StringBuilder buf, TypeReference type, IAttributeParserContext context, bool appendGeneric = true, bool useTypeProjection = true) { - List<TypeReference> decls = DocUtils.GetDeclaringTypes ( - type is GenericInstanceType ? type.GetElementType () : type); - List<TypeReference> genArgs = GetGenericArguments (type); int argIdx = 0; int prev = 0; bool insertNested = false; + var isGenericType = type is GenericInstanceType; + var declaringType = isGenericType ? type.GetElementType () : type; + List<TypeReference> decls = DocUtils.GetDeclaringTypes (declaringType); + List<TypeReference> genArgs = GetGenericArguments (type); foreach (var decl in decls) { - TypeReference declDef = decl.Resolve () ?? decl; + TypeReference declDef = decl; + if (!isGenericType) + { + // Generic type can't be resolve in WSL, macOS and Ubuntu OS environment that + // an AssemblyResolutionException will be throwing here but it can work in the Windows OS environment as well. + declDef = decl.Resolve (); + } + if (insertNested) { buf.Append (NestedTypeSeparator); @@ -316,16 +348,18 @@ namespace Mono.Documentation.Updater int ac = DocUtils.GetGenericArgumentCount (declDef); int c = ac - prev; prev = ac; - if ( appendGeneric && c > 0) + if (appendGeneric && c > 0) { buf.Append (GenericTypeContainer[0]); var origState = MemberFormatterState; MemberFormatterState = MemberFormatterState.WithinGenericTypeParameters; - _AppendTypeName (buf, genArgs[argIdx++], context, useTypeProjection: useTypeProjection); + AppendGenericTypeParameter (buf, genArgs[argIdx++], context, useTypeProjection); for (int i = 1; i < c; ++i) { - _AppendTypeName (buf.Append (","), genArgs[argIdx++], context, useTypeProjection: useTypeProjection); + buf.Append (","); + AppendGenericTypeParameter (buf, genArgs[argIdx++], context, useTypeProjection); } + MemberFormatterState = origState; buf.Append (GenericTypeContainer[1]); } @@ -333,6 +367,37 @@ namespace Mono.Documentation.Updater return buf; } + private void AppendGenericTypeParameter (StringBuilder buf, TypeReference type, IAttributeParserContext context, bool useTypeProjection = true) + { + var isNullableType = false; + if (type is GenericInstanceType) + { + isNullableType = context.IsNullable(); + buf.Append(GetTypeName(type, context)); + } + else + { + if (!type.IsValueType) + { + isNullableType = context.IsNullable(); + } + + _AppendTypeName(buf, type, context, useTypeProjection: useTypeProjection); + } + + AppendNullableSymbol(buf, type, isNullableType); + } + + private StringBuilder AppendNullableSymbol (StringBuilder buf, TypeReference type, bool? isNullableType) + { + return buf.Append(GetTypeNullableSymbol(type, isNullableType)); + } + + protected virtual string GetTypeNullableSymbol(TypeReference type, bool? isNullableType) + { + return string.Empty; + } + protected List<TypeReference> GetGenericArguments (TypeReference type) { var args = new List<TypeReference> (); @@ -465,8 +530,7 @@ namespace Mono.Documentation.Updater if (buf.Length != 0) buf.Append (" "); - buf.Append (GetTypeName (method.ReturnType, new DynamicParserContext (method.MethodReturnType))).Append (" "); - + AppendReturnTypeName (buf, method); AppendMethodName (buf, method); AppendGenericMethod (buf, method).Append (" "); AppendParameters (buf, method, method.Parameters); @@ -474,6 +538,19 @@ namespace Mono.Documentation.Updater return buf.ToString (); } + + private StringBuilder AppendReturnTypeName (StringBuilder buf, MethodDefinition method) + { + var context = AttributeParserContext.Create (method.MethodReturnType); + var isNullableType = context.IsNullable (); + var returnTypeName = GetTypeName (method.ReturnType, context); + buf.Append (returnTypeName); + buf.Append (GetTypeNullableSymbol (method.ReturnType, isNullableType)); + buf.Append (" "); + + return buf; + } + protected virtual StringBuilder AppendMethodName (StringBuilder buf, MethodDefinition method) { return buf.Append (method.Name); diff --git a/mdoc/Mono.Documentation/Updater/Formatters/MsxdocSlashDocMemberFormatter.cs b/mdoc/Mono.Documentation/Updater/Formatters/MsxdocSlashDocMemberFormatter.cs index 09760e88..8d0beac3 100644 --- a/mdoc/Mono.Documentation/Updater/Formatters/MsxdocSlashDocMemberFormatter.cs +++ b/mdoc/Mono.Documentation/Updater/Formatters/MsxdocSlashDocMemberFormatter.cs @@ -7,7 +7,7 @@ namespace Mono.Documentation.Updater {
public MsxdocSlashDocMemberFormatter(TypeMap map) : base(map) { }
- protected override StringBuilder AppendRefTypeName(StringBuilder buf, TypeReference type, DynamicParserContext context)
+ protected override StringBuilder AppendRefTypeName(StringBuilder buf, TypeReference type, IAttributeParserContext context)
{
TypeSpecification spec = type as TypeSpecification;
return _AppendTypeName(buf, spec != null ? spec.ElementType : type.GetElementType(), context).Append(RefTypeModifier);
diff --git a/mdoc/Mono.Documentation/Updater/Formatters/SlashDocMemberFormatter.cs b/mdoc/Mono.Documentation/Updater/Formatters/SlashDocMemberFormatter.cs index 9a4122e4..4bb986fc 100644 --- a/mdoc/Mono.Documentation/Updater/Formatters/SlashDocMemberFormatter.cs +++ b/mdoc/Mono.Documentation/Updater/Formatters/SlashDocMemberFormatter.cs @@ -23,7 +23,7 @@ namespace Mono.Documentation.Updater public SlashDocMemberFormatter(TypeMap map) : base(map) { } - protected override StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context) + protected override StringBuilder AppendTypeName (StringBuilder buf, TypeReference type, IAttributeParserContext context) { if (type is GenericParameter) { @@ -82,7 +82,7 @@ namespace Mono.Documentation.Updater return buf; } - protected override StringBuilder AppendRefTypeName (StringBuilder buf, TypeReference type, DynamicParserContext context) + protected override StringBuilder AppendRefTypeName (StringBuilder buf, TypeReference type, IAttributeParserContext context) { return base.AppendRefTypeName (buf, type, context).Append (RefTypeModifier); } @@ -102,7 +102,7 @@ namespace Mono.Documentation.Updater return buf.Append (ArrayDelimeters[1]); } - protected override StringBuilder AppendGenericType (StringBuilder buf, TypeReference type, DynamicParserContext context, bool appendGeneric = true, bool useTypeProjection = false) + protected override StringBuilder AppendGenericType (StringBuilder buf, TypeReference type, IAttributeParserContext context, bool appendGeneric = true, bool useTypeProjection = false) { if (!AddTypeCount) base.AppendGenericType (buf, type, context); @@ -111,7 +111,7 @@ namespace Mono.Documentation.Updater return buf; } - private StringBuilder AppendType (StringBuilder buf, TypeReference type, DynamicParserContext context) + private StringBuilder AppendType (StringBuilder buf, TypeReference type, IAttributeParserContext context) { List<TypeReference> decls = DocUtils.GetDeclaringTypes (type); bool insertNested = false; diff --git a/mdoc/Mono.Documentation/Updater/Formatters/VBFullMemberFormatter.cs b/mdoc/Mono.Documentation/Updater/Formatters/VBFullMemberFormatter.cs index 24f1a3fe..92a74cec 100644 --- a/mdoc/Mono.Documentation/Updater/Formatters/VBFullMemberFormatter.cs +++ b/mdoc/Mono.Documentation/Updater/Formatters/VBFullMemberFormatter.cs @@ -67,7 +67,7 @@ namespace Mono.Documentation.Updater return typeToCompare == t ? null : typeToCompare; } - protected override StringBuilder AppendTypeName(StringBuilder buf, TypeReference type, DynamicParserContext context) + protected override StringBuilder AppendTypeName(StringBuilder buf, TypeReference type, IAttributeParserContext context) { if (type is GenericParameter) return AppendGenericParameterConstraints(buf, (GenericParameter)type, context).Append(type.Name); @@ -80,15 +80,14 @@ namespace Mono.Documentation.Updater string s = GetVBType(t); if (s != null) { - if (context != null) - context.TransformIndex++; + context.NextDynamicFlag(); return buf.Append(s); } return base.AppendTypeName(buf, type, context); } - private StringBuilder AppendGenericParameterConstraints(StringBuilder buf, GenericParameter type, DynamicParserContext context) + private StringBuilder AppendGenericParameterConstraints(StringBuilder buf, GenericParameter type, IAttributeParserContext context) { if (MemberFormatterState != MemberFormatterState.WithinGenericTypeParameters) return buf; @@ -128,7 +127,7 @@ namespace Mono.Documentation.Updater if (isFunction) { buf.Append(" As "); - buf.Append(full.GetName(invoke.ReturnType, new DynamicParserContext(invoke.MethodReturnType))).Append(" "); + buf.Append(full.GetName(invoke.ReturnType, AttributeParserContext.Create(invoke.MethodReturnType))).Append(" "); } return buf.ToString(); @@ -210,7 +209,7 @@ namespace Mono.Documentation.Updater } } - protected override StringBuilder AppendGenericType(StringBuilder buf, TypeReference type, DynamicParserContext context, bool appendGeneric = true, bool useTypeProjection = false) + protected override StringBuilder AppendGenericType(StringBuilder buf, TypeReference type, IAttributeParserContext context, bool appendGeneric = true, bool useTypeProjection = false) { List<TypeReference> decls = DocUtils.GetDeclaringTypes( type is GenericInstanceType ? type.GetElementType() : type); @@ -377,7 +376,7 @@ namespace Mono.Documentation.Updater AppendParameters(buf, method, method.Parameters); AppendGenericMethodConstraints(buf, method); if (isFunction) - buf.Append(" As ").Append(GetTypeName(method.ReturnType, new DynamicParserContext(method.MethodReturnType))); + buf.Append(" As ").Append(GetTypeName(method.ReturnType, AttributeParserContext.Create(method.MethodReturnType))); if (DocUtils.IsExplicitlyImplemented(method)) { @@ -581,7 +580,7 @@ namespace Mono.Documentation.Updater } buf.Append(parameter.Name); buf.Append(" As "); - buf.Append(GetTypeName(parameter.ParameterType, new DynamicParserContext(parameter))); + buf.Append(GetTypeName(parameter.ParameterType, AttributeParserContext.Create(parameter))); if (parameter.HasDefault && parameter.IsOptional && parameter.HasConstant) { var parameterValue = new AttributeFormatter().MakeAttributesValueString(parameter.Constant, parameter.ParameterType); @@ -658,7 +657,7 @@ namespace Mono.Documentation.Updater AppendParameters(buf, method, property.Parameters, '(', ')'); } buf.Append(" As "); - buf.Append(GetTypeName(property.PropertyType, new DynamicParserContext(property))); + buf.Append(GetTypeName(property.PropertyType, AttributeParserContext.Create(property))); if (DocUtils.IsExplicitlyImplemented(property.GetMethod)) { TypeReference iface; @@ -694,7 +693,7 @@ namespace Mono.Documentation.Updater buf.Append(" Const"); buf.Append(' ').Append(field.Name); - buf.Append(" As ").Append(GetTypeName(field.FieldType, new DynamicParserContext(field))).Append(' '); + buf.Append(" As ").Append(GetTypeName(field.FieldType, AttributeParserContext.Create(field))).Append(' '); DocUtils.AppendFieldValue(buf, field); return buf.ToString(); @@ -747,7 +746,7 @@ namespace Mono.Documentation.Updater buf.Append(e.Name.Split('.').Last()); else buf.Append(e.Name); - buf.Append(" As ").Append(GetTypeName(e.EventType, new DynamicParserContext(e.AddMethod.Parameters[0]))).Append(' '); + buf.Append(" As ").Append(GetTypeName(e.EventType, AttributeParserContext.Create(e.AddMethod.Parameters[0]))).Append(' '); if (isPublicEII) { var dotIndex = e.Name.LastIndexOf ('.'); dotIndex = dotIndex > -1 ? dotIndex : e.Name.Length; diff --git a/mdoc/Mono.Documentation/Updater/IAttributeParserContext.cs b/mdoc/Mono.Documentation/Updater/IAttributeParserContext.cs new file mode 100644 index 00000000..aca6ba21 --- /dev/null +++ b/mdoc/Mono.Documentation/Updater/IAttributeParserContext.cs @@ -0,0 +1,9 @@ +namespace Mono.Documentation.Updater +{ + public interface IAttributeParserContext + { + void NextDynamicFlag(); + bool IsDynamic(); + bool IsNullable(); + } +} diff --git a/mdoc/Mono.Documentation/Updater/NullableReferenceTypeProvider.cs b/mdoc/Mono.Documentation/Updater/NullableReferenceTypeProvider.cs new file mode 100644 index 00000000..8832a2a0 --- /dev/null +++ b/mdoc/Mono.Documentation/Updater/NullableReferenceTypeProvider.cs @@ -0,0 +1,163 @@ +using Mono.Cecil; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Mono.Documentation.Updater +{ + // About Nullable Reference Types: https://github.com/dotnet/roslyn/blob/master/docs/features/nullable-reference-types.md + // About Nullable Metadata: https://github.com/dotnet/roslyn/blob/master/docs/features/nullable-metadata.md + public class NullableReferenceTypeProvider + { + private const string NullableAttributeFullName = "System.Runtime.CompilerServices.NullableAttribute"; + private const string NullableContextAttributeFullName = "System.Runtime.CompilerServices.NullableContextAttribute"; + private const byte ObliviousNullableAttribute = 0; + private const byte NotAnnotatedNullableAttribute = 1; + private const byte AnnotatedNullableAttribute = 2; + + private ICustomAttributeProvider provider; + + public NullableReferenceTypeProvider(ICustomAttributeProvider provider) + { + this.provider = provider; + } + + public IList<bool?> GetNullableReferenceTypeFlags() + { + var nullableAttribute = FindNullableAttribute(); + + return GetTypeNullability(nullableAttribute); + } + + private CustomAttribute FindNullableAttribute() + { + string findAttributeName = NullableAttributeFullName; + foreach (var item in GetTypeNullableAttributes()) + { + CustomAttribute nullableAttribute = FindCustomAttribute(item, findAttributeName); + if (nullableAttribute != null) + { + return nullableAttribute; + } + + findAttributeName = NullableContextAttributeFullName; + } + + return null; + } + + private CustomAttribute FindCustomAttribute(ICustomAttributeProvider customAttributeProvider, string customAttributeName) + { + if (customAttributeProvider.HasCustomAttributes) + { + return customAttributeProvider.CustomAttributes.SingleOrDefault(a => a.AttributeType.FullName.Equals(customAttributeName)); + } + + return null; + } + + private IList<bool?> GetTypeNullability(CustomAttribute typeCustomAttribute) + { + if (typeCustomAttribute == null) + { + return new List<bool?> + { + null + }; + } + + var nullableAttributeValue = typeCustomAttribute.ConstructorArguments[0].Value; + if (nullableAttributeValue is CustomAttributeArgument[] nullableAttributeArguments) + { + return nullableAttributeArguments.Select(a => IsAnnotated((byte)a.Value)).ToList(); + } + + return new List<bool?> + { + IsAnnotated((byte)nullableAttributeValue) + }; + } + + private bool? IsAnnotated(byte value) + { + switch (value) + { + case AnnotatedNullableAttribute: + return true; + + case NotAnnotatedNullableAttribute: + return false; + + case ObliviousNullableAttribute: + return null; + } + + throw new ArgumentOutOfRangeException(nameof(value), value, $"The nullable attribute value is not a valid type argument."); + } + + private ICollection<ICustomAttributeProvider> GetTypeNullableAttributes() + { + if (provider is ParameterDefinition parameterDefinition) + { + return GetTypeNullableAttributes(parameterDefinition); + } + + if (provider is MethodReturnType methodReturnType) + { + return GetTypeNullableAttributes(methodReturnType); + } + + if (provider is PropertyDefinition propertyDefinition) + { + return GetTypeNullableAttributes(propertyDefinition); + } + + if (provider is FieldDefinition fieldDefinition) + { + return GetTypeNullableAttributes(fieldDefinition); + } + + throw new ArgumentException("We don't support this custom attribute provider type now.", nameof(provider)); + } + + private ICollection<ICustomAttributeProvider> GetTypeNullableAttributes(ParameterDefinition parameterDefinition) + { + return GetTypeNullableAttributes(parameterDefinition, parameterDefinition.Method as MethodDefinition); + } + + private ICollection<ICustomAttributeProvider> GetTypeNullableAttributes(MethodReturnType methodReturnType) + { + return GetTypeNullableAttributes(methodReturnType, methodReturnType.Method as MethodDefinition); + } + + private ICollection<ICustomAttributeProvider> GetTypeNullableAttributes(PropertyDefinition propertyDefinition) + { + return GetTypeNullableAttributes(propertyDefinition, propertyDefinition.GetMethod); + } + + private ICollection<ICustomAttributeProvider> GetTypeNullableAttributes(FieldDefinition fieldDefinition) + { + return new List<ICustomAttributeProvider> + { + fieldDefinition, + fieldDefinition.DeclaringType + }; + } + + private ICollection<ICustomAttributeProvider> GetTypeNullableAttributes(ICustomAttributeProvider customAttributeProvider, MethodDefinition methodDefinition) + { + var resultList = new List<ICustomAttributeProvider> + { + customAttributeProvider + }; + + if (methodDefinition != null) + { + resultList.Add(methodDefinition); + resultList.Add(methodDefinition.DeclaringType); + } + + return resultList; + } + } +} diff --git a/mdoc/Test/en.expected-fsharp-wsl/Delegates+Delegate1.xml b/mdoc/Test/en.expected-fsharp-wsl/Delegates+Delegate1.xml index 35c80296..b9a1134c 100644 --- a/mdoc/Test/en.expected-fsharp-wsl/Delegates+Delegate1.xml +++ b/mdoc/Test/en.expected-fsharp-wsl/Delegates+Delegate1.xml @@ -1,5 +1,5 @@ <Type Name="Delegates+Delegate1" FullName="Delegates+Delegate1"> - <TypeSignature Language="C#" Value="public delegate int Delegates.Delegate1((int,int) );" /> + <TypeSignature Language="C#" Value="public delegate int Delegates.Delegate1(Tuple<int,int> );" /> <TypeSignature Language="ILAsm" Value=".class nested public auto ansi serializable sealed Delegates/Delegate1 extends System.MulticastDelegate" /> <TypeSignature Language="F#" Value="type Delegates.Delegate1 = delegate of (int * int) -> int" /> <AssemblyInfo> diff --git a/mdoc/Test/en.expected-fsharp-wsl/Delegates+Delegate9.xml b/mdoc/Test/en.expected-fsharp-wsl/Delegates+Delegate9.xml index 877a09ed..7da4ac51 100644 --- a/mdoc/Test/en.expected-fsharp-wsl/Delegates+Delegate9.xml +++ b/mdoc/Test/en.expected-fsharp-wsl/Delegates+Delegate9.xml @@ -1,5 +1,5 @@ <Type Name="Delegates+Delegate9" FullName="Delegates+Delegate9"> - <TypeSignature Language="C#" Value="public delegate char Delegates.Delegate9((int,int) );" /> + <TypeSignature Language="C#" Value="public delegate char Delegates.Delegate9(Tuple<int,int> );" /> <TypeSignature Language="ILAsm" Value=".class nested public auto ansi serializable sealed Delegates/Delegate9 extends System.MulticastDelegate" /> <TypeSignature Language="F#" Value="type Delegates.Delegate9 = delegate of (int * int) -> char" /> <AssemblyInfo> diff --git a/mdoc/Test/en.expected-fsharp-wsl/PatternMatching/PatternMatchingExamples.xml b/mdoc/Test/en.expected-fsharp-wsl/PatternMatching/PatternMatchingExamples.xml index 9a9db62e..6894bbba 100644 --- a/mdoc/Test/en.expected-fsharp-wsl/PatternMatching/PatternMatchingExamples.xml +++ b/mdoc/Test/en.expected-fsharp-wsl/PatternMatching/PatternMatchingExamples.xml @@ -588,7 +588,7 @@ </Docs> </Member> <Member MemberName="tuple1"> - <MemberSignature Language="C#" Value="public static (int,int) tuple1 { get; }" /> + <MemberSignature Language="C#" Value="public static Tuple<int,int> tuple1 { get; }" /> <MemberSignature Language="ILAsm" Value=".property class System.Tuple`2<int32, int32> tuple1" /> <MemberSignature Language="F#" Value="PatternMatching.PatternMatchingExamples.tuple1 : int * int" Usage="PatternMatching.PatternMatchingExamples.tuple1" /> <MemberType>Property</MemberType> diff --git a/mdoc/Test/en.expected-fsharp/Delegates+Delegate1.xml b/mdoc/Test/en.expected-fsharp/Delegates+Delegate1.xml index 35c80296..b9a1134c 100644 --- a/mdoc/Test/en.expected-fsharp/Delegates+Delegate1.xml +++ b/mdoc/Test/en.expected-fsharp/Delegates+Delegate1.xml @@ -1,5 +1,5 @@ <Type Name="Delegates+Delegate1" FullName="Delegates+Delegate1"> - <TypeSignature Language="C#" Value="public delegate int Delegates.Delegate1((int,int) );" /> + <TypeSignature Language="C#" Value="public delegate int Delegates.Delegate1(Tuple<int,int> );" /> <TypeSignature Language="ILAsm" Value=".class nested public auto ansi serializable sealed Delegates/Delegate1 extends System.MulticastDelegate" /> <TypeSignature Language="F#" Value="type Delegates.Delegate1 = delegate of (int * int) -> int" /> <AssemblyInfo> diff --git a/mdoc/Test/en.expected-fsharp/Delegates+Delegate9.xml b/mdoc/Test/en.expected-fsharp/Delegates+Delegate9.xml index 877a09ed..7da4ac51 100644 --- a/mdoc/Test/en.expected-fsharp/Delegates+Delegate9.xml +++ b/mdoc/Test/en.expected-fsharp/Delegates+Delegate9.xml @@ -1,5 +1,5 @@ <Type Name="Delegates+Delegate9" FullName="Delegates+Delegate9"> - <TypeSignature Language="C#" Value="public delegate char Delegates.Delegate9((int,int) );" /> + <TypeSignature Language="C#" Value="public delegate char Delegates.Delegate9(Tuple<int,int> );" /> <TypeSignature Language="ILAsm" Value=".class nested public auto ansi serializable sealed Delegates/Delegate9 extends System.MulticastDelegate" /> <TypeSignature Language="F#" Value="type Delegates.Delegate9 = delegate of (int * int) -> char" /> <AssemblyInfo> diff --git a/mdoc/Test/en.expected-fsharp/PatternMatching/PatternMatchingExamples.xml b/mdoc/Test/en.expected-fsharp/PatternMatching/PatternMatchingExamples.xml index 9a9db62e..6894bbba 100644 --- a/mdoc/Test/en.expected-fsharp/PatternMatching/PatternMatchingExamples.xml +++ b/mdoc/Test/en.expected-fsharp/PatternMatching/PatternMatchingExamples.xml @@ -588,7 +588,7 @@ </Docs> </Member> <Member MemberName="tuple1"> - <MemberSignature Language="C#" Value="public static (int,int) tuple1 { get; }" /> + <MemberSignature Language="C#" Value="public static Tuple<int,int> tuple1 { get; }" /> <MemberSignature Language="ILAsm" Value=".property class System.Tuple`2<int32, int32> tuple1" /> <MemberSignature Language="F#" Value="PatternMatching.PatternMatchingExamples.tuple1 : int * int" Usage="PatternMatching.PatternMatchingExamples.tuple1" /> <MemberType>Property</MemberType> diff --git a/mdoc/mdoc.Test/NullableReferenceTypesTests.cs b/mdoc/mdoc.Test/NullableReferenceTypesTests.cs new file mode 100644 index 00000000..48897efc --- /dev/null +++ b/mdoc/mdoc.Test/NullableReferenceTypesTests.cs @@ -0,0 +1,303 @@ +using Mono.Documentation; +using Mono.Documentation.Updater.Formatters; +using NUnit.Framework; +using System; +using System.Linq; + +namespace mdoc.Test +{ + public class NullableReferenceTypesTests : BasicFormatterTests<CSharpMemberFormatter> + { + private const string NullableReferenceTypesAssemblyPath = "../../../../external/Test/mdoc.Test.NullableReferenceTypes.dll"; + + private CSharpMemberFormatter csharpMemberFormatter = new CSharpMemberFormatter(); + + protected override CSharpMemberFormatter formatter => csharpMemberFormatter; + + [TestCase("dynamic", "DynamicType")] + [TestCase("dynamic?", "NullableDynamicType")] + [TestCase("DayOfWeek", "Enumeration")] + [TestCase("DayOfWeek?", "NullableEnumeration")] + [TestCase("int", "ValueType")] + [TestCase("int?", "NullableValueType")] + [TestCase("int[]", "ArrayOfValueType")] + [TestCase("int?[]", "ArrayOfValueTypeNullable")] + [TestCase("int[]?", "NullableArrayOfValueType")] + [TestCase("int?[]?", "NullableArrayOfNullableValueType")] + [TestCase("int[][]", "DimensionalArrayOfValueType")] + [TestCase("int?[][]", "DimensionalArrayOfNullableValueType")] + [TestCase("int?[]?[]", "DimensionalArrayOfNullableValueTypeOfNullableRow")] + [TestCase("int?[]?[]?", "NullableDimensionalArrayOfNullableValueTypeOfNullableRow")] + [TestCase("int?[][]?", "NullableDimensionalArrayOfNullableValueType")] + [TestCase("int[][]?", "NullableDimensionalArrayOfValueType")] + [TestCase("int[][]?[][]?", "NullableFourDimensionalArrayOfValueTypeOfMiddleNullableArray")] + [TestCase("int?[][]?[][]", "FourDimensionalArrayOfNullableValueTypeOfMiddleNullableArray")] + [TestCase("Tuple<int,int>", "TupleOfValueType")] + [TestCase("Tuple<int,int>?", "NullableTupleOfValueType")] + [TestCase("Tuple<int?,int?>", "TupleOfNullableValueType")] + [TestCase("Tuple<int?,int?>?", "NullableTupleOfNullableValueType")] + [TestCase("(int,int)", "ValueTupleOfValueType")] + [TestCase("(int,int)?", "NullableValueTupleOfValueType")] + [TestCase("(int?,int?)", "ValueTupleOfNullableValueType")] + [TestCase("(int?,int?)?", "NullableValueTupleOfNullableValueType")] + [TestCase("ICollection<int>", "InterfaceOfValueType")] + [TestCase("ICollection<int>?", "NullableInterfaceOfValueType")] + [TestCase("ICollection<int?>?", "NullableInterfaceOfNullableValueType")] + [TestCase("Action<int>", "ActionOfValueType")] + [TestCase("Action<int?>", "ActionOfNullableValueType")] + [TestCase("Action<int>?", "NullableActionOfValueType")] + [TestCase("Action<int?>?", "NullableActionOfNullableValueType")] + [TestCase("Dictionary<int,int>", "DictionaryOfValueType")] + [TestCase("Dictionary<int,int>?", "NullableDictionaryOfValueType")] + [TestCase("Dictionary<int?,int?>", "DictionaryOfNullableValueType")] + [TestCase("Dictionary<int?,int?>?", "NullableDictionaryOfNullableValueType")] + [TestCase("Dictionary<int,int?>", "DictionaryOfNullableValueTypeValue")] + [TestCase("Dictionary<int,int?>?", "NullableDictionaryOfNullableValueTypeValue")] + [TestCase("Dictionary<int?,int>?", "NullableDictionaryOfNullableValueTypeKey")] + [TestCase("Dictionary<int,Dictionary<int,int>>", "DictionaryOfValueTypeKeyAndDictionaryOfValueTypeValue")] + [TestCase("Dictionary<int,Dictionary<int,int>>?", "NullableDictionaryOfValueTypeKeyAndDictionaryOfValueTypeValue")] + [TestCase("Dictionary<int?,Dictionary<int,int>?>?", "NullableDictionaryOfNullableValueTypeKeyAndNullableDictionaryOfValueTypeValue")] + [TestCase("Dictionary<int?,Dictionary<int?,int?>?>?", "NullableDictionaryOfNullableValueTypeKeyAndNullableDictionaryOfNullableValueTypeValue")] + [TestCase("Dictionary<int,Tuple<int,int>>", "DictionaryOfValueTypeKeyAndTupleOfValueTypeValue")] + [TestCase("Dictionary<int,Tuple<int,int>>?", "NullableDictionaryOfValueTypeKeyAndTupleOfValueTypeValue")] + [TestCase("Dictionary<int?,Tuple<int,int>?>?", "NullableDictionaryOfNullableValueTypeKeyAndNullableTupleOfValueTypeValue")] + [TestCase("Dictionary<int?,Tuple<int?,int?>?>?", "NullableDictionaryOfNullableValueTypeKeyAndNullableTupleOfNullableValueTypeValue")] + [TestCase("Dictionary<Dictionary<int,int>,Dictionary<int,int>>", "DictionaryOfDictionaryOfValueType")] + [TestCase("Dictionary<Dictionary<int,int>?,Dictionary<int,int>?>?", "NullableDictionaryOfNullableDictionaryOfValueType")] + [TestCase("Dictionary<Dictionary<int?,int?>?,Dictionary<int?,int?>?>?", "NullableDictionaryOfNullableDictionaryOfNullableValueType")] + [TestCase("string", "ReferenceType")] + [TestCase("string?", "NullableReferenceType")] + [TestCase("string[]", "ArrayOfReferenceType")] + [TestCase("string?[]", "ArrayOfNullableReferenceType")] + [TestCase("string[]?", "NullableArrayOfReferenceType")] + [TestCase("string?[]?", "NullableArrayOfNullableReferenceType")] + [TestCase("string[][]", "DimensionalArrayOfReferenceType")] + [TestCase("string?[][]", "DimensionalArrayOfNullableReferenceType")] + [TestCase("string?[]?[]", "DimensionalArrayOfNullableReferenceTypeOfNullableRow")] + [TestCase("string?[]?[]?", "NullableDimensionalArrayOfNullableReferenceTypeOfNullableRow")] + [TestCase("string?[][]?", "NullableDimensionalArrayOfNullableReferenceType")] + [TestCase("string[][]?", "NullableDimensionalArrayOfReferenceType")] + [TestCase("string[][]?[][]?", "NullableFourDimensionalArrayOfReferenceTypeOfMiddleNullableArray")] + [TestCase("string?[][]?[][]", "FourDimensionalArrayOfNullableReferenceTypeOfMiddleNullableArray")] + [TestCase("Tuple<string,string>", "TupleOfReferenceType")] + [TestCase("Tuple<string,string>?", "NullableTupleOfReferenceType")] + [TestCase("Tuple<string?,string?>", "TupleOfNullableReferenceType")] + [TestCase("Tuple<string?,string?>?", "NullableTupleOfNullableReferenceType")] + [TestCase("(string,string)", "ValueTupleOfReferenceType")] + [TestCase("(string,string)?", "NullableValueTupleOfReferenceType")] + [TestCase("(string?,string?)", "ValueTupleOfNullableReferenceType")] + [TestCase("(string?,string?)?", "NullableValueTupleOfNullableReferenceType")] + [TestCase("ICollection<string>", "InterfaceOfReferenceType")] + [TestCase("ICollection<string>?", "NullableInterfaceOfReferenceType")] + [TestCase("ICollection<string?>?", "NullableInterfaceOfNullableReferenceType")] + [TestCase("ICollection<dynamic>", "InterfaceOfDynamicType")] + [TestCase("ICollection<dynamic>?", "NullableInterfaceOfDynamicType")] + [TestCase("ICollection<dynamic?>?", "NullableInterfaceOfNullableDynamicType")] + [TestCase("Action<string>", "ActionOfReferenceType")] + [TestCase("Action<string?>", "ActionOfNullableReferenceType")] + [TestCase("Action<string>?", "NullableActionOfReferenceType")] + [TestCase("Action<string?>?", "NullableActionOfNullableReferenceType")] + [TestCase("Dictionary<string,string>", "DictionaryOfReferenceType")] + [TestCase("Dictionary<string,string>?", "NullableDictionaryOfReferenceType")] + [TestCase("Dictionary<string?,string?>", "DictionaryOfNullableReferenceType")] + [TestCase("Dictionary<string?,string?>?", "NullableDictionaryOfNullableReferenceType")] + [TestCase("Dictionary<string,string?>", "DictionaryOfNullableReferenceTypeValue")] + [TestCase("Dictionary<string,string?>?", "NullableDictionaryOfNullableReferenceTypeValue")] + [TestCase("Dictionary<string?,string>?", "NullableDictionaryOfNullableReferenceTypeKey")] + [TestCase("Dictionary<Dictionary<string,string>,Dictionary<string,string>>", "DictionaryOfDictionaryOfReferenceType")] + [TestCase("Dictionary<Dictionary<string,string>?,Dictionary<string,string>?>?", "NullableDictionaryOfNullableDictionaryOfReferenceType")] + [TestCase("Dictionary<Dictionary<string?,string?>?,Dictionary<string?,string?>?>?", "NullableDictionaryOfNullableDictionaryOfNullableReferenceType")] + [TestCase("Dictionary<string,Dictionary<string,string>>", "DictionaryOfReferenceTypeKeyAndDictionaryOfReferenceTypeValue")] + [TestCase("Dictionary<string,Dictionary<string,string>>?", "NullableDictionaryOfReferenceTypeKeyAndDictionaryOfReferenceTypeValue")] + [TestCase("Dictionary<string?,Dictionary<string,string>?>?", "NullableDictionaryOfNullableReferenceTypeKeyAndNullableDictionaryOfReferenceTypeValue")] + [TestCase("Dictionary<string?,Dictionary<string?,string?>?>?", "NullableDictionaryOfNullableReferenceTypeKeyAndNullableDictionaryOfNullableReferenceTypeValue")] + [TestCase("Dictionary<string,Tuple<string,string>>", "DictionaryOfReferenceTypeKeyAndTupleOfReferenceTypeValue")] + [TestCase("Dictionary<string,Tuple<string,string>>?", "NullableDictionaryOfReferenceTypeKeyAndTupleOfReferenceTypeValue")] + [TestCase("Dictionary<string?,Tuple<string,string>?>?", "NullableDictionaryOfNullableReferenceTypeKeyAndNullableTupleOfReferenceTypeValue")] + [TestCase("Dictionary<string?,Tuple<string?,string?>?>?", "NullableDictionaryOfNullableReferenceTypeKeyAndNullableTupleOfNullableReferenceTypeValue")] + public void TypeName(string returnType, string methodName) + { + // Test single parameter and return type for method, extension method, property, field, event, delegate. + // They have the same process logic that we just test once the type of them. + TestMethodSignature(NullableReferenceTypesAssemblyPath, "mdoc.Test.NullableReferenceTypes.CommonType", + methodName, $"public {returnType} {methodName} ();"); + } + + [TestCase("ParamsArrayOfNullableValueType", "int i, params int?[] array")] + [TestCase("ParamsArrayOfNullableReferenceType", "string s, params object?[] array")] + [TestCase("NullableAndNonNullableValueType", "int i1, int? i2, int i3")] + [TestCase("NullableAndNonNullableReferenceType", "string s1, string? s2, string s3")] + [TestCase("NullableGenericValueTypeOfValueType", "ReadOnlySpan<int> s1, ReadOnlySpan<int?> s2, ReadOnlySpan<int> s3")] + [TestCase("NullableGenericValueTypeOfReferenceType", "ReadOnlySpan<string> s1, ReadOnlySpan<string?> s2, ReadOnlySpan<string> s3")] + [TestCase("NullableAndNonNullableInterfaceOfValueType", "ICollection<int> collection1, ICollection<int>? collection2, ICollection<int> collection3")] + [TestCase("NullableAndNonNullableInterfaceOfReferenceType", "ICollection<string> collection1, ICollection<string>? collection2, ICollection<string> collection3")] + public void MethodParameter(string methodName, string methodParameter) + { + TestMethodSignature(NullableReferenceTypesAssemblyPath, "mdoc.Test.NullableReferenceTypes.MethodParameter", + methodName, $"public void {methodName} ({methodParameter});"); + } + + [TestCase("GenericType", "<T> ({0})", "T t")] + [TestCase("GenericReferenceType", "<T> ({0}) where T : class", "T t")] + [TestCase("GenericNullableReferenceType", "<T> ({0}) where T : class", "T? t")] + [TestCase("ActionOfGenericNullableReferenceType", "<T> ({0}) where T : class", "Action<T?> t")] + [TestCase("NullableActionOfGenericNullableReferenceType", "<T> ({0}) where T : class", "Action<T?>? t")] + [TestCase("FuncGenericNullableReferenceType", "<T> ({0}) where T : class", "Func<T?> t")] + [TestCase("NullableFuncGenericNullableReferenceType", "<T> ({0}) where T : class", "Func<T?>? t")] + [TestCase("GenericNonNullableAndNullableReferenceType", "<T1,T2> ({0}) where T2 : class", "T1 t1, T2? t2")] + [TestCase("GenericValueType", "<T> ({0}) where T : struct", "T t")] + [TestCase("GenericNullableValueType", "<T> ({0}) where T : struct", "T? t")] + [TestCase("ActionOfGenericNullableValueType", "<T> ({0}) where T : struct", "Action<T?> action")] + [TestCase("NullableActionOfGenericNullableValueType", "<T> ({0}) where T : struct", "Action<T?>? action")] + [TestCase("FuncGenericNullableValueType", "<T> ({0}) where T : struct", "Func<T?> func")] + [TestCase("NullableFuncGenericNullableValueType", "<T> ({0}) where T : struct", "Func<T?>? func")] + [TestCase("GenericNonNullableAndNullableValueType", "<T1,T2> ({0}) where T2 : struct", "T1 t1, T2? t2")] + public void GenericMethodParameter(string methodName, string otherPartOfMethodSignature, string genericParameter) + { + TestMethodSignature(NullableReferenceTypesAssemblyPath, "mdoc.Test.NullableReferenceTypes.GenericMethodParameter", + methodName, $"public void {methodName}{string.Format(otherPartOfMethodSignature, genericParameter)};"); + } + + [TestCase("T", "GenericType", "<T> ()")] + [TestCase("T", "GenericReferenceType", "<T> () where T : class")] + [TestCase("T?", "GenericNullableReferenceType", "<T> () where T : class")] + [TestCase("T", "GenericValueType", "<T> () where T : struct")] + [TestCase("T?", "GenericNullableValueType", "<T> () where T : struct")] + public void GenericMethodReturnType(string returnType, string methodName, string otherPartOfMethodSignature) + { + TestMethodSignature(NullableReferenceTypesAssemblyPath, "mdoc.Test.NullableReferenceTypes.GenericMethodReturnType", + methodName, $"public {returnType} {methodName}{otherPartOfMethodSignature};"); + } + + [TestCase("T", "GenericType")] + [TestCase("TClass", "GenericReferenceType")] + [TestCase("TClass?", "GenericNullableReferenceType")] + [TestCase("TStruct", "GenericValueType")] + [TestCase("TStruct?", "GenericNullableValueType")] + public void GenericPropertyReturnType(string returnType, string propertyName) + { + TestPropertySignature(NullableReferenceTypesAssemblyPath, $"mdoc.Test.NullableReferenceTypes.{GetGenericTypeILName("GenericPropertyReturnType<T, TClass, TStruct>")}", + propertyName, $"public {returnType} {propertyName} {{ get; }}"); + } + + [TestCase("EventHandler", "EventHandler")] + [TestCase("EventHandler?", "NullableEventHandler")] + [TestCase("EventHandler<EventArgs>", "GenericEventHandler")] + [TestCase("EventHandler<EventArgs?>", "GenericEventHandlerOfNullableEventArgs")] + [TestCase("EventHandler<EventArgs>?", "NullableGenericEventHandler")] + [TestCase("EventHandler<EventArgs?>?", "NullableGenericEventHandlerOfNullableEventArgs")] + public void EventDelegateType(string delegateType, string eventName) + { + TestEventSignature(NullableReferenceTypesAssemblyPath, "mdoc.Test.NullableReferenceTypes.Event", + eventName, $"public event {delegateType} {eventName};"); + } + + [TestCase("Handler", "object sender, EventArgs args")] + [TestCase("NullableSender", "object? sender, EventArgs args")] + [TestCase("NullableSenderAndEventArgs", "object? sender, EventArgs? args")] + public void DelegateTypeParameter(string delegateName, string delegateParameter) + { + TestTypeSignature(NullableReferenceTypesAssemblyPath, $"mdoc.Test.NullableReferenceTypes.Delegate.{delegateName}", + $"public delegate void {delegateName}({delegateParameter});"); + } + + [TestCase("GenericHandler<TEventArgs>", "object sender, TEventArgs args", "{0}")] + [TestCase("NullableSender<TEventArgs>", "object? sender, TEventArgs args", "{0}")] + [TestCase("NullableSenderAndEventArgs<TEventArgs>", "object? sender, TEventArgs? args", "{0} where TEventArgs : class")] + [TestCase("ActionHandler<TClass,TStruct>", "TClass t1, TStruct t2", "{0} where TClass : class where TStruct : struct")] + [TestCase("NullableActionHandler<TClass,TStruct>", "TClass? t1, TStruct? t2", "{0} where TClass : class where TStruct : struct")] + public void GenericDelegateTypeParameter(string delegateName, string delegateParameter, string otherPartOfMethodSignature) + { + TestTypeSignature(NullableReferenceTypesAssemblyPath, $"mdoc.Test.NullableReferenceTypes.Delegate.{GetGenericTypeILName(delegateName)}", + $"public delegate void {delegateName}{string.Format(otherPartOfMethodSignature, $"({delegateParameter})")};"); + } + + [TestCase("TReturn", "FuncHandler<TReturn>", "()")] + [TestCase("TReturn?", "NullableReferenceType<TReturn>", "() where TReturn : class")] + [TestCase("TReturn?", "NullableValueType<TReturn>", "() where TReturn : struct")] + public void GenericDelegateTypeReturnType(string returnType, string delegateName, string otherPartOfMethodSignature) + { + TestTypeSignature(NullableReferenceTypesAssemblyPath, $"mdoc.Test.NullableReferenceTypes.Delegate.{GetGenericTypeILName(delegateName)}", + $"public delegate {returnType} {delegateName}{otherPartOfMethodSignature};"); + } + + [TestCase("NonNullableAndNullableValueType", "int i1, int? i2, int i3")] + [TestCase("NonNullableAndNullableReferenceType", "string s1, string? s2, string s3")] + [TestCase("InterfaceOfValueType", "ICollection<int> collection1, ICollection<int>? collection2, ICollection<int> collection3")] + [TestCase("InterfaceOfReferenceType", "ICollection<string> collection1, ICollection<string>? collection2, ICollection<string> collection3")] + public void ConstructorParameter(string typeName, string constructorParameter) + { + TestMethodSignature(NullableReferenceTypesAssemblyPath, $"mdoc.Test.NullableReferenceTypes.Constructor.{typeName}", + ".ctor", $"public {typeName} ({constructorParameter});"); + } + + [TestCase("GenericFieldType<T>", "T", "GenericType")] + [TestCase("GenericFieldTypeOfValueType<T>", "T", "GenericType")] + [TestCase("GenericFieldTypeOfValueType<T>", "T?", "NullableGenericType")] + [TestCase("GenericFieldTypeOfValueType<T>", "ICollection<T>", "InterfaceOfGenericType")] + [TestCase("GenericFieldTypeOfValueType<T>", "ICollection<T?>", "InterfaceOfNullableGenericType")] + [TestCase("GenericFieldTypeOfValueType<T>", "ICollection<T>?", "NullableInterfaceOfGenericType")] + [TestCase("GenericFieldTypeOfValueType<T>", "ICollection<T?>?", "NullableInterfaceOfNullableGenericType")] + [TestCase("GenericFieldTypeOfValueType<T>", "Dictionary<Dictionary<T,string>,string>", "DictionaryOfDictionary")] + [TestCase("GenericFieldTypeOfValueType<T>", "Dictionary<Dictionary<T?,string>,string>", "DictionaryOfDictionaryOfNullableGenericTypeKey")] + [TestCase("GenericFieldTypeOfValueType<T>", "Dictionary<Dictionary<T?,string>,string>?", "NullableDictionaryOfDictionaryOfNullableGenericTypeKey")] + [TestCase("GenericFieldTypeOfReferenceType<T>", "T", "GenericType")] + [TestCase("GenericFieldTypeOfReferenceType<T>", "T?", "NullableGenericType")] + [TestCase("GenericFieldTypeOfReferenceType<T>", "ICollection<T>", "InterfaceOfGenericType")] + [TestCase("GenericFieldTypeOfReferenceType<T>", "ICollection<T?>", "InterfaceOfNullableGenericType")] + [TestCase("GenericFieldTypeOfReferenceType<T>", "ICollection<T>?", "NullableInterfaceOfGenericType")] + [TestCase("GenericFieldTypeOfReferenceType<T>", "ICollection<T?>?", "NullableInterfaceOfNullableGenericType")] + [TestCase("GenericFieldTypeOfReferenceType<T>", "Dictionary<Dictionary<T,string>,string>", "DictionaryOfDictionary")] + [TestCase("GenericFieldTypeOfReferenceType<T>", "Dictionary<Dictionary<T?,string>,string>", "DictionaryOfDictionaryOfNullableGenericTypeKey")] + [TestCase("GenericFieldTypeOfReferenceType<T>", "Dictionary<Dictionary<T?,string>,string>?", "NullableDictionaryOfDictionaryOfNullableGenericTypeKey")] + public void GenericFieldReturnType(string typeName, string returnType, string fieldName) + { + TestFieldSignature(NullableReferenceTypesAssemblyPath, $"mdoc.Test.NullableReferenceTypes.{GetGenericTypeILName(typeName)}", + fieldName, $"public {returnType} {fieldName};"); + } + + [TestCase("NullableAndNonNullableValueType", "this int? type, int? i1, int i2, int? i3")] + [TestCase("NullableAndNonNullableNullableReferenceType", "this string? type, string? s1, string s2, string? s3")] + [TestCase("NullableAndNonNullableNullableReferenceTypeAndValueType", "this string? type, string? s1, int? i1, int i2, string s2, string? s3")] + public void ExtensionMethodParameter(string methodName, string methodParameter) + { + TestMethodSignature(NullableReferenceTypesAssemblyPath, "mdoc.Test.NullableReferenceTypes.ExtensionMethod", + methodName, $"public static void {methodName} ({methodParameter});"); + } + + [TestCase("Student", "op_Addition", "Student operator +", "Student s1, Student s2")] + [TestCase("Student", "op_Subtraction", "Student? operator -", "Student? s1, Student? s2")] + [TestCase("Student", "op_Multiply", "Student operator *", "Student s1, Student? s2")] + [TestCase("Student", "op_Division", "Student operator /", "Student? s1, Student s2")] + [TestCase("Student", "op_Implicit", "implicit operator ExamScore", "Student? s")] + [TestCase("Student", "op_Explicit", "explicit operator Student?", "ExamScore? s")] + [TestCase("ExamScore", "op_Addition", "ExamScore operator +", "ExamScore s1, ExamScore s2")] + [TestCase("ExamScore", "op_Subtraction", "ExamScore? operator -", "ExamScore? s1, ExamScore? s2")] + [TestCase("ExamScore", "op_Multiply", "ExamScore operator *", "ExamScore s1, ExamScore? s2")] + [TestCase("ExamScore", "op_Division", "ExamScore operator /", "ExamScore? s1, ExamScore s2")] + [TestCase("ExamScore", "op_Implicit", "implicit operator ExamScore", "Student? s")] + [TestCase("ExamScore", "op_Explicit", "explicit operator Student?", "ExamScore? s")] + public void OperatorOverloading(string typeName, string methodName, string operatorName, string methodParameter) + { + TestMethodSignature(NullableReferenceTypesAssemblyPath, $"mdoc.Test.NullableReferenceTypes.OperatorOverloading.{typeName}", + methodName, $"public static {operatorName} ({methodParameter});"); + } + + private string GetGenericTypeILName(string genericTypeName) + { + var startParameterIndex = genericTypeName.IndexOf('<'); + var endParameterIndex = genericTypeName.IndexOf('>'); + + if (startParameterIndex != -1 && endParameterIndex != -1 && endParameterIndex == genericTypeName.Length - 1) + { + var parameterList = genericTypeName.Substring(startParameterIndex + 1, endParameterIndex - startParameterIndex - 1); + var parameterCount = parameterList.Split(',').Count(); + var genericTypeNameWithoutParameter = genericTypeName.Substring(0, startParameterIndex); + + return $"{genericTypeNameWithoutParameter}`{parameterCount}"; + } + + throw new ArgumentException("The generic type name is not a valid value.", nameof(genericTypeName)); + } + } +} diff --git a/mdoc/mdoc.Test/mdoc.Test.csproj b/mdoc/mdoc.Test/mdoc.Test.csproj index 0b163d96..691e2bb8 100644 --- a/mdoc/mdoc.Test/mdoc.Test.csproj +++ b/mdoc/mdoc.Test/mdoc.Test.csproj @@ -92,6 +92,7 @@ <Compile Include="JsUsageFormatterTests.cs" />
<Compile Include="MDocFileSourceTests.cs" />
<Compile Include="MDocUpdaterTests.cs" />
+ <Compile Include="NullableReferenceTypesTests.cs" />
<Compile Include="SampleClasses\EiiImplementclass.cs" />
<Compile Include="SampleClasses\Interface_A.cs" />
<Compile Include="SampleClasses\Interface_B.cs" />
diff --git a/mdoc/mdoc.csproj b/mdoc/mdoc.csproj index 29314adb..f9b0d745 100644 --- a/mdoc/mdoc.csproj +++ b/mdoc/mdoc.csproj @@ -53,15 +53,19 @@ </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
+ <Compile Include="Mono.Documentation\Updater\DynamicTypeProvider.cs" />
+ <Compile Include="Mono.Documentation\Updater\EmptyAttributeParserContext.cs" />
<Compile Include="Mono.Documentation\Framework\FrameworkIndexHelper.cs" />
<Compile Include="Mono.Documentation\Framework\FrameworkTypeModel.cs" />
<Compile Include="Mono.Documentation\Framework\FrameworkNamespaceModel.cs" />
+ <Compile Include="Mono.Documentation\Updater\IAttributeParserContext.cs" />
<Compile Include="Mono.Documentation\Updater\Formatters\AttributeFormatters\ApplePlatformEnumFormatter.cs" />
<Compile Include="Mono.Documentation\Updater\Formatters\AttributeFormatters\AttributeFormatter.cs" />
<Compile Include="Mono.Documentation\Updater\Formatters\AttributeFormatters\CppWinRtAttributeFormatter.cs" />
<Compile Include="Mono.Documentation\Updater\Formatters\AttributeFormatters\FSharpAttributeFormatter.cs" />
<Compile Include="Mono.Documentation\Updater\Formatters\AttributeFormatters\CSharpAttributeFormatter.cs" />
<Compile Include="Mono.Documentation\Updater\Formatters\CppFormatters\CppWinRtMemberFormatter.cs" />
+ <Compile Include="Mono.Documentation\Updater\NullableReferenceTypeProvider.cs" />
<Compile Include="Mono.Documentation\Updater\Formatters\FormatterManager.cs" />
<Compile Include="Mono.Documentation\Updater\Formatters\FSharpFormatter.cs" />
<Compile Include="Mono.Documentation\Updater\Formatters\JsFormatter.cs" />
@@ -96,7 +100,7 @@ <Compile Include="Mono.Documentation\Updater\Formatters\DocIdFormatter.cs" />
<Compile Include="Mono.Documentation\Updater\Formatters\MemberFormatter.cs" />
<Compile Include="Mono.Documentation\Updater\MemberFormatterState.cs" />
- <Compile Include="Mono.Documentation\Updater\DynamicParserContext.cs" />
+ <Compile Include="Mono.Documentation\Updater\AttributeParserContext.cs" />
<Compile Include="Mono.Documentation\Updater\DocumentationMember.cs" />
<Compile Include="Mono.Documentation\Updater\EcmaDocumentationImporter.cs" />
<Compile Include="Mono.Documentation\Updater\MsxdocDocumentationImporter.cs" />
|