Skip to content

Commit c46eb58

Browse files
authored
Merge pull request #70138 from rintaro/astgen-compoundname
[ASTGen] Generate compound name expression
2 parents b6c7ff4 + 4796ea4 commit c46eb58

File tree

6 files changed

+273
-24
lines changed

6 files changed

+273
-24
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class DiagnosticArgument;
3434
class DiagnosticEngine;
3535
}
3636

37+
class BridgedASTContext;
38+
3739
//===----------------------------------------------------------------------===//
3840
// MARK: Identifier
3941
//===----------------------------------------------------------------------===//
@@ -71,6 +73,82 @@ struct BridgedIdentifierAndSourceLoc {
7173
BridgedSourceLoc NameLoc;
7274
};
7375

76+
class BridgedDeclBaseName {
77+
BridgedIdentifier Ident;
78+
79+
public:
80+
#ifdef USED_IN_CPP_SOURCE
81+
BridgedDeclBaseName(swift::DeclBaseName baseName) : Ident(baseName.Ident) {}
82+
83+
swift::DeclBaseName unbridged() const {
84+
return swift::DeclBaseName(Ident.unbridged());
85+
}
86+
#endif
87+
};
88+
89+
SWIFT_NAME("BridgedDeclBaseName.createConstructor()")
90+
BridgedDeclBaseName BridgedDeclBaseName_createConstructor();
91+
92+
SWIFT_NAME("BridgedDeclBaseName.createDestructor()")
93+
BridgedDeclBaseName BridgedDeclBaseName_createDestructor();
94+
95+
SWIFT_NAME("BridgedDeclBaseName.createSubscript()")
96+
BridgedDeclBaseName BridgedDeclBaseName_createSubscript();
97+
98+
SWIFT_NAME("BridgedDeclBaseName.createIdentifier(_:)")
99+
BridgedDeclBaseName
100+
BridgedDeclBaseName_createIdentifier(BridgedIdentifier identifier);
101+
102+
class BridgedDeclNameRef {
103+
void *_Nonnull opaque;
104+
105+
public:
106+
#ifdef USED_IN_CPP_SOURCE
107+
BridgedDeclNameRef(swift::DeclNameRef name) : opaque(name.getOpaqueValue()) {}
108+
109+
swift::DeclNameRef unbridged() const {
110+
return swift::DeclNameRef::getFromOpaqueValue(opaque);
111+
}
112+
#endif
113+
};
114+
115+
SWIFT_NAME("BridgedDeclNameRef.createParsed(_:baseName:argumentLabels:)")
116+
BridgedDeclNameRef
117+
BridgedDeclNameRef_createParsed(BridgedASTContext cContext,
118+
BridgedDeclBaseName cBaseName,
119+
BridgedArrayRef cLabels);
120+
121+
SWIFT_NAME("BridgedDeclNameRef.createParsed(_:)")
122+
BridgedDeclNameRef
123+
BridgedDeclNameRef_createParsed(BridgedDeclBaseName cBaseName);
124+
125+
class BridgedDeclNameLoc {
126+
const void *_Nonnull LocationInfo;
127+
size_t NumArgumentLabels;
128+
129+
public:
130+
#ifdef USED_IN_CPP_SOURCE
131+
BridgedDeclNameLoc(swift::DeclNameLoc loc)
132+
: LocationInfo(loc.LocationInfo),
133+
NumArgumentLabels(loc.NumArgumentLabels) {}
134+
135+
swift::DeclNameLoc unbridged() const {
136+
return swift::DeclNameLoc(LocationInfo, NumArgumentLabels);
137+
}
138+
#endif
139+
};
140+
141+
SWIFT_NAME("BridgedDeclNameLoc.createParsed(_:baseNameLoc:lParenLoc:"
142+
"argumentLabelLocs:rParenLoc:)")
143+
BridgedDeclNameLoc BridgedDeclNameLoc_createParsed(
144+
BridgedASTContext cContext, BridgedSourceLoc cBaseNameLoc,
145+
BridgedSourceLoc cLParenLoc, BridgedArrayRef cLabelLocs,
146+
BridgedSourceLoc cRParenLoc);
147+
148+
SWIFT_NAME("BridgedDeclNameLoc.createParsed(_:)")
149+
BridgedDeclNameLoc
150+
BridgedDeclNameLoc_createParsed(BridgedSourceLoc cBaseNameLoc);
151+
74152
//===----------------------------------------------------------------------===//
75153
// MARK: ASTContext
76154
//===----------------------------------------------------------------------===//
@@ -577,9 +655,11 @@ BridgedClosureExpr_createParsed(BridgedASTContext cContext,
577655
BridgedDeclContext cDeclContext,
578656
BridgedBraceStmt body);
579657

580-
SWIFT_NAME("BridgedUnresolvedDeclRefExpr.createParsed(_:base:loc:)")
581-
BridgedUnresolvedDeclRefExpr BridgedUnresolvedDeclRefExpr_createParsed(
582-
BridgedASTContext cContext, BridgedIdentifier base, BridgedSourceLoc cLoc);
658+
SWIFT_NAME("BridgedUnresolvedDeclRefExpr.createParsed(_:name:loc:)")
659+
BridgedUnresolvedDeclRefExpr
660+
BridgedUnresolvedDeclRefExpr_createParsed(BridgedASTContext cContext,
661+
BridgedDeclNameRef cName,
662+
BridgedDeclNameLoc cLoc);
583663

584664
SWIFT_NAME("BridgedSingleValueStmtExpr.createWithWrappedBranches(_:stmt:"
585665
"declContext:mustBeExpr:)")
@@ -630,7 +710,12 @@ BridgedArrayExpr BridgedArrayExpr_createParsed(BridgedASTContext cContext,
630710
SWIFT_NAME("BridgedUnresolvedDotExpr.createParsed(_:base:dotLoc:name:nameLoc:)")
631711
BridgedUnresolvedDotExpr BridgedUnresolvedDotExpr_createParsed(
632712
BridgedASTContext cContext, BridgedExpr base, BridgedSourceLoc cDotLoc,
633-
BridgedIdentifier name, BridgedSourceLoc cNameLoc);
713+
BridgedDeclNameRef cName, BridgedDeclNameLoc cNameLoc);
714+
715+
SWIFT_NAME("BridgedUnresolvedMemberExpr.createParsed(_:dotLoc:name:nameLoc:)")
716+
BridgedUnresolvedMemberExpr BridgedUnresolvedMemberExpr_createParsed(
717+
BridgedASTContext cContext, BridgedSourceLoc cDotLoc,
718+
BridgedDeclNameRef cName, BridgedDeclNameLoc cNameLoc);
634719

635720
SWIFT_NAME("BridgedExpr.dump(self:)")
636721
void BridgedExpr_dump(BridgedExpr expr);

include/swift/AST/DeclNameLoc.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@
2020
#include "swift/Basic/LLVM.h"
2121
#include "swift/Basic/SourceLoc.h"
2222

23+
class BridgedDeclNameLoc;
24+
2325
namespace swift {
2426

2527
class ASTContext;
2628

2729
/// Source location information for a declaration name (\c DeclName)
2830
/// written in the source.
2931
class DeclNameLoc {
32+
friend class ::BridgedDeclNameLoc;
33+
3034
/// Source location information.
3135
///
3236
/// If \c NumArgumentLabels == 0, this is the SourceLoc for the base name.
@@ -56,14 +60,16 @@ class DeclNameLoc {
5660
return reinterpret_cast<SourceLoc const *>(LocationInfo);
5761
}
5862

63+
DeclNameLoc(const void *LocationInfo, unsigned NumArgumentLabels)
64+
: LocationInfo(LocationInfo), NumArgumentLabels(NumArgumentLabels) {}
65+
5966
public:
6067
/// Create an invalid declaration name location.
61-
DeclNameLoc() : LocationInfo(0), NumArgumentLabels(0) { }
68+
DeclNameLoc() : DeclNameLoc(nullptr, 0) {}
6269

6370
/// Create declaration name location information for a base name.
6471
explicit DeclNameLoc(SourceLoc baseNameLoc)
65-
: LocationInfo(baseNameLoc.getOpaquePointerValue()),
66-
NumArgumentLabels(0) { }
72+
: DeclNameLoc(baseNameLoc.getOpaquePointerValue(), 0) {}
6773

6874
/// Create declaration name location information for a compound
6975
/// name.

include/swift/AST/Identifier.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,15 @@ namespace llvm {
260260

261261
} // end namespace llvm
262262

263+
class BridgedDeclBaseName;
264+
263265
namespace swift {
264266

265267
/// Wrapper that may either be an Identifier or a special name
266268
/// (e.g. for subscripts)
267269
class DeclBaseName {
270+
friend class ::BridgedDeclBaseName;
271+
268272
public:
269273
enum class Kind: uint8_t {
270274
Normal,

lib/AST/ASTBridging.cpp

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,63 @@ static TypeAttrKind unbridged(BridgedTypeAttrKind kind) {
4747
}
4848
}
4949

50+
//===----------------------------------------------------------------------===//
51+
// MARK: Identifier
52+
//===----------------------------------------------------------------------===//
53+
54+
BridgedDeclBaseName BridgedDeclBaseName_createConstructor() {
55+
return DeclBaseName::createConstructor();
56+
}
57+
58+
BridgedDeclBaseName BridgedDeclBaseName_createDestructor() {
59+
return DeclBaseName::createDestructor();
60+
}
61+
62+
BridgedDeclBaseName BridgedDeclBaseName_createSubscript() {
63+
return DeclBaseName::createSubscript();
64+
}
65+
66+
BridgedDeclBaseName
67+
BridgedDeclBaseName_createIdentifier(BridgedIdentifier identifier) {
68+
return DeclBaseName(identifier.unbridged());
69+
}
70+
71+
BridgedDeclNameRef
72+
BridgedDeclNameRef_createParsed(BridgedASTContext cContext,
73+
BridgedDeclBaseName cBaseName,
74+
BridgedArrayRef cLabels) {
75+
ASTContext &context = cContext.unbridged();
76+
SmallVector<Identifier, 4> labels;
77+
for (auto &cLabel : cLabels.unbridged<BridgedIdentifier>()) {
78+
labels.push_back(cLabel.unbridged());
79+
}
80+
return DeclNameRef(DeclName(context, cBaseName.unbridged(), labels));
81+
}
82+
83+
BridgedDeclNameRef
84+
BridgedDeclNameRef_createParsed(BridgedDeclBaseName cBaseName) {
85+
return DeclNameRef(cBaseName.unbridged());
86+
}
87+
88+
BridgedDeclNameLoc BridgedDeclNameLoc_createParsed(
89+
BridgedASTContext cContext, BridgedSourceLoc cBaseNameLoc,
90+
BridgedSourceLoc cLParenLoc, BridgedArrayRef cLabelLocs,
91+
BridgedSourceLoc cRParenLoc) {
92+
93+
ASTContext &context = cContext.unbridged();
94+
SmallVector<SourceLoc, 4> labelLocs;
95+
for (auto &cLabelLoc : cLabelLocs.unbridged<BridgedSourceLoc>())
96+
labelLocs.push_back(cLabelLoc.unbridged());
97+
98+
return DeclNameLoc(context, cBaseNameLoc.unbridged(), cLParenLoc.unbridged(),
99+
labelLocs, cRParenLoc.unbridged());
100+
}
101+
102+
BridgedDeclNameLoc
103+
BridgedDeclNameLoc_createParsed(BridgedSourceLoc cBaseNameLoc) {
104+
return DeclNameLoc(cBaseNameLoc.unbridged());
105+
}
106+
50107
//===----------------------------------------------------------------------===//
51108
// MARK: ASTContext
52109
//===----------------------------------------------------------------------===//
@@ -871,12 +928,13 @@ BridgedCallExpr BridgedCallExpr_createParsed(BridgedASTContext cContext,
871928
/*implicit*/ false);
872929
}
873930

874-
BridgedUnresolvedDeclRefExpr BridgedUnresolvedDeclRefExpr_createParsed(
875-
BridgedASTContext cContext, BridgedIdentifier base, BridgedSourceLoc cLoc) {
931+
BridgedUnresolvedDeclRefExpr
932+
BridgedUnresolvedDeclRefExpr_createParsed(BridgedASTContext cContext,
933+
BridgedDeclNameRef cName,
934+
BridgedDeclNameLoc cLoc) {
876935
ASTContext &context = cContext.unbridged();
877-
auto name = DeclNameRef{base.unbridged()};
878-
return new (context) UnresolvedDeclRefExpr(name, DeclRefKind::Ordinary,
879-
DeclNameLoc{cLoc.unbridged()});
936+
return new (context) UnresolvedDeclRefExpr(
937+
cName.unbridged(), DeclRefKind::Ordinary, cLoc.unbridged());
880938
}
881939

882940
BridgedStringLiteralExpr
@@ -930,13 +988,28 @@ BridgedSingleValueStmtExpr BridgedSingleValueStmtExpr_createWithWrappedBranches(
930988
context, S.unbridged(), declContext, mustBeExpr);
931989
}
932990

991+
BridgedTypeExpr BridgedTypeExpr_createParsed(BridgedASTContext cContext,
992+
BridgedTypeRepr cType) {
993+
ASTContext &context = cContext.unbridged();
994+
return new (context) TypeExpr(cType.unbridged());
995+
}
996+
933997
BridgedUnresolvedDotExpr BridgedUnresolvedDotExpr_createParsed(
934998
BridgedASTContext cContext, BridgedExpr base, BridgedSourceLoc cDotLoc,
935-
BridgedIdentifier name, BridgedSourceLoc cNameLoc) {
999+
BridgedDeclNameRef cName, BridgedDeclNameLoc cNameLoc) {
9361000
ASTContext &context = cContext.unbridged();
9371001
return new (context) UnresolvedDotExpr(
938-
base.unbridged(), cDotLoc.unbridged(), DeclNameRef(name.unbridged()),
939-
DeclNameLoc(cNameLoc.unbridged()), false);
1002+
base.unbridged(), cDotLoc.unbridged(), cName.unbridged(),
1003+
cNameLoc.unbridged(), /*isImplicit=*/false);
1004+
}
1005+
1006+
BridgedUnresolvedMemberExpr BridgedUnresolvedMemberExpr_createParsed(
1007+
BridgedASTContext cContext, BridgedSourceLoc cDotLoc,
1008+
BridgedDeclNameRef cName, BridgedDeclNameLoc cNameLoc) {
1009+
ASTContext &context = cContext.unbridged();
1010+
return new (context)
1011+
UnresolvedMemberExpr(cDotLoc.unbridged(), cNameLoc.unbridged(),
1012+
cName.unbridged(), /*isImplicit=*/false);
9401013
}
9411014

9421015
//===----------------------------------------------------------------------===//

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,13 @@ extension ASTGenVisitor {
147147
case .macroExpansionExpr:
148148
break
149149
case .memberAccessExpr(let node):
150-
return self.generate(memberAccessExpr: node).asExpr
150+
return self.generate(memberAccessExpr: node)
151151
case .missingExpr:
152152
break
153153
case .nilLiteralExpr(let node):
154154
return self.generate(nilLiteralExpr: node).asExpr
155155
case .optionalChainingExpr:
156+
// Need special care to wrap the entire postfix chain with OptionalEvaluationExpr.
156157
break
157158
case .packElementExpr:
158159
break
@@ -249,18 +250,77 @@ extension ASTGenVisitor {
249250
return .createParsed(self.ctx, fn: callee, args: argumentTuple)
250251
}
251252

252-
public func generate(declReferenceExpr node: DeclReferenceExprSyntax) -> BridgedUnresolvedDeclRefExpr {
253-
let (name, nameLoc) = node.baseName.bridgedIdentifierAndSourceLoc(in: self)
253+
private func createDeclNameRef(declReferenceExpr node: DeclReferenceExprSyntax) -> (name: BridgedDeclNameRef, loc: BridgedDeclNameLoc) {
254+
let baseName: BridgedDeclBaseName
255+
switch node.baseName.tokenKind {
256+
case .keyword(.`init`):
257+
baseName = .createConstructor()
258+
case .keyword(.deinit):
259+
baseName = .createDestructor()
260+
case .keyword(.subscript):
261+
baseName = .createSubscript()
262+
default:
263+
baseName = .createIdentifier(node.baseName.bridgedIdentifier(in: self))
264+
}
265+
let baseNameLoc = node.baseName.bridgedSourceLoc(in: self)
254266

255-
return .createParsed(self.ctx, base: name, loc: nameLoc)
267+
if let argumentClause = node.argumentNames {
268+
let labels = argumentClause.arguments.lazy.map {
269+
$0.name.bridgedIdentifier(in: self)
270+
}
271+
let labelLocs = argumentClause.arguments.lazy.map {
272+
$0.name.bridgedSourceLoc(in: self)
273+
}
274+
return (
275+
name: .createParsed(
276+
self.ctx,
277+
baseName: baseName,
278+
argumentLabels: labels.bridgedArray(in: self)
279+
),
280+
loc: .createParsed(
281+
self.ctx,
282+
baseNameLoc: baseNameLoc,
283+
lParenLoc: argumentClause.leftParen.bridgedSourceLoc(in: self),
284+
argumentLabelLocs: labelLocs.bridgedArray(in: self),
285+
rParenLoc: argumentClause.rightParen.bridgedSourceLoc(in: self)
286+
)
287+
)
288+
} else {
289+
return (
290+
name: .createParsed(baseName),
291+
loc: .createParsed(baseNameLoc)
292+
)
293+
}
256294
}
257295

258-
public func generate(memberAccessExpr node: MemberAccessExprSyntax) -> BridgedUnresolvedDotExpr {
259-
let loc = node.bridgedSourceLoc(in: self)
260-
let base = generate(expr: node.base!)
261-
let name = node.declName.baseName.bridgedIdentifier(in: self)
296+
public func generate(declReferenceExpr node: DeclReferenceExprSyntax) -> BridgedUnresolvedDeclRefExpr {
297+
let nameAndLoc = createDeclNameRef(declReferenceExpr: node)
298+
return .createParsed(
299+
self.ctx,
300+
name: nameAndLoc.name,
301+
loc: nameAndLoc.loc
302+
)
303+
}
304+
305+
public func generate(memberAccessExpr node: MemberAccessExprSyntax) -> BridgedExpr {
306+
let nameAndLoc = createDeclNameRef(declReferenceExpr: node.declName)
262307

263-
return .createParsed(ctx, base: base, dotLoc: loc, name: name, nameLoc: loc)
308+
if let base = node.base {
309+
return BridgedUnresolvedDotExpr.createParsed(
310+
self.ctx,
311+
base: self.generate(expr: base),
312+
dotLoc: node.period.bridgedSourceLoc(in: self),
313+
name: nameAndLoc.name,
314+
nameLoc: nameAndLoc.loc
315+
).asExpr
316+
} else {
317+
return BridgedUnresolvedMemberExpr.createParsed(
318+
self.ctx,
319+
dotLoc: node.period.bridgedSourceLoc(in: self),
320+
name: nameAndLoc.name,
321+
nameLoc: nameAndLoc.loc
322+
).asExpr
323+
}
264324
}
265325

266326
public func generate(ifExpr node: IfExprSyntax) -> BridgedSingleValueStmtExpr {

0 commit comments

Comments
 (0)