Skip to content

Commit 2e5f799

Browse files
committed
[ASTGen] Lower @abi in ASTGen
1 parent 5ad617c commit 2e5f799

File tree

11 files changed

+393
-62
lines changed

11 files changed

+393
-62
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,34 @@ SWIFT_NAME("BridgedDeclAttributes.add(self:_:)")
539539
void BridgedDeclAttributes_add(BridgedDeclAttributes *_Nonnull attrs,
540540
BridgedDeclAttribute add);
541541

542+
/// Retrieve the attribute from \p cAttrs that comes after \p cPriorAttr .
543+
/// If \c cPriorAttr is null, retrieves the first attribute. If there are no
544+
/// more attributes, returns null.
545+
SWIFT_NAME("BridgedDeclAttributes.attr(self:after:)")
546+
BridgedNullableDeclAttribute BridgedDeclAttributes_getAttrAfter(
547+
BridgedDeclAttributes cAttrs, BridgedNullableDeclAttribute cPriorAttr);
548+
542549
SWIFT_NAME("BridgedDeclAttribute.createSimple(_:kind:atLoc:nameLoc:)")
543550
BridgedDeclAttribute BridgedDeclAttribute_createSimple(
544551
BridgedASTContext cContext, BridgedDeclAttrKind cKind,
545552
BridgedSourceLoc cAtLoc, BridgedSourceLoc cNameLoc);
546553

554+
SWIFT_NAME("getter:BridgedDeclAttribute.asABIAttr(self:)")
555+
BridgedNullableABIAttr BridgedDeclAttribute_asABIAttr(
556+
BridgedDeclAttribute cAttr);
557+
558+
SWIFT_NAME("BridgedABIAttr.createParsed(_:atLoc:range:abiDecl:)")
559+
BridgedABIAttr BridgedABIAttr_createParsed(
560+
BridgedASTContext cContext, BridgedSourceLoc atLoc,
561+
BridgedSourceRange range, BridgedNullableDecl abiDecl);
562+
563+
SWIFT_NAME("BridgedABIAttr.createImplicitInverse(_:)")
564+
BridgedABIAttr BridgedABIAttr_createImplicitInverse(
565+
BridgedASTContext cContext);
566+
567+
SWIFT_NAME("BridgedABIAttr.connectToInverse(self:attachedTo:)")
568+
void BridgedABIAttr_connectToInverse(BridgedABIAttr cAttr, BridgedDecl cOwner);
569+
547570
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedAccessLevel {
548571
BridgedAccessLevelPrivate,
549572
BridgedAccessLevelFilePrivate,
@@ -797,6 +820,10 @@ BridgedDeclAttributes BridgedDecl_getAttrs(BridgedDecl decl);
797820
SWIFT_NAME("setter:BridgedDecl.attrs(self:newValue:)")
798821
void BridgedDecl_setAttrs(BridgedDecl decl, BridgedDeclAttributes attrs);
799822

823+
SWIFT_NAME("getter:BridgedDecl.asPatternBindingDecl(self:)")
824+
BridgedNullablePatternBindingDecl BridgedDecl_getAsPatternBindingDecl(
825+
BridgedDecl decl);
826+
800827
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedStaticSpelling {
801828
BridgedStaticSpellingNone,
802829
BridgedStaticSpellingStatic,
@@ -832,6 +859,14 @@ BridgedPatternBindingDecl BridgedPatternBindingDecl_createParsed(
832859
BridgedSourceLoc cBindingKeywordLoc, BridgedArrayRef cBindingEntries,
833860
bool isStatic, bool isLet);
834861

862+
SWIFT_NAME("getter:BridgedPatternBindingDecl.patternCount(self:)")
863+
SwiftInt BridgedPatternBindingDecl_getNumPatternEntries(
864+
BridgedPatternBindingDecl cPBD);
865+
866+
SWIFT_NAME("BridgedPatternBindingDecl.pattern(self:at:)")
867+
BridgedPattern BridgedPatternBindingDecl_getPattern(
868+
BridgedPatternBindingDecl cPBD, SwiftInt i);
869+
835870
SWIFT_NAME("BridgedParamDecl.createParsed(_:declContext:specifierLoc:argName:"
836871
"argNameLoc:paramName:paramNameLoc:type:defaultValue:)")
837872
BridgedParamDecl BridgedParamDecl_createParsed(
@@ -1903,6 +1938,21 @@ void BridgedTypeRepr_dump(BridgedTypeRepr type);
19031938
SWIFT_NAME("getter:BridgedPattern.singleVar(self:)")
19041939
BridgedNullableVarDecl BridgedPattern_getSingleVar(BridgedPattern cPattern);
19051940

1941+
/// Iterates over the pattern, collecting and returning all of the BridgedVarDecls
1942+
/// referenced by its subpatterns.
1943+
///
1944+
/// The exact semantics of this are as follows:
1945+
///
1946+
/// \li This method always returns the current count of BridgedVarDecls in the
1947+
/// pattern.
1948+
/// \li If \p capacity is greater than or equal to the current count \em and
1949+
/// \p varDeclArrayOut is non-null, this method also writes the
1950+
/// BridgedVarDecls into the buffer starting at \p varDeclArrayOut .
1951+
SWIFT_NAME("BridgedPattern.unsafeFetchVarDecls(self:into:capacity:)")
1952+
SwiftInt BridgedPattern_unsafeFetchVarDecls(
1953+
BridgedPattern cPattern, BridgedVarDecl * _Nullable varDeclArrayOut,
1954+
SwiftInt capacity);
1955+
19061956
SWIFT_NAME("BridgedAnyPattern.createParsed(_:loc:)")
19071957
BridgedAnyPattern BridgedAnyPattern_createParsed(BridgedASTContext cContext,
19081958
BridgedSourceLoc cLoc);

include/swift/AST/ASTBridgingWrappers.def

Lines changed: 2 additions & 2 deletions
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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@ static AccessLevel unbridged(BridgedAccessLevel level) {
9090
llvm_unreachable("unhandled BridgedAccessLevel");
9191
}
9292

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

lib/AST/Bridging/DeclBridging.cpp

Lines changed: 18 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 112 additions & 0 deletions
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 {}
@@ -37,6 +39,7 @@ extension BridgedNullableParameterList: /*@retroactive*/ swiftASTGen.BridgedNull
3739
extension BridgedNullablePatternBindingInitializer: /*@retroactive*/ swiftASTGen.BridgedNullable {}
3840
extension BridgedNullablePatternBindingDecl: /*@retroactive*/ swiftASTGen.BridgedNullable {}
3941
extension BridgedNullableVarDecl: /*@retroactive*/ swiftASTGen.BridgedNullable {}
42+
extension BridgedNullableABIAttr: /*@retroactive*/ swiftASTGen.BridgedNullable {}
4043

4144
extension BridgedIdentifier: /*@retroactive*/ Swift.Equatable {
4245
public static func == (lhs: Self, rhs: Self) -> Bool {
@@ -70,6 +73,12 @@ extension BridgedHasNullable {
7073
extension BridgedStmt: BridgedHasNullable {
7174
typealias Nullable = BridgedNullableStmt
7275
}
76+
extension BridgedDeclAttribute: BridgedHasNullable {
77+
typealias Nullable = BridgedNullableDeclAttribute
78+
}
79+
extension BridgedDecl: BridgedHasNullable {
80+
typealias Nullable = BridgedNullableDecl
81+
}
7382
extension BridgedExpr: BridgedHasNullable {
7483
typealias Nullable = BridgedNullableExpr
7584
}
@@ -97,6 +106,9 @@ extension BridgedPatternBindingDecl: BridgedHasNullable {
97106
extension BridgedVarDecl: BridgedHasNullable {
98107
typealias Nullable = BridgedNullableVarDecl
99108
}
109+
extension BridgedABIAttr: BridgedHasNullable {
110+
typealias Nullable = BridgedNullableABIAttr
111+
}
100112

101113
public extension BridgedSourceLoc {
102114
/// Form a source location at the given absolute position in `buffer`.
@@ -249,6 +261,56 @@ extension BridgedSourceRange {
249261
}
250262
}
251263

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

0 commit comments

Comments
 (0)