Skip to content

Commit

Permalink
Pack bits in SourceOrdinaryMethodSymbol into an existing bitflag stru…
Browse files Browse the repository at this point in the history
…cture we have for all source methods (#68158)

* Pack bits in SourceOrdinaryMethodSymbol into an existing bitflag structure we have for all source methods (#68132)

* In progresS

* Utilize

* initialize once

* Save space in other methods

* reorder

* reorder

* Move down

* Reorder

* Simplify

* Add docs

* move up

* Compute IsVarArg up front

* Remove unused usings

* Remove

* Remove passing of flag

* move up and make non-virtual

* "move up and make sealed"

* move up and make sealed

* use properties instead of flags

* Make parameters non-optional

* Pass all args

* move more down

* move more down

* Pass all args

* move more down

* move more down

* move more down

* move more down

* Name consistently

* Set flag consistently

* Rename

* Fix usage of hasBody where it means hasBlockBody

* Change to true

* Change to true

* Fix vararg check

* Change to true

* NRT

* Pass values explicitly

* Add assert

* Flip depending on if something is abstract or not.

* Update check

* Fix ocmment

* Fix ocmment

* Update src/Compilers/CSharp/Portable/Symbols/Source/SourceConstructorSymbol.cs
  • Loading branch information
CyrusNajmabadi authored May 16, 2023
1 parent 69f87c6 commit 35d72b8
Show file tree
Hide file tree
Showing 31 changed files with 195 additions and 316 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ protected override ImmutableArray<TypeSymbol> ExtraSynthesizedRefParameters

internal override bool InheritsBaseMethodAttributes => true;
internal override bool GenerateDebugInfo => !this.IsAsync;
internal override bool IsExpressionBodied => false;

internal override int CalculateLocalSyntaxOffset(int localPosition, SyntaxTree localTree)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ protected SynthesizedMethodBaseSymbol(NamedTypeSymbol containingType,

this.MakeFlags(
methodKind: MethodKind.Ordinary,
refKind: baseMethod.RefKind,
declarationModifiers: declarationModifiers,
returnsVoid: baseMethod.ReturnsVoid,
// Consider synthesized methods to always have bodies.
hasAnyBody: true,
isExtensionMethod: false,
isNullableAnalysisEnabled: false,
isMetadataVirtualIgnoringModifiers: false);
isVarArg: baseMethod.IsVararg,
isMetadataVirtualIgnoringModifiers: false,
isExpressionBodied: false);
}

protected void AssignTypeMapAndTypeParameters(TypeMap typeMap, ImmutableArray<TypeParameterSymbol> typeParameters)
Expand Down Expand Up @@ -196,11 +201,6 @@ internal sealed override IEnumerable<SecurityAttribute> GetSecurityInformation()

#nullable disable

public sealed override RefKind RefKind
{
get { return this.BaseMethod.RefKind; }
}

public sealed override TypeWithAnnotations ReturnTypeWithAnnotations
{
get { return this.TypeMap.SubstituteType(this.BaseMethod.OriginalDefinition.ReturnTypeWithAnnotations); }
Expand All @@ -212,11 +212,6 @@ public sealed override TypeWithAnnotations ReturnTypeWithAnnotations

public sealed override FlowAnalysisAnnotations FlowAnalysisAnnotations => FlowAnalysisAnnotations.None;

public sealed override bool IsVararg
{
get { return this.BaseMethod.IsVararg; }
}

public sealed override string Name
{
get { return _name; }
Expand All @@ -226,10 +221,5 @@ public sealed override bool IsImplicitlyDeclared
{
get { return true; }
}

internal override bool IsExpressionBodied
{
get { return false; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
internal sealed class SourceConstructorSymbol : SourceConstructorSymbolBase
{
private readonly bool _isExpressionBodied;
private readonly bool _hasThisInitializer;

public static SourceConstructorSymbol CreateConstructorSymbol(
Expand All @@ -35,14 +34,16 @@ private SourceConstructorSymbol(
base(containingType, location, syntax, SyntaxFacts.HasYieldOperations(syntax))
{
bool hasBlockBody = syntax.Body != null;
_isExpressionBodied = !hasBlockBody && syntax.ExpressionBody != null;
bool hasBody = hasBlockBody || _isExpressionBodied;
bool isExpressionBodied = !hasBlockBody && syntax.ExpressionBody != null;
bool hasAnyBody = hasBlockBody || isExpressionBodied;

_hasThisInitializer = syntax.Initializer?.Kind() == SyntaxKind.ThisConstructorInitializer;

bool modifierErrors;
var declarationModifiers = this.MakeModifiers(syntax.Modifiers, methodKind, hasBody, location, diagnostics, out modifierErrors);
this.MakeFlags(methodKind, declarationModifiers, returnsVoid: true, isExtensionMethod: false, isNullableAnalysisEnabled: isNullableAnalysisEnabled);
var declarationModifiers = this.MakeModifiers(syntax.Modifiers, methodKind, hasAnyBody, location, diagnostics, out modifierErrors);
this.MakeFlags(
methodKind, RefKind.None, declarationModifiers, returnsVoid: true, hasAnyBody: hasAnyBody, isExpressionBodied: isExpressionBodied,
isExtensionMethod: false, isVarArg: syntax.ParameterList.Parameters.Any(static p => p.IsArgList), isNullableAnalysisEnabled: isNullableAnalysisEnabled);

if (syntax.Identifier.ValueText != containingType.Name)
{
Expand All @@ -57,22 +58,22 @@ private SourceConstructorSymbol(
diagnostics.Add(ErrorCode.ERR_ExternHasConstructorInitializer, location, this);
}

if (hasBody)
if (hasAnyBody)
{
diagnostics.Add(ErrorCode.ERR_ExternHasBody, location, this);
}
}

if (methodKind == MethodKind.StaticConstructor)
{
CheckFeatureAvailabilityAndRuntimeSupport(syntax, location, hasBody, diagnostics);
CheckFeatureAvailabilityAndRuntimeSupport(syntax, location, hasAnyBody, diagnostics);
}

ModifierUtils.CheckAccessibility(this.DeclarationModifiers, this, isExplicitInterfaceImplementation: false, diagnostics, location);

if (!modifierErrors)
{
this.CheckModifiers(methodKind, hasBody, location, diagnostics);
this.CheckModifiers(methodKind, hasAnyBody, location, diagnostics);
}

CheckForBlockAndExpressionBody(
Expand Down Expand Up @@ -163,14 +164,6 @@ internal override OneOrMany<SyntaxList<AttributeListSyntax>> GetAttributeDeclara
return OneOrMany.Create(((ConstructorDeclarationSyntax)this.SyntaxNode).AttributeLists);
}

internal override bool IsExpressionBodied
{
get
{
return _isExpressionBodied;
}
}

internal override bool IsNullableAnalysisEnabled()
{
return _hasThisInitializer ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ internal abstract class SourceConstructorSymbolBase : SourceMemberMethodSymbol
{
protected ImmutableArray<ParameterSymbol> _lazyParameters;
private TypeWithAnnotations _lazyReturnType;
private bool _lazyIsVararg;

protected SourceConstructorSymbolBase(
SourceMemberContainerTypeSymbol containingType,
Expand Down Expand Up @@ -48,15 +47,13 @@ protected sealed override void MethodChecks(BindingDiagnosticBag diagnostics)
// instance). Constraints are checked in AfterAddingTypeMembersChecks.
var signatureBinder = bodyBinder.WithAdditionalFlagsAndContainingMemberOrLambda(BinderFlags.SuppressConstraintChecks, this);

SyntaxToken arglistToken;
_lazyParameters = ParameterHelpers.MakeParameters(
signatureBinder, this, parameterList, out arglistToken,
signatureBinder, this, parameterList, out _,
allowRefOrOut: AllowRefOrOut,
allowThis: false,
addRefReadOnlyModifier: false,
diagnostics: diagnostics).Cast<SourceParameterSymbol, ParameterSymbol>();

_lazyIsVararg = (arglistToken.Kind() == SyntaxKind.ArgListKeyword);
_lazyReturnType = TypeWithAnnotations.Create(bodyBinder.GetSpecialType(SpecialType.System_Void, diagnostics, syntax));

var location = this.GetFirstLocation();
Expand All @@ -72,7 +69,7 @@ protected sealed override void MethodChecks(BindingDiagnosticBag diagnostics)
this.CheckEffectiveAccessibility(_lazyReturnType, _lazyParameters, diagnostics);
this.CheckFileTypeUsage(_lazyReturnType, _lazyParameters, diagnostics);

if (_lazyIsVararg && (IsGenericMethod || ContainingType.IsGenericType || _lazyParameters.Length > 0 && _lazyParameters[_lazyParameters.Length - 1].IsParams))
if (this.IsVararg && (IsGenericMethod || ContainingType.IsGenericType || _lazyParameters.Length > 0 && _lazyParameters[_lazyParameters.Length - 1].IsParams))
{
diagnostics.Add(ErrorCode.ERR_BadVarargs, location);
}
Expand Down Expand Up @@ -100,15 +97,6 @@ internal sealed override void AfterAddingTypeMembersChecks(ConversionsBase conve
}
}

public sealed override bool IsVararg
{
get
{
LazyMethodChecks();
return _lazyIsVararg;
}
}

public sealed override bool IsImplicitlyDeclared
{
get
Expand Down Expand Up @@ -150,11 +138,6 @@ public sealed override ImmutableArray<ImmutableArray<TypeWithAnnotations>> GetTy
public sealed override ImmutableArray<TypeParameterConstraintKind> GetTypeParameterConstraintKinds()
=> ImmutableArray<TypeParameterConstraintKind>.Empty;

public override RefKind RefKind
{
get { return RefKind.None; }
}

public sealed override TypeWithAnnotations ReturnTypeWithAnnotations
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#nullable disable

using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslyn.Utilities;
Expand Down Expand Up @@ -32,7 +31,9 @@ internal SourceCustomEventAccessorSymbol(
syntax.Keyword.GetLocation(), explicitlyImplementedEventOpt, aliasQualifierOpt,
isAdder: syntax.Kind() == SyntaxKind.AddAccessorDeclaration,
isIterator: SyntaxFacts.HasYieldOperations(syntax.Body),
isNullableAnalysisEnabled: isNullableAnalysisEnabled)
isNullableAnalysisEnabled: isNullableAnalysisEnabled,
hasAnyBody: syntax.Body is not null || syntax.ExpressionBody is not null,
isExpressionBodied: syntax is { Body: null, ExpressionBody: not null })
{
Debug.Assert(syntax != null);
Debug.Assert(syntax.Kind() == SyntaxKind.AddAccessorDeclaration || syntax.Kind() == SyntaxKind.RemoveAccessorDeclaration);
Expand Down Expand Up @@ -91,16 +92,5 @@ internal override bool GenerateDebugInfo
{
get { return true; }
}

internal override bool IsExpressionBodied
{
get
{
var syntax = GetSyntax();
var hasBody = syntax.Body != null;
var hasExpressionBody = syntax.ExpressionBody != null;
return !hasBody && hasExpressionBody;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ internal SourceCustomEventSymbol(SourceMemberContainerTypeSymbol containingType,
diagnostics.Add(ErrorCode.ERR_RuntimeDoesNotSupportDefaultInterfaceImplementation, this.GetFirstLocation());
}

_addMethod = new SynthesizedEventAccessorSymbol(this, isAdder: true, explicitlyImplementedEvent, aliasQualifierOpt);
_removeMethod = new SynthesizedEventAccessorSymbol(this, isAdder: false, explicitlyImplementedEvent, aliasQualifierOpt);
// No body as this was an abstract event.
_addMethod = new SynthesizedEventAccessorSymbol(this, isAdder: true, hasAnyBody: false, isExpressionBodied: false, explicitlyImplementedEvent, aliasQualifierOpt);
_removeMethod = new SynthesizedEventAccessorSymbol(this, isAdder: false, hasAnyBody: false, isExpressionBodied: false, explicitlyImplementedEvent, aliasQualifierOpt);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ protected SourceDelegateMethodSymbol(
TypeWithAnnotations returnType,
DelegateDeclarationSyntax syntax,
MethodKind methodKind,
RefKind refKind,
DeclarationModifiers declarationModifiers)
: base(delegateType, syntax.GetReference(), location: syntax.Identifier.GetLocation(), isIterator: false)
{
_returnType = returnType;
this.MakeFlags(methodKind, declarationModifiers, _returnType.IsVoidType(), isExtensionMethod: false, isNullableAnalysisEnabled: false);
this.MakeFlags(
methodKind, refKind, declarationModifiers, _returnType.IsVoidType(), hasAnyBody: false, isExpressionBodied: false,
isExtensionMethod: false, isVarArg: false, isNullableAnalysisEnabled: false);
}

internal sealed override ExecutableCodeBinder TryGetBodyBinder(BinderFactory binderFactoryOpt = null, bool ignoreAccessibility = false) => throw ExceptionUtilities.Unreachable();
Expand Down Expand Up @@ -129,14 +132,6 @@ protected override void MethodChecks(BindingDiagnosticBag diagnostics)
// TODO: move more functionality into here, making these symbols more lazy
}

public sealed override bool IsVararg
{
get
{
return false;
}
}

public sealed override ImmutableArray<ParameterSymbol> Parameters
{
get
Expand Down Expand Up @@ -176,11 +171,6 @@ public sealed override bool IsImplicitlyDeclared
}
}

internal override bool IsExpressionBodied
{
get { return false; }
}

internal override bool GenerateDebugInfo
{
get { return false; }
Expand Down Expand Up @@ -219,7 +209,7 @@ internal Constructor(
TypeWithAnnotations objectType,
TypeWithAnnotations intPtrType,
DelegateDeclarationSyntax syntax)
: base(delegateType, voidType, syntax, MethodKind.Constructor, DeclarationModifiers.Public)
: base(delegateType, voidType, syntax, MethodKind.Constructor, RefKind.None, DeclarationModifiers.Public)
{
InitializeParameters(ImmutableArray.Create<ParameterSymbol>(
SynthesizedParameterSymbol.Create(this, objectType, 0, RefKind.None, "object"),
Expand All @@ -231,11 +221,6 @@ public override string Name
get { return WellKnownMemberNames.InstanceConstructorName; }
}

public override RefKind RefKind
{
get { return RefKind.None; }
}

internal override OneOrMany<SyntaxList<AttributeListSyntax>> GetReturnTypeAttributeDeclarations()
{
// Constructors don't have return type attributes
Expand All @@ -258,7 +243,6 @@ internal override LexicalSortKey GetLexicalSortKey()

private sealed class InvokeMethod : SourceDelegateMethodSymbol
{
private readonly RefKind _refKind;
private readonly ImmutableArray<CustomModifier> _refCustomModifiers;

internal InvokeMethod(
Expand All @@ -268,10 +252,8 @@ internal InvokeMethod(
DelegateDeclarationSyntax syntax,
Binder binder,
BindingDiagnosticBag diagnostics)
: base(delegateType, returnType, syntax, MethodKind.DelegateInvoke, DeclarationModifiers.Virtual | DeclarationModifiers.Public)
: base(delegateType, returnType, syntax, MethodKind.DelegateInvoke, refKind, DeclarationModifiers.Virtual | DeclarationModifiers.Public)
{
this._refKind = refKind;

SyntaxToken arglistToken;
var parameters = ParameterHelpers.MakeParameters(
binder, this, syntax.ParameterList, out arglistToken,
Expand All @@ -288,7 +270,7 @@ internal InvokeMethod(
diagnostics.Add(ErrorCode.ERR_IllegalVarArgs, new SourceLocation(arglistToken));
}

if (_refKind == RefKind.RefReadOnly)
if (this.RefKind == RefKind.RefReadOnly)
{
var modifierType = binder.GetWellKnownType(WellKnownType.System_Runtime_InteropServices_InAttribute, diagnostics, syntax.ReturnType);
_refCustomModifiers = ImmutableArray.Create(CSharpCustomModifier.CreateRequired(modifierType));
Expand All @@ -306,11 +288,6 @@ public override string Name
get { return WellKnownMemberNames.DelegateInvokeName; }
}

public override RefKind RefKind
{
get { return _refKind; }
}

internal override LexicalSortKey GetLexicalSortKey()
{
// associate "Invoke and .ctor" with whole delegate declaration for the sorting purposes
Expand All @@ -332,7 +309,7 @@ internal override void AfterAddingTypeMembersChecks(ConversionsBase conversions,

base.AfterAddingTypeMembersChecks(conversions, diagnostics);

if (_refKind == RefKind.RefReadOnly)
if (this.RefKind == RefKind.RefReadOnly)
{
compilation.EnsureIsReadOnlyAttributeExists(diagnostics, location, modifyCompilation: true);
}
Expand Down Expand Up @@ -367,7 +344,7 @@ internal BeginInvokeMethod(
TypeWithAnnotations objectType,
TypeWithAnnotations asyncCallbackType,
DelegateDeclarationSyntax syntax)
: base((SourceNamedTypeSymbol)invoke.ContainingType, iAsyncResultType, syntax, MethodKind.Ordinary, DeclarationModifiers.Virtual | DeclarationModifiers.Public)
: base((SourceNamedTypeSymbol)invoke.ContainingType, iAsyncResultType, syntax, MethodKind.Ordinary, RefKind.None, DeclarationModifiers.Virtual | DeclarationModifiers.Public)
{
var parameters = ArrayBuilder<ParameterSymbol>.GetInstance();
foreach (SourceParameterSymbol p in invoke.Parameters)
Expand All @@ -388,11 +365,6 @@ public override string Name
get { return WellKnownMemberNames.DelegateBeginInvokeName; }
}

public override RefKind RefKind
{
get { return RefKind.None; }
}

internal override OneOrMany<SyntaxList<AttributeListSyntax>> GetReturnTypeAttributeDeclarations()
{
// BeginInvoke method doesn't have return type attributes
Expand All @@ -410,7 +382,7 @@ internal EndInvokeMethod(
InvokeMethod invoke,
TypeWithAnnotations iAsyncResultType,
DelegateDeclarationSyntax syntax)
: base((SourceNamedTypeSymbol)invoke.ContainingType, invoke.ReturnTypeWithAnnotations, syntax, MethodKind.Ordinary, DeclarationModifiers.Virtual | DeclarationModifiers.Public)
: base((SourceNamedTypeSymbol)invoke.ContainingType, invoke.ReturnTypeWithAnnotations, syntax, MethodKind.Ordinary, invoke.RefKind, DeclarationModifiers.Virtual | DeclarationModifiers.Public)
{
_invoke = invoke;

Expand All @@ -434,8 +406,6 @@ internal EndInvokeMethod(

public override string Name => WellKnownMemberNames.DelegateEndInvokeName;

public override RefKind RefKind => _invoke.RefKind;

public override ImmutableArray<CustomModifier> RefCustomModifiers => _invoke.RefCustomModifiers;
}

Expand Down
Loading

0 comments on commit 35d72b8

Please sign in to comment.