Skip to content

Commit 1d969c0

Browse files
committed
ASTGen: Translate operator declarations
1 parent e22df21 commit 1d969c0

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,23 @@ BridgedDeclContextAndDecl ExtensionDecl_create(
459459
BridgedArrayRef cInheritedTypes,
460460
void *_Nullable opaqueGenericWhereClause);
461461

462+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) {
463+
BridgedOperatorFixityInfix,
464+
BridgedOperatorFixityPrefix,
465+
BridgedOperatorFixityPostfix,
466+
} BridgedOperatorFixity;
467+
468+
void *OperatorDecl_create(
469+
BridgedASTContext cContext,
470+
BridgedDeclContext cDeclContext,
471+
BridgedOperatorFixity cFixity,
472+
BridgedSourceLoc cOperatorKeywordLoc,
473+
BridgedIdentifier cName,
474+
BridgedSourceLoc cNameLoc,
475+
BridgedSourceLoc cColonLoc,
476+
BridgedIdentifier cPrecedenceGroupName,
477+
BridgedSourceLoc cPrecedenceGroupLoc);
478+
462479
void *GenericParamList_create(
463480
BridgedASTContext cContext,
464481
BridgedSourceLoc cLeftAngleLoc,

lib/AST/CASTBridging.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,52 @@ BridgedDeclContextAndDecl ExtensionDecl_create(
800800
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
801801
}
802802

803+
void *OperatorDecl_create(
804+
BridgedASTContext cContext,
805+
BridgedDeclContext cDeclContext,
806+
BridgedOperatorFixity cFixity,
807+
BridgedSourceLoc cOperatorKeywordLoc,
808+
BridgedIdentifier cName,
809+
BridgedSourceLoc cNameLoc,
810+
BridgedSourceLoc cColonLoc,
811+
BridgedIdentifier cPrecedenceGroupName,
812+
BridgedSourceLoc cPrecedenceGroupLoc) {
813+
assert(bool(cColonLoc.raw) == (bool)cPrecedenceGroupName.raw);
814+
assert(bool(cColonLoc.raw) == (bool)cPrecedenceGroupLoc.raw);
815+
816+
ASTContext &context = convertASTContext(cContext);
817+
auto operatorKeywordLoc = convertSourceLoc(cOperatorKeywordLoc);
818+
auto name = convertIdentifier(cName);
819+
auto nameLoc = convertSourceLoc(cNameLoc);
820+
auto *declContext = convertDeclContext(cDeclContext);
821+
822+
OperatorDecl *decl = nullptr;
823+
switch (cFixity) {
824+
case BridgedOperatorFixityInfix:
825+
decl = new (context) InfixOperatorDecl(
826+
declContext,
827+
operatorKeywordLoc,
828+
name,
829+
nameLoc,
830+
convertSourceLoc(cColonLoc),
831+
convertIdentifier(cPrecedenceGroupName),
832+
convertSourceLoc(cPrecedenceGroupLoc));
833+
break;
834+
case BridgedOperatorFixityPrefix:
835+
assert(!cColonLoc.raw);
836+
decl = new (context)
837+
PrefixOperatorDecl(declContext, operatorKeywordLoc, name, nameLoc);
838+
break;
839+
case BridgedOperatorFixityPostfix:
840+
assert(!cColonLoc.raw);
841+
decl = new (context)
842+
PostfixOperatorDecl(declContext, operatorKeywordLoc, name, nameLoc);
843+
break;
844+
}
845+
846+
return static_cast<Decl *>(decl);
847+
}
848+
803849
void *OptionalTypeRepr_create(BridgedASTContext cContext, void *base,
804850
BridgedSourceLoc cQuestionLoc) {
805851
ASTContext &context = convertASTContext(cContext);

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,39 @@ extension ASTGenVisitor {
301301
return .decl(out.decl)
302302
}
303303
}
304+
305+
// MARK: - OperatorDecl
306+
307+
extension BridgedOperatorFixity {
308+
fileprivate init(tokenKind: TokenKind) {
309+
switch tokenKind {
310+
case .keyword(.infix): self = .infix
311+
case .keyword(.prefix): self = .prefix
312+
case .keyword(.postfix): self = .postfix
313+
default: fatalError("Unknown operator fixity")
314+
}
315+
}
316+
}
317+
318+
extension ASTGenVisitor {
319+
func visit(_ node: OperatorDeclSyntax) -> ASTNode {
320+
let (name, nameLoc) = self.bridgedIdentifierAndSourceLoc(for: node.identifier)
321+
let (precedenceGroupName, precedenceGroupLoc) = self.bridgedIdentifierAndSourceLoc(
322+
for: node.operatorPrecedenceAndTypes?.precedenceGroup
323+
)
324+
325+
return .decl(
326+
OperatorDecl_create(
327+
self.ctx,
328+
self.declContext,
329+
BridgedOperatorFixity(tokenKind: node.fixity.tokenKind),
330+
self.bridgedSourceLoc(for: node.operatorKeyword),
331+
name,
332+
nameLoc,
333+
self.bridgedSourceLoc(for: node.operatorPrecedenceAndTypes?.colon),
334+
precedenceGroupName,
335+
precedenceGroupLoc
336+
)
337+
)
338+
}
339+
}

test/ASTGen/verify-parse.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,15 @@ extension
159159
Class: Proto2 where T: Proto1 {
160160
func method2(_ b: Bool) {}
161161
}
162+
163+
prefix
164+
operator ⎭^-^⎭
165+
166+
infix
167+
operator
168+
~^-^~
169+
:
170+
AdditionPrecedence
171+
172+
postfix
173+
operator ⎩^-^⎩

0 commit comments

Comments
 (0)