Skip to content

Commit 8ca707b

Browse files
authored
Merge pull request swiftlang#69829 from rintaro/astgen-node-syntaxenum
[ASTGen] Utilize base specific SyntaxEnum and start Pattern generation
2 parents 0eded5f + 215abe6 commit 8ca707b

File tree

14 files changed

+450
-206
lines changed

14 files changed

+450
-206
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ namespace swift {
177177
#define ABSTRACT_TYPEREPR(Id, Parent) TYPEREPR(Id, Parent)
178178
#include "swift/AST/TypeReprNodes.def"
179179

180+
// Declare `.asPattern` on each BridgedXXXPattern type, which upcasts a wrapper
181+
// for a Pattern subclass to a BridgedPattern.
182+
#define PATTERN(Id, Parent) \
183+
SWIFT_NAME("getter:Bridged" #Id "Pattern.asPattern(self:)") \
184+
BridgedPattern Bridged##Id##Pattern_asPattern(Bridged##Id##Pattern pattern);
185+
#include "swift/AST/PatternNodes.def"
186+
180187
//===----------------------------------------------------------------------===//
181188
// MARK: Diagnostic Engine
182189
//===----------------------------------------------------------------------===//
@@ -289,10 +296,10 @@ void BridgedDiagnostic_finish(BridgedDiagnostic cDiag);
289296

290297
SWIFT_NAME(
291298
"BridgedPatternBindingDecl.createParsed(_:declContext:bindingKeywordLoc:"
292-
"nameExpr:initializer:isStatic:isLet:)")
299+
"pattern:initializer:isStatic:isLet:)")
293300
BridgedPatternBindingDecl BridgedPatternBindingDecl_createParsed(
294301
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
295-
BridgedSourceLoc cBindingKeywordLoc, BridgedExpr opaqueNameExpr,
302+
BridgedSourceLoc cBindingKeywordLoc, BridgedPattern pattern,
296303
BridgedExpr opaqueInitExpr, bool isStatic, bool isLet);
297304

298305
SWIFT_NAME("BridgedParamDecl.createParsed(_:declContext:specifierLoc:firstName:"
@@ -860,6 +867,16 @@ BridgedVarargTypeRepr_createParsed(BridgedASTContext cContext,
860867
SWIFT_NAME("BridgedTypeRepr.dump(self:)")
861868
void BridgedTypeRepr_dump(BridgedTypeRepr type);
862869

870+
//===----------------------------------------------------------------------===//
871+
// MARK: Patterns
872+
//===----------------------------------------------------------------------===//
873+
874+
SWIFT_NAME("BridgedNamedPattern.createParsed(_:declContext:name:loc:)")
875+
BridgedNamedPattern
876+
BridgedNamedPattern_createParsed(BridgedASTContext astContext,
877+
BridgedDeclContext declContext,
878+
BridgedIdentifier name, BridgedSourceLoc cLoc);
879+
863880
//===----------------------------------------------------------------------===//
864881
// MARK: Misc
865882
//===----------------------------------------------------------------------===//

include/swift/AST/ASTBridgingWrappers.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
#define ABSTRACT_TYPEREPR(Id, Parent) TYPEREPR(Id, Parent)
5656
#include "swift/AST/TypeReprNodes.def"
5757

58+
#ifndef PATTERN
59+
#define PATTERN(Id, Parent) AST_BRIDGING_WRAPPER_NONNULL(Id##Pattern)
60+
#endif
61+
#include "swift/AST/PatternNodes.def"
62+
5863
// Some of the base classes need to be nullable to allow them to be used as
5964
// optional parameters.
6065
AST_BRIDGING_WRAPPER_NONNULL(Decl)

include/swift/AST/Decl.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5838,13 +5838,6 @@ class VarDecl : public AbstractStorageDecl {
58385838
SourceLoc nameLoc, Identifier name, DeclContext *dc,
58395839
StorageIsMutable_t supportsMutation);
58405840

5841-
protected:
5842-
// Only \c ParamDecl::setSpecifier is allowed to flip this - and it's also
5843-
// on the way out of that business.
5844-
void setIntroducer(Introducer value) {
5845-
Bits.VarDecl.Introducer = uint8_t(value);
5846-
}
5847-
58485841
public:
58495842
VarDecl(bool isStatic, Introducer introducer,
58505843
SourceLoc nameLoc, Identifier name, DeclContext *dc)
@@ -6064,6 +6057,12 @@ class VarDecl : public AbstractStorageDecl {
60646057
/// upon any kind of access?
60656058
bool isOrdinaryStoredProperty() const;
60666059

6060+
/// Set the introducer kind.
6061+
/// Note: do not call this after type checking begun.
6062+
void setIntroducer(Introducer value) {
6063+
Bits.VarDecl.Introducer = uint8_t(value);
6064+
}
6065+
60676066
Introducer getIntroducer() const {
60686067
return Introducer(Bits.VarDecl.Introducer);
60696068
}

lib/AST/ASTBridging.cpp

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ bool BridgedASTContext_langOptsHasFeature(BridgedASTContext cContext,
117117
#define ABSTRACT_TYPEREPR(Id, Parent) TYPEREPR(Id, Parent)
118118
#include "swift/AST/TypeReprNodes.def"
119119

120+
// Define `.asPattern` on each BridgedXXXPattern type.
121+
#define PATTERN(Id, Parent) \
122+
BridgedPattern Bridged##Id##Pattern_asPattern( \
123+
Bridged##Id##Pattern pattern) { \
124+
return static_cast<Pattern *>(pattern.unbridged()); \
125+
}
126+
#include "swift/AST/PatternNodes.def"
127+
120128
//===----------------------------------------------------------------------===//
121129
// MARK: Diagnostics
122130
//===----------------------------------------------------------------------===//
@@ -274,18 +282,25 @@ void BridgedDiagnostic_finish(BridgedDiagnostic cDiag) {
274282

275283
BridgedPatternBindingDecl BridgedPatternBindingDecl_createParsed(
276284
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
277-
BridgedSourceLoc cBindingKeywordLoc, BridgedExpr nameExpr,
285+
BridgedSourceLoc cBindingKeywordLoc, BridgedPattern cPattern,
278286
BridgedExpr initExpr, bool isStatic, bool isLet) {
279287
ASTContext &context = cContext.unbridged();
280288
DeclContext *declContext = cDeclContext.unbridged();
281289

282-
auto *name = cast<UnresolvedDeclRefExpr>(nameExpr.unbridged());
283-
auto *varDecl = new (context) VarDecl(
284-
isStatic, isLet ? VarDecl::Introducer::Let : VarDecl::Introducer::Var,
285-
name->getLoc(), name->getName().getBaseIdentifier(), declContext);
286-
auto *pattern = new (context) NamedPattern(varDecl);
290+
Pattern *pattern = cPattern.unbridged();
291+
292+
VarDecl::Introducer introducer =
293+
isLet ? VarDecl::Introducer::Let : VarDecl::Introducer::Var;
294+
295+
// Configure all vars.
296+
pattern->forEachVariable([&](VarDecl *VD) {
297+
VD->setStatic(isStatic);
298+
VD->setIntroducer(introducer);
299+
});
300+
287301
return PatternBindingDecl::create(context,
288-
/*StaticLoc=*/SourceLoc(), // FIXME
302+
/*StaticLoc=*/SourceLoc(),
303+
// FIXME: 'class' spelling kind.
289304
isStatic ? StaticSpellingKind::KeywordStatic
290305
: StaticSpellingKind::None,
291306
cBindingKeywordLoc.unbridged(), pattern,
@@ -1255,6 +1270,26 @@ BridgedExistentialTypeRepr_createParsed(BridgedASTContext cContext,
12551270
ExistentialTypeRepr(cAnyLoc.unbridged(), baseTy.unbridged());
12561271
}
12571272

1273+
//===----------------------------------------------------------------------===//
1274+
// MARK: Patterns
1275+
//===----------------------------------------------------------------------===//
1276+
1277+
BridgedNamedPattern
1278+
BridgedNamedPattern_createParsed(BridgedASTContext cContext,
1279+
BridgedDeclContext cDeclContext,
1280+
BridgedIdentifier name, BridgedSourceLoc loc) {
1281+
auto &context = cContext.unbridged();
1282+
auto *dc = cDeclContext.unbridged();
1283+
1284+
// Note 'isStatic' and the introducer value are temporary.
1285+
// The outer context should set the correct values.
1286+
auto *varDecl = new (context) VarDecl(
1287+
/*isStatic=*/false, VarDecl::Introducer::Let, loc.unbridged(),
1288+
name.unbridged(), dc);
1289+
auto *pattern = new (context) NamedPattern(varDecl);
1290+
return pattern;
1291+
}
1292+
12581293
//===----------------------------------------------------------------------===//
12591294
// MARK: Misc
12601295
//===----------------------------------------------------------------------===//

lib/ASTGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_pure_swift_host_library(swiftASTGen STATIC
1717
Sources/ASTGen/Literals.swift
1818
Sources/ASTGen/Macros.swift
1919
Sources/ASTGen/ParameterClause.swift
20+
Sources/ASTGen/Patterns.swift
2021
Sources/ASTGen/PluginHost.swift
2122
Sources/ASTGen/SourceFile.swift
2223
Sources/ASTGen/SourceManager.swift

0 commit comments

Comments
 (0)