Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,43 @@ public static ITypeReference GetEnumType(this ITypeDefinition type)
throw new InvalidOperationException("All enums should have a value__ field!");
}

public static bool IsOrContainsReferenceType(this ITypeReference type)
{
Queue<ITypeReference> typesToCheck = new Queue<ITypeReference>();
HashSet<ITypeReference> visited = new HashSet<ITypeReference>();

typesToCheck.Enqueue(type);

while (typesToCheck.Count != 0)
{
var typeToCheck = typesToCheck.Dequeue();
visited.Add(typeToCheck);

var resolvedType = typeToCheck.ResolvedType;

// If it is dummy we cannot really check so assume it does because that is will be the most conservative
if (resolvedType is Dummy)
return true;

if (resolvedType.IsReferenceType)
return true;

// ByReference<T> is a special type understood by runtime to hold a ref T.
if (resolvedType.AreEquivalent("System.ByReference<T>"))
return true;

foreach (var field in resolvedType.Fields.Where(f => !f.IsStatic))
{
if (!visited.Contains(field.Type))
{
typesToCheck.Enqueue(field.Type);
}
}
}

return false;
}

public static bool IsConversionOperator(this IMethodDefinition method)
{
return (method.IsSpecialName &&
Expand Down Expand Up @@ -420,7 +457,7 @@ public static IMethodDefinition GetHiddenBaseMethod(this IMethodDefinition metho
if (IsAssembly(baseMethod) && !InSameUnit(baseMethod, method))
continue;

if (filter != null && !filter.Include(baseMethod))
if (filter != null && !filter.Include(baseMethod.UnWrapMember()))
continue;

// NOTE: Do not check method.IsHiddenBySignature here. C# is *always* hide-by-signature regardless of the metadata flag.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public virtual string GetTypeKey(ITypeDefinition type)

public virtual void Visit(ITypeDefinition type)
{
Visit(type.Fields);
Visit(type, type.Fields);
Visit(type.Methods.Where(m => m.IsConstructor));
Visit(type.Properties);
Visit(type.Events);
Expand All @@ -86,6 +86,11 @@ public virtual void Visit(IEnumerable<ITypeDefinitionMember> members)
Visit(member);
}

public virtual void Visit(ITypeDefinition parentType, IEnumerable<IFieldDefinition> fields)
{
this.Visit((IEnumerable<ITypeDefinitionMember>)fields);
}

public virtual string GetMemberKey(ITypeDefinitionMember member)
{
return member.UniqueId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Generic;
using Microsoft.Cci.Extensions.CSharp;

namespace Microsoft.Cci.Writers.CSharp
Expand All @@ -14,7 +15,7 @@ private void WriteFieldDefinition(IFieldDefinition field)
return;

WriteAttributes(field.Attributes);
if (!field.IsStatic && field.ContainingTypeDefinition.Layout == LayoutKind.Explicit)
if (!field.IsStatic && field.ContainingTypeDefinition.Layout == LayoutKind.Explicit && !(field is DummyPrivateField))
{
WriteFakeAttribute("System.Runtime.InteropServices.FieldOffsetAttribute", field.Offset.ToString());
}
Expand Down Expand Up @@ -51,7 +52,13 @@ private void WriteFieldDefinition(IFieldDefinition field)
WriteKeyword("new");

WriteTypeName(field.Type);
WriteIdentifier(field.Name);

string name = field.Name.Value;
if (name.Contains("<") || name.Contains(">"))
{
name = name.Replace("<", "_").Replace(">", "_");
}
WriteIdentifier(name, true);

if (field.Constant != null && field.IsCompileTimeConstant)
{
Expand Down Expand Up @@ -102,4 +109,88 @@ private void WriteFieldDefinitionValue(IFieldDefinition field) {
WriteMetadataConstant(field.Constant);
}
}

public class DummyPrivateField : IFieldDefinition
{
private ITypeDefinition _parentType;
private ITypeReference _type;
private IName _name;

public DummyPrivateField(ITypeDefinition parentType, ITypeReference type)
{
_parentType = parentType;
_type = type;
_name = new NameTable().GetNameFor("_dummy");
}

public uint BitLength => 0;

public IMetadataConstant CompileTimeValue => null;

public ISectionBlock FieldMapping => null;

public bool IsBitField => false;

public bool IsCompileTimeConstant => false;

public bool IsMapped => throw new System.NotImplementedException();

public bool IsMarshalledExplicitly => throw new System.NotImplementedException();

public bool IsNotSerialized => false;

public bool IsReadOnly => _parentType.Attributes.HasIsReadOnlyAttribute();

public bool IsRuntimeSpecial => false;

public bool IsSpecialName => false;

public IMarshallingInformation MarshallingInformation => throw new System.NotImplementedException();

public uint Offset => 0;

public int SequenceNumber => throw new System.NotImplementedException();

public ITypeDefinition ContainingTypeDefinition => _parentType;

public TypeMemberVisibility Visibility => TypeMemberVisibility.Private;

public ITypeDefinition Container => throw new System.NotImplementedException();

public IName Name => _name;

public IScope<ITypeDefinitionMember> ContainingScope => throw new System.NotImplementedException();

public IEnumerable<ICustomModifier> CustomModifiers => System.Linq.Enumerable.Empty<ICustomModifier>();

public uint InternedKey => throw new System.NotImplementedException();

public bool IsModified => throw new System.NotImplementedException();

public bool IsStatic => false;

public ITypeReference Type => _type;

public IFieldDefinition ResolvedField => throw new System.NotImplementedException();

public ITypeReference ContainingType => _parentType;

public ITypeDefinitionMember ResolvedTypeDefinitionMember => throw new System.NotImplementedException();

public IEnumerable<ICustomAttribute> Attributes => System.Linq.Enumerable.Empty<ICustomAttribute>();

public IEnumerable<ILocation> Locations => throw new System.NotImplementedException();

public IMetadataConstant Constant => null;

public void Dispatch(IMetadataVisitor visitor)
{
throw new System.NotImplementedException();
}

public void DispatchAsReference(IMetadataVisitor visitor)
{
throw new System.NotImplementedException();
}
}
}
78 changes: 76 additions & 2 deletions src/Microsoft.Cci.Extensions/Writers/CSharp/CSharpWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ public class CSharpWriter : SimpleTypeMemberTraverser, ICciWriter
private readonly IStyleSyntaxWriter _styleWriter;
private readonly CSDeclarationWriter _declarationWriter;
private readonly bool _writeAssemblyAttributes;
private readonly bool _apiOnly;
private readonly ICciFilter _cciFilter;

private bool _firstMemberGroup;

public CSharpWriter(ISyntaxWriter writer, ICciFilter filter, bool apiOnly, bool writeAssemblyAttributes = false)
: base(filter)
{
_syntaxWriter = writer;
_styleWriter = writer as IStyleSyntaxWriter;
_apiOnly = apiOnly;
_cciFilter = filter;
_declarationWriter = new CSDeclarationWriter(_syntaxWriter, filter, !apiOnly);
_writeAssemblyAttributes = writeAssemblyAttributes;
}
Expand Down Expand Up @@ -59,8 +64,8 @@ public string PlatformNotSupportedExceptionMessage

public void WriteAssemblies(IEnumerable<IAssembly> assemblies)
{
foreach (var assembly in assemblies)
Visit(assembly);
foreach (var assembly in assemblies)
Visit(assembly);
}

public override void Visit(IAssembly assembly)
Expand Down Expand Up @@ -115,6 +120,7 @@ public override void Visit(ITypeDefinition type)
_declarationWriter.WriteDeclaration(CSDeclarationWriter.GetDummyConstructor(type));
_syntaxWriter.WriteLine();
}

_firstMemberGroup = true;
base.Visit(type);
}
Expand All @@ -128,6 +134,74 @@ public override void Visit(IEnumerable<ITypeDefinitionMember> members)
base.Visit(members);
}

public override void Visit(ITypeDefinition parentType, IEnumerable<IFieldDefinition> fields)
{
if (parentType.IsStruct && !_apiOnly)
{
// For compile-time compat, the following rules should work for producing a reference assembly. We drop all private fields, but add back certain synthesized private fields for a value type (struct) as follows:
// - If there are any private fields that are or contain any value type members, add a single private field of type int
// - If there are any private fields that are or contain any reference type members, add a single private field of type object.
// - If the type is generic, then for every type parameter of the type, if there are any private fields that are or contain any members whose type is that type parameter, we add a direct private field of that type.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

// For more details see issue https://github.com/dotnet/corefx/issues/6185
// this blog is helpful as well http://blog.paranoidcoding.com/2016/02/15/are-private-members-api-surface.html

List<IFieldDefinition> newFields = new List<IFieldDefinition>();
var includedVisibleFields = fields.Where(f => f.IsVisibleOutsideAssembly()).Where(_cciFilter.Include);
includedVisibleFields = includedVisibleFields.OrderBy(GetMemberKey, StringComparer.OrdinalIgnoreCase);

var excludedFields = fields.Except(includedVisibleFields).Where(f => !f.IsStatic);

if (excludedFields.Any())
{
var genericTypedFields = excludedFields.Where(f => f.Type.UnWrap().IsGenericParameter());

// Compiler needs to see any fields, even private, that have generic arugments to be able
// to validate there aren't any struct layout cycles
foreach (var genericField in genericTypedFields)
newFields.Add(genericField);

if (!genericTypedFields.Any())
{
// For definiteassignment checks the compiler needs to know there is a private field
// that has not be initialized so if there are any we need to add a dummy private
// field to help the compiler do its job and error about uninitialized structs
bool hasRefPrivateField = excludedFields.Any(f => f.Type.IsOrContainsReferenceType());
ITypeReference fieldType = parentType.PlatformType.SystemInt32;

// If at least one of the private fields contains a reference type then we need to
// set this field type to object or reference field to inform the compiler to block
// taking pointers to this struct because the GC will not track updating those references
if (hasRefPrivateField)
fieldType = parentType.PlatformType.SystemObject;

// For primitive types that have a field of their type set the dummy field to that type
if (excludedFields.Count() == 1)
{
var onlyField = excludedFields.First();

if (TypeHelper.TypesAreEquivalent(onlyField.Type, parentType))
{
fieldType = parentType;
}
}

newFields.Add(new DummyPrivateField(parentType, fieldType));
}
}

foreach (var visibleField in includedVisibleFields)
newFields.Add(visibleField);

foreach (var field in newFields)
Visit(field);
}
else
{
base.Visit(parentType, fields);
}
}

public override void Visit(ITypeDefinitionMember member)
{
IDisposable style = null;
Expand Down