Skip to content

Commit

Permalink
Merge features/GeneratedCodeAttributes to main (#76602)
Browse files Browse the repository at this point in the history
Closes #73920.
  • Loading branch information
AlekseyTs authored Jan 3, 2025
2 parents fd40510 + bfa4f1a commit 14944a9
Show file tree
Hide file tree
Showing 156 changed files with 2,102 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ bool Cci.IMethodDefinition.RequiresSecurityObject
CheckDefinitionInvariant();

ImmutableArray<CSharpAttributeData> userDefined = AdaptedMethodSymbol.GetReturnTypeAttributes();
ArrayBuilder<SynthesizedAttributeData> synthesized = null;
ArrayBuilder<CSharpAttributeData> synthesized = null;
AdaptedMethodSymbol.AddSynthesizedReturnTypeAttributes((PEModuleBuilder)context.Module, ref synthesized);

// Note that callers of this method (CCI and ReflectionEmitter) have to enumerate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal IEnumerable<CSharpAttributeData> GetCustomAttributesToEmit(PEModuleBuil
CheckDefinitionInvariant();

ImmutableArray<CSharpAttributeData> userDefined = this.GetAttributes();
ArrayBuilder<SynthesizedAttributeData> synthesized = null;
ArrayBuilder<CSharpAttributeData> synthesized = null;
this.AddSynthesizedAttributes(moduleBuilder, ref synthesized);

if (emittingRefAssembly && !HasReferenceAssemblyAttribute)
Expand Down
6 changes: 3 additions & 3 deletions src/Compilers/CSharp/Portable/Emitter/Model/SymbolAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ internal IEnumerable<CSharpAttributeData> GetCustomAttributesToEmit(PEModuleBuil
Debug.Assert(this.Kind != SymbolKind.Assembly);

ImmutableArray<CSharpAttributeData> userDefined;
ArrayBuilder<SynthesizedAttributeData> synthesized = null;
ArrayBuilder<CSharpAttributeData> synthesized = null;
userDefined = this.GetAttributes();
this.AddSynthesizedAttributes(moduleBuilder, ref synthesized);

Expand All @@ -110,7 +110,7 @@ internal IEnumerable<CSharpAttributeData> GetCustomAttributesToEmit(PEModuleBuil
/// </summary>
internal IEnumerable<CSharpAttributeData> GetCustomAttributesToEmit(
ImmutableArray<CSharpAttributeData> userDefined,
ArrayBuilder<SynthesizedAttributeData> synthesized,
ArrayBuilder<CSharpAttributeData> synthesized,
bool isReturnType,
bool emittingAssemblyAttributesInNetModule)
{
Expand All @@ -127,7 +127,7 @@ internal IEnumerable<CSharpAttributeData> GetCustomAttributesToEmit(

private IEnumerable<CSharpAttributeData> GetCustomAttributesToEmitIterator(
ImmutableArray<CSharpAttributeData> userDefined,
ArrayBuilder<SynthesizedAttributeData> synthesized,
ArrayBuilder<CSharpAttributeData> synthesized,
bool isReturnType,
bool emittingAssemblyAttributesInNetModule)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@
using Roslyn.Utilities;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Symbols;
using Microsoft.CodeAnalysis.CSharp.Emit;
using Microsoft.CodeAnalysis.PooledObjects;

namespace Microsoft.CodeAnalysis.CSharp
{
/// <summary>
/// A field of a frame class that represents a variable that has been captured in a lambda.
/// </summary>
internal sealed class LambdaCapturedVariable : SynthesizedFieldSymbolBase, ISynthesizedMethodBodyImplementationSymbol
internal class LambdaCapturedVariable : SynthesizedFieldSymbolBase, ISynthesizedMethodBodyImplementationSymbol
{
private readonly TypeWithAnnotations _type;
private readonly bool _isThis;

private LambdaCapturedVariable(SynthesizedClosureEnvironment frame, TypeWithAnnotations type, string fieldName, bool isThisParameter)
protected LambdaCapturedVariable(SynthesizedClosureEnvironment frame, TypeWithAnnotations type, string fieldName, bool isThisParameter)
: base(frame,
fieldName,
isPublic: true,
Expand All @@ -39,21 +41,38 @@ private LambdaCapturedVariable(SynthesizedClosureEnvironment frame, TypeWithAnno
public SynthesizedClosureEnvironment Frame
=> (SynthesizedClosureEnvironment)ContainingType;

#nullable enable

public static LambdaCapturedVariable Create(SynthesizedClosureEnvironment frame, Symbol captured, ref int uniqueId)
{
Debug.Assert(captured is LocalSymbol || captured is ParameterSymbol);

string fieldName = GetCapturedVariableFieldName(captured, ref uniqueId);
TypeSymbol type = GetCapturedVariableFieldType(frame, captured);
return new LambdaCapturedVariable(frame, TypeWithAnnotations.Create(type), fieldName, IsThis(captured));
bool isThis = IsThis(captured, out ParameterSymbol? parameter);
return parameter is not null && !isThis ?
new LambdaCapturedVariableForRegularParameter(frame, TypeWithAnnotations.Create(type), fieldName, parameter) :
new LambdaCapturedVariable(frame, TypeWithAnnotations.Create(type), fieldName, isThis);
}

private static bool IsThis(Symbol captured)
{
var parameter = captured as ParameterSymbol;
return (object)parameter != null && parameter.IsThis;
return IsThis(captured, out _);
}

/// <param name="captured"></param>
/// <param name="parameter">
/// Is assigned to <paramref name="captured"/> when it is a <see cref="ParameterSymbol"/>,
/// null otherwise. Note, the value is not correlated with returned boolean.
/// </param>
private static bool IsThis(Symbol captured, out ParameterSymbol? parameter)
{
parameter = captured as ParameterSymbol;
return (object?)parameter != null && parameter.IsThis;
}

#nullable disable

private static string GetCapturedVariableFieldName(Symbol variable, ref int uniqueId)
{
if (IsThis(variable))
Expand Down Expand Up @@ -151,4 +170,34 @@ public IMethodSymbolInternal Method
public bool HasMethodBodyDependency
=> false;
}

internal sealed class LambdaCapturedVariableForRegularParameter : LambdaCapturedVariable
{
private readonly ParameterSymbol _parameter;

public LambdaCapturedVariableForRegularParameter(SynthesizedClosureEnvironment frame, TypeWithAnnotations type, string fieldName, ParameterSymbol parameter)
: base(frame, type, fieldName, isThisParameter: false)
{
Debug.Assert(parameter is { IsThis: false });
_parameter = parameter;
}

internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<CSharpAttributeData> attributes)
{
if (_parameter.OriginalDefinition is SourceParameterSymbolBase definition &&
ContainingModule == definition.ContainingModule)
{
foreach (CSharpAttributeData attr in definition.GetAttributes())
{
if (attr.AttributeClass is { HasCompilerLoweringPreserveAttribute: true } attributeType &&
(attributeType.GetAttributeUsageInfo().ValidTargets & System.AttributeTargets.Field) != 0)
{
AddSynthesizedAttribute(ref attributes, attr);
}
}
}

base.AddSynthesizedAttributes(moduleBuilder, ref attributes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal IteratorConstructor(StateMachineTypeSymbol container)
SynthesizedParameterSymbol.Create(this, TypeWithAnnotations.Create(intType), 0, RefKind.None, GeneratedNames.MakeStateMachineStateFieldName()));
}

internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<SynthesizedAttributeData> attributes)
internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<CSharpAttributeData> attributes)
{
base.AddSynthesizedAttributes(moduleBuilder, ref attributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public override BoundNode VisitLocalFunctionStatement(BoundLocalFunctionStatemen
if (_factory.CompilationState.Compilation.ShouldEmitNullableAttributes(localFunction))
{
bool constraintsNeedNullableAttribute = typeParameters.Any(
static typeParameter => ((SourceTypeParameterSymbolBase)typeParameter).ConstraintsNeedNullableAttribute());
static typeParameter => ((SourceTypeParameterSymbol)typeParameter).ConstraintsNeedNullableAttribute());

if (constraintsNeedNullableAttribute || hasReturnTypeOrParameter(localFunction, static t => t.NeedsNullableAttribute()))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ internal BaseMethodWrapperSymbol(NamedTypeSymbol containingType, MethodSymbol me
AssignTypeMapAndTypeParameters(typeMap, typeParameters);
}

internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<SynthesizedAttributeData> attributes)
internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<CSharpAttributeData> attributes)
{
base.AddSynthesizedAttributes(moduleBuilder, ref attributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis.CodeGen;
using Microsoft.CodeAnalysis.CSharp.Emit;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Symbols;
using Roslyn.Utilities;

Expand All @@ -16,7 +18,7 @@ namespace Microsoft.CodeAnalysis.CSharp
/// <summary>
/// Represents a synthesized state machine field.
/// </summary>
internal sealed class StateMachineFieldSymbol : SynthesizedFieldSymbolBase, ISynthesizedMethodBodyImplementationSymbol
internal class StateMachineFieldSymbol : SynthesizedFieldSymbolBase, ISynthesizedMethodBodyImplementationSymbol
{
private readonly TypeWithAnnotations _type;
private readonly bool _isThis;
Expand Down Expand Up @@ -83,4 +85,34 @@ internal override bool IsCapturedFrame
get { return _isThis; }
}
}

internal sealed class StateMachineFieldSymbolForRegularParameter : StateMachineFieldSymbol
{
private readonly ParameterSymbol _parameter;

public StateMachineFieldSymbolForRegularParameter(NamedTypeSymbol stateMachineType, TypeWithAnnotations type, string name, ParameterSymbol parameter, bool isPublic)
: base(stateMachineType, type, name, isPublic, isThis: false)
{
Debug.Assert(parameter is { IsThis: false });
_parameter = parameter;
}

internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<CSharpAttributeData> attributes)
{
if (_parameter.OriginalDefinition is SourceParameterSymbolBase definition &&
ContainingModule == definition.ContainingModule)
{
foreach (CSharpAttributeData attr in definition.GetAttributes())
{
if (attr.AttributeClass is { HasCompilerLoweringPreserveAttribute: true } attributeType &&
(attributeType.GetAttributeUsageInfo().ValidTargets & System.AttributeTargets.Field) != 0)
{
AddSynthesizedAttribute(ref attributes, attr);
}
}
}

base.AddSynthesizedAttributes(moduleBuilder, ref attributes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,12 @@ private void CreateNonReusableLocalProxies(
{
// The field needs to be public iff it is initialized directly from the kickoff method
// (i.e. not for IEnumerable which loads the values from parameter proxies).
var proxyField = F.StateMachineField(typeMap.SubstituteType(parameter.Type).Type, parameter.Name, isPublic: !PreserveInitialParameterValuesAndThreadId);
var proxyField = F.StateMachineFieldForRegularParameter(typeMap.SubstituteType(parameter.Type).Type, parameter.Name, parameter, isPublic: !PreserveInitialParameterValuesAndThreadId);
proxiesBuilder.Add(parameter, new CapturedToStateMachineFieldReplacement(proxyField, isReusable: false));

if (PreserveInitialParameterValuesAndThreadId)
{
var field = F.StateMachineField(typeMap.SubstituteType(parameter.Type).Type, GeneratedNames.StateMachineParameterProxyFieldName(parameter.Name), isPublic: true);
var field = F.StateMachineFieldForRegularParameter(typeMap.SubstituteType(parameter.Type).Type, GeneratedNames.StateMachineParameterProxyFieldName(parameter.Name), parameter, isPublic: true);
initialParameters.Add(parameter, new CapturedToStateMachineFieldReplacement(field, isReusable: false));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,7 @@ public sealed override ImmutableArray<CSharpAttributeData> GetAttributes()
public sealed override bool AreLocalsZeroed => KickoffMethod.AreLocalsZeroed;

internal override bool HasCodeAnalysisEmbeddedAttribute => false;

internal override bool HasCompilerLoweringPreserveAttribute => false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public SynthesizedStateMachineDebuggerHiddenMethod(
{
}

internal sealed override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<SynthesizedAttributeData> attributes)
internal sealed override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<CSharpAttributeData> attributes)
{
var compilation = this.DeclaringCompilation;
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerHiddenAttribute__ctor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected override void MethodChecks(BindingDiagnosticBag diagnostics)
// TODO: move more functionality into here, making these symbols more lazy
}

internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<SynthesizedAttributeData> attributes)
internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<CSharpAttributeData> attributes)
{
base.AddSynthesizedAttributes(moduleBuilder, ref attributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ public StateMachineFieldSymbol StateMachineField(TypeSymbol type, string name, b
return result;
}

public StateMachineFieldSymbol StateMachineFieldForRegularParameter(TypeSymbol type, string name, ParameterSymbol parameter, bool isPublic)
{
Debug.Assert(CurrentType is { });
var result = new StateMachineFieldSymbolForRegularParameter(CurrentType, TypeWithAnnotations.Create(type), name, parameter, isPublic);
AddField(CurrentType, result);
return result;
}

public StateMachineFieldSymbol StateMachineField(TypeSymbol type, string name, SynthesizedLocalKind synthesizedKind, int slotIndex)
{
Debug.Assert(CurrentType is { });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ internal sealed override IEnumerable<FieldSymbol> GetFieldsToEmit()

internal sealed override bool HasCodeAnalysisEmbeddedAttribute => false;

internal sealed override bool HasCompilerLoweringPreserveAttribute => false;

internal sealed override bool IsInterpolatedStringHandlerType => false;

internal sealed override ImmutableArray<Symbol> GetEarlyAttributeDecodingMembers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private static ImmutableArray<Symbol> CreateMembers(MethodSymbol constructor, Me

public override ImmutableArray<TypeParameterSymbol> TypeParameters { get; }

internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<SynthesizedAttributeData> attributes)
internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<CSharpAttributeData> attributes)
{
base.AddSynthesizedAttributes(moduleBuilder, ref attributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public override bool IsImplicitlyDeclared
get { return true; }
}

internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<SynthesizedAttributeData> attributes)
internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<CSharpAttributeData> attributes)
{
base.AddSynthesizedAttributes(moduleBuilder, ref attributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ internal override bool IsMetadataFinal
}
}

internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<SynthesizedAttributeData> attributes)
internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<CSharpAttributeData> attributes)
{
// Do not call base.AddSynthesizedAttributes.
// Dev11 does not emit DebuggerHiddenAttribute in property accessors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ internal sealed override bool IsMetadataNewSlot(bool ignoreInterfaceImplementati
return false;
}

internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<SynthesizedAttributeData> attributes)
internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<CSharpAttributeData> attributes)
{
base.AddSynthesizedAttributes(moduleBuilder, ref attributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public override IEnumerable<string> MemberNames
get { return _nameToSymbols.Keys; }
}

internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<SynthesizedAttributeData> attributes)
internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<CSharpAttributeData> attributes)
{
base.AddSynthesizedAttributes(moduleBuilder, ref attributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ internal override bool GetGuidString(out string? guidString)

internal sealed override bool HasCodeAnalysisEmbeddedAttribute => false;

internal sealed override bool HasCompilerLoweringPreserveAttribute => false;

internal sealed override bool IsInterpolatedStringHandlerType => false;

internal sealed override ImmutableArray<Symbol> GetEarlyAttributeDecodingMembers()
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Symbols/ConstraintsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal static class ConstraintsHelper
/// generic method. In those cases, additional constraint checks are applied.
/// </summary>
public static TypeParameterBounds ResolveBounds(
this SourceTypeParameterSymbolBase typeParameter,
this SourceTypeParameterSymbol typeParameter,
AssemblySymbol corLibrary,
ConsList<TypeParameterSymbol> inProgress,
ImmutableArray<TypeWithAnnotations> constraintTypes,
Expand Down
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Symbols/ErrorTypeSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ internal override bool GetGuidString(out string? guidString)

internal override bool HasCodeAnalysisEmbeddedAttribute => false;

internal override bool HasCompilerLoweringPreserveAttribute => false;

internal override bool IsInterpolatedStringHandlerType => false;

internal override ImmutableArray<NamedTypeSymbol> InterfacesNoUseSiteDiagnostics(ConsList<TypeSymbol>? basesBeingResolved)
Expand Down
Loading

0 comments on commit 14944a9

Please sign in to comment.