Skip to content

Commit c2cd7b7

Browse files
committed
[ASTGen] Lower @abi in ASTGen
1 parent b09d57c commit c2cd7b7

File tree

10 files changed

+314
-8
lines changed

10 files changed

+314
-8
lines changed

include/swift/AST/ASTBridging.h

+50
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,34 @@ SWIFT_NAME("BridgedDeclAttributes.add(self:_:)")
564564
void BridgedDeclAttributes_add(BridgedDeclAttributes *_Nonnull attrs,
565565
BridgedDeclAttribute add);
566566

567+
/// Retrieve the attribute from \p cAttrs that comes after \p cPriorAttr .
568+
/// If \c cPriorAttr is null, retrieves the first attribute. If there are no
569+
/// more attributes, returns null.
570+
SWIFT_NAME("BridgedDeclAttributes.attr(self:after:)")
571+
BridgedNullableDeclAttribute BridgedDeclAttributes_getAttrAfter(
572+
BridgedDeclAttributes cAttrs, BridgedNullableDeclAttribute cPriorAttr);
573+
567574
SWIFT_NAME("BridgedDeclAttribute.createSimple(_:kind:atLoc:nameLoc:)")
568575
BridgedDeclAttribute BridgedDeclAttribute_createSimple(
569576
BridgedASTContext cContext, BridgedDeclAttrKind cKind,
570577
BridgedSourceLoc cAtLoc, BridgedSourceLoc cNameLoc);
571578

579+
SWIFT_NAME("getter:BridgedDeclAttribute.asABIAttr(self:)")
580+
BridgedNullableABIAttr BridgedDeclAttribute_asABIAttr(
581+
BridgedDeclAttribute cAttr);
582+
583+
SWIFT_NAME("BridgedABIAttr.createParsed(_:atLoc:range:abiDecl:)")
584+
BridgedABIAttr BridgedABIAttr_createParsed(
585+
BridgedASTContext cContext, BridgedSourceLoc atLoc,
586+
BridgedSourceRange range, BridgedNullableDecl abiDecl);
587+
588+
SWIFT_NAME("BridgedABIAttr.createImplicitInverse(_:)")
589+
BridgedABIAttr BridgedABIAttr_createImplicitInverse(
590+
BridgedASTContext cContext);
591+
592+
SWIFT_NAME("BridgedABIAttr.connectToInverse(self:attachedTo:)")
593+
void BridgedABIAttr_connectToInverse(BridgedABIAttr cAttr, BridgedDecl cOwner);
594+
572595
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedAccessLevel {
573596
BridgedAccessLevelPrivate,
574597
BridgedAccessLevelFilePrivate,
@@ -869,6 +892,10 @@ BridgedDeclAttributes BridgedDecl_getAttrs(BridgedDecl decl);
869892
SWIFT_NAME("setter:BridgedDecl.attrs(self:newValue:)")
870893
void BridgedDecl_setAttrs(BridgedDecl decl, BridgedDeclAttributes attrs);
871894

895+
SWIFT_NAME("getter:BridgedDecl.asPatternBindingDecl(self:)")
896+
BridgedNullablePatternBindingDecl BridgedDecl_getAsPatternBindingDecl(
897+
BridgedDecl decl);
898+
872899
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedStaticSpelling {
873900
BridgedStaticSpellingNone,
874901
BridgedStaticSpellingStatic,
@@ -904,6 +931,14 @@ BridgedPatternBindingDecl BridgedPatternBindingDecl_createParsed(
904931
BridgedSourceLoc cBindingKeywordLoc, BridgedArrayRef cBindingEntries,
905932
bool isStatic, bool isLet);
906933

934+
SWIFT_NAME("getter:BridgedPatternBindingDecl.patternCount(self:)")
935+
SwiftInt BridgedPatternBindingDecl_getNumPatternEntries(
936+
BridgedPatternBindingDecl cPBD);
937+
938+
SWIFT_NAME("BridgedPatternBindingDecl.pattern(self:at:)")
939+
BridgedPattern BridgedPatternBindingDecl_getPattern(
940+
BridgedPatternBindingDecl cPBD, SwiftInt i);
941+
907942
SWIFT_NAME("BridgedParamDecl.createParsed(_:declContext:specifierLoc:argName:"
908943
"argNameLoc:paramName:paramNameLoc:type:defaultValue:)")
909944
BridgedParamDecl BridgedParamDecl_createParsed(
@@ -2061,6 +2096,21 @@ void BridgedTypeRepr_dump(BridgedTypeRepr type);
20612096
SWIFT_NAME("getter:BridgedPattern.singleVar(self:)")
20622097
BridgedNullableVarDecl BridgedPattern_getSingleVar(BridgedPattern cPattern);
20632098

2099+
/// Iterates over the pattern, collecting and returning all of the BridgedVarDecls
2100+
/// referenced by its subpatterns.
2101+
///
2102+
/// The exact semantics of this are as follows:
2103+
///
2104+
/// \li This method always returns the current count of BridgedVarDecls in the
2105+
/// pattern.
2106+
/// \li If \p capacity is greater than or equal to the current count \em and
2107+
/// \p varDeclArrayOut is non-null, this method also writes the
2108+
/// BridgedVarDecls into the buffer starting at \p varDeclArrayOut .
2109+
SWIFT_NAME("BridgedPattern.unsafeFetchVarDecls(self:into:capacity:)")
2110+
SwiftInt BridgedPattern_unsafeFetchVarDecls(
2111+
BridgedPattern cPattern, BridgedVarDecl * _Nullable varDeclArrayOut,
2112+
SwiftInt capacity);
2113+
20642114
SWIFT_NAME("BridgedAnyPattern.createParsed(_:loc:)")
20652115
BridgedAnyPattern BridgedAnyPattern_createParsed(BridgedASTContext cContext,
20662116
BridgedSourceLoc cLoc);

include/swift/AST/ASTBridgingWrappers.def

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@
6262

6363
#ifndef DECL_ATTR
6464
#define SIMPLE_DECL_ATTR(...)
65-
#define DECL_ATTR(_, Id, ...) AST_BRIDGING_WRAPPER_NONNULL(Id##Attr)
65+
#define DECL_ATTR(_, Id, ...) AST_BRIDGING_WRAPPER_NULLABLE(Id##Attr)
6666
#endif
6767
#include "swift/AST/DeclAttr.def"
6868

6969
// Some of the base classes need to be nullable to allow them to be used as
7070
// optional parameters.
71-
AST_BRIDGING_WRAPPER_NONNULL(Decl)
71+
AST_BRIDGING_WRAPPER_NULLABLE(Decl)
7272
AST_BRIDGING_WRAPPER_NONNULL(DeclContext)
7373
AST_BRIDGING_WRAPPER_NONNULL(SourceFile)
7474
AST_BRIDGING_WRAPPER_NULLABLE(Stmt)

lib/AST/Bridging/DeclAttributeBridging.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,42 @@ static AccessLevel unbridged(BridgedAccessLevel level) {
9191
llvm_unreachable("unhandled BridgedAccessLevel");
9292
}
9393

94+
BridgedNullableDeclAttribute BridgedDeclAttributes_getAttrAfter(
95+
BridgedDeclAttributes cAttrs, BridgedNullableDeclAttribute cPriorAttr) {
96+
auto attrs = cAttrs.unbridged();
97+
auto priorAttr = cPriorAttr.unbridged();
98+
99+
auto i = priorAttr ? std::next(DeclAttributes::iterator(priorAttr))
100+
: attrs.begin();
101+
return *i;
102+
}
103+
104+
BridgedNullableABIAttr BridgedDeclAttribute_asABIAttr(
105+
BridgedDeclAttribute cAttr) {
106+
return dyn_cast<ABIAttr>(cAttr.unbridged());
107+
}
108+
109+
BridgedABIAttr BridgedABIAttr_createParsed(BridgedASTContext cContext,
110+
BridgedSourceLoc atLoc,
111+
BridgedSourceRange range,
112+
BridgedNullableDecl abiDecl) {
113+
return new (cContext.unbridged()) ABIAttr(abiDecl.unbridged(),
114+
atLoc.unbridged(),
115+
range.unbridged(),
116+
/*isInverse=*/false,
117+
/*isImplicit=*/false);
118+
}
119+
120+
BridgedABIAttr BridgedABIAttr_createImplicitInverse(BridgedASTContext cContext){
121+
return new (cContext.unbridged()) ABIAttr(nullptr,
122+
/*isInverse=*/true,
123+
/*isImplicit=*/true);
124+
}
125+
126+
void BridgedABIAttr_connectToInverse(BridgedABIAttr cAttr, BridgedDecl cOwner) {
127+
return cAttr.unbridged()->connectToInverse(cOwner.unbridged());
128+
}
129+
94130
BridgedAccessControlAttr
95131
BridgedAccessControlAttr_createParsed(BridgedASTContext cContext,
96132
BridgedSourceRange cRange,

lib/AST/Bridging/DeclBridging.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ void BridgedDecl_setAttrs(BridgedDecl decl, BridgedDeclAttributes attrs) {
123123
decl.unbridged()->getAttrs() = attrs.unbridged();
124124
}
125125

126+
// FIXME: We should do this systematically for downcasting BridgedDecl to all
127+
// subclasses.
128+
BridgedNullablePatternBindingDecl BridgedDecl_getAsPatternBindingDecl(
129+
BridgedDecl decl) {
130+
return dyn_cast<PatternBindingDecl>(decl.unbridged());
131+
}
132+
126133
BridgedAccessorDecl BridgedAccessorDecl_createParsed(
127134
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
128135
BridgedAccessorKind cKind, BridgedAbstractStorageDecl cStorage,
@@ -167,6 +174,17 @@ BridgedPatternBindingDecl BridgedPatternBindingDecl_createParsed(
167174
cBindingKeywordLoc.unbridged(), entries, declContext);
168175
}
169176

177+
SwiftInt BridgedPatternBindingDecl_getNumPatternEntries(
178+
BridgedPatternBindingDecl cPBD) {
179+
return cPBD.unbridged()->getNumPatternEntries();
180+
}
181+
182+
BridgedPattern BridgedPatternBindingDecl_getPattern(
183+
BridgedPatternBindingDecl cPBD, SwiftInt i) {
184+
ASSERT(i >= 0 && i < cPBD.unbridged()->getNumPatternEntries());
185+
return cPBD.unbridged()->getPattern(i);
186+
}
187+
170188
BridgedParamDecl BridgedParamDecl_createParsed(
171189
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
172190
BridgedSourceLoc cSpecifierLoc, BridgedIdentifier cArgName,

lib/AST/Bridging/PatternBridging.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ BridgedNullableVarDecl BridgedPattern_getSingleVar(BridgedPattern cPattern) {
3737
return cPattern.unbridged()->getSingleVar();
3838
}
3939

40+
SwiftInt BridgedPattern_unsafeFetchVarDecls(BridgedPattern cPattern,
41+
BridgedVarDecl *varDeclArrayOut,
42+
SwiftInt capacity) {
43+
SmallVector<VarDecl *, 32> vars;
44+
cPattern.unbridged()->collectVariables(vars);
45+
46+
if (varDeclArrayOut && vars.size() <= (size_t)capacity) {
47+
for (auto i : indices(vars)) {
48+
varDeclArrayOut[i] = vars[i];
49+
}
50+
}
51+
52+
return vars.size();
53+
}
54+
4055
BridgedAnyPattern BridgedAnyPattern_createParsed(BridgedASTContext cContext,
4156
BridgedSourceLoc cLoc) {
4257
return new (cContext.unbridged()) AnyPattern(cLoc.unbridged());

lib/ASTGen/Sources/ASTGen/Bridge.swift

+112
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ extension BridgedNullable {
2727

2828
extension BridgedSourceLoc: /*@retroactive*/ swiftASTGen.BridgedNullable {}
2929
extension BridgedIdentifier: /*@retroactive*/ swiftASTGen.BridgedNullable {}
30+
extension BridgedNullableDeclAttribute: /*@retroactive*/ swiftASTGen.BridgedNullable {}
31+
extension BridgedNullableDecl: /*@retroactive*/ swiftASTGen.BridgedNullable {}
3032
extension BridgedNullableExpr: /*@retroactive*/ swiftASTGen.BridgedNullable {}
3133
extension BridgedNullableStmt: /*@retroactive*/ swiftASTGen.BridgedNullable {}
3234
extension BridgedNullableTypeRepr: /*@retroactive*/ swiftASTGen.BridgedNullable {}
@@ -38,6 +40,7 @@ extension BridgedNullablePatternBindingInitializer: /*@retroactive*/ swiftASTGen
3840
extension BridgedNullableArgumentList: /*@retroactive*/ swiftASTGen.BridgedNullable {}
3941
extension BridgedNullablePatternBindingDecl: /*@retroactive*/ swiftASTGen.BridgedNullable {}
4042
extension BridgedNullableVarDecl: /*@retroactive*/ swiftASTGen.BridgedNullable {}
43+
extension BridgedNullableABIAttr: /*@retroactive*/ swiftASTGen.BridgedNullable {}
4144

4245
extension BridgedIdentifier: /*@retroactive*/ Swift.Equatable {
4346
public static func == (lhs: Self, rhs: Self) -> Bool {
@@ -71,6 +74,12 @@ extension BridgedHasNullable {
7174
extension BridgedStmt: BridgedHasNullable {
7275
typealias Nullable = BridgedNullableStmt
7376
}
77+
extension BridgedDeclAttribute: BridgedHasNullable {
78+
typealias Nullable = BridgedNullableDeclAttribute
79+
}
80+
extension BridgedDecl: BridgedHasNullable {
81+
typealias Nullable = BridgedNullableDecl
82+
}
7483
extension BridgedExpr: BridgedHasNullable {
7584
typealias Nullable = BridgedNullableExpr
7685
}
@@ -101,6 +110,9 @@ extension BridgedPatternBindingDecl: BridgedHasNullable {
101110
extension BridgedVarDecl: BridgedHasNullable {
102111
typealias Nullable = BridgedNullableVarDecl
103112
}
113+
extension BridgedABIAttr: BridgedHasNullable {
114+
typealias Nullable = BridgedNullableABIAttr
115+
}
104116

105117
public extension BridgedSourceLoc {
106118
/// Form a source location at the given absolute position in `buffer`.
@@ -253,6 +265,56 @@ extension BridgedSourceRange {
253265
}
254266
}
255267

268+
extension BridgedPatternBindingDecl {
269+
var patterns: [BridgedPattern] {
270+
return (0 ..< patternCount).map(pattern(at:))
271+
}
272+
}
273+
274+
extension BridgedPattern {
275+
private enum FetchVarDeclsError: Error {
276+
case insufficientCapacity(Int)
277+
}
278+
279+
private func fetchVarDecls(capacity: Int) throws -> [BridgedVarDecl] {
280+
return try Array(unsafeUninitializedCapacity: capacity) { buffer, initializedCount in
281+
let fullCount = self.unsafeFetchVarDecls(
282+
into: buffer.baseAddress,
283+
capacity: buffer.count
284+
)
285+
286+
guard fullCount <= buffer.count else {
287+
throw FetchVarDeclsError.insufficientCapacity(fullCount)
288+
}
289+
290+
// `unsafeFetchVarDecls` writes nothing if `fullCount` > `capacity`, so
291+
// it's correct to put this after the `throw`.
292+
initializedCount = fullCount
293+
}
294+
}
295+
296+
var varDecls: [BridgedVarDecl] {
297+
do {
298+
return try fetchVarDecls(capacity: 8) // "probably big enough" guess
299+
}
300+
catch FetchVarDeclsError.insufficientCapacity(let neededCapacity) {
301+
return try! fetchVarDecls(capacity: neededCapacity)
302+
}
303+
catch {
304+
fatalError("Unknown error \(error)")
305+
}
306+
}
307+
}
308+
309+
extension BridgedDeclAttributes {
310+
var attrs: UnfoldSequence<BridgedDeclAttribute, BridgedNullableDeclAttribute> {
311+
return sequence(state: nil) { prior in
312+
prior = self.attr(after: prior)
313+
return BridgedDeclAttribute(prior)
314+
}
315+
}
316+
}
317+
256318
/// Helper collection type that lazily concatenates two collections.
257319
struct ConcatCollection<C1: Collection, C2: Collection> where C1.Element == C2.Element {
258320
let c1: C1
@@ -294,3 +356,53 @@ extension ConcatCollection: LazyCollectionProtocol {
294356
}
295357
}
296358
}
359+
360+
enum BridgedNominalTypeOrExtensionDecl {
361+
case nominalType(BridgedNominalTypeDecl)
362+
case `extension`(BridgedExtensionDecl)
363+
364+
var asDeclContext: BridgedDeclContext {
365+
switch self {
366+
case .nominalType(let decl):
367+
return decl.asDeclContext
368+
case .extension(let decl):
369+
return decl.asDeclContext
370+
}
371+
}
372+
373+
func setParsedMembers(_ members: BridgedArrayRef) {
374+
switch self {
375+
case .nominalType(let decl):
376+
decl.setParsedMembers(members)
377+
case .extension(let decl):
378+
decl.setParsedMembers(members)
379+
}
380+
}
381+
382+
var asDecl: BridgedDecl {
383+
switch self {
384+
case .nominalType(let decl):
385+
decl.asDecl
386+
case .extension(let decl):
387+
decl.asDecl
388+
}
389+
}
390+
391+
var asNominalTypeDecl: BridgedNominalTypeDecl? {
392+
switch self {
393+
case .nominalType(let decl):
394+
decl
395+
case .extension:
396+
nil
397+
}
398+
}
399+
400+
var asExtensionDecl: BridgedExtensionDecl? {
401+
switch self {
402+
case .nominalType:
403+
nil
404+
case .extension(let decl):
405+
decl
406+
}
407+
}
408+
}

0 commit comments

Comments
 (0)