Skip to content

[ASTGen] Generate AvailableAttr #79125

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

Merged
merged 1 commit into from
Feb 5, 2025

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Feb 4, 2025

  • Move AvailabilitySpec handling logic to AST, so they can be shared between libParse and ASTGen
  • Requestify -define-availability arguments parsing and parse them with SwiftParser according to the ParserASTGen feature flag
  • Implement AvailableAttr generation in ASTGen

@rintaro
Copy link
Member Author

rintaro commented Feb 4, 2025

swiftlang/swift-syntax#2954
@swift-ci Please smoke test

2 similar comments
@rintaro
Copy link
Member Author

rintaro commented Feb 4, 2025

swiftlang/swift-syntax#2954
@swift-ci Please smoke test

@rintaro
Copy link
Member Author

rintaro commented Feb 4, 2025

swiftlang/swift-syntax#2954
@swift-ci Please smoke test

@rintaro rintaro force-pushed the astgen-available-attr branch 3 times, most recently from 9333a24 to b8c8b20 Compare February 4, 2025 20:01
@rintaro rintaro marked this pull request as ready for review February 4, 2025 21:21
@rintaro rintaro force-pushed the astgen-available-attr branch from b8c8b20 to 6a0fda6 Compare February 4, 2025 21:51
@rintaro
Copy link
Member Author

rintaro commented Feb 4, 2025

swiftlang/swift-syntax#2954
@swift-ci Please smoke test

@rintaro rintaro force-pushed the astgen-available-attr branch from 6a0fda6 to cf62f3d Compare February 5, 2025 03:38
* Move `AvailabilitySpec` handling logic to AST, so they can be shared
  between libParse and ASTGen
* Requestify '-define-availability' arguments parsing and parse them
  with 'SwiftParser' according to the 'ParserASTGen' feature flag
* Implement 'AvailableAttr' generation in ASTGen
@rintaro rintaro force-pushed the astgen-available-attr branch from cf62f3d to df2ada3 Compare February 5, 2025 07:40
@rintaro
Copy link
Member Author

rintaro commented Feb 5, 2025

swiftlang/swift-syntax#2954
@swift-ci Please smoke test

Comment on lines +167 to +173
const void *_Nullable BridgedArrayRef_data(const BridgedArrayRef arr);

SWIFT_NAME("getter:BridgedArrayRef.count(self:)")
BRIDGED_INLINE SwiftInt BridgedArrayRef_count(BridgedArrayRef arr);
BRIDGED_INLINE SwiftInt BridgedArrayRef_count(const BridgedArrayRef arr);

SWIFT_NAME("getter:BridgedArrayRef.isEmpty(self:)")
BRIDGED_INLINE bool BridgedArrayRef_isEmpty(const BridgedArrayRef arr);
Copy link
Contributor

Choose a reason for hiding this comment

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

The consts on the parameters don't do anything, right?

Comment on lines +26 to +36
const void *_Nullable BridgedArrayRef_data(const BridgedArrayRef arr) {
return arr.Data;
}

SwiftInt BridgedArrayRef_count(BridgedArrayRef arr) {
SwiftInt BridgedArrayRef_count(const BridgedArrayRef arr) {
return static_cast<SwiftInt>(arr.Length);
}

bool BridgedArrayRef_isEmpty(const BridgedArrayRef arr) {
return arr.Length == 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar comment, although these do prevent mutating the variable itself, but that's not too helpful

@@ -0,0 +1,426 @@
//===--- Attrs.swift ------------------------------------------------------===//
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
//===--- Attrs.swift ------------------------------------------------------===//
//===--- Availability.swift -----------------------------------------------===//

let atLoc = self.generateSourceLoc(node.atSign)
let range = self.generateAttrSourceRange(node)
let specs = self.generateAvailabilitySpecList(
args: node.arguments!.cast(AvailabilityArgumentListSyntax.self),
Copy link
Contributor

Choose a reason for hiding this comment

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

Worth passing through the args so you don't have to do this again?

}

func generateAvailableAttrExtended(attribute node: AttributeSyntax) -> BridgedAvailableAttr? {
var args = node.arguments!.cast(AvailabilityArgumentListSyntax.self)[...]
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto

var renamed: BridgedStringRef? = nil

func generateVersion(arg: AvailabilityLabeledArgumentSyntax, into target: inout VersionAndRange?) {
guard let versionSytnax = arg.value.as(VersionTupleSyntax.self) else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
guard let versionSytnax = arg.value.as(VersionTupleSyntax.self) else {
guard let versionSyntax = arg.value.as(VersionTupleSyntax.self) else {


extension BridgedArrayRef {
public func withElements<T, R>(ofType ty: T.Type, _ c: (UnsafeBufferPointer<T>) -> R) -> R {
let start = data?.bindMemory(to: ty, capacity: count)
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like this should probably either use withMemoryRebound(to:) or assumingMemoryBound(to:)

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, I'd go with assumingMemoryBound. I copied this from SwiftCompilerSources, so I will change both in followups.

/// Parse the '-define-availability' arguments.
class AvailabilityMacroArgumentsRequest
: public SimpleRequest<AvailabilityMacroArgumentsRequest,
AvailabilityMacroMap *(ASTContext *),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should hand back a const pointer

Suggested change
AvailabilityMacroMap *(ASTContext *),
const AvailabilityMacroMap *(ASTContext *),


BridgedAvailabilityMacroMap
BridgedASTContext_getAvailabilityMacroMap(BridgedASTContext cContext) {
return const_cast<AvailabilityMacroMap *>(
Copy link
Contributor

@hamishknight hamishknight Feb 5, 2025

Choose a reason for hiding this comment

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

Would be nice if BridgedAvailabilityMacroMap could store a const pointer, maybe we should define it manually instead of using AST_BRIDGING_WRAPPER? Or maybe we could add a const variant of that macro

@rintaro
Copy link
Member Author

rintaro commented Feb 5, 2025

Thanks @hamishknight ! I will address your comments in followups

@rintaro rintaro merged commit a619daf into swiftlang:main Feb 5, 2025
3 checks passed
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.

3 participants