Skip to content

[cxx-interop] Import iterator dereference operators #59465

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
Jun 16, 2022

Conversation

egorzhdan
Copy link
Contributor

@egorzhdan egorzhdan commented Jun 15, 2022

C++ iterator dereference operator is mapped to a Swift computed property called pointee.

For example:

struct ConstIterator {
  // ...
  const int &operator*() const { /* ... */ }
};

is imported as

struct ConstIterator {
  var pointee: Int32 { get }
  @available(*, unavailable, message: "use .pointee property")
  func __operatorStar() -> UnsafePointer<Int32>
}

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Jun 15, 2022
@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-operator-dereference branch 2 times, most recently from e7dfa93 to 8e3e243 Compare June 16, 2022 10:39
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@@ -8101,6 +8123,47 @@ SwiftDeclConverter::makeSubscript(FuncDecl *getter, FuncDecl *setter) {
return subscript;
}

VarDecl *
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Side note: ImportDecl.cpp is way too big, I'll move the decl synthesis logic into a separate file in another PR.

@egorzhdan egorzhdan requested review from hyp, Huddie and zoecarver June 16, 2022 10:57
@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-operator-dereference branch 2 times, most recently from ca4cb6a to 4c38e37 Compare June 16, 2022 11:34
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

Copy link
Contributor

@Huddie Huddie left a comment

Choose a reason for hiding this comment

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

This looks great! One naming question: Is operator*() known as an iterator dereference or just "dereference" ? Sounds like if a class has operator*() its by def an Iterator, just want to confirm thats the case

@egorzhdan
Copy link
Contributor Author

Hmm, I can't find the conventional name of this operator in C++ reference.
operator*() is sometimes also defined for custom pointers, e.g. std::unique_ptr. However, we might want to prohibit using those types from Swift. I think the only valid use case of operator*() from Swift is iterator dereferencing (but I might be missing something).

@egorzhdan
Copy link
Contributor Author

Actually that's a good point @Huddie. Calling operator*() "iterator dereference" is probably not always correct, I'll adjust the naming. Thanks!

C++ iterator dereference operator is mapped to a Swift computed property called `pointee`.

For example:
```cpp
struct ConstIterator {
  // ...
  const int &operator*() const { /* ... */ }
};
```
is imported as
```swift
struct ConstIterator {
  var pointee: Int32 { get }
  @available(*, unavailable, message: "use .pointee property")
  func __operatorStar() -> UnsafePointer<Int32>
}
```
@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-operator-dereference branch from 4c38e37 to 40a7e68 Compare June 16, 2022 15:59
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

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

Lovely, as per usual :)

@egorzhdan egorzhdan merged commit 9b1f5e6 into main Jun 16, 2022
@egorzhdan egorzhdan deleted the egorzhdan/cxx-operator-dereference branch June 16, 2022 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants