Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SourceClonedParameterSymbol abstract #54355

Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,27 @@

#nullable disable

using System;
using System.Collections.Immutable;
using System.Diagnostics;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{

// PROTOTYPE(caller-expr): Make SourceClonedParameterSymbol abstract and introduce new specialized types (same as being done in VB).

/// <summary>
/// Represents a source parameter cloned from another <see cref="SourceParameterSymbol"/>, when they must share attribute data and default constant value.
/// For example, parameters on a property symbol are cloned to generate parameters on accessors.
/// Similarly parameters on delegate invoke method are cloned to delegate begin/end invoke methods.
/// </summary>
internal sealed class SourceClonedParameterSymbol : SourceParameterSymbolBase
internal abstract class SourceClonedParameterSymbol : SourceParameterSymbolBase
{
// if true suppresses params-array and default value:
private readonly bool _suppressOptional;

private readonly SourceParameterSymbol _originalParam;
protected readonly SourceParameterSymbol _originalParam;

internal SourceClonedParameterSymbol(SourceParameterSymbol originalParam, Symbol newOwner, int newOrdinal, bool suppressOptional)
: base(newOwner, newOrdinal)
{
Debug.Assert((object)originalParam != null);

_suppressOptional = suppressOptional;
_originalParam = originalParam;
}
Expand Down Expand Up @@ -76,15 +71,6 @@ internal override ConstantValue DefaultValueFromAttributes
get { return _originalParam.DefaultValueFromAttributes; }
}

internal override ParameterSymbol WithCustomModifiersAndParams(TypeSymbol newType, ImmutableArray<CustomModifier> newCustomModifiers, ImmutableArray<CustomModifier> newRefCustomModifiers, bool newIsParams)
{
return new SourceClonedParameterSymbol(
_originalParam.WithCustomModifiersAndParamsCore(newType, newCustomModifiers, newRefCustomModifiers, newIsParams),
this.ContainingSymbol,
this.Ordinal,
_suppressOptional);
}

#region Forwarded

public override TypeWithAnnotations TypeWithAnnotations
Expand Down Expand Up @@ -142,26 +128,6 @@ internal override bool IsIUnknownConstant
get { return _originalParam.IsIUnknownConstant; }
}

internal override bool IsCallerFilePath
{
get { return _originalParam.IsCallerFilePath; }
}

internal override bool IsCallerLineNumber
{
get { return _originalParam.IsCallerLineNumber; }
}

internal override bool IsCallerMemberName
{
get { return _originalParam.IsCallerMemberName; }
}

internal override int CallerArgumentExpressionParameterIndex
{
get { return _originalParam.CallerArgumentExpressionParameterIndex; }
}

internal override FlowAnalysisAnnotations FlowAnalysisAnnotations
{
get { return FlowAnalysisAnnotations.None; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Immutable;
using System.Diagnostics;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
internal sealed class SourceDelegateClonedParameterSymbolForBeginAndEndInvoke : SourceClonedParameterSymbol
{
internal SourceDelegateClonedParameterSymbolForBeginAndEndInvoke(SourceParameterSymbol originalParam, SourceDelegateMethodSymbol newOwner, int newOrdinal)
: base(originalParam, newOwner, newOrdinal, suppressOptional: true)
{
333fred marked this conversation as resolved.
Show resolved Hide resolved
}

internal override bool IsCallerFilePath => _originalParam.IsCallerFilePath;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't understand why properties that are implemented the same way in SourceDelegateClonedParameterSymbolForBeginAndEndInvoke and SourcePropertyClonedParameterSymbolForAccessors were moved out of the base type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcouv This was requested by @AlekseyTs. The reasoning is to force new types to implement it. i.e, avoid future mistakes if a new type should implement it another way.


internal override bool IsCallerLineNumber => _originalParam.IsCallerLineNumber;

internal override bool IsCallerMemberName => _originalParam.IsCallerMemberName;

// We don't currently support caller argument expression for cloned begin/end invoke since
// they throw PlatformNotSupportedException at runtime and we feel it's unnecessary to support them.
internal override int CallerArgumentExpressionParameterIndex => -1;
333fred marked this conversation as resolved.
Show resolved Hide resolved

internal override ParameterSymbol WithCustomModifiersAndParams(TypeSymbol newType, ImmutableArray<CustomModifier> newCustomModifiers, ImmutableArray<CustomModifier> newRefCustomModifiers, bool newIsParams)
{
return new SourceDelegateClonedParameterSymbolForBeginAndEndInvoke(
_originalParam.WithCustomModifiersAndParamsCore(newType, newCustomModifiers, newRefCustomModifiers, newIsParams),
(SourceDelegateMethodSymbol)ContainingSymbol,
Ordinal);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ internal BeginInvokeMethod(
var parameters = ArrayBuilder<ParameterSymbol>.GetInstance();
foreach (SourceParameterSymbol p in invoke.Parameters)
{
var synthesizedParam = new SourceClonedParameterSymbol(originalParam: p, newOwner: this, newOrdinal: p.Ordinal, suppressOptional: true);
var synthesizedParam = new SourceDelegateClonedParameterSymbolForBeginAndEndInvoke(originalParam: p, newOwner: this, newOrdinal: p.Ordinal);
parameters.Add(synthesizedParam);
}

Expand Down Expand Up @@ -403,7 +403,7 @@ internal EndInvokeMethod(
{
if (p.RefKind != RefKind.None)
{
var synthesizedParam = new SourceClonedParameterSymbol(originalParam: p, newOwner: this, newOrdinal: ordinal++, suppressOptional: true);
var synthesizedParam = new SourceDelegateClonedParameterSymbolForBeginAndEndInvoke(originalParam: p, newOwner: this, newOrdinal: ordinal++);
parameters.Add(synthesizedParam);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ private ImmutableArray<ParameterSymbol> ComputeParameters(BindingDiagnosticBag d
// since the ContainingSymbol needs to be set to the accessor.
foreach (SourceParameterSymbol propertyParam in propertyParameters)
{
parameters.Add(new SourceClonedParameterSymbol(propertyParam, this, propertyParam.Ordinal, suppressOptional: false));
parameters.Add(new SourcePropertyClonedParameterSymbolForAccessors(propertyParam, this));
}

if (!isGetMethod)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
internal sealed class SourcePropertyClonedParameterSymbolForAccessors : SourceClonedParameterSymbol
{
internal SourcePropertyClonedParameterSymbolForAccessors(SourceParameterSymbol originalParam, Symbol newOwner)
: base(originalParam, newOwner, originalParam.Ordinal, suppressOptional: false)
{
}

internal override bool IsCallerFilePath => _originalParam.IsCallerFilePath;

internal override bool IsCallerLineNumber => _originalParam.IsCallerLineNumber;

internal override bool IsCallerMemberName => _originalParam.IsCallerMemberName;

internal override int CallerArgumentExpressionParameterIndex => _originalParam.CallerArgumentExpressionParameterIndex;

internal override ParameterSymbol WithCustomModifiersAndParams(TypeSymbol newType, ImmutableArray<CustomModifier> newCustomModifiers, ImmutableArray<CustomModifier> newRefCustomModifiers, bool newIsParams)
{
return new SourcePropertyClonedParameterSymbolForAccessors(
_originalParam.WithCustomModifiersAndParamsCore(newType, newCustomModifiers, newRefCustomModifiers, newIsParams),
this.ContainingSymbol);
}
}
}
Loading