Skip to content

Commit c83b59a

Browse files
committed
Allow the command line to specify extra instance methods that should be readonly
1 parent a56a906 commit c83b59a

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
using System.Linq;
88
using System.Runtime.InteropServices;
99
using System.Text;
10-
using System.Text.RegularExpressions;
1110
using ClangSharp.Abstractions;
1211
using ClangSharp.CSharp;
1312
using static ClangSharp.Interop.CX_CastKind;
14-
using static ClangSharp.Interop.CX_CharacterKind;
1513
using static ClangSharp.Interop.CX_DeclKind;
1614
using static ClangSharp.Interop.CX_StmtClass;
1715
using static ClangSharp.Interop.CX_StorageClass;
@@ -607,7 +605,7 @@ private void VisitFunctionDecl(FunctionDecl functionDecl)
607605
IsCxx = cxxMethodDecl is not null,
608606
IsStatic = isDllImport || (cxxMethodDecl is null) || cxxMethodDecl.IsStatic,
609607
NeedsNewKeyword = NeedsNewKeyword(escapedName, functionDecl.Parameters),
610-
IsReadOnly = (cxxMethodDecl is not null) && cxxMethodDecl.IsConst,
608+
IsReadOnly = IsReadonly(cxxMethodDecl),
611609
IsUnsafe = IsUnsafe(functionDecl),
612610
IsCtxCxxRecord = cxxRecordDecl is not null,
613611
IsCxxRecordCtxUnsafe = cxxRecordDecl is not null && IsUnsafe(cxxRecordDecl),
@@ -2228,7 +2226,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
22282226
HasFnPtrCodeGen = !_config.ExcludeFnptrCodegen,
22292227
IsCtxCxxRecord = true,
22302228
IsCxxRecordCtxUnsafe = IsUnsafe(cxxRecordDecl),
2231-
IsReadOnly = cxxMethodDecl.IsConst,
2229+
IsReadOnly = IsReadonly(cxxMethodDecl),
22322230
IsUnsafe = true,
22332231
NeedsReturnFixup = needsReturnFixup,
22342232
ReturnType = returnTypeName,
@@ -2353,7 +2351,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
23532351
body.Write(escapedCXXRecordDeclName);
23542352
body.Write("*)Unsafe.AsPointer(");
23552353

2356-
if (cxxMethodDecl.IsConst)
2354+
if (IsReadonly(cxxMethodDecl))
23572355
{
23582356
if (!_config.GenerateLatestCode)
23592357
{

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5402,6 +5402,15 @@ private bool IsPrevContextStmt<T>([MaybeNullWhen(false)] out T cursor, out objec
54025402
}
54035403
}
54045404

5405+
private bool IsReadonly(CXXMethodDecl? cxxMethodDecl)
5406+
{
5407+
if (cxxMethodDecl is not null)
5408+
{
5409+
return cxxMethodDecl.IsConst || HasRemapping(cxxMethodDecl, _config._withReadonlys, matchStar: true);
5410+
}
5411+
return false;
5412+
}
5413+
54055414
private static bool IsStmtAsWritten<T>(Cursor cursor, [MaybeNullWhen(false)] out T value, bool removeParens = false)
54065415
where T : Stmt
54075416
{

sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public sealed class PInvokeGeneratorConfiguration
3333
private readonly HashSet<string> _nativeTypeNamesToStrip;
3434
private readonly HashSet<string> _withManualImports;
3535
private readonly HashSet<string> _traversalNames;
36+
internal readonly HashSet<string> _withReadonlys;
3637
internal readonly HashSet<string> _withSetLastErrors;
3738
internal readonly HashSet<string> _withSuppressGCTransitions;
3839

@@ -87,6 +88,7 @@ public PInvokeGeneratorConfiguration(string language, string languageStandard, s
8788
_nativeTypeNamesToStrip = new HashSet<string>(StringComparer.Ordinal);
8889
_withManualImports = new HashSet<string>(StringComparer.Ordinal);
8990
_traversalNames = new HashSet<string>(StringComparer.Ordinal);
91+
_withReadonlys = new HashSet<string>(QualifiedNameComparer.Default);
9092
_withSetLastErrors = new HashSet<string>(QualifiedNameComparer.Default);
9193
_withSuppressGCTransitions = new HashSet<string>(QualifiedNameComparer.Default);
9294

@@ -516,6 +518,20 @@ public IReadOnlyDictionary<string, string> WithNamespaces
516518
}
517519
}
518520

521+
[AllowNull]
522+
public IReadOnlyCollection<string> WithReadonlys
523+
{
524+
get
525+
{
526+
return _withReadonlys;
527+
}
528+
529+
init
530+
{
531+
AddRange(_withReadonlys, value);
532+
}
533+
}
534+
519535
[AllowNull]
520536
public IReadOnlyCollection<string> WithSetLastErrors
521537
{

sources/ClangSharpPInvokeGenerator/Program.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ internal static class Program
5454
private static readonly string[] s_withManualImportOptionAliases = ["--with-manual-import", "-wmi"];
5555
private static readonly string[] s_withNamespaceOptionAliases = ["--with-namespace", "-wn"];
5656
private static readonly string[] s_withPackingOptionAliases = ["--with-packing", "-wp"];
57+
private static readonly string[] s_withReadonlyOptionAliases = ["--with-readonly", "-wro"];
5758
private static readonly string[] s_withSetLastErrorOptionAliases = ["--with-setlasterror", "-wsle"];
5859
private static readonly string[] s_withSuppressGCTransitionOptionAliases = ["--with-suppressgctransition", "-wsgct"];
5960
private static readonly string[] s_withTransparentStructOptionAliases = ["--with-transparent-struct", "-wts"];
@@ -92,6 +93,7 @@ internal static class Program
9293
private static readonly Option<string[]> s_withManualImports = GetWithManualImportOption();
9394
private static readonly Option<string[]> s_withNamespaceNameValuePairs = GetWithNamespaceOption();
9495
private static readonly Option<string[]> s_withPackingNameValuePairs = GetWithPackingOption();
96+
private static readonly Option<string[]> s_withReadonlys = GetWithReadonlyOption();
9597
private static readonly Option<string[]> s_withSetLastErrors = GetWithSetLastErrorOption();
9698
private static readonly Option<string[]> s_withSuppressGCTransitions = GetWithSuppressGCTransitionOption();
9799
private static readonly Option<string[]> s_withTransparentStructNameValuePairs = GetWithTransparentStructOption();
@@ -255,6 +257,7 @@ public static void Run(InvocationContext context)
255257
var withLibraryPathNameValuePairs = context.ParseResult.GetValueForOption(s_withLibraryPathNameValuePairs) ?? [];
256258
var withManualImports = context.ParseResult.GetValueForOption(s_withManualImports) ?? [];
257259
var withNamespaceNameValuePairs = context.ParseResult.GetValueForOption(s_withNamespaceNameValuePairs) ?? [];
260+
var withReadonlys = context.ParseResult.GetValueForOption(s_withReadonlys) ?? [];
258261
var withSetLastErrors = context.ParseResult.GetValueForOption(s_withSetLastErrors) ?? [];
259262
var withSuppressGCTransitions = context.ParseResult.GetValueForOption(s_withSuppressGCTransitions) ?? [];
260263
var withTransparentStructNameValuePairs = context.ParseResult.GetValueForOption(s_withTransparentStructNameValuePairs) ?? [];
@@ -729,6 +732,7 @@ public static void Run(InvocationContext context)
729732
WithLibraryPaths = withLibraryPaths,
730733
WithManualImports = withManualImports,
731734
WithNamespaces = withNamespaces,
735+
WithReadonlys = withReadonlys,
732736
WithSetLastErrors = withSetLastErrors,
733737
WithSuppressGCTransitions = withSuppressGCTransitions,
734738
WithTransparentStructs = withTransparentStructs,
@@ -1202,6 +1206,7 @@ private static RootCommand GetRootCommand()
12021206
s_withManualImports,
12031207
s_withNamespaceNameValuePairs,
12041208
s_withPackingNameValuePairs,
1209+
s_withReadonlys,
12051210
s_withSetLastErrors,
12061211
s_withSuppressGCTransitions,
12071212
s_withTransparentStructNameValuePairs,
@@ -1350,6 +1355,17 @@ private static Option<string[]> GetWithNamespaceOption()
13501355
};
13511356
}
13521357

1358+
private static Option<string[]> GetWithReadonlyOption()
1359+
{
1360+
return new Option<string[]>(
1361+
aliases: s_withReadonlyOptionAliases,
1362+
description: "Add the readonly modifier to a given instance method. Supports wildcards.",
1363+
getDefaultValue: Array.Empty<string>
1364+
) {
1365+
AllowMultipleArgumentsPerToken = true
1366+
};
1367+
}
1368+
13531369
private static Option<string[]> GetWithSetLastErrorOption()
13541370
{
13551371
return new Option<string[]>(

sources/ClangSharpPInvokeGenerator/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"GenerateLocal": {
1313
"commandName": "Project",
1414
"commandLineArgs": "@generate.rsp",
15-
"workingDirectory": "D:\\repos\\terrafx.interop.windows\\generation\\DirectX\\um\\dcommon"
15+
"workingDirectory": "D:\\repos\\terrafx.interop.windows\\generation\\windows\\um\\wingdi"
1616
}
1717
}
1818
}

0 commit comments

Comments
 (0)