Skip to content

Commit fbc1f14

Browse files
committed
ASTGen: Translate precedence group declarations
1 parent 028b9b7 commit fbc1f14

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,30 @@ void *OperatorDecl_create(
488488
BridgedIdentifier cPrecedenceGroupName,
489489
BridgedSourceLoc cPrecedenceGroupLoc);
490490

491+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) {
492+
BridgedAssociativityNone,
493+
BridgedAssociativityLeft,
494+
BridgedAssociativityRight,
495+
} BridgedAssociativity;
496+
497+
void *PrecedenceGroupDecl_create(
498+
BridgedDeclContext cDeclContext,
499+
BridgedSourceLoc cPrecedencegroupKeywordLoc,
500+
BridgedIdentifier cName,
501+
BridgedSourceLoc cNameLoc,
502+
BridgedSourceLoc cLeftBraceLoc,
503+
BridgedSourceLoc cAssociativityKeywordLoc,
504+
BridgedSourceLoc cAssociativityValueLoc,
505+
BridgedAssociativity cAssociativity,
506+
BridgedSourceLoc cAssignmentKeywordLoc,
507+
BridgedSourceLoc cAssignmentValueLoc,
508+
_Bool isAssignment,
509+
BridgedSourceLoc cHigherThanKeywordLoc,
510+
BridgedArrayRef cHigherThanNames,
511+
BridgedSourceLoc cLowerThanKeywordLoc,
512+
BridgedArrayRef cLowerThanNames,
513+
BridgedSourceLoc cRightBraceLoc);
514+
491515
typedef enum ENUM_EXTENSIBILITY_ATTR(open) {
492516
BridgedImportKindModule,
493517
BridgedImportKindType,

lib/AST/CASTBridging.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,63 @@ void *OperatorDecl_create(
875875
return static_cast<Decl *>(decl);
876876
}
877877

878+
void *PrecedenceGroupDecl_create(
879+
BridgedDeclContext cDeclContext,
880+
BridgedSourceLoc cPrecedencegroupKeywordLoc,
881+
BridgedIdentifier cName,
882+
BridgedSourceLoc cNameLoc,
883+
BridgedSourceLoc cLeftBraceLoc,
884+
BridgedSourceLoc cAssociativityKeywordLoc,
885+
BridgedSourceLoc cAssociativityValueLoc,
886+
BridgedAssociativity cAssociativity,
887+
BridgedSourceLoc cAssignmentKeywordLoc,
888+
BridgedSourceLoc cAssignmentValueLoc,
889+
bool isAssignment,
890+
BridgedSourceLoc cHigherThanKeywordLoc,
891+
BridgedArrayRef cHigherThanNames,
892+
BridgedSourceLoc cLowerThanKeywordLoc,
893+
BridgedArrayRef cLowerThanNames,
894+
BridgedSourceLoc cRightBraceLoc) {
895+
896+
SmallVector<PrecedenceGroupDecl::Relation, 2> higherThanNames;
897+
for (auto &pair :
898+
convertArrayRef<BridgedIdentifierAndSourceLoc>(cHigherThanNames)) {
899+
higherThanNames.push_back(
900+
{convertSourceLoc(pair.nameLoc),
901+
convertIdentifier(pair.name),
902+
nullptr});
903+
}
904+
905+
SmallVector<PrecedenceGroupDecl::Relation, 2> lowerThanNames;
906+
for (auto &pair :
907+
convertArrayRef<BridgedIdentifierAndSourceLoc>(cLowerThanNames)) {
908+
lowerThanNames.push_back(
909+
{convertSourceLoc(pair.nameLoc),
910+
convertIdentifier(pair.name),
911+
nullptr});
912+
}
913+
914+
auto *decl = PrecedenceGroupDecl::create(
915+
convertDeclContext(cDeclContext),
916+
convertSourceLoc(cPrecedencegroupKeywordLoc),
917+
convertSourceLoc(cNameLoc),
918+
convertIdentifier(cName),
919+
convertSourceLoc(cLeftBraceLoc),
920+
convertSourceLoc(cAssociativityKeywordLoc),
921+
convertSourceLoc(cAssociativityValueLoc),
922+
static_cast<Associativity>(cAssociativity),
923+
convertSourceLoc(cAssignmentKeywordLoc),
924+
convertSourceLoc(cAssignmentValueLoc),
925+
isAssignment,
926+
convertSourceLoc(cHigherThanKeywordLoc),
927+
higherThanNames,
928+
convertSourceLoc(cLowerThanKeywordLoc),
929+
lowerThanNames,
930+
convertSourceLoc(cRightBraceLoc));
931+
932+
return static_cast<Decl *>(decl);
933+
}
934+
878935
void *ImportDecl_create(
879936
BridgedASTContext cContext,
880937
BridgedDeclContext cDeclContext,

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,86 @@ extension ASTGenVisitor {
378378
}
379379
}
380380

381+
extension BridgedAssociativity {
382+
fileprivate init(tokenKind: TokenKind) {
383+
switch tokenKind {
384+
case .keyword(.none): self = .none
385+
case .keyword(.left): self = .left
386+
case .keyword(.right): self = .right
387+
default: fatalError("Unknown precedence group associativity value")
388+
}
389+
}
390+
}
391+
392+
extension ASTGenVisitor {
393+
func visit(_ node: PrecedenceGroupDeclSyntax) -> ASTNode {
394+
let (name, nameLoc) = self.bridgedIdentifierAndSourceLoc(for: node.identifier)
395+
396+
struct PrecedenceGroupBody {
397+
var associativity: PrecedenceGroupAssociativitySyntax? = nil
398+
var assignment: PrecedenceGroupAssignmentSyntax? = nil
399+
var higherThanRelation: PrecedenceGroupRelationSyntax? = nil
400+
var lowerThanRelation: PrecedenceGroupRelationSyntax? = nil
401+
}
402+
403+
let body = node.groupAttributes.reduce(into: PrecedenceGroupBody()) { partialResult, element in
404+
switch element {
405+
case .precedenceGroupRelation(let node):
406+
switch node.higherThanOrLowerThan.tokenKind {
407+
case .keyword(.higherThan): partialResult.higherThanRelation = node
408+
case .keyword(.lowerThan): partialResult.lowerThanRelation = node
409+
default: fatalError("Unknown precedence group relation attribute")
410+
}
411+
case .precedenceGroupAssignment(let node):
412+
partialResult.assignment = node
413+
case .precedenceGroupAssociativity(let node):
414+
partialResult.associativity = node
415+
}
416+
}
417+
418+
let associativityValue = body.associativity.map { BridgedAssociativity(tokenKind: $0.value.tokenKind) } ?? .none
419+
let assignmentValue =
420+
body.assignment.map {
421+
switch $0.flag.tokenKind {
422+
case .keyword(.true): return true
423+
case .keyword(.false): return false
424+
default: fatalError("Unknown precedence group assignment value")
425+
}
426+
} ?? false
427+
428+
return .decl(
429+
(body.higherThanRelation?.otherNames).map {
430+
let (name, nameLoc) = self.bridgedIdentifierAndSourceLoc(for: $0.name)
431+
return BridgedIdentifierAndSourceLoc(name: name, nameLoc: nameLoc)
432+
} withBridgedArrayRef: { higherThanNames in
433+
(body.lowerThanRelation?.otherNames).map {
434+
let (name, nameLoc) = self.bridgedIdentifierAndSourceLoc(for: $0.name)
435+
return BridgedIdentifierAndSourceLoc(name: name, nameLoc: nameLoc)
436+
} withBridgedArrayRef: { lowerThanNames in
437+
PrecedenceGroupDecl_create(
438+
self.declContext,
439+
self.bridgedSourceLoc(for: node.precedencegroupKeyword),
440+
name,
441+
nameLoc,
442+
self.bridgedSourceLoc(for: node.leftBrace),
443+
self.bridgedSourceLoc(for: body.associativity?.associativityKeyword),
444+
self.bridgedSourceLoc(for: body.associativity?.value),
445+
associativityValue,
446+
self.bridgedSourceLoc(for: body.assignment?.assignmentKeyword),
447+
self.bridgedSourceLoc(for: body.assignment?.flag),
448+
assignmentValue,
449+
self.bridgedSourceLoc(for: body.higherThanRelation?.higherThanOrLowerThan),
450+
higherThanNames,
451+
self.bridgedSourceLoc(for: body.lowerThanRelation?.higherThanOrLowerThan),
452+
lowerThanNames,
453+
self.bridgedSourceLoc(for: node.rightBrace)
454+
)
455+
}
456+
}
457+
)
458+
}
459+
}
460+
381461
// MARK: - ImportDecl
382462

383463
extension BridgedImportKind {

test/ASTGen/verify-parse.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,14 @@ operator
191191

192192
postfix
193193
operator ⎩^-^⎩
194+
195+
196+
precedencegroup Precedence1 {
197+
}
198+
199+
precedencegroup Precedence2 {
200+
lowerThan: BitwiseShiftPrecedence, MultiplicationPrecedence
201+
higherThan: Precedence1, AdditionPrecedence
202+
associativity: left
203+
assignment: true
204+
}

0 commit comments

Comments
 (0)