Skip to content

Commit 96f13f5

Browse files
committed
ASTGen: Translate operator declarations
1 parent de155c9 commit 96f13f5

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,21 @@ BridgedDeclContextAndDecl ExtensionDecl_create(
412412
BridgedArrayRef cInheritedTypes, void *_Nullable opaqueGenericWhereClause,
413413
BridgedSourceRange cBraceRange);
414414

415+
typedef enum ENUM_EXTENSIBILITY_ATTR(closed) {
416+
BridgedOperatorFixityInfix,
417+
BridgedOperatorFixityPrefix,
418+
BridgedOperatorFixityPostfix,
419+
} BridgedOperatorFixity;
420+
421+
void *OperatorDecl_create(BridgedASTContext cContext,
422+
BridgedDeclContext cDeclContext,
423+
BridgedOperatorFixity cFixity,
424+
BridgedSourceLoc cOperatorKeywordLoc,
425+
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
426+
BridgedSourceLoc cColonLoc,
427+
BridgedIdentifier cPrecedenceGroupName,
428+
BridgedSourceLoc cPrecedenceGroupLoc);
429+
415430
void *GenericParamList_create(BridgedASTContext cContext,
416431
BridgedSourceLoc cLeftAngleLoc,
417432
BridgedArrayRef cParameters,

lib/AST/CASTBridging.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,46 @@ BridgedDeclContextAndDecl ExtensionDecl_create(
747747
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
748748
}
749749

750+
void *OperatorDecl_create(BridgedASTContext cContext,
751+
BridgedDeclContext cDeclContext,
752+
BridgedOperatorFixity cFixity,
753+
BridgedSourceLoc cOperatorKeywordLoc,
754+
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
755+
BridgedSourceLoc cColonLoc,
756+
BridgedIdentifier cPrecedenceGroupName,
757+
BridgedSourceLoc cPrecedenceGroupLoc) {
758+
assert(bool(cColonLoc.raw) == (bool)cPrecedenceGroupName.raw);
759+
assert(bool(cColonLoc.raw) == (bool)cPrecedenceGroupLoc.raw);
760+
761+
ASTContext &context = convertASTContext(cContext);
762+
auto operatorKeywordLoc = convertSourceLoc(cOperatorKeywordLoc);
763+
auto name = convertIdentifier(cName);
764+
auto nameLoc = convertSourceLoc(cNameLoc);
765+
auto *declContext = convertDeclContext(cDeclContext);
766+
767+
OperatorDecl *decl = nullptr;
768+
switch (cFixity) {
769+
case BridgedOperatorFixityInfix:
770+
decl = new (context) InfixOperatorDecl(
771+
declContext, operatorKeywordLoc, name, nameLoc,
772+
convertSourceLoc(cColonLoc), convertIdentifier(cPrecedenceGroupName),
773+
convertSourceLoc(cPrecedenceGroupLoc));
774+
break;
775+
case BridgedOperatorFixityPrefix:
776+
assert(!cColonLoc.raw);
777+
decl = new (context)
778+
PrefixOperatorDecl(declContext, operatorKeywordLoc, name, nameLoc);
779+
break;
780+
case BridgedOperatorFixityPostfix:
781+
assert(!cColonLoc.raw);
782+
decl = new (context)
783+
PostfixOperatorDecl(declContext, operatorKeywordLoc, name, nameLoc);
784+
break;
785+
}
786+
787+
return static_cast<Decl *>(decl);
788+
}
789+
750790
void *OptionalTypeRepr_create(BridgedASTContext cContext, void *base,
751791
BridgedSourceLoc cQuestionLoc) {
752792
ASTContext &context = convertASTContext(cContext);

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import CASTBridging
33
// Needed to use SyntaxTransformVisitor's visit method.
44
@_spi(SyntaxTransformVisitor)
55
import SwiftSyntax
6+
import SwiftDiagnostics
67

78
// MARK: - TypeDecl
89

@@ -262,6 +263,48 @@ extension ASTGenVisitor {
262263
}
263264
}
264265

266+
// MARK: - OperatorDecl
267+
268+
extension BridgedOperatorFixity {
269+
fileprivate init?(from tokenKind: TokenKind) {
270+
switch tokenKind {
271+
case .keyword(.infix): self = .infix
272+
case .keyword(.prefix): self = .prefix
273+
case .keyword(.postfix): self = .postfix
274+
default: return nil
275+
}
276+
}
277+
}
278+
279+
extension ASTGenVisitor {
280+
func visit(_ node: OperatorDeclSyntax) -> ASTNode {
281+
let (name, nameLoc) = node.name.bridgedIdentifierAndSourceLoc(in: self)
282+
let (precedenceGroupName, precedenceGroupLoc) = (node.operatorPrecedenceAndTypes?.precedenceGroup).bridgedIdentifierAndSourceLoc(in: self)
283+
284+
let fixity: BridgedOperatorFixity
285+
if let value = BridgedOperatorFixity(from: node.fixitySpecifier.tokenKind) {
286+
fixity = value
287+
} else {
288+
fixity = .infix
289+
self.diagnose(Diagnostic(node: node.fixitySpecifier, message: UnexpectedTokenKindError(token: node.fixitySpecifier)))
290+
}
291+
292+
return .decl(
293+
OperatorDecl_create(
294+
self.ctx,
295+
self.declContext,
296+
fixity,
297+
self.bridgedSourceLoc(for: node.operatorKeyword),
298+
name,
299+
nameLoc,
300+
self.bridgedSourceLoc(for: node.operatorPrecedenceAndTypes?.colon),
301+
precedenceGroupName,
302+
precedenceGroupLoc
303+
)
304+
)
305+
}
306+
}
307+
265308
extension ASTGenVisitor {
266309
@inline(__always)
267310
func visit(_ node: MemberBlockItemListSyntax) -> BridgedArrayRef {

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)