Skip to content

Commit

Permalink
Implement GenericRequirement support for member type disambiguation (#…
Browse files Browse the repository at this point in the history
…1283)

* Implement GenericRequirement support for member type disambiguation

* Reverted redundant change

* Added missing argument for Type_Linux

* Updated generated files

* Moved Type+SwiftSyntax to separate file

* Removed redundant protocol conformance
  • Loading branch information
art-divin authored Mar 3, 2024
1 parent 8873f60 commit 0783692
Show file tree
Hide file tree
Showing 18 changed files with 398 additions and 105 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "cd793adf5680e138bf2bcbaacc292490175d0dcd",
"version" : "508.0.0"
"revision" : "2c49d66d34dfd6f8130afdba889de77504b58ec0",
"version" : "508.0.1"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ extension Class {
convenience init(_ node: ClassDeclSyntax, parent: Type?, annotationsParser: AnnotationsParser) {
let modifiers = node.modifiers?.map(Modifier.init) ?? []

let genericRequirements: [GenericRequirement] = node.genericWhereClause?.requirementList.compactMap { requirement in
if let sameType = requirement.body.as(SameTypeRequirementSyntax.self) {
return GenericRequirement(sameType)
} else if let conformanceType = requirement.body.as(ConformanceRequirementSyntax.self) {
return GenericRequirement(conformanceType)
}
return nil
} ?? []

self.init(
name: node.identifier.text.trimmingCharacters(in: .whitespaces),
parent: parent,
Expand All @@ -17,6 +26,7 @@ extension Class {
inheritedTypes: node.inheritanceClause?.inheritedTypeCollection.map { $0.typeName.description.trimmed } ?? [],
containedTypes: [],
typealiases: [],
genericRequirements: genericRequirements,
attributes: Attribute.from(node.attributes),
modifiers: modifiers.map(SourceryModifier.init),
annotations: annotationsParser.annotations(from: node),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import SourceryRuntime
extension GenericRequirement {
convenience init(_ node: SameTypeRequirementSyntax) {
let leftType = node.leftTypeIdentifier.description.trimmed
let rightType = TypeName(node.rightTypeIdentifier.description.trimmed)
self.init(leftType: .init(name: leftType), rightType: .init(typeName: rightType), relationship: .equals)
let rightTypeName = TypeName(node.rightTypeIdentifier.description.trimmed)
let rightType = Type(name: rightTypeName.unwrappedTypeName)
let protocolType = SourceryProtocol(name: rightTypeName.unwrappedTypeName, implements: [rightTypeName.unwrappedTypeName: rightType])
self.init(leftType: .init(name: leftType), rightType: .init(typeName: rightTypeName, type: protocolType), relationship: .equals)
}

convenience init(_ node: ConformanceRequirementSyntax) {
let leftType = node.leftTypeIdentifier.description.trimmed
let rightType = TypeName(node.rightTypeIdentifier.description.trimmed)
self.init(leftType: .init(name: leftType), rightType: .init(typeName: rightType), relationship: .conformsTo)
let rightTypeName = TypeName(node.rightTypeIdentifier.description.trimmed)
let rightType = Type(name: rightTypeName.unwrappedTypeName)
let protocolType = SourceryProtocol(name: rightTypeName.unwrappedTypeName, implements: [rightTypeName.unwrappedTypeName: rightType])
self.init(leftType: .init(name: leftType), rightType: .init(typeName: rightTypeName, type: protocolType), relationship: .conformsTo)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ extension Struct {
convenience init(_ node: StructDeclSyntax, parent: Type?, annotationsParser: AnnotationsParser) {
let modifiers = node.modifiers?.map(Modifier.init) ?? []

let genericRequirements: [GenericRequirement] = node.genericWhereClause?.requirementList.compactMap { requirement in
if let sameType = requirement.body.as(SameTypeRequirementSyntax.self) {
return GenericRequirement(sameType)
} else if let conformanceType = requirement.body.as(ConformanceRequirementSyntax.self) {
return GenericRequirement(conformanceType)
}
return nil
} ?? []

self.init(
name: node.identifier.text.trimmed,
parent: parent,
Expand All @@ -17,6 +26,7 @@ extension Struct {
inheritedTypes: node.inheritanceClause?.inheritedTypeCollection.map { $0.typeName.description.trimmed } ?? [],
containedTypes: [],
typealiases: [],
genericRequirements: genericRequirements,
attributes: Attribute.from(node.attributes),
modifiers: modifiers.map(SourceryModifier.init),
annotations: annotationsParser.annotations(from: node),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation
import SourceryRuntime
import SwiftSyntax

extension Type {
convenience init?(_ node: TypeSyntax) {
guard let typeIdentifier = node.as(SimpleTypeIdentifierSyntax.self) else { return nil }
let name = typeIdentifier.name.text.trimmed
let generic = typeIdentifier.genericArgumentClause.map { GenericType(name: typeIdentifier.name.text, node: $0) }
self.init(name: name, isGeneric: generic != nil)
}
}
8 changes: 6 additions & 2 deletions SourceryRuntime/Sources/AST/Actor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public final class Actor: Type {
inheritedTypes: [String] = [],
containedTypes: [Type] = [],
typealiases: [Typealias] = [],
genericRequirements: [GenericRequirement] = [],
attributes: AttributeList = [:],
modifiers: [SourceryModifier] = [],
annotations: [String: NSObject] = [:],
documentation: [String] = [],
isGeneric: Bool = false) {
isGeneric: Bool = false,
implements: [String: Type] = [:]) {
super.init(
name: name,
parent: parent,
Expand All @@ -41,11 +43,13 @@ public final class Actor: Type {
inheritedTypes: inheritedTypes,
containedTypes: containedTypes,
typealiases: typealiases,
genericRequirements: genericRequirements,
attributes: attributes,
modifiers: modifiers,
annotations: annotations,
documentation: documentation,
isGeneric: isGeneric
isGeneric: isGeneric,
implements: implements
)
}

Expand Down
8 changes: 6 additions & 2 deletions SourceryRuntime/Sources/AST/Class.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ public final class Class: Type {
inheritedTypes: [String] = [],
containedTypes: [Type] = [],
typealiases: [Typealias] = [],
genericRequirements: [GenericRequirement] = [],
attributes: AttributeList = [:],
modifiers: [SourceryModifier] = [],
annotations: [String: NSObject] = [:],
documentation: [String] = [],
isGeneric: Bool = false) {
isGeneric: Bool = false,
implements: [String: Type] = [:]) {
super.init(
name: name,
parent: parent,
Expand All @@ -40,11 +42,13 @@ public final class Class: Type {
inheritedTypes: inheritedTypes,
containedTypes: containedTypes,
typealiases: typealiases,
genericRequirements: genericRequirements,
attributes: attributes,
modifiers: modifiers,
annotations: annotations,
documentation: documentation,
isGeneric: isGeneric
isGeneric: isGeneric,
implements: implements
)
}

Expand Down
21 changes: 6 additions & 15 deletions SourceryRuntime/Sources/AST/Protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class Protocol: Type {
}

/// list of generic requirements
public var genericRequirements: [GenericRequirement] {
public override var genericRequirements: [GenericRequirement] {
didSet {
isGeneric = !associatedTypes.isEmpty || !genericRequirements.isEmpty
}
Expand All @@ -50,8 +50,8 @@ public final class Protocol: Type {
attributes: AttributeList = [:],
modifiers: [SourceryModifier] = [],
annotations: [String: NSObject] = [:],
documentation: [String] = []) {
self.genericRequirements = genericRequirements
documentation: [String] = [],
implements: [String: Type] = [:]) {
self.associatedTypes = associatedTypes
super.init(
name: name,
Expand All @@ -64,11 +64,13 @@ public final class Protocol: Type {
inheritedTypes: inheritedTypes,
containedTypes: containedTypes,
typealiases: typealiases,
genericRequirements: genericRequirements,
attributes: attributes,
modifiers: modifiers,
annotations: annotations,
documentation: documentation,
isGeneric: !associatedTypes.isEmpty || !genericRequirements.isEmpty
isGeneric: !associatedTypes.isEmpty || !genericRequirements.isEmpty,
implements: implements
)
}

Expand All @@ -78,7 +80,6 @@ public final class Protocol: Type {
string += ", "
string += "kind = \(String(describing: self.kind)), "
string += "associatedTypes = \(String(describing: self.associatedTypes)), "
string += "genericRequirements = \(String(describing: self.genericRequirements))"
return string
}

Expand All @@ -89,15 +90,13 @@ public final class Protocol: Type {
return results
}
results.append(contentsOf: DiffableResult(identifier: "associatedTypes").trackDifference(actual: self.associatedTypes, expected: castObject.associatedTypes))
results.append(contentsOf: DiffableResult(identifier: "genericRequirements").trackDifference(actual: self.genericRequirements, expected: castObject.genericRequirements))
results.append(contentsOf: super.diffAgainst(castObject))
return results
}

public override var hash: Int {
var hasher = Hasher()
hasher.combine(self.associatedTypes)
hasher.combine(self.genericRequirements)
hasher.combine(super.hash)
return hasher.finalize()
}
Expand All @@ -106,7 +105,6 @@ public final class Protocol: Type {
public override func isEqual(_ object: Any?) -> Bool {
guard let rhs = object as? Protocol else { return false }
if self.associatedTypes != rhs.associatedTypes { return false }
if self.genericRequirements != rhs.genericRequirements { return false }
return super.isEqual(rhs)
}

Expand All @@ -120,20 +118,13 @@ public final class Protocol: Type {
}
fatalError()
}; self.associatedTypes = associatedTypes
guard let genericRequirements: [GenericRequirement] = aDecoder.decode(forKey: "genericRequirements") else {
withVaList(["genericRequirements"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
fatalError()
}; self.genericRequirements = genericRequirements
super.init(coder: aDecoder)
}

/// :nodoc:
override public func encode(with aCoder: NSCoder) {
super.encode(with: aCoder)
aCoder.encode(self.associatedTypes, forKey: "associatedTypes")
aCoder.encode(self.genericRequirements, forKey: "genericRequirements")
}
// sourcery:end
}
6 changes: 4 additions & 2 deletions SourceryRuntime/Sources/AST/ProtocolComposition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public final class ProtocolComposition: Type {
annotations: [String: NSObject] = [:],
isGeneric: Bool = false,
composedTypeNames: [TypeName] = [],
composedTypes: [Type]? = nil) {
composedTypes: [Type]? = nil,
implements: [String: Type] = [:]) {
self.composedTypeNames = composedTypeNames
self.composedTypes = composedTypes
super.init(
Expand All @@ -49,7 +50,8 @@ public final class ProtocolComposition: Type {
containedTypes: containedTypes,
typealiases: typealiases,
annotations: annotations,
isGeneric: isGeneric
isGeneric: isGeneric,
implements: implements
)
}

Expand Down
8 changes: 6 additions & 2 deletions SourceryRuntime/Sources/AST/Struct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public final class Struct: Type {
inheritedTypes: [String] = [],
containedTypes: [Type] = [],
typealiases: [Typealias] = [],
genericRequirements: [GenericRequirement] = [],
attributes: AttributeList = [:],
modifiers: [SourceryModifier] = [],
annotations: [String: NSObject] = [:],
documentation: [String] = [],
isGeneric: Bool = false) {
isGeneric: Bool = false,
implements: [String: Type] = [:]) {
super.init(
name: name,
parent: parent,
Expand All @@ -45,11 +47,13 @@ public final class Struct: Type {
inheritedTypes: inheritedTypes,
containedTypes: containedTypes,
typealiases: typealiases,
genericRequirements: genericRequirements,
attributes: attributes,
modifiers: modifiers,
annotations: annotations,
documentation: documentation,
isGeneric: isGeneric
isGeneric: isGeneric,
implements: implements
)
}

Expand Down
Loading

0 comments on commit 0783692

Please sign in to comment.