Skip to content
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 @@ -872,7 +872,7 @@ private void VisitIndirectFieldDecl(IndirectFieldDecl indirectFieldDecl)
return;
}

if (IsPrevContextDecl<RecordDecl>(out var prevContext, out _) && prevContext.IsAnonymous)
if (IsPrevContextDecl<RecordDecl>(out var prevContext, out _) && prevContext.IsAnonymousStructOrUnion)
{
// We shouldn't process indirect fields where the prev context is an anonymous record decl
return;
Expand All @@ -887,7 +887,7 @@ private void VisitIndirectFieldDecl(IndirectFieldDecl indirectFieldDecl)
var contextNameParts = new Stack<string>();
var contextTypeParts = new Stack<string>();

while (rootRecordDecl.IsAnonymous && (rootRecordDecl.Parent is RecordDecl parentRecordDecl))
while (rootRecordDecl.IsAnonymousStructOrUnion && (rootRecordDecl.Parent is RecordDecl parentRecordDecl))
{
// The name of a field of an anonymous type should be same as the type's name minus the
// type kind tag at the end and the leading `_`.
Expand Down Expand Up @@ -1390,7 +1390,7 @@ private void VisitRecordDecl(RecordDecl recordDecl)
var maxAlignm = recordDecl.Fields.Any() ? recordDecl.Fields.Max((fieldDecl) => Math.Max(fieldDecl.Type.Handle.AlignOf, 1)) : alignment;

var isTopLevelStruct = _config.WithTypes.TryGetValue(name, out var withType) && withType.Equals("struct", StringComparison.Ordinal);
var generateTestsClass = !recordDecl.IsAnonymous && recordDecl.DeclContext is not RecordDecl;
var generateTestsClass = !recordDecl.IsAnonymousStructOrUnion && recordDecl.DeclContext is not RecordDecl;
var testOutputStarted = false;

var nullableUuid = (Guid?)null;
Expand Down
93 changes: 68 additions & 25 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3210,7 +3210,7 @@ private string GetRemappedCursorName(NamedDecl namedDecl, out string nativeTypeN
}
else if ((namedDecl is FieldDecl fieldDecl) && name.StartsWith("__AnonymousFieldDecl_", StringComparison.Ordinal))
{
if (fieldDecl.Type.AsCXXRecordDecl?.IsAnonymous == true)
if (fieldDecl.Type.AsCXXRecordDecl?.IsAnonymousStructOrUnion == true)
{
// For fields of anonymous types, use the name of the type but clean off the type
// kind tag at the end.
Expand Down Expand Up @@ -3242,12 +3242,72 @@ private string GetRemappedCursorName(NamedDecl namedDecl, out string nativeTypeN
return remappedName;
}

private static int GetAnonymousRecordIndex(RecordDecl recordDecl, RecordDecl parentRecordDecl)
{
var index = -1;
var parentAnonRecordCount = parentRecordDecl.AnonymousRecords.Count;

if (parentAnonRecordCount != 0)
{
index = parentRecordDecl.AnonymousRecords.IndexOf(recordDecl);

if (index != -1)
{
if (parentAnonRecordCount > 1)
{
index++;
}

if (parentRecordDecl.Parent is RecordDecl grandParentRecordDecl)
{
var parentIndex = GetAnonymousRecordIndex(parentRecordDecl, grandParentRecordDecl);

// We can't have the nested anonymous record have the same name as the parent
// so skip that index and just go one higher instead. This could still conflict
// with another anonymous record at a different level, but that is less likely
// and will still be unambiguous in total.

if (parentIndex == index)
{
if (recordDecl.IsUnion == parentRecordDecl.IsUnion)
{
index++;
}
}
else if ((parentIndex != 0) && (index > parentIndex))
{
if (recordDecl.IsUnion == parentRecordDecl.AnonymousRecords[parentIndex].IsUnion)
{
index++;
}
}
}
}
}

return index;
}

private string GetRemappedNameForAnonymousRecord(RecordDecl recordDecl)
{
if (recordDecl.Parent is RecordDecl parentRecordDecl)
{
var remappedNameBuilder = new StringBuilder();
var matchingField = parentRecordDecl.Fields.Where((fieldDecl) => fieldDecl.Type.CanonicalType == recordDecl.TypeForDecl.CanonicalType).FirstOrDefault();
var matchingField = null as FieldDecl;

if (!recordDecl.IsAnonymousStructOrUnion)
{
matchingField = parentRecordDecl.Fields.Where((fieldDecl) => {
var fieldType = fieldDecl.Type.CanonicalType;

if (fieldType is ArrayType arrayType)
{
fieldType = arrayType.ElementType.CanonicalType;
}

return fieldType == recordDecl.TypeForDecl.CanonicalType;
}).FirstOrDefault();
}

if ((matchingField is not null) && !matchingField.IsAnonymousField)
{
Expand All @@ -3258,28 +3318,11 @@ private string GetRemappedNameForAnonymousRecord(RecordDecl recordDecl)
{
_ = remappedNameBuilder.Append("_Anonymous");

// If there is more than one anonymous type, then add a numeral to differentiate.
if (parentRecordDecl.AnonymousRecords.Count > 1)
{
var index = parentRecordDecl.AnonymousRecords.IndexOf(recordDecl) + 1;
Debug.Assert(index > 0);
_ = remappedNameBuilder.Append(index);
}
var index = GetAnonymousRecordIndex(recordDecl, parentRecordDecl);

// C# doesn't allow a nested type to have the same name as the parent, so if the
// parent is also anonymous, add the nesting depth as a way to avoid conflicts with
// the parent's name.
if (parentRecordDecl.IsAnonymous)
if (index != 0)
{
var depth = 1;
var currentParent = parentRecordDecl.Parent;
while ((currentParent is RecordDecl currentParentRecordDecl) && currentParentRecordDecl.IsAnonymous)
{
depth++;
currentParent = currentParentRecordDecl.Parent;
}
_ = remappedNameBuilder.Append('_');
_ = remappedNameBuilder.Append(depth);
_ = remappedNameBuilder.Append(index);
}
}

Expand Down Expand Up @@ -4625,7 +4668,7 @@ private bool HasBaseField(CXXRecordDecl cxxRecordDecl)

private bool HasField(RecordDecl recordDecl)
{
var hasField = recordDecl.Fields.Any() || recordDecl.Decls.Any((decl) => (decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymous && HasField(nestedRecordDecl));
var hasField = recordDecl.Fields.Any() || recordDecl.Decls.Any((decl) => (decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymousStructOrUnion && HasField(nestedRecordDecl));

if (!hasField && (recordDecl is CXXRecordDecl cxxRecordDecl))
{
Expand Down Expand Up @@ -5170,7 +5213,7 @@ bool IsEmptyRecord(RecordDecl recordDecl)

foreach (var decl in recordDecl.Decls)
{
if ((decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymous && !IsEmptyRecord(nestedRecordDecl))
if ((decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymousStructOrUnion && !IsEmptyRecord(nestedRecordDecl))
{
return false;
}
Expand Down Expand Up @@ -6195,7 +6238,7 @@ private bool IsUnsafe(RecordDecl recordDecl)
{
return true;
}
else if ((decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymous && (IsUnsafe(nestedRecordDecl) || Config.GenerateCompatibleCode))
else if ((decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymousStructOrUnion && (IsUnsafe(nestedRecordDecl) || Config.GenerateCompatibleCode))
{
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions sources/ClangSharp/Cursors/Decls/RecordDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ private protected RecordDecl(CXCursor handle, CXCursorKind expectedCursorKind, C

_fields = LazyList.Create<FieldDecl>(Handle.NumFields, (i) => TranslationUnit.GetOrCreate<FieldDecl>(Handle.GetField(unchecked((uint)i))));
_anonymousFields = new ValueLazy<List<FieldDecl>>(() => [.. Decls.OfType<FieldDecl>().Where(decl => decl.IsAnonymousField)]);
_anonymousRecords = new ValueLazy<List<RecordDecl>>(() => [.. Decls.OfType<RecordDecl>().Where(decl => decl.IsAnonymous && !decl.IsInjectedClassName)]);
_anonymousRecords = new ValueLazy<List<RecordDecl>>(() => [.. Decls.OfType<RecordDecl>().Where(decl => decl.IsAnonymousStructOrUnion && !decl.IsInjectedClassName)]);
_indirectFields = new ValueLazy<List<IndirectFieldDecl>>(() => [.. Decls.OfType<IndirectFieldDecl>()]);
_injectedClassName = new ValueLazy<RecordDecl?>(() => Decls.OfType<RecordDecl>().Where(decl => decl.IsInjectedClassName).SingleOrDefault());
}

public bool IsAnonymous => Handle.IsAnonymous;
public bool IsAnonymousStructOrUnion => Handle.IsAnonymousStructOrUnion;

public IReadOnlyList<FieldDecl> AnonymousFields => _anonymousFields.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"GenerateLocal": {
"commandName": "Project",
"commandLineArgs": "@generate.rsp",
"workingDirectory": "D:\\Users\\tagoo\\source\\repos\\terrafx.interop.windows\\generation\\Windows\\um\\sapi"
"workingDirectory": "D:\\repos\\terrafx.interop.windows\\generation\\DirectX\\um\\dcommon"
}
}
}
Loading
Loading