Skip to content

Commit

Permalink
Added workaround with the associated function being wrong for vtable …
Browse files Browse the repository at this point in the history
…function pointers.

See #239
  • Loading branch information
PathogenDavid committed Feb 21, 2022
1 parent b0de034 commit 972b363
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Biohazrd.CSharp/CSharpLibraryGenerator.Records.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,26 @@ private void EmitVTable(VisitorContext context, TranslatedVTableField field, Tra

Writer.Write($"{entry.Accessibility.ToCSharpKeyword()} ");

if (entry.IsFunctionPointer && entry.MethodReference?.TryResolve(context.Library) is TranslatedFunction associatedFunction)
TranslatedFunction? associatedFunction = null;

// Prefer methods which are a sibling to the vtable
// (This is a workaround for https://github.com/MochiLibraries/Biohazrd/issues/239)
if (entry.MethodReference is DeclarationReference methodReference && context.ParentDeclaration is TranslatedRecord record)
{
foreach (TranslatedDeclaration sibling in record)
{
if (sibling is TranslatedFunction function && methodReference.__HACK__CouldResolveTo(function))
{
associatedFunction = function;
break;
}
}
}

// If we didn't find it as a sibling search the entire library
associatedFunction ??= entry.MethodReference?.TryResolve(context.Library) as TranslatedFunction;

if (entry.IsFunctionPointer && associatedFunction is not null)
{
if (associatedFunction.Metadata.TryGet(out TrampolineCollection trampolines))
{
Expand Down
3 changes: 3 additions & 0 deletions Biohazrd/#TypeReferences/ClangDeclTranslatedTypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ internal ClangDeclTranslatedTypeReference(Decl clangDecl)

public override string ToString()
=> $"`Ref resolved by {ClangDecl}{ToStringSuffix}`";

internal override bool __HACK__CouldResolveTo(TranslatedDeclaration declaration)
=> declaration.IsTranslationOf(ClangDecl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ internal DeclarationIdTranslatedTypeReference(DeclarationId id)

public override string ToString()
=> $"`Ref resolved by {Id}{ToStringSuffix}`";

internal override bool __HACK__CouldResolveTo(TranslatedDeclaration declaration)
=> declaration.MatchesId(Id);
}
}
3 changes: 3 additions & 0 deletions Biohazrd/#TypeReferences/PreResolvedTypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ public PreResolvedTypeReference(VisitorContext context, TranslatedDeclaration de

public override string ToString()
=> $"`Pre-resolved reference to {Declaration.Name}`";

internal override bool __HACK__CouldResolveTo(TranslatedDeclaration declaration)
=> ReferenceEquals(declaration, Declaration);
}
}
3 changes: 3 additions & 0 deletions Biohazrd/#TypeReferences/TranslatedTypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@ public static TranslatedTypeReference Create(TranslatedDeclaration declaration)
else
{ return new DeclarationIdTranslatedTypeReference(declaration.Id); }
}

// This is used for a workaround to https://github.com/MochiLibraries/Biohazrd/issues/239
internal abstract bool __HACK__CouldResolveTo(TranslatedDeclaration declaration);
}
}
2 changes: 2 additions & 0 deletions Biohazrd/Biohazrd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
<!-- Extra InternalsVisibleTo attributes -->
<ItemGroup>
<InternalsVisibleTo Include="$(MSBuildProjectName).CSharp.Tests" />
<!-- For temporary workaround methods on TranslatedTypeReference -->
<InternalsVisibleTo Include="$(MSBuildProjectName).CSharp" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions Biohazrd/DeclarationReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@ public DeclarationReference(TranslatedDeclaration declaration)

public override string ToString()
=> Reference.ToString();

internal bool __HACK__CouldResolveTo(TranslatedDeclaration declaration)
=> Reference.__HACK__CouldResolveTo(declaration);
}
}

0 comments on commit 972b363

Please sign in to comment.