Skip to content

Commit 91da8d8

Browse files
committed
Removed some misguided features and configuration.
* Removed the ability to pass implicit byref by reference. This actually doesn't have the correct semantics from a C++ perspective because the callee assumes it owns the buffer. * Removed the ability to disable translating const& as pointer to byval. This is less efficient for no good reason. * Removed being able to disable friendly handling of both situations. If we somehow missed an edge case here people can expose the raw P/Invoke or let us know. I don't know why I'm writing a detailed commit message for a temporary commit.
1 parent 1ba676f commit 91da8d8

File tree

4 files changed

+7
-108
lines changed

4 files changed

+7
-108
lines changed

Biohazrd.CSharp/#Transformations/CreateTrampolinesTransformation.cs

+7-57
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,6 @@ public sealed class CreateTrampolinesTransformation : CSharpTransformationBase
1212
{
1313
public TargetRuntime TargetRuntime { get; init; } = CSharpGenerationOptions.Default.TargetRuntime; //TODO: This ideally should come from some central context to ensure consistency
1414

15-
private ReferenceTypeOutputBehavior _CppReferenceOutputBehavior = ReferenceTypeOutputBehavior.AsRefOrByValue;
16-
public ReferenceTypeOutputBehavior CppReferenceOutputBehavior
17-
{
18-
get => _CppReferenceOutputBehavior;
19-
init
20-
{
21-
if (!Enum.IsDefined(value))
22-
{ throw new ArgumentOutOfRangeException(nameof(value)); }
23-
24-
_CppReferenceOutputBehavior = value;
25-
}
26-
}
27-
28-
private ReferenceTypeOutputBehavior _ImplicitReferenceOutputBehavior = ReferenceTypeOutputBehavior.AsRefOrByValue;
29-
public ReferenceTypeOutputBehavior ImplicitReferenceOutputBehavior
30-
{
31-
get => _ImplicitReferenceOutputBehavior;
32-
init
33-
{
34-
if (!Enum.IsDefined(value))
35-
{ throw new ArgumentOutOfRangeException(nameof(value)); }
36-
37-
_ImplicitReferenceOutputBehavior = value;
38-
}
39-
}
40-
4115
protected override TransformationResult TransformFunction(TransformationContext context, TranslatedFunction declaration)
4216
{
4317
// Don't try to add trampolines to a function which already has them
@@ -181,19 +155,11 @@ void CreateNativeReturnByReferenceAdapter()
181155
Adapter nativeAdapter = new PassthroughAdapter(parameter, new PointerTypeReference(parameter.Type));
182156
nativeAdapters.Add(nativeAdapter);
183157

184-
// Pick friendly adapter
185-
switch (ImplicitReferenceOutputBehavior)
186-
{
187-
case ReferenceTypeOutputBehavior.AlwaysByRef:
188-
AddFriendlyAdapter(nativeAdapter, new ByRefAdapter(nativeAdapter, ByRefKind.In));
189-
break;
190-
case ReferenceTypeOutputBehavior.AsRefOrByValue:
191-
AddFriendlyAdapter(nativeAdapter, new ToPointerAdapter(nativeAdapter));
192-
break;
193-
default:
194-
Debug.Assert(ImplicitReferenceOutputBehavior == ReferenceTypeOutputBehavior.AsPointer);
195-
break;
196-
}
158+
// Parameters which are written as being passed by value but are implicitly passed by reference will be adapted to behave the same.
159+
// Using byref here might seem tempting from a performance standpoint, but doing so would change semantics since the callee assumes it owns the buffer.
160+
// In theory if the native function receives a const byval we could use a readonly byref, but in partice C++ compilers don't do that even for PODs so we won't either.
161+
// (const byvals are weird and are not consdiered a good practice in C++ anyway.)
162+
AddFriendlyAdapter(nativeAdapter, new ToPointerAdapter(nativeAdapter));
197163

198164
continue;
199165
}
@@ -204,24 +170,8 @@ void CreateNativeReturnByReferenceAdapter()
204170
nativeAdapters.Add(nativeAdapter);
205171

206172
// Handle C++-style reference
207-
if (CppReferenceOutputBehavior != ReferenceTypeOutputBehavior.AsPointer && parameter.Type is PointerTypeReference { WasReference: true } referenceType)
208-
{
209-
switch (CppReferenceOutputBehavior)
210-
{
211-
case ReferenceTypeOutputBehavior.AsRefOrByValue:
212-
if (referenceType.InnerIsConst)
213-
{ AddFriendlyAdapter(nativeAdapter, new ToPointerAdapter(nativeAdapter)); }
214-
else
215-
{ AddFriendlyAdapter(nativeAdapter, new ByRefAdapter(nativeAdapter, ByRefKind.Ref)); }
216-
break;
217-
case ReferenceTypeOutputBehavior.AlwaysByRef:
218-
AddFriendlyAdapter(nativeAdapter, new ByRefAdapter(nativeAdapter, referenceType.InnerIsConst ? ByRefKind.In : ByRefKind.Ref));
219-
break;
220-
default:
221-
Debug.Fail("Unreachable.");
222-
break;
223-
}
224-
}
173+
if (parameter.Type is PointerTypeReference { WasReference: true } referenceType)
174+
{ AddFriendlyAdapter(nativeAdapter, new ByRefAdapter(nativeAdapter, referenceType.InnerIsConst ? ByRefKind.In : ByRefKind.Ref)); }
225175
}
226176
}
227177

Biohazrd.CSharp/BiohzardExtensions.cs

-20
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,6 @@ internal static TDeclaration WithWarning<TDeclaration>(this TDeclaration declara
7474
_ => null
7575
};
7676

77-
/// <summary>Gets the effective <see cref="ReferenceTypeOutputBehavior"/> for the parameter if applicable.</summary>
78-
internal static ParameterOutputMode GetParameterOutputMode(this TranslatedParameter parameter, ReferenceTypeOutputBehavior globalOutputBehavior)
79-
{
80-
if (parameter.Type is not PointerTypeReference { WasReference: true } referenceType)
81-
{ return ParameterOutputMode.Normal; }
82-
83-
ReferenceTypeOutputBehavior outputBehavior = globalOutputBehavior;
84-
85-
if (parameter.Metadata.TryGet(out OverrideReferenceTypeOutputBehavior overrideMetadata))
86-
{ outputBehavior = overrideMetadata.ReferenceTypeOutputBehavior; }
87-
88-
return outputBehavior switch
89-
{
90-
ReferenceTypeOutputBehavior.AsPointer => ParameterOutputMode.Normal,
91-
ReferenceTypeOutputBehavior.AsRefOrByValue => referenceType.InnerIsConst ? ParameterOutputMode.RefByValue : ParameterOutputMode.RefByRef,
92-
ReferenceTypeOutputBehavior.AlwaysByRef => referenceType.InnerIsConst ? ParameterOutputMode.RefByReadonlyRef : ParameterOutputMode.RefByRef,
93-
_ => throw new InvalidOperationException($"Unknown/unsupported {nameof(ReferenceTypeOutputBehavior)} '{outputBehavior}' applied to {parameter}.")
94-
};
95-
}
96-
9777
public static Trampoline? TryGetPrimaryTrampoline(this TranslatedFunction function)
9878
=> function.Metadata.TryGet(out TrampolineCollection trampolines) ? trampolines.PrimaryTrampoline : null;
9979

Biohazrd.CSharp/Metadata/OverrideReferenceTypeOutputBehavior.cs

-10
This file was deleted.

Biohazrd.CSharp/ReferenceTypeOutputBehavior.cs

-21
This file was deleted.

0 commit comments

Comments
 (0)