Skip to content

Commit e9ac984

Browse files
committed
[ASTGen] Lower @abi in ASTGen
1 parent 173ae91 commit e9ac984

File tree

11 files changed

+394
-62
lines changed

11 files changed

+394
-62
lines changed

include/swift/AST/ASTBridging.h

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

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

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

825+
SWIFT_NAME("getter:BridgedDecl.asPatternBindingDecl(self:)")
826+
BridgedNullablePatternBindingDecl BridgedDecl_getAsPatternBindingDecl(
827+
BridgedDecl decl);
828+
802829
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedStaticSpelling {
803830
BridgedStaticSpellingNone,
804831
BridgedStaticSpellingStatic,
@@ -834,6 +861,14 @@ BridgedPatternBindingDecl BridgedPatternBindingDecl_createParsed(
834861
BridgedSourceLoc cBindingKeywordLoc, BridgedArrayRef cBindingEntries,
835862
bool isStatic, bool isLet);
836863

864+
SWIFT_NAME("getter:BridgedPatternBindingDecl.patternCount(self:)")
865+
SwiftInt BridgedPatternBindingDecl_getNumPatternEntries(
866+
BridgedPatternBindingDecl cPBD);
867+
868+
SWIFT_NAME("BridgedPatternBindingDecl.pattern(self:at:)")
869+
BridgedPattern BridgedPatternBindingDecl_getPattern(
870+
BridgedPatternBindingDecl cPBD, SwiftInt i);
871+
837872
SWIFT_NAME("BridgedParamDecl.createParsed(_:declContext:specifierLoc:argName:"
838873
"argNameLoc:paramName:paramNameLoc:type:defaultValue:)")
839874
BridgedParamDecl BridgedParamDecl_createParsed(
@@ -1921,6 +1956,21 @@ void BridgedTypeRepr_dump(BridgedTypeRepr type);
19211956
SWIFT_NAME("getter:BridgedPattern.singleVar(self:)")
19221957
BridgedNullableVarDecl BridgedPattern_getSingleVar(BridgedPattern cPattern);
19231958

1959+
/// Iterates over the pattern, collecting and returning all of the BridgedVarDecls
1960+
/// referenced by its subpatterns.
1961+
///
1962+
/// The exact semantics of this are as follows:
1963+
///
1964+
/// \li This method always returns the current count of BridgedVarDecls in the
1965+
/// pattern.
1966+
/// \li If \p capacity is greater than or equal to the current count \em and
1967+
/// \p varDeclArrayOut is non-null, this method also writes the
1968+
/// BridgedVarDecls into the buffer starting at \p varDeclArrayOut .
1969+
SWIFT_NAME("BridgedPattern.unsafeFetchVarDecls(self:into:capacity:)")
1970+
SwiftInt BridgedPattern_unsafeFetchVarDecls(
1971+
BridgedPattern cPattern, BridgedVarDecl * _Nullable varDeclArrayOut,
1972+
SwiftInt capacity);
1973+
19241974
SWIFT_NAME("BridgedAnyPattern.createParsed(_:loc:)")
19251975
BridgedAnyPattern BridgedAnyPattern_createParsed(BridgedASTContext cContext,
19261976
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 {}
@@ -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)