Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarTawfik committed Feb 19, 2018
1 parent 3c47bf4 commit 97d0909
Show file tree
Hide file tree
Showing 19 changed files with 173 additions and 147 deletions.
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5265,7 +5265,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<value>The type '{2}' cannot be a reference type, or contain reference type fields at any level of nesting, in order to use it as parameter '{1}' in the generic type or method '{0}'</value>
</data>
<data name="ERR_UnmanagedConstraintWithLocalFunctions" xml:space="preserve">
<value>Using unmanaged constraint on local functions type parameters is not supported.</value>
<value>Using 'unmanaged' constraint on local functions type parameters is not supported.</value>
</data>
<data name="ERR_ConWithUnmanagedCon" xml:space="preserve">
<value>Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ internal sealed class PETypeParameterSymbol
#region Metadata
private readonly string _name;
private readonly ushort _ordinal; // 0 for first, 1 for second, ...
private readonly GenericParameterAttributes _flags;
private readonly bool _hasIsUnmanagedAttribute;
#endregion

private TypeParameterBounds _lazyBounds = TypeParameterBounds.Unset;

/// <summary>
/// First error calculating bounds.
/// </summary>
private DiagnosticInfo _lazyConstraintsUseSiteErrorInfo = CSDiagnosticInfo.EmptyErrorInfo; // Indicates unknown state.

private GenericParameterAttributes _lazyFlags;
private ThreeState _lazyHasIsUnmanagedAttribute;
private TypeParameterBounds _lazyBounds = TypeParameterBounds.Unset;
private ImmutableArray<TypeSymbol> _lazyDeclaredConstraintTypes;
private ImmutableArray<CSharpAttributeData> _lazyCustomAttributes;

internal PETypeParameterSymbol(
Expand Down Expand Up @@ -87,15 +87,7 @@ private PETypeParameterSymbol(

// Clear the '.ctor' flag if both '.ctor' and 'valuetype' are
// set since '.ctor' is redundant in that case.
_flags = ((flags & GenericParameterAttributes.NotNullableValueTypeConstraint) == 0) ? flags : (flags & ~GenericParameterAttributes.DefaultConstructorConstraint);
_hasIsUnmanagedAttribute = moduleSymbol.Module.HasIsUnmanagedAttribute(handle);

if (_hasIsUnmanagedAttribute && (_flags & (GenericParameterAttributes.ReferenceTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint)) != 0)
{
// Unmanaged constraint cannot be combined with new() or class constraints
_flags = _flags & ~(GenericParameterAttributes.ReferenceTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint);
_lazyConstraintsUseSiteErrorInfo = new CSDiagnosticInfo(ErrorCode.ERR_BindToBogus, this);
}
_lazyFlags = ((flags & GenericParameterAttributes.NotNullableValueTypeConstraint) == 0) ? flags : (flags & ~GenericParameterAttributes.DefaultConstructorConstraint);

_ordinal = ordinal;
_handle = handle;
Expand Down Expand Up @@ -147,83 +139,99 @@ public override AssemblySymbol ContainingAssembly

private ImmutableArray<TypeSymbol> GetDeclaredConstraintTypes()
{
PEMethodSymbol containingMethod = null;
PENamedTypeSymbol containingType;

if (_containingSymbol.Kind == SymbolKind.Method)
{
containingMethod = (PEMethodSymbol)_containingSymbol;
containingType = (PENamedTypeSymbol)containingMethod.ContainingSymbol;
}
else
if (_lazyDeclaredConstraintTypes.IsDefault)
{
containingType = (PENamedTypeSymbol)_containingSymbol;
}
ImmutableArray<TypeSymbol> declaredConstraintTypes;

var moduleSymbol = containingType.ContainingPEModule;
var metadataReader = moduleSymbol.Module.MetadataReader;
GenericParameterConstraintHandleCollection constraints;
PEMethodSymbol containingMethod = null;
PENamedTypeSymbol containingType;

try
{
constraints = metadataReader.GetGenericParameter(_handle).GetConstraints();
}
catch (BadImageFormatException)
{
constraints = default(GenericParameterConstraintHandleCollection);
Interlocked.CompareExchange(ref _lazyConstraintsUseSiteErrorInfo, new CSDiagnosticInfo(ErrorCode.ERR_BindToBogus, this), CSDiagnosticInfo.EmptyErrorInfo);
}
if (_containingSymbol.Kind == SymbolKind.Method)
{
containingMethod = (PEMethodSymbol)_containingSymbol;
containingType = (PENamedTypeSymbol)containingMethod.ContainingSymbol;
}
else
{
containingType = (PENamedTypeSymbol)_containingSymbol;
}

if (constraints.Count > 0)
{
var symbolsBuilder = ArrayBuilder<TypeSymbol>.GetInstance();
MetadataDecoder tokenDecoder;
var moduleSymbol = containingType.ContainingPEModule;
var metadataReader = moduleSymbol.Module.MetadataReader;
GenericParameterConstraintHandleCollection constraints;

if ((object)containingMethod != null)
try
{
tokenDecoder = new MetadataDecoder(moduleSymbol, containingMethod);
constraints = metadataReader.GetGenericParameter(_handle).GetConstraints();
}
else
catch (BadImageFormatException)
{
tokenDecoder = new MetadataDecoder(moduleSymbol, containingType);
constraints = default(GenericParameterConstraintHandleCollection);
Interlocked.CompareExchange(ref _lazyConstraintsUseSiteErrorInfo, new CSDiagnosticInfo(ErrorCode.ERR_BindToBogus, this), CSDiagnosticInfo.EmptyErrorInfo);
}

foreach (var constraintHandle in constraints)
this.GetAttributes();
if (_lazyHasIsUnmanagedAttribute.Value() && (_lazyFlags & (GenericParameterAttributes.ReferenceTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint)) != 0)
{
var constraint = metadataReader.GetGenericParameterConstraint(constraintHandle);
var typeSymbol = tokenDecoder.DecodeGenericParameterConstraint(constraint.Type, out bool hasUnmanagedModreq);
_lazyFlags = _lazyFlags & ~(GenericParameterAttributes.ReferenceTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint);
_lazyConstraintsUseSiteErrorInfo = new CSDiagnosticInfo(ErrorCode.ERR_BindToBogus, this);
}

if (hasUnmanagedModreq != this._hasIsUnmanagedAttribute)
if (constraints.Count > 0)
{
var symbolsBuilder = ArrayBuilder<TypeSymbol>.GetInstance();
MetadataDecoder tokenDecoder;

if ((object)containingMethod != null)
{
// The presence of UnmanagedType modreq has to match the presence of the IsUnmanagedAttribute
Interlocked.CompareExchange(ref _lazyConstraintsUseSiteErrorInfo, new CSDiagnosticInfo(ErrorCode.ERR_BindToBogus, this), CSDiagnosticInfo.EmptyErrorInfo);
continue;
tokenDecoder = new MetadataDecoder(moduleSymbol, containingMethod);
}

// Drop 'System.Object' constraint type.
if (typeSymbol.SpecialType == SpecialType.System_Object)
else
{
continue;
tokenDecoder = new MetadataDecoder(moduleSymbol, containingType);
}

// Drop 'System.ValueType' constraint type if the 'valuetype' constraint was also specified.
if (((_flags & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0) &&
(typeSymbol.SpecialType == SpecialType.System_ValueType))
foreach (var constraintHandle in constraints)
{
continue;
}
var constraint = metadataReader.GetGenericParameterConstraint(constraintHandle);
var typeSymbol = tokenDecoder.DecodeGenericParameterConstraint(constraint.Type, out bool hasUnmanagedModreq);

if (hasUnmanagedModreq != this._lazyHasIsUnmanagedAttribute.Value())
{
// The presence of UnmanagedType modreq has to match the presence of the IsUnmanagedAttribute
Interlocked.CompareExchange(ref _lazyConstraintsUseSiteErrorInfo, new CSDiagnosticInfo(ErrorCode.ERR_BindToBogus, this), CSDiagnosticInfo.EmptyErrorInfo);
continue;
}

// Drop 'System.Object' constraint type.
if (typeSymbol.SpecialType == SpecialType.System_Object)
{
continue;
}

// Drop 'System.ValueType' constraint type if the 'valuetype' constraint was also specified.
if (((_lazyFlags & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0) &&
(typeSymbol.SpecialType == SpecialType.System_ValueType))
{
continue;
}

typeSymbol = TupleTypeDecoder.DecodeTupleTypesIfApplicable(typeSymbol, constraintHandle, moduleSymbol);

typeSymbol = TupleTypeDecoder.DecodeTupleTypesIfApplicable(typeSymbol, constraintHandle, moduleSymbol);
symbolsBuilder.Add(typeSymbol);
}

symbolsBuilder.Add(typeSymbol);
declaredConstraintTypes = symbolsBuilder.ToImmutableAndFree();
}
else
{
declaredConstraintTypes = ImmutableArray<TypeSymbol>.Empty;
}

return symbolsBuilder.ToImmutableAndFree();
}
else
{
return ImmutableArray<TypeSymbol>.Empty;
ImmutableInterlocked.InterlockedInitialize(ref _lazyDeclaredConstraintTypes, declaredConstraintTypes);
}

return _lazyDeclaredConstraintTypes;
}

public override ImmutableArray<Location> Locations
Expand All @@ -246,39 +254,44 @@ public override bool HasConstructorConstraint
{
get
{
return (_flags & GenericParameterAttributes.DefaultConstructorConstraint) != 0;
GetDeclaredConstraintTypes();
return (_lazyFlags & GenericParameterAttributes.DefaultConstructorConstraint) != 0;
}
}

public override bool HasReferenceTypeConstraint
{
get
{
return (_flags & GenericParameterAttributes.ReferenceTypeConstraint) != 0;
GetDeclaredConstraintTypes();
return (_lazyFlags & GenericParameterAttributes.ReferenceTypeConstraint) != 0;
}
}

public override bool HasValueTypeConstraint
{
get
{
return (_flags & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0 || HasUnmanagedTypeConstraint;
GetDeclaredConstraintTypes();
return (_lazyFlags & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0 || HasUnmanagedTypeConstraint;
}
}

public override bool HasUnmanagedTypeConstraint
{
get
{
return this._hasIsUnmanagedAttribute;
GetDeclaredConstraintTypes();
return this._lazyHasIsUnmanagedAttribute.Value();
}
}

public override VarianceKind Variance
{
get
{
return (VarianceKind)(_flags & GenericParameterAttributes.VarianceMask);
GetDeclaredConstraintTypes();
return (VarianceKind)(_lazyFlags & GenericParameterAttributes.VarianceMask);
}
}

Expand Down Expand Up @@ -321,9 +334,18 @@ public override ImmutableArray<CSharpAttributeData> GetAttributes()
{
if (_lazyCustomAttributes.IsDefault)
{
var containingPEModuleSymbol = (PEModuleSymbol)this.ContainingModule;
containingPEModuleSymbol.LoadCustomAttributes(this.Handle, ref _lazyCustomAttributes);
var loadedCustomAttributes = ((PEModuleSymbol)this.ContainingModule).GetCustomAttributesForToken(
this.Handle,
out CustomAttributeHandle isUnmanagedAttribute,
AttributeDescription.IsUnmanagedAttribute,
out _,
default);

this._lazyHasIsUnmanagedAttribute = (!isUnmanagedAttribute.IsNil).ToThreeState();

ImmutableInterlocked.InterlockedInitialize(ref _lazyCustomAttributes, loadedCustomAttributes);
}

return _lazyCustomAttributes;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -8631,8 +8631,8 @@ Pokud chcete odstranit toto varování, můžete místo toho použít /reference
<note />
</trans-unit>
<trans-unit id="ERR_UnmanagedConstraintWithLocalFunctions">
<source>Using unmanaged constraint on local functions type parameters is not supported.</source>
<target state="new">Using unmanaged constraint on local functions type parameters is not supported.</target>
<source>Using 'unmanaged' constraint on local functions type parameters is not supported.</source>
<target state="new">Using 'unmanaged' constraint on local functions type parameters is not supported.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConWithUnmanagedCon">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -8631,8 +8631,8 @@ Um die Warnung zu beheben, können Sie stattdessen /reference verwenden (Einbett
<note />
</trans-unit>
<trans-unit id="ERR_UnmanagedConstraintWithLocalFunctions">
<source>Using unmanaged constraint on local functions type parameters is not supported.</source>
<target state="new">Using unmanaged constraint on local functions type parameters is not supported.</target>
<source>Using 'unmanaged' constraint on local functions type parameters is not supported.</source>
<target state="new">Using 'unmanaged' constraint on local functions type parameters is not supported.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConWithUnmanagedCon">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -8631,8 +8631,8 @@ Para eliminar la advertencia puede usar /reference (establezca la propiedad Embe
<note />
</trans-unit>
<trans-unit id="ERR_UnmanagedConstraintWithLocalFunctions">
<source>Using unmanaged constraint on local functions type parameters is not supported.</source>
<target state="new">Using unmanaged constraint on local functions type parameters is not supported.</target>
<source>Using 'unmanaged' constraint on local functions type parameters is not supported.</source>
<target state="new">Using 'unmanaged' constraint on local functions type parameters is not supported.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConWithUnmanagedCon">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -8631,8 +8631,8 @@ Pour supprimer l'avertissement, vous pouvez utiliser la commande /reference (dé
<note />
</trans-unit>
<trans-unit id="ERR_UnmanagedConstraintWithLocalFunctions">
<source>Using unmanaged constraint on local functions type parameters is not supported.</source>
<target state="new">Using unmanaged constraint on local functions type parameters is not supported.</target>
<source>Using 'unmanaged' constraint on local functions type parameters is not supported.</source>
<target state="new">Using 'unmanaged' constraint on local functions type parameters is not supported.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConWithUnmanagedCon">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -8631,8 +8631,8 @@ Per rimuovere l'avviso, è invece possibile usare /reference (impostare la propr
<note />
</trans-unit>
<trans-unit id="ERR_UnmanagedConstraintWithLocalFunctions">
<source>Using unmanaged constraint on local functions type parameters is not supported.</source>
<target state="new">Using unmanaged constraint on local functions type parameters is not supported.</target>
<source>Using 'unmanaged' constraint on local functions type parameters is not supported.</source>
<target state="new">Using 'unmanaged' constraint on local functions type parameters is not supported.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConWithUnmanagedCon">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -8631,8 +8631,8 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<note />
</trans-unit>
<trans-unit id="ERR_UnmanagedConstraintWithLocalFunctions">
<source>Using unmanaged constraint on local functions type parameters is not supported.</source>
<target state="new">Using unmanaged constraint on local functions type parameters is not supported.</target>
<source>Using 'unmanaged' constraint on local functions type parameters is not supported.</source>
<target state="new">Using 'unmanaged' constraint on local functions type parameters is not supported.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConWithUnmanagedCon">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -8631,8 +8631,8 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<note />
</trans-unit>
<trans-unit id="ERR_UnmanagedConstraintWithLocalFunctions">
<source>Using unmanaged constraint on local functions type parameters is not supported.</source>
<target state="new">Using unmanaged constraint on local functions type parameters is not supported.</target>
<source>Using 'unmanaged' constraint on local functions type parameters is not supported.</source>
<target state="new">Using 'unmanaged' constraint on local functions type parameters is not supported.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConWithUnmanagedCon">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -8631,8 +8631,8 @@ Aby usunąć ostrzeżenie, możesz zamiast tego użyć opcji /reference (ustaw w
<note />
</trans-unit>
<trans-unit id="ERR_UnmanagedConstraintWithLocalFunctions">
<source>Using unmanaged constraint on local functions type parameters is not supported.</source>
<target state="new">Using unmanaged constraint on local functions type parameters is not supported.</target>
<source>Using 'unmanaged' constraint on local functions type parameters is not supported.</source>
<target state="new">Using 'unmanaged' constraint on local functions type parameters is not supported.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConWithUnmanagedCon">
Expand Down
Loading

0 comments on commit 97d0909

Please sign in to comment.