Skip to content

[ASTGen] Generate compound name expression #70138

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 1 commit into from
Dec 1, 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
93 changes: 89 additions & 4 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class DiagnosticArgument;
class DiagnosticEngine;
}

class BridgedASTContext;

//===----------------------------------------------------------------------===//
// MARK: Identifier
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -71,6 +73,82 @@ struct BridgedIdentifierAndSourceLoc {
BridgedSourceLoc NameLoc;
};

class BridgedDeclBaseName {
BridgedIdentifier Ident;

public:
#ifdef USED_IN_CPP_SOURCE
BridgedDeclBaseName(swift::DeclBaseName baseName) : Ident(baseName.Ident) {}

swift::DeclBaseName unbridged() const {
return swift::DeclBaseName(Ident.unbridged());
}
#endif
};

SWIFT_NAME("BridgedDeclBaseName.createConstructor()")
BridgedDeclBaseName BridgedDeclBaseName_createConstructor();

SWIFT_NAME("BridgedDeclBaseName.createDestructor()")
BridgedDeclBaseName BridgedDeclBaseName_createDestructor();

SWIFT_NAME("BridgedDeclBaseName.createSubscript()")
BridgedDeclBaseName BridgedDeclBaseName_createSubscript();
Comment on lines +89 to +96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can’t these be static functions of BridgedDeclBaseName. IIRC I used static members in NameMatcherBridging and it worked.

Similar for the other create functions here.


SWIFT_NAME("BridgedDeclBaseName.createIdentifier(_:)")
BridgedDeclBaseName
BridgedDeclBaseName_createIdentifier(BridgedIdentifier identifier);

class BridgedDeclNameRef {
void *_Nonnull opaque;

public:
#ifdef USED_IN_CPP_SOURCE
BridgedDeclNameRef(swift::DeclNameRef name) : opaque(name.getOpaqueValue()) {}

swift::DeclNameRef unbridged() const {
return swift::DeclNameRef::getFromOpaqueValue(opaque);
}
#endif
};

SWIFT_NAME("BridgedDeclNameRef.createParsed(_:baseName:argumentLabels:)")
BridgedDeclNameRef
BridgedDeclNameRef_createParsed(BridgedASTContext cContext,
BridgedDeclBaseName cBaseName,
BridgedArrayRef cLabels);

SWIFT_NAME("BridgedDeclNameRef.createParsed(_:)")
BridgedDeclNameRef
BridgedDeclNameRef_createParsed(BridgedDeclBaseName cBaseName);

class BridgedDeclNameLoc {
const void *_Nonnull LocationInfo;
size_t NumArgumentLabels;

public:
#ifdef USED_IN_CPP_SOURCE
BridgedDeclNameLoc(swift::DeclNameLoc loc)
: LocationInfo(loc.LocationInfo),
NumArgumentLabels(loc.NumArgumentLabels) {}

swift::DeclNameLoc unbridged() const {
return swift::DeclNameLoc(LocationInfo, NumArgumentLabels);
}
#endif
};

SWIFT_NAME("BridgedDeclNameLoc.createParsed(_:baseNameLoc:lParenLoc:"
"argumentLabelLocs:rParenLoc:)")
BridgedDeclNameLoc BridgedDeclNameLoc_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cBaseNameLoc,
BridgedSourceLoc cLParenLoc, BridgedArrayRef cLabelLocs,
BridgedSourceLoc cRParenLoc);

SWIFT_NAME("BridgedDeclNameLoc.createParsed(_:)")
BridgedDeclNameLoc
BridgedDeclNameLoc_createParsed(BridgedSourceLoc cBaseNameLoc);

//===----------------------------------------------------------------------===//
// MARK: ASTContext
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -577,9 +655,11 @@ BridgedClosureExpr_createParsed(BridgedASTContext cContext,
BridgedDeclContext cDeclContext,
BridgedBraceStmt body);

SWIFT_NAME("BridgedUnresolvedDeclRefExpr.createParsed(_:base:loc:)")
BridgedUnresolvedDeclRefExpr BridgedUnresolvedDeclRefExpr_createParsed(
BridgedASTContext cContext, BridgedIdentifier base, BridgedSourceLoc cLoc);
SWIFT_NAME("BridgedUnresolvedDeclRefExpr.createParsed(_:name:loc:)")
BridgedUnresolvedDeclRefExpr
BridgedUnresolvedDeclRefExpr_createParsed(BridgedASTContext cContext,
BridgedDeclNameRef cName,
BridgedDeclNameLoc cLoc);

SWIFT_NAME("BridgedSingleValueStmtExpr.createWithWrappedBranches(_:stmt:"
"declContext:mustBeExpr:)")
Expand Down Expand Up @@ -630,7 +710,12 @@ BridgedArrayExpr BridgedArrayExpr_createParsed(BridgedASTContext cContext,
SWIFT_NAME("BridgedUnresolvedDotExpr.createParsed(_:base:dotLoc:name:nameLoc:)")
BridgedUnresolvedDotExpr BridgedUnresolvedDotExpr_createParsed(
BridgedASTContext cContext, BridgedExpr base, BridgedSourceLoc cDotLoc,
BridgedIdentifier name, BridgedSourceLoc cNameLoc);
BridgedDeclNameRef cName, BridgedDeclNameLoc cNameLoc);

SWIFT_NAME("BridgedUnresolvedMemberExpr.createParsed(_:dotLoc:name:nameLoc:)")
BridgedUnresolvedMemberExpr BridgedUnresolvedMemberExpr_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cDotLoc,
BridgedDeclNameRef cName, BridgedDeclNameLoc cNameLoc);

SWIFT_NAME("BridgedExpr.dump(self:)")
void BridgedExpr_dump(BridgedExpr expr);
Expand Down
12 changes: 9 additions & 3 deletions include/swift/AST/DeclNameLoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
#include "swift/Basic/LLVM.h"
#include "swift/Basic/SourceLoc.h"

class BridgedDeclNameLoc;

namespace swift {

class ASTContext;

/// Source location information for a declaration name (\c DeclName)
/// written in the source.
class DeclNameLoc {
friend class ::BridgedDeclNameLoc;

/// Source location information.
///
/// If \c NumArgumentLabels == 0, this is the SourceLoc for the base name.
Expand Down Expand Up @@ -56,14 +60,16 @@ class DeclNameLoc {
return reinterpret_cast<SourceLoc const *>(LocationInfo);
}

DeclNameLoc(const void *LocationInfo, unsigned NumArgumentLabels)
: LocationInfo(LocationInfo), NumArgumentLabels(NumArgumentLabels) {}

public:
/// Create an invalid declaration name location.
DeclNameLoc() : LocationInfo(0), NumArgumentLabels(0) { }
DeclNameLoc() : DeclNameLoc(nullptr, 0) {}

/// Create declaration name location information for a base name.
explicit DeclNameLoc(SourceLoc baseNameLoc)
: LocationInfo(baseNameLoc.getOpaquePointerValue()),
NumArgumentLabels(0) { }
: DeclNameLoc(baseNameLoc.getOpaquePointerValue(), 0) {}

/// Create declaration name location information for a compound
/// name.
Expand Down
4 changes: 4 additions & 0 deletions include/swift/AST/Identifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,15 @@ namespace llvm {

} // end namespace llvm

class BridgedDeclBaseName;

namespace swift {

/// Wrapper that may either be an Identifier or a special name
/// (e.g. for subscripts)
class DeclBaseName {
friend class ::BridgedDeclBaseName;

public:
enum class Kind: uint8_t {
Normal,
Expand Down
89 changes: 81 additions & 8 deletions lib/AST/ASTBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,63 @@ static TypeAttrKind unbridged(BridgedTypeAttrKind kind) {
}
}

//===----------------------------------------------------------------------===//
// MARK: Identifier
//===----------------------------------------------------------------------===//

BridgedDeclBaseName BridgedDeclBaseName_createConstructor() {
return DeclBaseName::createConstructor();
}

BridgedDeclBaseName BridgedDeclBaseName_createDestructor() {
return DeclBaseName::createDestructor();
}

BridgedDeclBaseName BridgedDeclBaseName_createSubscript() {
return DeclBaseName::createSubscript();
}

BridgedDeclBaseName
BridgedDeclBaseName_createIdentifier(BridgedIdentifier identifier) {
return DeclBaseName(identifier.unbridged());
}

BridgedDeclNameRef
BridgedDeclNameRef_createParsed(BridgedASTContext cContext,
BridgedDeclBaseName cBaseName,
BridgedArrayRef cLabels) {
ASTContext &context = cContext.unbridged();
SmallVector<Identifier, 4> labels;
for (auto &cLabel : cLabels.unbridged<BridgedIdentifier>()) {
labels.push_back(cLabel.unbridged());
}
return DeclNameRef(DeclName(context, cBaseName.unbridged(), labels));
}

BridgedDeclNameRef
BridgedDeclNameRef_createParsed(BridgedDeclBaseName cBaseName) {
return DeclNameRef(cBaseName.unbridged());
}

BridgedDeclNameLoc BridgedDeclNameLoc_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cBaseNameLoc,
BridgedSourceLoc cLParenLoc, BridgedArrayRef cLabelLocs,
BridgedSourceLoc cRParenLoc) {

ASTContext &context = cContext.unbridged();
SmallVector<SourceLoc, 4> labelLocs;
for (auto &cLabelLoc : cLabelLocs.unbridged<BridgedSourceLoc>())
labelLocs.push_back(cLabelLoc.unbridged());

return DeclNameLoc(context, cBaseNameLoc.unbridged(), cLParenLoc.unbridged(),
labelLocs, cRParenLoc.unbridged());
}

BridgedDeclNameLoc
BridgedDeclNameLoc_createParsed(BridgedSourceLoc cBaseNameLoc) {
return DeclNameLoc(cBaseNameLoc.unbridged());
}

//===----------------------------------------------------------------------===//
// MARK: ASTContext
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -871,12 +928,13 @@ BridgedCallExpr BridgedCallExpr_createParsed(BridgedASTContext cContext,
/*implicit*/ false);
}

BridgedUnresolvedDeclRefExpr BridgedUnresolvedDeclRefExpr_createParsed(
BridgedASTContext cContext, BridgedIdentifier base, BridgedSourceLoc cLoc) {
BridgedUnresolvedDeclRefExpr
BridgedUnresolvedDeclRefExpr_createParsed(BridgedASTContext cContext,
BridgedDeclNameRef cName,
BridgedDeclNameLoc cLoc) {
ASTContext &context = cContext.unbridged();
auto name = DeclNameRef{base.unbridged()};
return new (context) UnresolvedDeclRefExpr(name, DeclRefKind::Ordinary,
DeclNameLoc{cLoc.unbridged()});
return new (context) UnresolvedDeclRefExpr(
cName.unbridged(), DeclRefKind::Ordinary, cLoc.unbridged());
}

BridgedStringLiteralExpr
Expand Down Expand Up @@ -930,13 +988,28 @@ BridgedSingleValueStmtExpr BridgedSingleValueStmtExpr_createWithWrappedBranches(
context, S.unbridged(), declContext, mustBeExpr);
}

BridgedTypeExpr BridgedTypeExpr_createParsed(BridgedASTContext cContext,
BridgedTypeRepr cType) {
ASTContext &context = cContext.unbridged();
return new (context) TypeExpr(cType.unbridged());
}

BridgedUnresolvedDotExpr BridgedUnresolvedDotExpr_createParsed(
BridgedASTContext cContext, BridgedExpr base, BridgedSourceLoc cDotLoc,
BridgedIdentifier name, BridgedSourceLoc cNameLoc) {
BridgedDeclNameRef cName, BridgedDeclNameLoc cNameLoc) {
ASTContext &context = cContext.unbridged();
return new (context) UnresolvedDotExpr(
base.unbridged(), cDotLoc.unbridged(), DeclNameRef(name.unbridged()),
DeclNameLoc(cNameLoc.unbridged()), false);
base.unbridged(), cDotLoc.unbridged(), cName.unbridged(),
cNameLoc.unbridged(), /*isImplicit=*/false);
}

BridgedUnresolvedMemberExpr BridgedUnresolvedMemberExpr_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cDotLoc,
BridgedDeclNameRef cName, BridgedDeclNameLoc cNameLoc) {
ASTContext &context = cContext.unbridged();
return new (context)
UnresolvedMemberExpr(cDotLoc.unbridged(), cNameLoc.unbridged(),
cName.unbridged(), /*isImplicit=*/false);
}

//===----------------------------------------------------------------------===//
Expand Down
78 changes: 69 additions & 9 deletions lib/ASTGen/Sources/ASTGen/Exprs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,13 @@ extension ASTGenVisitor {
case .macroExpansionExpr:
break
case .memberAccessExpr(let node):
return self.generate(memberAccessExpr: node).asExpr
return self.generate(memberAccessExpr: node)
case .missingExpr:
break
case .nilLiteralExpr(let node):
return self.generate(nilLiteralExpr: node).asExpr
case .optionalChainingExpr:
// Need special care to wrap the entire postfix chain with OptionalEvaluationExpr.
break
case .packElementExpr:
break
Expand Down Expand Up @@ -249,18 +250,77 @@ extension ASTGenVisitor {
return .createParsed(self.ctx, fn: callee, args: argumentTuple)
}

public func generate(declReferenceExpr node: DeclReferenceExprSyntax) -> BridgedUnresolvedDeclRefExpr {
let (name, nameLoc) = node.baseName.bridgedIdentifierAndSourceLoc(in: self)
private func createDeclNameRef(declReferenceExpr node: DeclReferenceExprSyntax) -> (name: BridgedDeclNameRef, loc: BridgedDeclNameLoc) {
let baseName: BridgedDeclBaseName
switch node.baseName.tokenKind {
case .keyword(.`init`):
baseName = .createConstructor()
case .keyword(.deinit):
baseName = .createDestructor()
case .keyword(.subscript):
baseName = .createSubscript()
default:
baseName = .createIdentifier(node.baseName.bridgedIdentifier(in: self))
}
let baseNameLoc = node.baseName.bridgedSourceLoc(in: self)

return .createParsed(self.ctx, base: name, loc: nameLoc)
if let argumentClause = node.argumentNames {
let labels = argumentClause.arguments.lazy.map {
$0.name.bridgedIdentifier(in: self)
}
let labelLocs = argumentClause.arguments.lazy.map {
$0.name.bridgedSourceLoc(in: self)
}
return (
name: .createParsed(
self.ctx,
baseName: baseName,
argumentLabels: labels.bridgedArray(in: self)
),
loc: .createParsed(
self.ctx,
baseNameLoc: baseNameLoc,
lParenLoc: argumentClause.leftParen.bridgedSourceLoc(in: self),
argumentLabelLocs: labelLocs.bridgedArray(in: self),
rParenLoc: argumentClause.rightParen.bridgedSourceLoc(in: self)
)
)
} else {
return (
name: .createParsed(baseName),
loc: .createParsed(baseNameLoc)
)
}
}

public func generate(memberAccessExpr node: MemberAccessExprSyntax) -> BridgedUnresolvedDotExpr {
let loc = node.bridgedSourceLoc(in: self)
let base = generate(expr: node.base!)
let name = node.declName.baseName.bridgedIdentifier(in: self)
public func generate(declReferenceExpr node: DeclReferenceExprSyntax) -> BridgedUnresolvedDeclRefExpr {
let nameAndLoc = createDeclNameRef(declReferenceExpr: node)
return .createParsed(
self.ctx,
name: nameAndLoc.name,
loc: nameAndLoc.loc
)
}

public func generate(memberAccessExpr node: MemberAccessExprSyntax) -> BridgedExpr {
let nameAndLoc = createDeclNameRef(declReferenceExpr: node.declName)

return .createParsed(ctx, base: base, dotLoc: loc, name: name, nameLoc: loc)
if let base = node.base {
return BridgedUnresolvedDotExpr.createParsed(
self.ctx,
base: self.generate(expr: base),
dotLoc: node.period.bridgedSourceLoc(in: self),
name: nameAndLoc.name,
nameLoc: nameAndLoc.loc
).asExpr
} else {
return BridgedUnresolvedMemberExpr.createParsed(
self.ctx,
dotLoc: node.period.bridgedSourceLoc(in: self),
name: nameAndLoc.name,
nameLoc: nameAndLoc.loc
).asExpr
}
}

public func generate(ifExpr node: IfExprSyntax) -> BridgedSingleValueStmtExpr {
Expand Down
Loading