diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index 4a6af5fe..61300251 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -18,6 +18,7 @@ using static ClangSharp.Interop.CXUnaryOperatorKind; using static ClangSharp.Interop.CXEvalResultKind; using static ClangSharp.Interop.CXTypeKind; +using ClangSharp.Interop; namespace ClangSharp; @@ -822,13 +823,33 @@ void VisitCtorInitializers(CXXConstructorDecl cxxConstructorDecl, int firstCtorI memberInitName = GetRemappedCursorName(declRefExpr.Decl); } - _outputBuilder.BeginConstructorInitializer(memberRefName, memberInitName); + var skipInitializer = false; + var typeName = ""; + + if (memberInit is InitListExpr initListExpr) + { + typeName = GetRemappedTypeName(initListExpr, context: null, initListExpr.Type, out _); + } + + if (string.Equals(memberRefName, typeName, StringComparison.Ordinal)) + { + skipInitializer = true; + } + else + { + _outputBuilder.BeginConstructorInitializer(memberRefName, memberInitName); + } var memberRefTypeName = GetRemappedTypeName(memberRef, context: null, memberRef.Type, out var memberRefNativeTypeName); + _ = _context.AddLast((cxxConstructorDecl, skipInitializer)); UncheckStmt(memberRefTypeName, memberInit); + _context.RemoveLast(); - _outputBuilder.EndConstructorInitializer(); + if (!skipInitializer) + { + _outputBuilder.EndConstructorInitializer(); + } } } } @@ -1083,15 +1104,32 @@ private void VisitIndirectFieldDecl(IndirectFieldDecl indirectFieldDecl) if (isFixedSizedBuffer) { + var arraySize = IsType(indirectFieldDecl, type, out var constantArrayType) ? constantArrayType.Size : 0; + arraySize = Math.Max(arraySize, 1); + if (isSupportedFixedSizedBufferType) { code.Write("[0], "); - code.Write(Math.Max(IsType(indirectFieldDecl, type, out var constantArrayType) ? constantArrayType.Size : 0, 1)); + code.Write(arraySize); code.Write(')'); } - else if (!_config.GenerateLatestCode) + else if (!_config.GenerateLatestCode || arraySize == 1) { - code.Write(".AsSpan()"); + code.Write(".AsSpan("); + + if (arraySize == 1) + { + if (TryGetRemappedValue(indirectFieldDecl, _config.WithLengths, out var length)) + { + code.Write(length); + } + else + { + AddDiagnostic(DiagnosticLevel.Warning, $"Found variable length array: '{GetCursorQualifiedName(indirectFieldDecl)}'. Please specify the length using `--with-length `.", indirectFieldDecl); + } + } + + code.Write(')'); } } else @@ -1639,11 +1677,9 @@ private void VisitRecordDecl(RecordDecl recordDecl) var cxxBaseSpecifier = cxxRecordDecl.Bases[index]; var baseCxxRecordDecl = GetRecordDecl(cxxBaseSpecifier); - if (HasField(baseCxxRecordDecl)) + if (HasField(baseCxxRecordDecl) && !IsBaseExcluded(cxxRecordDecl, baseCxxRecordDecl, cxxBaseSpecifier, out var baseFieldName)) { var parent = GetRemappedCursorName(baseCxxRecordDecl); - var baseFieldName = GetAnonymousName(cxxBaseSpecifier, "Base"); - baseFieldName = GetRemappedName(baseFieldName, cxxBaseSpecifier, tryRemapOperatorName: true, out var wasRemapped, skipUsing: true); var fieldDesc = new FieldDesc { AccessSpecifier = GetAccessSpecifier(baseCxxRecordDecl, matchStar: true), @@ -1771,14 +1807,22 @@ private void VisitRecordDecl(RecordDecl recordDecl) { foreach (var cxxConstructorDecl in cxxRecordDecl.Ctors) { - Visit(cxxConstructorDecl); - _outputBuilder.WriteDivider(); + if (!IsExcluded(cxxConstructorDecl)) + { + Visit(cxxConstructorDecl); + _outputBuilder.WriteDivider(); + } } - if (cxxRecordDecl.HasUserDeclaredDestructor && !cxxRecordDecl.Destructor.IsVirtual) + if (cxxRecordDecl.HasUserDeclaredDestructor) { - Visit(cxxRecordDecl.Destructor); - _outputBuilder.WriteDivider(); + var cxxDestructorDecl = cxxRecordDecl.Destructor; + + if (!cxxDestructorDecl.IsVirtual && !IsExcluded(cxxDestructorDecl)) + { + Visit(cxxDestructorDecl); + _outputBuilder.WriteDivider(); + } } if (hasVtbl || hasBaseVtbl) @@ -3714,14 +3758,14 @@ private bool IsConstant(string targetTypeName, Expr initExpr) case CX_StmtClass_CStyleCastExpr: case CX_StmtClass_CXXStaticCastExpr: case CX_StmtClass_CXXFunctionalCastExpr: + case CX_StmtClass_CXXConstCastExpr: + case CX_StmtClass_CXXDynamicCastExpr: + case CX_StmtClass_CXXReinterpretCastExpr: { var cxxFunctionalCastExpr = (ExplicitCastExpr)initExpr; return IsConstant(targetTypeName, cxxFunctionalCastExpr.SubExprAsWritten); } - // case CX_StmtClass_CXXConstCastExpr: - // case CX_StmtClass_CXXDynamicCastExpr: - // case CX_StmtClass_CXXReinterpretCastExpr: // case CX_StmtClass_ObjCBridgedCastExpr: case CX_StmtClass_ImplicitCastExpr: diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs index 6fcf3d0e..b465fb3f 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs @@ -124,14 +124,39 @@ private void VisitCallExpr(CallExpr callExpr) } } + var functionName = ""; var isUnusedValue = false; + var parentName = ""; + var parentNamespace = ""; + + if (calleeDecl is FunctionDecl functionDecl) + { + functionName = functionDecl.Name; + + if (functionDecl.IsStatic && (functionDecl.Parent is CXXRecordDecl parent)) + { + parentName = GetRemappedCursorName(parent); + parentNamespace = GetNamespace(parentName, parent); + + if (IsPrevContextDecl(out var functionDeclContext, out _) && (functionDeclContext.Parent is CXXRecordDecl parentContext)) + { + var contextName = GetRemappedCursorName(parentContext); + + if (string.Equals(parentName, contextName, StringComparison.Ordinal)) + { + parentName = ""; + parentNamespace = ""; + } + } + } + } if (!IsTypeVoid(callExpr, callExpr.Type)) { isUnusedValue = IsPrevContextStmt(out _, out _) || IsPrevContextStmt(out _, out _); - if ((calleeDecl is FunctionDecl functionDecl) && (functionDecl.Name is "memcpy" or "memset")) + if (functionName is "memcpy" or "memset") { isUnusedValue = false; } @@ -142,6 +167,14 @@ private void VisitCallExpr(CallExpr callExpr) outputBuilder.Write("_ = "); } + if (!string.IsNullOrWhiteSpace(parentName)) + { + AddUsingDirective(outputBuilder, parentNamespace); + + outputBuilder.Write(parentName); + outputBuilder.Write('.'); + } + if (calleeDecl is null) { Visit(callExpr.Callee); @@ -152,14 +185,23 @@ private void VisitCallExpr(CallExpr callExpr) Visit(callExpr.Callee); VisitArgs(callExpr); } - else if (calleeDecl is FunctionDecl functionDecl) + else if (calleeDecl is FunctionDecl) { - switch (functionDecl.Name) + switch (functionName) { case "memcpy": { - outputBuilder.AddUsingDirective("System.Runtime.CompilerServices"); - outputBuilder.Write("Unsafe.CopyBlockUnaligned"); + if (Config.GenerateLatestCode) + { + outputBuilder.AddUsingDirective("System.Runtime.InteropServices"); + outputBuilder.Write("NativeMemory.Copy"); + } + else + { + outputBuilder.AddUsingDirective("System.Runtime.CompilerServices"); + outputBuilder.Write("Unsafe.CopyBlockUnaligned"); + } + VisitArgs(callExpr); break; } @@ -168,6 +210,8 @@ private void VisitCallExpr(CallExpr callExpr) { NamedDecl? namedDecl = null; + var args = callExpr.Args; + if (callExpr.NumArgs == 3) { if (IsStmtAsWritten(callExpr.Args[1], out var integerLiteralExpr, removeParens: true) && (integerLiteralExpr.Value == 0) && @@ -197,11 +241,25 @@ private void VisitCallExpr(CallExpr callExpr) outputBuilder.Write(" = default"); break; } + + if (Config.GenerateLatestCode) + { + args = [args[0], args[2], args[1]]; + } } - outputBuilder.AddUsingDirective("System.Runtime.CompilerServices"); - outputBuilder.Write("Unsafe.InitBlockUnaligned"); - VisitArgs(callExpr); + if (Config.GenerateLatestCode) + { + outputBuilder.AddUsingDirective("System.Runtime.InteropServices"); + outputBuilder.Write("NativeMemory.Fill"); + } + else + { + outputBuilder.AddUsingDirective("System.Runtime.CompilerServices"); + outputBuilder.Write("Unsafe.InitBlockUnaligned"); + } + + VisitArgs(callExpr, args); break; } @@ -243,7 +301,7 @@ private void VisitCallExpr(CallExpr callExpr) AddDiagnostic(DiagnosticLevel.Error, $"Unsupported callee declaration: '{calleeDecl.DeclKindName}'. Generated bindings may be incomplete.", calleeDecl); } - void VisitArgs(CallExpr callExpr) + void VisitArgs(CallExpr callExpr, IReadOnlyList? args = null) { var callExprType = (callExpr.Callee is MemberExpr memberExpr) ? memberExpr.MemberDecl.Type @@ -256,9 +314,13 @@ void VisitArgs(CallExpr callExpr) outputBuilder.Write('('); - var args = callExpr.Args; + args ??= callExpr.Args; var needsComma = false; + var paramCount = IsType(callExpr, callExprType, out var functionProtoType) + ? (int)functionProtoType.NumParams + : args.Count; + for (var i = 0; i < args.Count; i++) { var arg = args[i]; @@ -268,13 +330,28 @@ void VisitArgs(CallExpr callExpr) outputBuilder.Write(", "); } - if (IsType(callExpr, callExprType, out var functionProtoType)) + if ((functionProtoType is not null) && (i < paramCount)) { - if (IsType(callExpr, functionProtoType.ParamTypes[i])) + if (IsType(callExpr, functionProtoType.ParamTypes[i], out var referenceType)) { if (IsStmtAsWritten(arg, out var unaryOperator, removeParens: true) && (unaryOperator.Opcode == CXUnaryOperator_Deref)) { - arg = unaryOperator.SubExpr; + var subExpr = unaryOperator.SubExpr; + + if (subExpr is CXXThisExpr) + { + var referenceTypeName = GetTypeName(callExpr, context: null, referenceType, ignoreTransparentStructsWhereRequired: true, out _); + + outputBuilder.AddUsingDirective("System.Runtime.CompilerServices"); + outputBuilder.Write('('); + outputBuilder.Write(referenceTypeName); + outputBuilder.Write(")Unsafe.AsPointer(ref this)"); + + needsComma = true; + continue; + } + + arg = subExpr; } else if (IsStmtAsWritten(arg, out var declRefExpr, removeParens: true)) { @@ -288,6 +365,14 @@ void VisitArgs(CallExpr callExpr) outputBuilder.Write('&'); } } + else if (IsStmtAsWritten(arg, out _) && (functionName == "memcpy")) + { + outputBuilder.AddUsingDirective("System.Runtime.CompilerServices"); + outputBuilder.Write("Unsafe.AsPointer(ref this)"); + + needsComma = true; + continue; + } } Visit(arg); @@ -509,12 +594,24 @@ private void VisitCXXConstructExpr(CXXConstructExpr cxxConstructExpr) { outputBuilder.Write("ref "); } - Visit(args[0]); - for (var i = 1; i < args.Count; i++) + var needsComma = false; + + for (var i = 0; i < args.Count; i++) { - outputBuilder.Write(", "); - Visit(args[i]); + var arg = args[i]; + + if (needsComma && (arg is not CXXDefaultArgExpr)) + { + outputBuilder.Write(", "); + } + + Visit(arg); + + if (arg is not CXXDefaultArgExpr) + { + needsComma = true; + } } } @@ -624,12 +721,6 @@ private void VisitCXXOperatorCallExpr(CXXOperatorCallExpr cxxOperatorCallExpr) var functionDeclName = GetCursorName(functionDecl); var args = cxxOperatorCallExpr.Args; - if (functionDecl.DeclContext is CXXRecordDecl) - { - Visit(cxxOperatorCallExpr.Args[0]); - outputBuilder.Write('.'); - } - if (IsEnumOperator(functionDecl, functionDeclName)) { switch (functionDeclName) @@ -666,23 +757,75 @@ private void VisitCXXOperatorCallExpr(CXXOperatorCallExpr cxxOperatorCallExpr) } var name = GetRemappedCursorName(functionDecl); - outputBuilder.Write(name); + var firstIndex = 0; - outputBuilder.Write('('); - var firstIndex = (functionDecl.DeclContext is CXXRecordDecl) ? 1 : 0; + if (functionDecl.DeclContext is CXXRecordDecl) + { + Visit(cxxOperatorCallExpr.Args[0]); + firstIndex++; + } - if (args.Count > firstIndex) + switch (name) { - Visit(args[firstIndex]); + case "operator=": + { + if (!functionDecl.IsDefaulted) + { + goto default; + } - for (var i = firstIndex + 1; i < args.Count; i++) + var expr = args[firstIndex]; + + if (firstIndex != 0) + { + outputBuilder.Write(" = "); + + if (!IsTypePointerOrReference(args[firstIndex - 1])) + { + if (IsType(expr)) + { + outputBuilder.Write('*'); + } + else if (IsStmtAsWritten(expr, out var declRefExpr, removeParens: true)) + { + if (IsTypePointerOrReference(declRefExpr.Decl)) + { + outputBuilder.Write('*'); + } + } + } + } + + Visit(expr); + break; + } + + default: { - outputBuilder.Write(", "); - Visit(args[i]); + if (firstIndex != 0) + { + outputBuilder.Write('.'); + } + + outputBuilder.Write(name); + + outputBuilder.Write('('); + + if (args.Count > firstIndex) + { + Visit(args[firstIndex]); + + for (var i = firstIndex + 1; i < args.Count; i++) + { + outputBuilder.Write(", "); + Visit(args[i]); + } + } + + outputBuilder.Write(')'); + break; } } - - outputBuilder.Write(')'); } else { @@ -701,7 +844,8 @@ private static void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr cxxPseu private void VisitCXXThisExpr(CXXThisExpr cxxThisExpr) { - StartCSharpCode().Write("this"); + var outputBuilder = StartCSharpCode(); + outputBuilder.Write("this"); StopCSharpCode(); } @@ -1330,6 +1474,7 @@ void ForRecordType(CSharpOutputBuilder outputBuilder, InitListExpr initListExpr, var typeName = GetRemappedTypeName(initListExpr, context: null, type, out _); var isUnmanagedConstant = false; var escapedName = ""; + var skipInitializer = false; if (IsPrevContextDecl(out _, out var userData)) { @@ -1339,6 +1484,13 @@ void ForRecordType(CSharpOutputBuilder outputBuilder, InitListExpr initListExpr, isUnmanagedConstant = (valueDesc.Kind == ValueKind.Unmanaged) && valueDesc.IsConstant; } } + else if (IsPrevContextDecl(out _, out userData, includeLast: true)) + { + if (userData is bool boolValue) + { + skipInitializer = boolValue; + } + } if (_config.GenerateUnmanagedConstants && isUnmanagedConstant && (_testStmtOutputBuilder is null)) { @@ -1346,8 +1498,11 @@ void ForRecordType(CSharpOutputBuilder outputBuilder, InitListExpr initListExpr, } else { - outputBuilder.Write("new "); - outputBuilder.Write(typeName); + if (!skipInitializer) + { + outputBuilder.Write("new "); + outputBuilder.Write(typeName); + } if (typeName.Equals("Guid", StringComparison.Ordinal)) { @@ -1373,8 +1528,11 @@ void ForRecordType(CSharpOutputBuilder outputBuilder, InitListExpr initListExpr, } else { - outputBuilder.WriteNewline(); - outputBuilder.WriteBlockStart(); + if (!skipInitializer) + { + outputBuilder.WriteNewline(); + outputBuilder.WriteBlockStart(); + } var decl = recordType.Decl; @@ -1387,17 +1545,42 @@ void ForRecordType(CSharpOutputBuilder outputBuilder, InitListExpr initListExpr, continue; } - var fieldName = GetRemappedCursorName(decl.Fields[i]); + var fieldDecl = decl.Fields[i]; + var fieldName = GetRemappedCursorName(fieldDecl); outputBuilder.WriteIndented(fieldName); outputBuilder.Write(" = "); + + if (!IsTypePointerOrReference(fieldDecl) && (init is Expr initExpr)) + { + if ((initExpr is CXXConstructExpr cxxConstructExpr) && cxxConstructExpr.Constructor.IsDefaulted) + { + initExpr = cxxConstructExpr.Args[0]; + } + + if (IsType(initExpr)) + { + outputBuilder.Write('*'); + } + else if (IsStmtAsWritten(initExpr, out var declRefExpr, removeParens: true)) + { + if (IsTypePointerOrReference(declRefExpr.Decl)) + { + outputBuilder.Write('*'); + } + } + } + Visit(init); - outputBuilder.WriteLine(','); + outputBuilder.WriteLine(skipInitializer ? ';' : ','); } - outputBuilder.DecreaseIndentation(); - outputBuilder.WriteIndented('}'); - outputBuilder.NeedsSemicolon = true; + if (!skipInitializer) + { + outputBuilder.DecreaseIndentation(); + outputBuilder.WriteIndented('}'); + outputBuilder.NeedsSemicolon = true; + } } } } @@ -1719,10 +1902,18 @@ private void VisitIntegerLiteral(IntegerLiteral integerLiteral) { valueString = valueString[0..^3]; } + else if (valueString.EndsWith("ui32", StringComparison.OrdinalIgnoreCase)) + { + valueString = valueString[0..^4] + "U"; + } else if (valueString.EndsWith("i32", StringComparison.OrdinalIgnoreCase)) { valueString = valueString[0..^3]; } + else if (valueString.EndsWith("ui64", StringComparison.OrdinalIgnoreCase)) + { + valueString = valueString[0..^4] + "UL"; + } else if (valueString.EndsWith("i64", StringComparison.OrdinalIgnoreCase)) { valueString = valueString[0..^3] + "L"; @@ -1787,10 +1978,9 @@ private void VisitMemberExpr(MemberExpr memberExpr) { var cxxBaseSpecifier = _cxxRecordDeclContext.Bases.Where((baseSpecifier) => baseSpecifier.Referenced == parent).SingleOrDefault(); - if (cxxBaseSpecifier is not null) + if ((cxxBaseSpecifier is not null) && IsBaseExcluded(_cxxRecordDeclContext, GetRecordDecl(cxxBaseSpecifier), cxxBaseSpecifier, out baseFieldName)) { - baseFieldName = GetAnonymousName(cxxBaseSpecifier, "Base"); - baseFieldName = GetRemappedName(baseFieldName, cxxBaseSpecifier, tryRemapOperatorName: true, out var wasRemapped, skipUsing: true); + baseFieldName = ""; } } } @@ -1803,10 +1993,9 @@ private void VisitMemberExpr(MemberExpr memberExpr) { var cxxBaseSpecifier = _cxxRecordDeclContext.Bases.Where((baseSpecifier) => baseSpecifier.Referenced == parent).SingleOrDefault(); - if (cxxBaseSpecifier is not null) + if ((cxxBaseSpecifier is not null) && IsBaseExcluded(_cxxRecordDeclContext, GetRecordDecl(cxxBaseSpecifier), cxxBaseSpecifier, out baseFieldName)) { - baseFieldName = GetAnonymousName(cxxBaseSpecifier, "Base"); - baseFieldName = GetRemappedName(baseFieldName, cxxBaseSpecifier, tryRemapOperatorName: true, out var wasRemapped, skipUsing: true); + baseFieldName = ""; } } } @@ -1899,17 +2088,40 @@ private void VisitReturnStmt(ReturnStmt returnStmt) if (IsPrevContextDecl(out var functionDecl, out _) && !IsTypeVoid(functionDecl, functionDecl.ReturnType)) { outputBuilder.Write("return"); + var retValue = returnStmt.RetValue; - if (returnStmt.RetValue != null) + if (retValue != null) { outputBuilder.Write(' '); - if (!IsTypePointerOrReference(functionDecl) && IsType(returnStmt.RetValue)) + if (!IsTypePointerOrReference(functionDecl) && IsType(retValue)) { outputBuilder.Write('*'); } + else if (IsType(functionDecl, functionDecl.ReturnType, out var referenceType)) + { + if (IsStmtAsWritten(retValue, out var unaryOperator, removeParens: true) && (unaryOperator.Opcode == CXUnaryOperator_Deref)) + { + var subExpr = unaryOperator.SubExpr; + + if (subExpr is CXXThisExpr) + { + var referenceTypeName = GetTypeName(returnStmt, context: null, referenceType, ignoreTransparentStructsWhereRequired: true, out _); + + outputBuilder.AddUsingDirective("System.Runtime.CompilerServices"); + outputBuilder.Write('('); + outputBuilder.Write(referenceTypeName); + outputBuilder.Write(")Unsafe.AsPointer(ref this)"); + + StopCSharpCode(); + return; + } + + retValue = subExpr; + } + } - Visit(returnStmt.RetValue); + Visit(retValue); } } else if (returnStmt.RetValue != null) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index 78f87dee..5d905ae0 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -5028,6 +5028,17 @@ bool IsEmptyRecord(RecordDecl recordDecl) } } + private bool IsBaseExcluded(CXXRecordDecl cxxRecordDecl, CXXRecordDecl baseCxxRecordDecl, CXXBaseSpecifier cxxBaseSpecifier, out string baseFieldName) + { + baseFieldName = GetAnonymousName(cxxBaseSpecifier, "Base"); + baseFieldName = GetRemappedName(baseFieldName, cxxBaseSpecifier, tryRemapOperatorName: true, out _, skipUsing: true); + + var qualifiedName = $"{GetCursorQualifiedName(cxxRecordDecl)}::{baseFieldName}"; + var dottedQualifiedName = qualifiedName.Replace("::", ".", StringComparison.Ordinal); + + return _config.ExcludedNames.Contains(qualifiedName) || _config.ExcludedNames.Contains(dottedQualifiedName); + } + private bool IsFixedSize(Cursor cursor, Type type) { // We don't want to handle these using IsType because we need to specially @@ -5100,18 +5111,23 @@ private static bool IsNativeTypeNameEquivalent(string nativeTypeName, string typ || nativeTypeName.Replace(" ", "", StringComparison.Ordinal).Equals(typeName, StringComparison.OrdinalIgnoreCase); } - private bool IsPrevContextDecl([MaybeNullWhen(false)] out T cursor, out object? userData) + private bool IsPrevContextDecl([MaybeNullWhen(false)] out T cursor, out object? userData, bool includeLast = false) where T : Decl { var previousContext = _context.Last; Debug.Assert(previousContext != null); - do + if (!includeLast) + { + previousContext = previousContext.Previous; + Debug.Assert(previousContext != null); + } + + while (previousContext.Value.Cursor is not Decl) { previousContext = previousContext.Previous; Debug.Assert(previousContext != null); } - while (previousContext.Value.Cursor is not Decl); var value = previousContext.Value; @@ -5515,15 +5531,14 @@ private bool IsUnchecked(string targetTypeName, Stmt stmt) || (IsUnsigned(targetTypeName) != IsUnsigned(explicitCastExprTypeName)); } - // case CX_StmtClass_CXXConstCastExpr: - // case CX_StmtClass_CXXDynamicCastExpr: - + case CX_StmtClass_CXXConstCastExpr: + case CX_StmtClass_CXXDynamicCastExpr: case CX_StmtClass_CXXReinterpretCastExpr: { - var reinterpretCastExpr = (CXXReinterpretCastExpr)stmt; + var namedCastExpr = (CXXNamedCastExpr)stmt; - return IsUnchecked(targetTypeName, reinterpretCastExpr.SubExprAsWritten) - || IsUnchecked(targetTypeName, reinterpretCastExpr.Handle.Evaluate); + return IsUnchecked(targetTypeName, namedCastExpr.SubExprAsWritten) + || IsUnchecked(targetTypeName, namedCastExpr.Handle.Evaluate); } // case CX_StmtClass_ObjCBridgedCastExpr: diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs index 485b3d55..2ef121d1 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs @@ -42,6 +42,7 @@ public sealed class PInvokeGeneratorConfiguration private readonly SortedDictionary _withCallConvs; private readonly SortedDictionary _withClasses; private readonly SortedDictionary _withGuids; + private readonly SortedDictionary _withLengths; private readonly SortedDictionary _withLibraryPaths; private readonly SortedDictionary _withNamespaces; private readonly SortedDictionary _withTransparentStructs; @@ -95,6 +96,7 @@ public PInvokeGeneratorConfiguration(string language, string languageStandard, s _withCallConvs = []; _withClasses = []; _withGuids = []; + _withLengths = []; _withLibraryPaths = []; _withNamespaces = []; _withTransparentStructs = []; @@ -452,6 +454,20 @@ public IReadOnlyDictionary WithGuids } } + [AllowNull] + public IReadOnlyDictionary WithLengths + { + get + { + return _withLengths; + } + + init + { + AddRange(_withLengths, value); + } + } + [AllowNull] public IReadOnlyDictionary WithLibraryPaths { diff --git a/sources/ClangSharpPInvokeGenerator/Program.cs b/sources/ClangSharpPInvokeGenerator/Program.cs index 6b355de1..c5d522a9 100644 --- a/sources/ClangSharpPInvokeGenerator/Program.cs +++ b/sources/ClangSharpPInvokeGenerator/Program.cs @@ -49,6 +49,7 @@ public static class Program private static readonly string[] s_withCallConvOptionAliases = ["--with-callconv", "-wcc"]; private static readonly string[] s_withClassOptionAliases = ["--with-class", "-wc"]; private static readonly string[] s_withGuidOptionAliases = ["--with-guid", "-wg"]; + private static readonly string[] s_withLengthOptionAliases = ["--with-length", "-wl"]; private static readonly string[] s_withLibraryPathOptionAliases = ["--with-librarypath", "-wlb"]; private static readonly string[] s_withManualImportOptionAliases = ["--with-manual-import", "-wmi"]; private static readonly string[] s_withNamespaceOptionAliases = ["--with-namespace", "-wn"]; @@ -86,6 +87,7 @@ public static class Program private static readonly Option s_withCallConvNameValuePairs = GetWithCallConvOption(); private static readonly Option s_withClassNameValuePairs = GetWithClassOption(); private static readonly Option s_withGuidNameValuePairs = GetWithGuidOption(); + private static readonly Option s_withLengthNameValuePairs = GetWithLengthOption(); private static readonly Option s_withLibraryPathNameValuePairs = GetWithLibraryPathOption(); private static readonly Option s_withManualImports = GetWithManualImportOption(); private static readonly Option s_withNamespaceNameValuePairs = GetWithNamespaceOption(); @@ -242,6 +244,7 @@ public static void Run(InvocationContext context) var withCallConvNameValuePairs = context.ParseResult.GetValueForOption(s_withCallConvNameValuePairs) ?? []; var withClassNameValuePairs = context.ParseResult.GetValueForOption(s_withClassNameValuePairs) ?? []; var withGuidNameValuePairs = context.ParseResult.GetValueForOption(s_withGuidNameValuePairs) ?? []; + var withLengthNameValuePairs = context.ParseResult.GetValueForOption(s_withLengthNameValuePairs) ?? []; var withLibraryPathNameValuePairs = context.ParseResult.GetValueForOption(s_withLibraryPathNameValuePairs) ?? []; var withManualImports = context.ParseResult.GetValueForOption(s_withManualImports) ?? []; var withNamespaceNameValuePairs = context.ParseResult.GetValueForOption(s_withNamespaceNameValuePairs) ?? []; @@ -286,6 +289,7 @@ public static void Run(InvocationContext context) ParseKeyValuePairs(withCallConvNameValuePairs, errorList, out Dictionary withCallConvs); ParseKeyValuePairs(withClassNameValuePairs, errorList, out Dictionary withClasses); ParseKeyValuePairs(withGuidNameValuePairs, errorList, out Dictionary withGuids); + ParseKeyValuePairs(withLengthNameValuePairs, errorList, out Dictionary withLengths); ParseKeyValuePairs(withLibraryPathNameValuePairs, errorList, out Dictionary withLibraryPaths); ParseKeyValuePairs(withNamespaceNameValuePairs, errorList, out Dictionary withNamespaces); ParseKeyValuePairs(withTransparentStructNameValuePairs, errorList, out Dictionary withTransparentStructs); @@ -693,6 +697,7 @@ public static void Run(InvocationContext context) WithCallConvs = withCallConvs, WithClasses = withClasses, WithGuids = withGuids, + WithLengths = withLengths, WithLibraryPaths = withLibraryPaths, WithManualImports = withManualImports, WithNamespaces = withNamespaces, @@ -1164,6 +1169,7 @@ private static RootCommand GetRootCommand() s_withCallConvNameValuePairs, s_withClassNameValuePairs, s_withGuidNameValuePairs, + s_withLengthNameValuePairs, s_withLibraryPathNameValuePairs, s_withManualImports, s_withNamespaceNameValuePairs, @@ -1272,6 +1278,17 @@ private static Option GetWithGuidOption() }; } + private static Option GetWithLengthOption() + { + return new Option( + aliases: s_withLengthOptionAliases, + description: "A length to be used for the given declaration during binding generation. Supports wildcards.", + getDefaultValue: Array.Empty + ) { + AllowMultipleArgumentsPerToken = true + }; + } + private static Option GetWithLibraryPathOption() { return new Option( diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs index 4f390096..49c6c171 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs @@ -1948,7 +1948,7 @@ protected override Task WithPackingTestImpl() struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; @@ -1960,12 +1960,13 @@ namespace ClangSharp.Test [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { - [NativeTypeName(""size_t[1]"")] + [NativeTypeName(""size_t[2]"")] public _FixedBuffer_e__FixedBuffer FixedBuffer; public partial struct _FixedBuffer_e__FixedBuffer { public UIntPtr e0; + public UIntPtr e1; public unsafe ref UIntPtr this[int index] { diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs index f7c5ef99..d3eb663f 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs @@ -1954,7 +1954,7 @@ protected override Task WithPackingTestImpl() { const string InputContents = @"struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; @@ -1966,12 +1966,13 @@ namespace ClangSharp.Test [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { - [NativeTypeName(""size_t[1]"")] + [NativeTypeName(""size_t[2]"")] public _FixedBuffer_e__FixedBuffer FixedBuffer; public partial struct _FixedBuffer_e__FixedBuffer { public UIntPtr e0; + public UIntPtr e1; public unsafe ref UIntPtr this[int index] { diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/StructDeclarationTest.cs index 2763e702..8b519a65 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/StructDeclarationTest.cs @@ -1926,7 +1926,7 @@ protected override Task WithPackingTestImpl() struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; @@ -1938,22 +1938,23 @@ namespace ClangSharp.Test [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { - [NativeTypeName(""size_t[1]"")] + [NativeTypeName(""size_t[2]"")] public _FixedBuffer_e__FixedBuffer FixedBuffer; public partial struct _FixedBuffer_e__FixedBuffer { public nuint e0; + public nuint e1; public ref nuint this[int index] { get { - return ref Unsafe.Add(ref e0, index); + return ref AsSpan()[index]; } } - public Span AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length); + public Span AsSpan() => MemoryMarshal.CreateSpan(ref e0, 2); } } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultWindows/StructDeclarationTest.cs index a258fb2e..cff1dc8f 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultWindows/StructDeclarationTest.cs @@ -1932,7 +1932,7 @@ protected override Task WithPackingTestImpl() { const string InputContents = @"struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; @@ -1944,22 +1944,23 @@ namespace ClangSharp.Test [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { - [NativeTypeName(""size_t[1]"")] + [NativeTypeName(""size_t[2]"")] public _FixedBuffer_e__FixedBuffer FixedBuffer; public partial struct _FixedBuffer_e__FixedBuffer { public nuint e0; + public nuint e1; public ref nuint this[int index] { get { - return ref Unsafe.Add(ref e0, index); + return ref AsSpan()[index]; } } - public Span AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length); + public Span AsSpan() => MemoryMarshal.CreateSpan(ref e0, 2); } } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs index f3bf95e8..b0d04972 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs @@ -1898,12 +1898,11 @@ protected override Task WithPackingTestImpl() struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; - const string ExpectedOutputContents = @"using System; -using System.Diagnostics.CodeAnalysis; + const string ExpectedOutputContents = @"using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace ClangSharp.Test @@ -1911,24 +1910,13 @@ namespace ClangSharp.Test [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { - [NativeTypeName(""size_t[1]"")] + [NativeTypeName(""size_t[2]"")] public _FixedBuffer_e__FixedBuffer FixedBuffer; + [InlineArray(2)] public partial struct _FixedBuffer_e__FixedBuffer { public nuint e0; - - [UnscopedRef] - public ref nuint this[int index] - { - get - { - return ref Unsafe.Add(ref e0, index); - } - } - - [UnscopedRef] - public Span AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length); } } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs index ae2c19ba..b54c00f2 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs @@ -1904,12 +1904,11 @@ protected override Task WithPackingTestImpl() { const string InputContents = @"struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; - const string ExpectedOutputContents = @"using System; -using System.Diagnostics.CodeAnalysis; + const string ExpectedOutputContents = @"using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace ClangSharp.Test @@ -1917,24 +1916,13 @@ namespace ClangSharp.Test [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { - [NativeTypeName(""size_t[1]"")] + [NativeTypeName(""size_t[2]"")] public _FixedBuffer_e__FixedBuffer FixedBuffer; + [InlineArray(2)] public partial struct _FixedBuffer_e__FixedBuffer { public nuint e0; - - [UnscopedRef] - public ref nuint this[int index] - { - get - { - return ref Unsafe.Add(ref e0, index); - } - } - - [UnscopedRef] - public Span AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length); } } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs index 2224c37e..fb7cf492 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs @@ -1898,12 +1898,11 @@ protected override Task WithPackingTestImpl() struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; - const string ExpectedOutputContents = @"using System; -using System.Diagnostics.CodeAnalysis; + const string ExpectedOutputContents = @"using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace ClangSharp.Test @@ -1911,24 +1910,13 @@ namespace ClangSharp.Test [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { - [NativeTypeName(""size_t[1]"")] + [NativeTypeName(""size_t[2]"")] public _FixedBuffer_e__FixedBuffer FixedBuffer; + [InlineArray(2)] public partial struct _FixedBuffer_e__FixedBuffer { public nuint e0; - - [UnscopedRef] - public ref nuint this[int index] - { - get - { - return ref Unsafe.Add(ref e0, index); - } - } - - [UnscopedRef] - public Span AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length); } } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs index 199c74e7..8299bd09 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs @@ -1904,12 +1904,11 @@ protected override Task WithPackingTestImpl() { const string InputContents = @"struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; - const string ExpectedOutputContents = @"using System; -using System.Diagnostics.CodeAnalysis; + const string ExpectedOutputContents = @"using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace ClangSharp.Test @@ -1917,24 +1916,13 @@ namespace ClangSharp.Test [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { - [NativeTypeName(""size_t[1]"")] + [NativeTypeName(""size_t[2]"")] public _FixedBuffer_e__FixedBuffer FixedBuffer; + [InlineArray(2)] public partial struct _FixedBuffer_e__FixedBuffer { public nuint e0; - - [UnscopedRef] - public ref nuint this[int index] - { - get - { - return ref Unsafe.Add(ref e0, index); - } - } - - [UnscopedRef] - public Span AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length); } } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs index ef5026d4..17a8ed66 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs @@ -1911,7 +1911,7 @@ protected override Task WithPackingTestImpl() struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; @@ -1920,12 +1920,15 @@ struct MyStruct - UIntPtr + UIntPtr UIntPtr + + UIntPtr + ref UIntPtr diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs index 1425181e..263ba30c 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs @@ -1920,7 +1920,7 @@ protected override Task WithPackingTestImpl() { const string InputContents = @"struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; @@ -1929,12 +1929,15 @@ protected override Task WithPackingTestImpl() - UIntPtr + UIntPtr UIntPtr + + UIntPtr + ref UIntPtr diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultUnix/StructDeclarationTest.cs index 32a0fed2..a3cca916 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultUnix/StructDeclarationTest.cs @@ -1891,7 +1891,7 @@ protected override Task WithPackingTestImpl() struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; @@ -1900,27 +1900,27 @@ struct MyStruct - nuint + nuint nuint + + nuint + ref nuint int - return ref Unsafe.Add(ref e0, index); + return ref AsSpan()[index]; Span<nuint> - - int - - MemoryMarshal.CreateSpan(ref e0, length); + MemoryMarshal.CreateSpan(ref e0, 2); diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultWindows/StructDeclarationTest.cs index d3e1fb70..6873e848 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlDefaultWindows/StructDeclarationTest.cs @@ -1901,7 +1901,7 @@ protected override Task WithPackingTestImpl() { const string InputContents = @"struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; @@ -1910,27 +1910,27 @@ protected override Task WithPackingTestImpl() - nuint + nuint nuint + + nuint + ref nuint int - return ref Unsafe.Add(ref e0, index); + return ref AsSpan()[index]; Span<nuint> - - int - - MemoryMarshal.CreateSpan(ref e0, length); + MemoryMarshal.CreateSpan(ref e0, 2); diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs index de1caedf..c195d55d 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs @@ -1780,7 +1780,7 @@ protected override Task WithPackingTestImpl() struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; @@ -1789,28 +1789,13 @@ struct MyStruct - nuint + nuint + InlineArray(2) nuint - - ref nuint - - int - - - return ref Unsafe.Add(ref e0, index); - - - - Span<nuint> - - int - - MemoryMarshal.CreateSpan(ref e0, length); - diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs index 005aea7b..173ae57e 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs @@ -1790,7 +1790,7 @@ protected override Task WithPackingTestImpl() { const string InputContents = @"struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; @@ -1799,28 +1799,13 @@ protected override Task WithPackingTestImpl() - nuint + nuint + InlineArray(2) nuint - - ref nuint - - int - - - return ref Unsafe.Add(ref e0, index); - - - - Span<nuint> - - int - - MemoryMarshal.CreateSpan(ref e0, length); - diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs index 24e0ce5b..d11a6c97 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs @@ -1780,7 +1780,7 @@ protected override Task WithPackingTestImpl() struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; @@ -1789,28 +1789,13 @@ struct MyStruct - nuint + nuint + InlineArray(2) nuint - - ref nuint - - int - - - return ref Unsafe.Add(ref e0, index); - - - - Span<nuint> - - int - - MemoryMarshal.CreateSpan(ref e0, length); - diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs index aa53930b..75b64af4 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs @@ -1790,7 +1790,7 @@ protected override Task WithPackingTestImpl() { const string InputContents = @"struct MyStruct { - size_t FixedBuffer[1]; + size_t FixedBuffer[2]; }; "; @@ -1799,28 +1799,13 @@ protected override Task WithPackingTestImpl() - nuint + nuint + InlineArray(2) nuint - - ref nuint - - int - - - return ref Unsafe.Add(ref e0, index); - - - - Span<nuint> - - int - - MemoryMarshal.CreateSpan(ref e0, length); -