Skip to content

Remove CBasicBridging + CASTBridging #69440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 31, 2023

Conversation

hamishknight
Copy link
Contributor

@hamishknight hamishknight commented Oct 26, 2023

Merge CASTBridging and CBasicBridging with ASTBridging and BasicBridging respectively, allowing ASTGen to use C++ interop, and for both ASTGen and SwiftCompilerSources to share the same bridging code. There's more work that can be done here to improve the bridging (e.g we could potentially remove the bridged and unbridged functions in ASTBridging.cpp now), but I'm leaving that for a future PR as there's already enough here.

@hamishknight hamishknight force-pushed the double-plus branch 6 times, most recently from e572b4a to 57384f4 Compare October 30, 2023 13:59
@hamishknight hamishknight changed the title [WIP] Remove CBasicBridging + CASTBridging Remove CBasicBridging + CASTBridging Oct 30, 2023
@hamishknight hamishknight marked this pull request as ready for review October 30, 2023 14:01
#ifndef DECL
#define DECL(Id, Parent) AST_BRIDGING_WRAPPER_NONNULL(Id##Decl)
#define DECL(Id, Parent) AST_BRIDGING_WRAPPER_NULLABLE(Id##Decl)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point I kind of wonder whether we're really gaining much from defining separate macros for can-be-nullable, and never-nullable, and should just stamp out nullable bridging wrappers for everything. My main worry with that initially was that it would unnecessarily pollute the type namespace, but at this point a good chunk of types need nullable wrappers anyways. In any case, I can explore that in a follow up.


SWIFT_END_NULLABILITY_ANNOTATIONS

#ifndef PURE_BRIDGING_MODE
// In _not_ PURE_BRIDGING_MODE, briding functions are inlined and therefore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// In _not_ PURE_BRIDGING_MODE, briding functions are inlined and therefore
// In _not_ PURE_BRIDGING_MODE, bridging functions are inlined and therefore

But also, why is that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the bridging functions can require C++ headers that we don't want to include in "pure bridging mode", since that can trigger the Windows and LLDB C++ interop issues. I can update the comment to explain this (FWIW I copied it from SILBridging.h)

Comment on lines 107 to 108
const void *_Nullable data;
size_t numElements;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppose these should both start with an uppercase :(. Looks like other Bridged* are similar too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look again :) But yeah I should probably fix the others too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, nice. How far apart was your push and my hitting comment 😅? Doesn't have to be in this PR FWIW.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think I'll do this in a follow up

try withUTF8 { buffer in
try body(BridgedString(data: buffer.baseAddress, length: buffer.count))
try body(BridgedStringRef(data: buffer.baseAddress, count: buffer.count))
}
}
}

/// Allocate a copy of the given string as a null-terminated UTF-8 string.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could/should uses of this use BridgedOwnedString now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah seems like it probably should

assert(!externalDef.isError());
swift_ASTGen_expandFreestandingMacro(
&ctx.Diags, externalDef.opaqueHandle,
static_cast<uint32_t>(externalDef.kind), discriminator->c_str(),
getRawMacroRole(macroRole), astGenSourceFile,
expansion->getSourceRange().Start.getOpaquePointerValue(),
&evaluatedSourceOut);
if (!evaluatedSourceOut.data)
if (!evaluatedSourceOut.get().data())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge fan of get. unbridged would match the bridging .cpp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah that seems fair enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I remember why I named it this way now, it's because it matches all the wrappers in SILBridging.h. If we want to change to unbridged we probably ought to change those too. Not sure if you have any strong opinions on this @eeckstein?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no strong opinions. We can change it

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@xedin xedin removed their request for review October 30, 2023 18:31
@hamishknight
Copy link
Contributor Author

Will address the rest of the feedback in a follow up

Tweak the compiler flags such that it can be
built from within Xcode. This is only useful when
editing, but I find it very handy.
Improve APIs for BridgedStringRef,
BridgedOwnedString, and BridgedSourceLoc.
Migrate onto BridgedStringRef, and the
BasicBridging equivalents of BridgedSourceLoc
and BridgedArrayRef.
These are `void *` on the C side.
Introduce a macro that can stamp out wrapper
classes for underlying C++ pointers, and use
it to define BridgedDiagnosticEngine in
ASTBridging. Then, migrate users of
BridgedDiagEngine onto it.
And renaming OptionalBridgedVarDecl to
BridgedNullableVarDecl for consistency with the
existing nullable AST node wrappers.
Merge with BasicBridging and ASTBridging
respectively. The changes here should be pretty
uncontroversial, I tried to keep it to just moving
code about.
These are now stamped out by the ASTBridgingWrappers.
Move these into ASTBridgingWrappers.def.
These aren't needed in C++
Add a `Bridged` prefix to match the name being
exposed to Swift, and to match the other bridging
functions. Also while here, use `SWIFT_NAME` for
some bridging functions that were missing it.
Sort AST node bridging functions into Expr, Stmt,
Decl, and TypeRepr, and tweak the headers to use
`MARK`.
Remove the default constructor footgun present with
the struct implementations, and sprinkle some
`SWIFT_NAME` and bridging utilities to make them
nicer to work with.
These are now empty, and it seems like generally
we ought to prefer putting the bridging logic on
the BridgedXXX wrappers.
@hamishknight
Copy link
Contributor Author

@swift-ci please test

@hamishknight
Copy link
Contributor Author

🚢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants