Skip to content

Add support for Objective-C class/static methods#7762

Merged
dlang-bot merged 1 commit intodlang:masterfrom
jacob-carlborg:objc_class_methods
Mar 3, 2018
Merged

Add support for Objective-C class/static methods#7762
dlang-bot merged 1 commit intodlang:masterfrom
jacob-carlborg:objc_class_methods

Conversation

@jacob-carlborg
Copy link
Copy Markdown
Contributor

@jacob-carlborg jacob-carlborg commented Jan 22, 2018

A few missing things:

  • The header files need to be updated
  • Could use some more documentation
  • Need to add changelog
  • A PR to update the documentation in dlang.org

Documentation of the Objective-C ABI has been started, available in a separate branch: https://github.com/jacob-carlborg/dmd/blob/objc_abi_docs/docs/objective-c_abi.md.

The dlang.org part dlang/dlang.org#2246.

@dlang-bot
Copy link
Copy Markdown
Contributor

dlang-bot commented Jan 22, 2018

Thanks for your pull request and interest in making D better, @jacob-carlborg! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the annotated coverage diff directly on GitHub with CodeCov's browser extension
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

@jacob-carlborg jacob-carlborg force-pushed the objc_class_methods branch 4 times, most recently from 60c78e8 to 2019571 Compare January 23, 2018 15:40
@jacob-carlborg jacob-carlborg force-pushed the objc_class_methods branch 3 times, most recently from 741d089 to 4eadda2 Compare January 25, 2018 16:04
@stefan-koch-sociomantic
Copy link
Copy Markdown

There is not a word about what this actually does.

@jacob-carlborg
Copy link
Copy Markdown
Contributor Author

There is not a word about what this actually does.

Yeah, that's why the unchecked list item, "Could use some more documentation", in the description.

@jacob-carlborg
Copy link
Copy Markdown
Contributor Author

jacob-carlborg commented Jan 25, 2018

@stefan-koch-sociomantic On a high level the Objective-C ABI looks something like this:

extern (Objective-C) interface NSObject
{
    NSObject init() @selector("init");
}
NSObject o;
o.init();

The above method call is translated to a regular C function call in the form of: objc_msgSend(o, "init"). o would be the this pointer and "init" is what was specified in the @selector UDA in the method declaration. That is for a regular instance method. For a class method it works the same way, it's just a different this pointer. In Objective-C, a class is an object and it's an instance of a metaclass. An instance of ClassDeclaration is used as the Objective-C metaclass.

For a more detailed explanation see the commit message of the first phase of the Objective-C integration 867d547.

There's also this Wikipedia page about Objective-C metaclasses [1].

[1] https://en.wikipedia.org/wiki/Metaclass#In_Objective-C

@jacob-carlborg jacob-carlborg force-pushed the objc_class_methods branch 2 times, most recently from 9217b2c to ff304fa Compare January 28, 2018 22:19

There are several Objective-C ABIs available:

* Apple legacy ABI (also know as fragile ABI) - used on macOS 32bit and the old
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just so I have the link correct in my head, this is also known as the Nextstep abi?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. I've moved the work on the ABI documentation to a separate branch until it's ready [1]. The description in the separate branch is a bit more clear and thorough.

[1] https://github.com/jacob-carlborg/dmd/blob/objc_abi_docs/docs/objective-c_abi.md

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK.

I've just checked, and gcc objc seems to support all of these ABIs. I should see if I can re-use the existing code sometime in gdc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Before Apple used Clang, they used GCC with this ABI that is implemented here. I don't think anything has changed since then. I'm not sure if that was only supported in their own version of GCC or in the standard version as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

BTW, some code review would be nice 😉.

@JinShil JinShil added the Review:WIP Work In Progress - not ready for review or pulling label Feb 18, 2018
@JinShil
Copy link
Copy Markdown
Contributor

JinShil commented Feb 18, 2018

I don't see any issue with the code in this PR, but I don't know anything about Objective-C, or what D's goals are in supporting it. Isn't Objective-C reaching end of life with the emergence of Swift?

@jacob-carlborg
Copy link
Copy Markdown
Contributor Author

I don't see any issue with the code in this PR, but I don't know anything about Objective-C, or what D's goals are in supporting it.

D's goals are to support as much of Objective-C as possible or what's necessary to implement meaningful Cocoa applications [1].

Isn't Objective-C reaching end of life with the emergence of Swift?

All of the Apple frameworks are still implemented in Objective-C. Apple is not, most likely, going to replace them in the near future. Swift can interface with Objective-C as well, which is a crucial part since all the frameworks are written in Objective-C. That means that D can interface with Swift code as well, assuming both use the Objective-C interface. Apple might implement new frameworks using Swift and possible rewrite existing frameworks as well, but they would need to support an Objective-C interface for a significant future.

Why was the WIP label added? I think it's possible to review even if some documentation is missing.

[1] https://github.com/dlang/DIPs/blob/master/DIPs/archive/DIP43.md

@JinShil
Copy link
Copy Markdown
Contributor

JinShil commented Feb 18, 2018

Why was the WIP label added? I think it's possible to review even if some documentation is missing.

Documentation helps the review, and if, from the author's perspective, a PR is not complete enough to be merged, it is indeed a WIP.

@jacob-carlborg
Copy link
Copy Markdown
Contributor Author

Documentation helps the review, and if, from the author's perspective, a PR is not complete enough to be merged, it is indeed a WIP.

Well, it depends. I think the code is ready as is. Documentation is missing yes, but keep in mind that most code in DMD was reviewed and merged without documentation. I mean, I don't see any documentation describing any of the C++ ABIs.

src/dmd/objc.d Outdated
else
{
error("base " ~ errorType ~ " for an Objective-C " ~
errorType ~ " must be extern (Objective-C)");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

tick marks around extern (Objective-C), I believe.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, good catch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed.


private:

char[] format(size_t bufLength, Args...)(return ref char[bufLength] buffer,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ddoc comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's why the "Could use some more documentation" checkbox in the PR description is not checked 😃.

return symbol;
}

Symbol* getClassReference(const ClassDeclaration classDeclaration)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ddoc comment

return moduleInfo;
}

Symbol* getClassName(const ClassDeclaration classDeclaration)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ddoc comment

return false;
}

Symbol* symbolName(const(char)[] name, int sclass, type* t)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ddoc comment for these three

src/dmd/objc.d Outdated
}
}

private void setMetaclass(alias newMetaclass, T)(T classDeclaration, Scope* sc)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ddoc comment

src/dmd/objc.d Outdated


/**
* This struct contains all data for a class declaration that is needed for the
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

remove This struct contains. It's redundant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok.

@jacob-carlborg
Copy link
Copy Markdown
Contributor Author

Please see #7923 as well.

@jacob-carlborg jacob-carlborg force-pushed the objc_class_methods branch 2 times, most recently from 87eafec to 201059e Compare February 23, 2018 13:36
@jacob-carlborg
Copy link
Copy Markdown
Contributor Author

Added documentation.

@jacob-carlborg
Copy link
Copy Markdown
Contributor Author

jacob-carlborg commented Feb 23, 2018

Created a PR for the dlang.org part now as well: dlang/dlang.org#2246.

Everything on my todo list has been completed. This is ready to go.

@JinShil JinShil removed the Review:WIP Work In Progress - not ready for review or pulling label Feb 28, 2018
@JinShil JinShil added the Merge:72h no objection -> merge The PR will be merged if there are no objections raised. label Feb 28, 2018
@jacob-carlborg
Copy link
Copy Markdown
Contributor Author

@JinShil more than 72 hours have passed.

@dlang-bot dlang-bot merged commit acacaad into dlang:master Mar 3, 2018
@jacob-carlborg
Copy link
Copy Markdown
Contributor Author

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merge:auto-merge Merge:72h no objection -> merge The PR will be merged if there are no objections raised.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants