Skip to content

Commit 215abe6

Browse files
committed
[ASTGen] Start generating Patterns
1 parent 038597c commit 215abe6

File tree

8 files changed

+104
-34
lines changed

8 files changed

+104
-34
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/Sources/ASTGen/ASTGen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import ASTBridging
1414
import BasicBridging
1515
import ParseBridging
1616
// Needed to use BumpPtrAllocator
17-
@_spi(BumpPtrAllocator) @_spi(ExperimentalLanguageFeatures) import SwiftSyntax
17+
@_spi(BumpPtrAllocator) import SwiftSyntax
1818

1919
import struct SwiftDiagnostics.Diagnostic
2020

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ extension ASTGenVisitor {
305305
self.ctx,
306306
declContext: self.declContext,
307307
bindingKeywordLoc: node.bindingSpecifier.bridgedSourceLoc(in: self),
308-
nameExpr: pattern.castToExpr,
308+
pattern: pattern,
309309
initializer: initializer,
310310
isStatic: isStatic,
311311
isLet: isLet

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,6 @@ extension ASTGenVisitor {
255255
return .createParsed(self.ctx, base: name, loc: nameLoc)
256256
}
257257

258-
public func generate(_ node: IdentifierPatternSyntax) -> BridgedUnresolvedDeclRefExpr {
259-
let (name, nameLoc) = node.identifier.bridgedIdentifierAndSourceLoc(in: self)
260-
261-
return .createParsed(self.ctx, base: name, loc: nameLoc)
262-
}
263-
264258
public func generate(_ node: MemberAccessExprSyntax) -> BridgedUnresolvedDotExpr {
265259
let loc = node.bridgedSourceLoc(in: self)
266260
let base = generate(expr: node.base!)

lib/ASTGen/Sources/ASTGen/Patterns.swift

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
1+
//===--- Patterns.swift ---------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
112

213
import ASTBridging
314
import BasicBridging
415
import SwiftDiagnostics
516
@_spi(ExperimentalLanguageFeatures) import SwiftSyntax
617

718
extension ASTGenVisitor {
8-
func generate(pattern node: PatternSyntax) -> ASTNode {
19+
func generate(pattern node: PatternSyntax) -> BridgedPattern {
920
switch node.as(PatternSyntaxEnum.self) {
10-
case .expressionPattern(_):
21+
case .expressionPattern:
1122
break
1223
case .identifierPattern(let node):
13-
// FIXME: Generate proper pattern instead of temporary Expr.
14-
return .expr(self.generate(node).asExpr)
15-
case .isTypePattern(_):
24+
return self.generate(identifierPattern: node).asPattern
25+
case .isTypePattern:
1626
break
17-
case .missingPattern(_):
27+
case .missingPattern:
1828
break
19-
case .tuplePattern(_):
29+
case .tuplePattern:
2030
break
21-
case .valueBindingPattern(_):
31+
case .valueBindingPattern:
2232
break
23-
case .wildcardPattern(_):
33+
case .wildcardPattern:
2434
break
2535
}
26-
fatalError("unimplmented")
36+
37+
preconditionFailure("unimplemented")
38+
}
39+
40+
func generate(identifierPattern node: IdentifierPatternSyntax) -> BridgedNamedPattern {
41+
let (name, nameLoc) = node.identifier.bridgedIdentifierAndSourceLoc(in: self)
42+
return .createParsed(
43+
ctx, declContext: declContext,
44+
name: name,
45+
loc: nameLoc
46+
)
2747
}
2848
}
2949

0 commit comments

Comments
 (0)