Browse Source

Add MemberNotNullWhenAttribute to IMethod, IProperty and IEvent. Make sure that it's guaranteed that AccessorOwner is non-null, when IsAccessor is true.

pull/2792/head
Siegfried Pammer 3 years ago
parent
commit
ed02b2eb9e
  1. 1
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
  2. 25
      ICSharpCode.Decompiler/NRTAttributes.cs
  3. 5
      ICSharpCode.Decompiler/TypeSystem/IEvent.cs
  4. 2
      ICSharpCode.Decompiler/TypeSystem/IMethod.cs
  5. 4
      ICSharpCode.Decompiler/TypeSystem/IProperty.cs
  6. 4
      ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataMethod.cs
  7. 22
      ICSharpCode.Decompiler/TypeSystem/NormalizeTypeVisitor.cs
  8. 20
      ICSharpCode.Decompiler/TypeSystem/Nullability.cs
  9. 2
      ICSharpCode.Decompiler/TypeSystem/TypeVisitor.cs

1
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -90,6 +90,7 @@
<Compile Include="CSharp\Annotations.cs" /> <Compile Include="CSharp\Annotations.cs" />
<Compile Include="CSharp\CallBuilder.cs" /> <Compile Include="CSharp\CallBuilder.cs" />
<Compile Include="CSharp\CSharpLanguageVersion.cs" /> <Compile Include="CSharp\CSharpLanguageVersion.cs" />
<Compile Include="NRTAttributes.cs" />
<Compile Include="PartialTypeInfo.cs" /> <Compile Include="PartialTypeInfo.cs" />
<Compile Include="CSharp\ProjectDecompiler\IProjectFileWriter.cs" /> <Compile Include="CSharp\ProjectDecompiler\IProjectFileWriter.cs" />
<Compile Include="CSharp\OutputVisitor\GenericGrammarAmbiguityVisitor.cs" /> <Compile Include="CSharp\OutputVisitor\GenericGrammarAmbiguityVisitor.cs" />

25
ICSharpCode.Decompiler/NRTAttributes.cs

@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
internal sealed class MemberNotNullWhenAttribute : Attribute
{
public bool ReturnValue { get; }
public string[] Members { get; }
public MemberNotNullWhenAttribute(bool returnValue, string member)
{
ReturnValue = returnValue;
Members = new string[1] { member };
}
public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
{
ReturnValue = returnValue;
Members = members;
}
}
}

5
ICSharpCode.Decompiler/TypeSystem/IEvent.cs

@ -18,12 +18,17 @@
#nullable enable #nullable enable
using System.Diagnostics.CodeAnalysis;
namespace ICSharpCode.Decompiler.TypeSystem namespace ICSharpCode.Decompiler.TypeSystem
{ {
public interface IEvent : IMember public interface IEvent : IMember
{ {
[MemberNotNullWhen(true, nameof(AddAccessor))]
bool CanAdd { get; } bool CanAdd { get; }
[MemberNotNullWhen(true, nameof(RemoveAccessor))]
bool CanRemove { get; } bool CanRemove { get; }
[MemberNotNullWhen(true, nameof(InvokeAccessor))]
bool CanInvoke { get; } bool CanInvoke { get; }
IMethod? AddAccessor { get; } IMethod? AddAccessor { get; }

2
ICSharpCode.Decompiler/TypeSystem/IMethod.cs

@ -19,6 +19,7 @@
#nullable enable #nullable enable
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection; using System.Reflection;
namespace ICSharpCode.Decompiler.TypeSystem namespace ICSharpCode.Decompiler.TypeSystem
@ -81,6 +82,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
/// <summary> /// <summary>
/// Gets whether the method is a property/event accessor. /// Gets whether the method is a property/event accessor.
/// </summary> /// </summary>
[MemberNotNullWhen(true, nameof(AccessorOwner))]
bool IsAccessor { get; } bool IsAccessor { get; }
/// <summary> /// <summary>

4
ICSharpCode.Decompiler/TypeSystem/IProperty.cs

@ -18,6 +18,8 @@
#nullable enable #nullable enable
using System.Diagnostics.CodeAnalysis;
namespace ICSharpCode.Decompiler.TypeSystem namespace ICSharpCode.Decompiler.TypeSystem
{ {
/// <summary> /// <summary>
@ -25,7 +27,9 @@ namespace ICSharpCode.Decompiler.TypeSystem
/// </summary> /// </summary>
public interface IProperty : IParameterizedMember public interface IProperty : IParameterizedMember
{ {
[MemberNotNullWhen(true, nameof(Getter))]
bool CanGet { get; } bool CanGet { get; }
[MemberNotNullWhen(true, nameof(Setter))]
bool CanSet { get; } bool CanSet { get; }
IMethod? Getter { get; } IMethod? Getter { get; }

4
ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataMethod.cs

@ -67,7 +67,8 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation
var (accessorOwner, semanticsAttribute) = module.PEFile.MethodSemanticsLookup.GetSemantics(handle); var (accessorOwner, semanticsAttribute) = module.PEFile.MethodSemanticsLookup.GetSemantics(handle);
const MethodAttributes finalizerAttributes = (MethodAttributes.Virtual | MethodAttributes.Family | MethodAttributes.HideBySig); const MethodAttributes finalizerAttributes = (MethodAttributes.Virtual | MethodAttributes.Family | MethodAttributes.HideBySig);
this.typeParameters = MetadataTypeParameter.Create(module, this, def.GetGenericParameters()); this.typeParameters = MetadataTypeParameter.Create(module, this, def.GetGenericParameters());
if (semanticsAttribute != 0) if (semanticsAttribute != 0 && !accessorOwner.IsNil
&& accessorOwner.Kind is HandleKind.PropertyDefinition or HandleKind.EventDefinition)
{ {
this.symbolKind = SymbolKind.Accessor; this.symbolKind = SymbolKind.Accessor;
this.accessorOwner = accessorOwner; this.accessorOwner = accessorOwner;
@ -129,7 +130,6 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation
public bool HasBody => module.metadata.GetMethodDefinition(handle).HasBody(); public bool HasBody => module.metadata.GetMethodDefinition(handle).HasBody();
public IMember AccessorOwner { public IMember AccessorOwner {
get { get {
if (accessorOwner.IsNil) if (accessorOwner.IsNil)

22
ICSharpCode.Decompiler/TypeSystem/NormalizeTypeVisitor.cs

@ -1,6 +1,22 @@
using System; // Copyright (c) 2019 Daniel Grunwald
using System.Collections.Generic; //
using System.Text; // Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#nullable enable
using ICSharpCode.Decompiler.TypeSystem.Implementation; using ICSharpCode.Decompiler.TypeSystem.Implementation;

20
ICSharpCode.Decompiler/TypeSystem/Nullability.cs

@ -1,6 +1,20 @@
using System; // Copyright (c) 2019 Daniel Grunwald
using System.Collections.Generic; //
using System.Text; // Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
namespace ICSharpCode.Decompiler.TypeSystem namespace ICSharpCode.Decompiler.TypeSystem
{ {

2
ICSharpCode.Decompiler/TypeSystem/TypeVisitor.cs

@ -16,6 +16,8 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
#nullable enable
using ICSharpCode.Decompiler.TypeSystem.Implementation; using ICSharpCode.Decompiler.TypeSystem.Implementation;
namespace ICSharpCode.Decompiler.TypeSystem namespace ICSharpCode.Decompiler.TypeSystem

Loading…
Cancel
Save