Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeSyntax util functions #2886

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

hnrklssn
Copy link

This moves isVoid from SwiftRefactor to be publicly accessible in SwiftSyntax. Also adds isSwiftCoreModule and isSwiftInt and calls isSwiftCoreModule in isVoid and isSwiftInt to properly handle the qualified cases of Swift.Void and Swift.Int.

This moves isVoid from SwiftRefactor to be publicly
accessible in SwiftSyntax. Also adds isSwiftCoreModule and isSwiftInt
and calls isSwiftCoreModule in isVoid and isSwiftInt to properly handle
the qualified cases of Swift.Void and Swift.Int.
@hnrklssn
Copy link
Author

@swift-ci Please smoke test

Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like these APIs are in an unfortunate middle ground where they aren't just one-offs we should add and live with, nor are they along the path to incrementally grow into the full solution. I think a full solution needs to be able to incorporate many (most?) of the Swift standard library types, so we can use it in various source-to-source translation tools. As an example, we have something similar to these APIs in the swift-java project... but there we handle all of the integer and floating-point types that have Java primitive equivalents, as well as things like unsafe (buffer) pointers.

Sources/SwiftSyntax/Utils.swift Outdated Show resolved Hide resolved
@@ -102,3 +102,29 @@ extension RawUnexpectedNodesSyntax {
self.init(raw: raw)
}
}

extension TypeSyntax {
public var isVoid: Bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide documentation comments for all public APIs. Once we settle on them, please add an entry to the release notes.

Comment on lines 116 to 122
public var isSwiftInt: Bool {
switch self.as(TypeSyntaxEnum.self) {
case .identifierType(let identifierType) where identifierType.name.text == "Int": return true
case .memberType(let memberType) where memberType.name.text == "Int": return memberType.baseType.isSwiftCoreModule
default: return false
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is largely a copy of the isVoid implementation above. I'm concerned that this approach to the API doesn't really scale: are we going to end up growing isSwiftUInt, isSwiftBool, isSwiftSet, and others as separate properties? That doesn't seem like a desirable end state.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a way to generalise it to types where the canonical name only contains MemberTypeSyntax or IdentifierTypeSyntax. This doesn't include Void since it's a tuple, and there doesn't seem to be a good way to generalise meta types for tuples; I couldn't find anything like Tuple.Type, so handling tuple types would involve enumerating every arity. Not sure how to handle type shadowing either, since there doesn't seem to be a way to reify module<Swift>.self.

canRepresentBasicType() can match against any canonical type named by
only dots and identifiers.
@hnrklssn
Copy link
Author

@swift-ci Please smoke test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants