Skip to content
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

[ASTGen] Generate PatternBindingDecl at top level #79355

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,13 @@ SWIFT_NAME("getter:BridgedDeclContext.astContext(self:)")
BRIDGED_INLINE BridgedASTContext
BridgedDeclContext_getASTContext(BridgedDeclContext dc);

SWIFT_NAME("getter:BridgedDeclContext.parentSourceFile(self:)")
BRIDGED_INLINE BridgedSourceFile
BridgedDeclContext_getParentSourceFile(BridgedDeclContext dc);

SWIFT_NAME("getter:BridgedSourceFile.isScriptMode(self:)")
BRIDGED_INLINE bool BridgedSourceFile_isScriptMode(BridgedSourceFile sf);

SWIFT_NAME("BridgedPatternBindingInitializer.create(declContext:)")
BridgedPatternBindingInitializer
BridgedPatternBindingInitializer_create(BridgedDeclContext cDeclContext);
Expand Down Expand Up @@ -1143,6 +1150,10 @@ struct BridgedFingerprint;
SWIFT_NAME("BridgedDecl.attachParsedAttrs(self:_:)")
void BridgedDecl_attachParsedAttrs(BridgedDecl decl, BridgedDeclAttributes attrs);

SWIFT_NAME("BridgedDecl.forEachDeclToHoist(self:_:)")
void BridgedDecl_forEachDeclToHoist(BridgedDecl decl,
BridgedSwiftClosure closure);

enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedStaticSpelling {
BridgedStaticSpellingNone,
BridgedStaticSpellingStatic,
Expand Down Expand Up @@ -1434,19 +1445,14 @@ BridgedSubscriptDecl BridgedSubscriptDecl_createParsed(
BridgedParameterList cParamList, BridgedSourceLoc cArrowLoc,
BridgedTypeRepr returnType);

SWIFT_NAME(
"BridgedTopLevelCodeDecl.createParsed(_:declContext:startLoc:stmt:endLoc:)")
BridgedTopLevelCodeDecl BridgedTopLevelCodeDecl_createStmt(
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
BridgedSourceLoc cStartLoc, BridgedStmt statement,
BridgedSourceLoc cEndLoc);
SWIFT_NAME("BridgedTopLevelCodeDecl.create(_:declContext:)")
BridgedTopLevelCodeDecl
BridgedTopLevelCodeDecl_create(BridgedASTContext cContext,
BridgedDeclContext cDeclContext);

SWIFT_NAME(
"BridgedTopLevelCodeDecl.createParsed(_:declContext:startLoc:expr:endLoc:)")
BridgedTopLevelCodeDecl BridgedTopLevelCodeDecl_createExpr(
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
BridgedSourceLoc cStartLoc, BridgedExpr expression,
BridgedSourceLoc cEndLoc);
SWIFT_NAME("BridgedTopLevelCodeDecl.setBody(self:body:)")
void BridgedTopLevelCodeDecl_setBody(BridgedTopLevelCodeDecl cDecl,
BridgedBraceStmt cBody);

SWIFT_NAME("BridgedTopLevelCodeDecl.dump(self:)")
void BridgedTopLevelCodeDecl_dump(BridgedTopLevelCodeDecl decl);
Expand Down Expand Up @@ -2019,6 +2025,12 @@ BridgedBraceStmt BridgedBraceStmt_createParsed(BridgedASTContext cContext,
BridgedArrayRef elements,
BridgedSourceLoc cRBLoc);

SWIFT_NAME("BridgedBraceStmt.createImplicit(_:lBraceLoc:element:rBraceLoc:)")
BridgedBraceStmt BridgedBraceStmt_createImplicit(BridgedASTContext cContext,
BridgedSourceLoc cLBLoc,
BridgedASTNode element,
BridgedSourceLoc cRBLoc);

SWIFT_NAME("BridgedBreakStmt.createParsed(_:loc:targetName:targetLoc:)")
BridgedBreakStmt BridgedBreakStmt_createParsed(BridgedDeclContext cDeclContext,
BridgedSourceLoc cLoc,
Expand Down
13 changes: 13 additions & 0 deletions include/swift/AST/ASTBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ BridgedASTContext BridgedDeclContext_getASTContext(BridgedDeclContext dc) {
return dc.unbridged()->getASTContext();
}

BridgedSourceFile
BridgedDeclContext_getParentSourceFile(BridgedDeclContext dc) {
return dc.unbridged()->getParentSourceFile();
}

//===----------------------------------------------------------------------===//
// MARK: BridgedSoureFile
//===----------------------------------------------------------------------===//

BRIDGED_INLINE bool BridgedSourceFile_isScriptMode(BridgedSourceFile sf) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: BRIDGED_INLINE isn't needed here

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy paste error... let me fix it later along with another BRIDGED_INLINE in this file.

return sf.unbridged()->isScriptMode();
}

//===----------------------------------------------------------------------===//
// MARK: BridgedDeclObj
//===----------------------------------------------------------------------===//
Expand Down
4 changes: 4 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
/// Returns true if this declaration has any `@backDeployed` attributes.
bool hasBackDeployedAttr() const;

/// Apply the specified function to decls that should be placed _next_ to
/// this decl when constructing AST.
void forEachDeclToHoist(llvm::function_ref<void(Decl *)> callback) const;

/// Emit a diagnostic tied to this declaration.
template<typename ...ArgTypes>
InFlightDiagnostic diagnose(
Expand Down
12 changes: 12 additions & 0 deletions include/swift/Basic/BasicBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,18 @@ struct BridgedVersionTuple {
llvm::VersionTuple unbridged() const;
};

//===----------------------------------------------------------------------===//
// MARK: BridgedSwiftClosure
//===----------------------------------------------------------------------===//

/// Wrapping a pointer to a Swift closure `(UnsafeRawPointer?) -> Void`
/// See 'withBridgedSwiftClosure(closure:call:)' in ASTGen.
struct BridgedSwiftClosure {
const void *_Nonnull closure;

BRIDGED_INLINE void operator()(const void *_Nullable);
};

SWIFT_END_NULLABILITY_ANNOTATIONS

#ifndef PURE_BRIDGING_MODE
Expand Down
8 changes: 8 additions & 0 deletions include/swift/Basic/BasicBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ BridgedSwiftVersion::BridgedSwiftVersion(SwiftInt major, SwiftInt minor)
ASSERT(major == Major && minor == Minor);
}

extern "C" void
swift_ASTGen_bridgedSwiftClosureCall_1(const void *_Nonnull closure,
const void *_Nullable arg1);

void BridgedSwiftClosure::operator()(const void *_Nullable arg1) {
swift_ASTGen_bridgedSwiftClosureCall_1(closure, arg1);
}

SWIFT_END_NULLABILITY_ANNOTATIONS

#endif // SWIFT_BASIC_BASICBRIDGINGIMPL_H
9 changes: 9 additions & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,15 @@ namespace {
printFlag(VD->getAttrs().hasAttribute<LazyAttr>(), "lazy",
DeclModifierColor);
printStorageImpl(VD);
printFlag(VD->isSelfParamCapture(), "self_param_capture",
DeclModifierColor);
printFlag(VD->isDebuggerVar(), "debugger_var", DeclModifierColor);
printFlag(VD->isLazyStorageProperty(), "lazy_storage_property",
DeclModifierColor);
printFlag(VD->isTopLevelGlobal(), "top_level_global", DeclModifierColor);
printFlag(VD->isLazyStorageProperty(), "lazy_storage_property",
DeclModifierColor);

printFlag(VD->getAttrs().hasAttribute<KnownToBeLocalAttr>(),
"known_to_be_local", DeclModifierColor);
if (auto *nonisolatedAttr =
Expand Down
67 changes: 21 additions & 46 deletions lib/AST/Bridging/DeclBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ void BridgedDecl_attachParsedAttrs(BridgedDecl decl,
decl.unbridged()->attachParsedAttrs(attrs.unbridged());
}

void BridgedDecl_forEachDeclToHoist(BridgedDecl cDecl,
BridgedSwiftClosure closure) {
cDecl.unbridged()->forEachDeclToHoist([&](Decl *D) {
BridgedDecl bridged(D);
closure(&bridged);
});
}

BridgedAccessorDecl BridgedAccessorDecl_createParsed(
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
BridgedAccessorKind cKind, BridgedAbstractStorageDecl cStorage,
Expand Down Expand Up @@ -150,6 +158,7 @@ BridgedPatternBindingDecl BridgedPatternBindingDecl_createParsed(
VD->attachParsedAttrs(cAttrs.unbridged());
VD->setStatic(isStatic);
VD->setIntroducer(introducer);
VD->setTopLevelGlobal(isa<TopLevelCodeDecl>(declContext));
});

entries.emplace_back(pattern, entry.equalLoc.unbridged(),
Expand Down Expand Up @@ -345,32 +354,15 @@ static void setParsedMembers(IterableDeclContext *IDC, BridgedArrayRef cMembers,

Fingerprint fp = cFingerprint.unbridged();

SmallVector<Decl *> members;
for (auto *decl : cMembers.unbridged<Decl *>()) {
members.push_back(decl);

// Add any variables bound to the list of decls.
if (auto *PBD = dyn_cast<PatternBindingDecl>(decl)) {
for (auto idx : range(PBD->getNumPatternEntries())) {
PBD->getPattern(idx)->forEachVariable(
[&](VarDecl *VD) { members.push_back(VD); });
}
}
// Each enum case element is also part of the members list according to the
// legacy parser.
if (auto *ECD = dyn_cast<EnumCaseDecl>(decl)) {
for (auto *EED : ECD->getElements()) {
members.push_back(EED);
}
}
}
ArrayRef<Decl *> members =
ctx.AllocateTransform<Decl *>(cMembers.unbridged<BridgedDecl>(),
[](auto decl) { return decl.unbridged(); });

IDC->setMaybeHasOperatorDeclarations();
IDC->setMaybeHasNestedClassDeclarations();
// FIXME: Split requests. e.g. DeclMembersFingerprintRequest.
ctx.evaluator.cacheOutput(
ParseMembersRequest{IDC},
FingerprintAndMembers{fp, ctx.AllocateCopy(members)});
ctx.evaluator.cacheOutput(ParseMembersRequest{IDC},
FingerprintAndMembers{fp, members});
}

void BridgedNominalTypeDecl_setParsedMembers(BridgedNominalTypeDecl cDecl,
Expand Down Expand Up @@ -660,32 +652,15 @@ BridgedSubscriptDecl BridgedSubscriptDecl_createParsed(
cGenericParamList.unbridged());
}

BridgedTopLevelCodeDecl BridgedTopLevelCodeDecl_createStmt(
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
BridgedSourceLoc cStartLoc, BridgedStmt statement,
BridgedSourceLoc cEndLoc) {
ASTContext &context = cContext.unbridged();
DeclContext *declContext = cDeclContext.unbridged();

auto *S = statement.unbridged();
auto Brace = BraceStmt::create(context, cStartLoc.unbridged(), {S},
cEndLoc.unbridged(),
/*Implicit=*/true);
return new (context) TopLevelCodeDecl(declContext, Brace);
BridgedTopLevelCodeDecl
BridgedTopLevelCodeDecl_create(BridgedASTContext cContext,
BridgedDeclContext cDeclContext) {
return new (cContext.unbridged()) TopLevelCodeDecl(cDeclContext.unbridged());
}

BridgedTopLevelCodeDecl BridgedTopLevelCodeDecl_createExpr(
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
BridgedSourceLoc cStartLoc, BridgedExpr expression,
BridgedSourceLoc cEndLoc) {
ASTContext &context = cContext.unbridged();
DeclContext *declContext = cDeclContext.unbridged();

auto *E = expression.unbridged();
auto Brace = BraceStmt::create(context, cStartLoc.unbridged(), {E},
cEndLoc.unbridged(),
/*Implicit=*/true);
return new (context) TopLevelCodeDecl(declContext, Brace);
void BridgedTopLevelCodeDecl_setBody(BridgedTopLevelCodeDecl cDecl,
BridgedBraceStmt cBody) {
cDecl.unbridged()->setBody(cBody.unbridged());
}

BridgedVarDecl BridgedVarDec_createImplicitStringInterpolationVar(
Expand Down
35 changes: 12 additions & 23 deletions lib/AST/Bridging/StmtBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,35 +73,24 @@ BridgedBraceStmt BridgedBraceStmt_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cLBLoc,
BridgedArrayRef elements,
BridgedSourceLoc cRBLoc) {
llvm::SmallVector<ASTNode, 6> nodes;
for (auto node : elements.unbridged<BridgedASTNode>()) {
if (node.Kind == ASTNodeKindExpr) {
auto expr = (Expr *)node.Raw;
nodes.push_back(expr);
} else if (node.Kind == ASTNodeKindStmt) {
auto stmt = (Stmt *)node.Raw;
nodes.push_back(stmt);
} else {
assert(node.Kind == ASTNodeKindDecl);
auto decl = (Decl *)node.Raw;
nodes.push_back(decl);

// Variable declarations are part of the list on par with pattern binding
// declarations per the legacy parser.
if (auto *bindingDecl = dyn_cast<PatternBindingDecl>(decl)) {
for (auto i : range(bindingDecl->getNumPatternEntries())) {
bindingDecl->getPattern(i)->forEachVariable(
[&nodes](VarDecl *variable) { nodes.push_back(variable); });
}
}
}
}
llvm::SmallVector<ASTNode, 16> nodes;
for (auto node : elements.unbridged<BridgedASTNode>())
nodes.push_back(node.unbridged());

ASTContext &context = cContext.unbridged();
return BraceStmt::create(context, cLBLoc.unbridged(), nodes,
cRBLoc.unbridged());
}

BridgedBraceStmt BridgedBraceStmt_createImplicit(BridgedASTContext cContext,
BridgedSourceLoc cLBLoc,
BridgedASTNode element,
BridgedSourceLoc cRBLoc) {
return BraceStmt::create(cContext.unbridged(), cLBLoc.unbridged(),
{element.unbridged()}, cRBLoc.unbridged(),
/*Implicit=*/true);
}

BridgedBreakStmt BridgedBreakStmt_createParsed(BridgedDeclContext cDeclContext,
BridgedSourceLoc cLoc,
BridgedIdentifier cTargetName,
Expand Down
33 changes: 33 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,39 @@ bool Decl::hasBackDeployedAttr() const {
return false;
}

void Decl::forEachDeclToHoist(llvm::function_ref<void(Decl *)> callback) const {
switch (getKind()) {
case DeclKind::TopLevelCode: {
auto *TLCD = cast<TopLevelCodeDecl>(this);
if (TLCD->getBody()) {
for (auto node : TLCD->getBody()->getElements())
if (auto *d = node.dyn_cast<Decl *>())
d->forEachDeclToHoist(callback);
}
break;
}
case DeclKind::PatternBinding: {
auto *PBD = cast<PatternBindingDecl>(this);
// Variable declarations are part of the list on par with pattern binding
// declarations.
for (auto i : range(PBD->getNumPatternEntries())) {
PBD->getPattern(i)->forEachVariable([&](VarDecl *VD) { callback(VD); });
}
break;
}
case DeclKind::EnumCase: {
auto *ECD = cast<EnumCaseDecl>(this);
// Each enum case element is part of the members list.
for (auto *EED : ECD->getElements()) {
callback(EED);
}
break;
}
default:
break;
}
}

llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &OS,
StaticSpellingKind SSK) {
switch (SSK) {
Expand Down
Loading