Skip to content

Commit eb59f3e

Browse files
committed
Progress
1 parent c190dc7 commit eb59f3e

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

Biohazrd.CSharp/CSharpGenerationOptions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public TargetLanguageVersion TargetLanguageVersion
9292
}
9393
}
9494

95+
/// <summary>If true, default parameter values will not be emitted for non-public APIs.</summary>
96+
/// <remarks>
97+
/// This setting is enabled by default to avoid unecessary metadata bloat.
98+
/// If you intend to manually write trampolines for native functions it may be desirable to turn this off.
99+
/// </remarks>
95100
public bool SuppressDefaultParameterValuesOnNonPublicMethods { get; init; } = true;
96101

97102
public CSharpGenerationOptions()

Biohazrd.CSharp/Trampolines/Trampoline.cs

+28-13
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ internal void Write(ICSharpOutputGenerator outputGenerator, VisitorContext conte
254254
{
255255
constructorName = constructorType.Name;
256256

257+
//TODO: This does not handle input-no-output adapters
257258
// Parameterless constructors require C# 10
258259
if (declaration.Parameters.Length == 0 && outputGenerator.Options.TargetLanguageVersion < TargetLanguageVersion.CSharp10)
259260
{ constructorName = null; }
@@ -365,11 +366,38 @@ internal void Write(ICSharpOutputGenerator outputGenerator, VisitorContext conte
365366
else
366367
{ ReturnAdapter.WriteResultCapture(functionContext, writer); }
367368

369+
bool targetIsActuallyConstructor = false;
370+
if (declaration.SpecialFunctionKind == SpecialFunctionKind.Constructor && !Target.IsNativeFunction)
371+
{
372+
targetIsActuallyConstructor = true;
373+
bool anyTargetAdaptersAreInputs = false;
374+
375+
foreach (Adapter adapter in Target.Adapters)
376+
{
377+
if (adapter.AcceptsInput)
378+
{
379+
anyTargetAdaptersAreInputs = true;
380+
381+
if (adapter.SpecialKind == SpecialAdapterKind.ThisPointer)
382+
{
383+
targetIsActuallyConstructor = false;
384+
break;
385+
}
386+
}
387+
}
388+
389+
// If the target could be a constructor but it has no inputs, it will not be a constructor unless we're targeting C# 10 or newer
390+
if (targetIsActuallyConstructor && !anyTargetAdaptersAreInputs && outputGenerator.Options.TargetLanguageVersion < TargetLanguageVersion.CSharp10)
391+
{ targetIsActuallyConstructor = false; }
392+
}
393+
368394
if (virtualMethodAccess is not null)
369395
{
370396
Debug.Assert(declaration.IsVirtual);
371397
writer.Write(virtualMethodAccess);
372398
}
399+
else if (targetIsActuallyConstructor)
400+
{ writer.Write("this = new"); }
373401
else
374402
//TODO: Need to handle constructor dispatch
375403
{ writer.WriteIdentifier(Target.Name); }
@@ -379,7 +407,6 @@ internal void Write(ICSharpOutputGenerator outputGenerator, VisitorContext conte
379407

380408
bool first = true;
381409
ImmutableArray<TranslatedParameter> parameters = declaration.Parameters;
382-
int parameterIndex = -1;
383410
int adapterIndex = -1;
384411

385412
foreach (Adapter adapter in Adapters)
@@ -389,18 +416,6 @@ internal void Write(ICSharpOutputGenerator outputGenerator, VisitorContext conte
389416
if (!adapter.ProvidesOutput)
390417
{ continue; }
391418

392-
// Validate the parameter and this adapter correspond to eachother
393-
#if false //TODO: Does this actually make sense? It would not handle parameters being removed. Also needs to handle implicit parameters
394-
parameterIndex++;
395-
if (parameterIndex >= parameters.Length)
396-
{ throw new InvalidOperationException($"The trampoline is malformed, {adapter.GetType().Name} @ {adapterIndex} provides an output but all parameters in the target function have been accounted for."); }
397-
398-
//TODO: Should we support reordering parameters?
399-
TranslatedParameter parameter = parameters[parameterIndex];
400-
if (!adapter.CorrespondsTo(parameter))
401-
{ throw new InvalidOperationException($"The trampoline is malformed, {adapter.GetType().Name} @ {adapterIndex} does not correspond to '{parameter}' @ {parameterIndex}."); }
402-
#endif
403-
404419
if (first)
405420
{ first = false; }
406421
else

0 commit comments

Comments
 (0)