Skip to content

[ASTGen] Utilize base specific SyntaxEnum and start Pattern generation #69829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ namespace swift {
#define ABSTRACT_TYPEREPR(Id, Parent) TYPEREPR(Id, Parent)
#include "swift/AST/TypeReprNodes.def"

// Declare `.asPattern` on each BridgedXXXPattern type, which upcasts a wrapper
// for a Pattern subclass to a BridgedPattern.
#define PATTERN(Id, Parent) \
SWIFT_NAME("getter:Bridged" #Id "Pattern.asPattern(self:)") \
BridgedPattern Bridged##Id##Pattern_asPattern(Bridged##Id##Pattern pattern);
#include "swift/AST/PatternNodes.def"

//===----------------------------------------------------------------------===//
// MARK: Diagnostic Engine
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -289,10 +296,10 @@ void BridgedDiagnostic_finish(BridgedDiagnostic cDiag);

SWIFT_NAME(
"BridgedPatternBindingDecl.createParsed(_:declContext:bindingKeywordLoc:"
"nameExpr:initializer:isStatic:isLet:)")
"pattern:initializer:isStatic:isLet:)")
BridgedPatternBindingDecl BridgedPatternBindingDecl_createParsed(
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
BridgedSourceLoc cBindingKeywordLoc, BridgedExpr opaqueNameExpr,
BridgedSourceLoc cBindingKeywordLoc, BridgedPattern pattern,
BridgedExpr opaqueInitExpr, bool isStatic, bool isLet);

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

//===----------------------------------------------------------------------===//
// MARK: Patterns
//===----------------------------------------------------------------------===//

SWIFT_NAME("BridgedNamedPattern.createParsed(_:declContext:name:loc:)")
BridgedNamedPattern
BridgedNamedPattern_createParsed(BridgedASTContext astContext,
BridgedDeclContext declContext,
BridgedIdentifier name, BridgedSourceLoc cLoc);

//===----------------------------------------------------------------------===//
// MARK: Misc
//===----------------------------------------------------------------------===//
Expand Down
5 changes: 5 additions & 0 deletions include/swift/AST/ASTBridgingWrappers.def
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
#define ABSTRACT_TYPEREPR(Id, Parent) TYPEREPR(Id, Parent)
#include "swift/AST/TypeReprNodes.def"

#ifndef PATTERN
#define PATTERN(Id, Parent) AST_BRIDGING_WRAPPER_NONNULL(Id##Pattern)
#endif
#include "swift/AST/PatternNodes.def"

// Some of the base classes need to be nullable to allow them to be used as
// optional parameters.
AST_BRIDGING_WRAPPER_NONNULL(Decl)
Expand Down
13 changes: 6 additions & 7 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5838,13 +5838,6 @@ class VarDecl : public AbstractStorageDecl {
SourceLoc nameLoc, Identifier name, DeclContext *dc,
StorageIsMutable_t supportsMutation);

protected:
// Only \c ParamDecl::setSpecifier is allowed to flip this - and it's also
// on the way out of that business.
void setIntroducer(Introducer value) {
Bits.VarDecl.Introducer = uint8_t(value);
}

public:
VarDecl(bool isStatic, Introducer introducer,
SourceLoc nameLoc, Identifier name, DeclContext *dc)
Expand Down Expand Up @@ -6064,6 +6057,12 @@ class VarDecl : public AbstractStorageDecl {
/// upon any kind of access?
bool isOrdinaryStoredProperty() const;

/// Set the introducer kind.
/// Note: do not call this after type checking begun.
void setIntroducer(Introducer value) {
Bits.VarDecl.Introducer = uint8_t(value);
}

Introducer getIntroducer() const {
return Introducer(Bits.VarDecl.Introducer);
}
Expand Down
49 changes: 42 additions & 7 deletions lib/AST/ASTBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ bool BridgedASTContext_langOptsHasFeature(BridgedASTContext cContext,
#define ABSTRACT_TYPEREPR(Id, Parent) TYPEREPR(Id, Parent)
#include "swift/AST/TypeReprNodes.def"

// Define `.asPattern` on each BridgedXXXPattern type.
#define PATTERN(Id, Parent) \
BridgedPattern Bridged##Id##Pattern_asPattern( \
Bridged##Id##Pattern pattern) { \
return static_cast<Pattern *>(pattern.unbridged()); \
}
#include "swift/AST/PatternNodes.def"

//===----------------------------------------------------------------------===//
// MARK: Diagnostics
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -274,18 +282,25 @@ void BridgedDiagnostic_finish(BridgedDiagnostic cDiag) {

BridgedPatternBindingDecl BridgedPatternBindingDecl_createParsed(
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
BridgedSourceLoc cBindingKeywordLoc, BridgedExpr nameExpr,
BridgedSourceLoc cBindingKeywordLoc, BridgedPattern cPattern,
BridgedExpr initExpr, bool isStatic, bool isLet) {
ASTContext &context = cContext.unbridged();
DeclContext *declContext = cDeclContext.unbridged();

auto *name = cast<UnresolvedDeclRefExpr>(nameExpr.unbridged());
auto *varDecl = new (context) VarDecl(
isStatic, isLet ? VarDecl::Introducer::Let : VarDecl::Introducer::Var,
name->getLoc(), name->getName().getBaseIdentifier(), declContext);
auto *pattern = new (context) NamedPattern(varDecl);
Pattern *pattern = cPattern.unbridged();

VarDecl::Introducer introducer =
isLet ? VarDecl::Introducer::Let : VarDecl::Introducer::Var;

// Configure all vars.
pattern->forEachVariable([&](VarDecl *VD) {
VD->setStatic(isStatic);
VD->setIntroducer(introducer);
});

return PatternBindingDecl::create(context,
/*StaticLoc=*/SourceLoc(), // FIXME
/*StaticLoc=*/SourceLoc(),
// FIXME: 'class' spelling kind.
isStatic ? StaticSpellingKind::KeywordStatic
: StaticSpellingKind::None,
cBindingKeywordLoc.unbridged(), pattern,
Expand Down Expand Up @@ -1255,6 +1270,26 @@ BridgedExistentialTypeRepr_createParsed(BridgedASTContext cContext,
ExistentialTypeRepr(cAnyLoc.unbridged(), baseTy.unbridged());
}

//===----------------------------------------------------------------------===//
// MARK: Patterns
//===----------------------------------------------------------------------===//

BridgedNamedPattern
BridgedNamedPattern_createParsed(BridgedASTContext cContext,
BridgedDeclContext cDeclContext,
BridgedIdentifier name, BridgedSourceLoc loc) {
auto &context = cContext.unbridged();
auto *dc = cDeclContext.unbridged();

// Note 'isStatic' and the introducer value are temporary.
// The outer context should set the correct values.
auto *varDecl = new (context) VarDecl(
/*isStatic=*/false, VarDecl::Introducer::Let, loc.unbridged(),
name.unbridged(), dc);
auto *pattern = new (context) NamedPattern(varDecl);
return pattern;
}

//===----------------------------------------------------------------------===//
// MARK: Misc
//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 0 deletions lib/ASTGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_pure_swift_host_library(swiftASTGen STATIC
Sources/ASTGen/Literals.swift
Sources/ASTGen/Macros.swift
Sources/ASTGen/ParameterClause.swift
Sources/ASTGen/Patterns.swift
Sources/ASTGen/PluginHost.swift
Sources/ASTGen/SourceFile.swift
Sources/ASTGen/SourceManager.swift
Expand Down
Loading