|
| 1 | +import SwiftParser |
| 2 | +import SwiftSyntax |
| 3 | + |
| 4 | +import CASTBridging |
| 5 | + |
| 6 | +extension ASTGenVisitor { |
| 7 | + public func visit(_ node: SimpleTypeIdentifierSyntax) -> UnsafeMutableRawPointer { |
| 8 | + let loc = self.base.advanced(by: node.position.utf8Offset).raw |
| 9 | + |
| 10 | + var text = node.name.text |
| 11 | + let id = text.withUTF8 { buf in |
| 12 | + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) |
| 13 | + } |
| 14 | + |
| 15 | + return SimpleIdentTypeRepr_create(ctx, loc, id) |
| 16 | + } |
| 17 | + |
| 18 | + public func visit(_ node: MemberTypeIdentifierSyntax) -> UnsafeMutableRawPointer { |
| 19 | + var path = [(TokenSyntax, GenericArgumentClauseSyntax?)]() |
| 20 | + var memberRef: Syntax? = Syntax(node) |
| 21 | + while let nestedMember = memberRef?.as(MemberTypeIdentifierSyntax.self) { |
| 22 | + path.append((nestedMember.name, nestedMember.genericArgumentClause)) |
| 23 | + memberRef = Syntax(nestedMember.baseType) |
| 24 | + } |
| 25 | + |
| 26 | + if let base = memberRef?.as(SimpleTypeIdentifierSyntax.self) { |
| 27 | + path.append((base.name, base.genericArgumentClause)) |
| 28 | + } |
| 29 | + |
| 30 | + var elements = [UnsafeMutableRawPointer]() |
| 31 | + for (pathElement, generics) in path.reversed() { |
| 32 | + var nameText = pathElement.text |
| 33 | + let name = nameText.withUTF8 { buf in |
| 34 | + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) |
| 35 | + } |
| 36 | + let nameLoc = self.base.advanced(by: pathElement.position.utf8Offset).raw |
| 37 | + |
| 38 | + elements.append(SimpleIdentTypeRepr_create(self.ctx, nameLoc, name)) |
| 39 | + } |
| 40 | + |
| 41 | + return elements.withBridgedArrayRef { elements in |
| 42 | + return IdentTypeRepr_create(self.ctx, elements) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public func visit(_ node: ArrayTypeSyntax) -> UnsafeMutableRawPointer { |
| 47 | + let elementType = visit(node.elementType) |
| 48 | + let lSquareLoc = self.base.advanced(by: node.leftSquareBracket.position.utf8Offset).raw |
| 49 | + let rSquareLoc = self.base.advanced(by: node.rightSquareBracket.position.utf8Offset).raw |
| 50 | + return ArrayTypeRepr_create(self.ctx, elementType, lSquareLoc, rSquareLoc) |
| 51 | + } |
| 52 | + |
| 53 | + public func visit(_ node: DictionaryTypeSyntax) -> UnsafeMutableRawPointer { |
| 54 | + let keyType = visit(node.keyType) |
| 55 | + let valueType = visit(node.valueType) |
| 56 | + let colonLoc = self.base.advanced(by: node.colon.position.utf8Offset).raw |
| 57 | + let lSquareLoc = self.base.advanced(by: node.leftSquareBracket.position.utf8Offset).raw |
| 58 | + let rSquareLoc = self.base.advanced(by: node.rightSquareBracket.position.utf8Offset).raw |
| 59 | + return DictionaryTypeRepr_create(self.ctx, keyType, valueType, colonLoc, lSquareLoc, rSquareLoc) |
| 60 | + } |
| 61 | + |
| 62 | + public func visit(_ node: MetatypeTypeSyntax) -> UnsafeMutableRawPointer { |
| 63 | + let baseType = visit(node.baseType) |
| 64 | + let tyLoc = self.base.advanced(by: node.typeOrProtocol.position.utf8Offset).raw |
| 65 | + if node.typeOrProtocol.text == "Type" { |
| 66 | + return MetatypeTypeRepr_create(self.ctx, baseType, tyLoc) |
| 67 | + } else { |
| 68 | + assert(node.typeOrProtocol.text == "Protocol") |
| 69 | + return ProtocolTypeRepr_create(self.ctx, baseType, tyLoc) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public func visit(_ node: ImplicitlyUnwrappedOptionalTypeSyntax) -> UnsafeMutableRawPointer { |
| 74 | + let base = visit(node.wrappedType) |
| 75 | + let exclaimLoc = self.base.advanced(by: node.exclamationMark.position.utf8Offset).raw |
| 76 | + return ImplicitlyUnwrappedOptionalTypeRepr_create(self.ctx, base, exclaimLoc) |
| 77 | + } |
| 78 | + |
| 79 | + public func visit(_ node: OptionalTypeSyntax) -> UnsafeMutableRawPointer { |
| 80 | + let base = visit(node.wrappedType) |
| 81 | + let questionLoc = self.base.advanced(by: node.questionMark.position.utf8Offset).raw |
| 82 | + return OptionalTypeRepr_create(self.ctx, base, questionLoc) |
| 83 | + } |
| 84 | + |
| 85 | + public func visit(_ node: PackExpansionTypeSyntax) -> UnsafeMutableRawPointer { |
| 86 | + let base = visit(node.patternType) |
| 87 | + let ellipsisLoc = self.base.advanced(by: node.ellipsis.position.utf8Offset).raw |
| 88 | + return PackExpansionTypeRepr_create(self.ctx, base, ellipsisLoc) |
| 89 | + } |
| 90 | + |
| 91 | + public func visit(_ node: TupleTypeSyntax) -> UnsafeMutableRawPointer { |
| 92 | + return self.withBridgedTupleElements(node.elements) { elements in |
| 93 | + let lParenLoc = self.base.advanced(by: node.leftParen.position.utf8Offset).raw |
| 94 | + let rParenLoc = self.base.advanced(by: node.rightParen.position.utf8Offset).raw |
| 95 | + return TupleTypeRepr_create(self.ctx, elements, lParenLoc, rParenLoc) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public func visit(_ node: CompositionTypeSyntax) -> UnsafeMutableRawPointer { |
| 100 | + assert(node.elements.count > 1) |
| 101 | + let types = node.elements.map { visit($0.type) } |
| 102 | + let firstTypeLoc = self.base.advanced(by: node.elements.first!.type.position.utf8Offset).raw |
| 103 | + return types.withBridgedArrayRef { types in |
| 104 | + return CompositionTypeRepr_create(self.ctx, types, firstTypeLoc) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public func visit(_ node: FunctionTypeSyntax) -> UnsafeMutableRawPointer { |
| 109 | + return self.withBridgedTupleElements(node.arguments) { elements in |
| 110 | + let lParenLoc = self.base.advanced(by: node.leftParen.position.utf8Offset).raw |
| 111 | + let rParenLoc = self.base.advanced(by: node.rightParen.position.utf8Offset).raw |
| 112 | + let args = TupleTypeRepr_create(self.ctx, elements, lParenLoc, rParenLoc) |
| 113 | + let asyncLoc = node.asyncKeyword.map { self.base.advanced(by: $0.position.utf8Offset).raw } |
| 114 | + let throwsLoc = node.throwsOrRethrowsKeyword.map { self.base.advanced(by: $0.position.utf8Offset).raw } |
| 115 | + let arrowLoc = self.base.advanced(by: node.arrow.position.utf8Offset).raw |
| 116 | + let retTy = visit(node.returnType) |
| 117 | + return FunctionTypeRepr_create(self.ctx, args, asyncLoc, throwsLoc, arrowLoc, retTy) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public func visit(_ node: NamedOpaqueReturnTypeSyntax) -> UnsafeMutableRawPointer { |
| 122 | + let baseTy = visit(node.baseType) |
| 123 | + return NamedOpaqueReturnTypeRepr_create(self.ctx, baseTy) |
| 124 | + } |
| 125 | + |
| 126 | + public func visit(_ node: ConstrainedSugarTypeSyntax) -> UnsafeMutableRawPointer { |
| 127 | + let someOrAnyLoc = self.base.advanced(by: node.someOrAnySpecifier.position.utf8Offset).raw |
| 128 | + let baseTy = visit(node.baseType) |
| 129 | + if node.someOrAnySpecifier.text == "some" { |
| 130 | + return OpaqueReturnTypeRepr_create(self.ctx, someOrAnyLoc, baseTy) |
| 131 | + } else { |
| 132 | + assert(node.someOrAnySpecifier.text == "any") |
| 133 | + return ExistentialTypeRepr_create(self.ctx, someOrAnyLoc, baseTy) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public func visit(_ node: AttributedTypeSyntax) -> UnsafeMutableRawPointer { |
| 138 | + // FIXME: Respect the attributes |
| 139 | + return visit(node.baseType) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +extension ASTGenVisitor { |
| 144 | + private func withBridgedTupleElements<T>( |
| 145 | + _ elementList: TupleTypeElementListSyntax, |
| 146 | + action: (BridgedArrayRef) -> T |
| 147 | + ) -> T { |
| 148 | + var elements = [BridgedTupleTypeElement]() |
| 149 | + for element in elementList { |
| 150 | + var nameText = element.name?.text |
| 151 | + let name = nameText?.withUTF8 { buf in |
| 152 | + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) |
| 153 | + } |
| 154 | + let nameLoc = element.name.map { self.base.advanced(by: $0.position.utf8Offset).raw } |
| 155 | + var secondNameText = element.secondName?.text |
| 156 | + let secondName = secondNameText?.withUTF8 { buf in |
| 157 | + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) |
| 158 | + } |
| 159 | + let secondNameLoc = element.secondName.map { self.base.advanced(by: $0.position.utf8Offset).raw } |
| 160 | + let colonLoc = element.colon.map { self.base.advanced(by: $0.position.utf8Offset).raw } |
| 161 | + let type = visit(element.type) |
| 162 | + let trailingCommaLoc = element.trailingComma.map { self.base.advanced(by: $0.position.utf8Offset).raw } |
| 163 | + |
| 164 | + elements.append(BridgedTupleTypeElement( |
| 165 | + Name: name, |
| 166 | + NameLoc: nameLoc, |
| 167 | + SecondName: secondName, |
| 168 | + SecondNameLoc: secondNameLoc, |
| 169 | + UnderscoreLoc: nil, /*N.B. Only important for SIL*/ |
| 170 | + ColonLoc: colonLoc, |
| 171 | + Type: type, |
| 172 | + TrailingCommaLoc: trailingCommaLoc)) |
| 173 | + } |
| 174 | + return elements.withBridgedArrayRef { elements in |
| 175 | + return action(elements) |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments