This is an archive of the discontinued LLVM Phabricator instance.

[ObjC++] Make parameter passing and function return compatible with ObjC
ClosedPublic

Authored by ahatanak on Mar 26 2018, 1:16 PM.

Details

Summary

r326307 and r327870 made changes that enable strong and weak fields to be declared in C structs. This patch changes the ObjC++ ABI so that structs with strong or weak pointers can be passed to or returned from functions compiled in C mode.

ObjC and ObjC++ are currently incompatible because of the differences in the way structs are passed and destructed.

For example:

typedef struct {
  id f0;
  __weak id f1;
} S;

// this code is compiled in c++.
extern “C” {
  void foo(S s);
}

void caller(S a) {
  foo(a); // clang currently passes 'a' indirectly and the caller destructs 'a'.
}

// this function is compiled in c.
void foo(S a) {  // 'a' is passed directly and is destructed in the callee.
}

This patch fixes the incompatibility by passing and returning structs with strong or weak fields using the C ABI in C++ mode: strong and weak fields in a struct do not cause the struct to be destructed in the caller and __strong fields do not make the struct to be passed indirectly.

Also, this patch fixes the microsoft ABI bug mentioned here:

https://reviews.llvm.org/D41039?id=128767#inline-364710

Diff Detail

Repository
rL LLVM

Event Timeline

ahatanak created this revision.Mar 26 2018, 1:16 PM

I tried to move the code in MicrosoftCXXABI::getRecordArgABI and ItaniumCXXABI::passClassIndirect that determine whether a struct is passed indirectly to Sema::CheckCompletedCXXClass, but couldn't do so since getClangABICompat() is a method of CodeGenOpts, which Sema doesn't have access to. This would allow removing ForcePassIndirectly.

I think it's reasonable to pull that into LangOpts. It would need to be in LangOpts anyway if we e.g. fixed an ABI bug in struct layout and needed a compatibility handling to preserve the old behavior.

ahatanak updated this revision to Diff 139935.Mar 27 2018, 8:29 AM

Make ClangABICompat a LangOption and remove ForcePassIndirectly. Move the logic in CodeGen that decides whether a parameter should be passed indirectly to Sema.

rjmccall accepted this revision.Mar 27 2018, 9:50 AM

LGTM.

This revision is now accepted and ready to land.Mar 27 2018, 9:50 AM
This revision was automatically updated to reflect the committed changes.
This revision was automatically updated to reflect the committed changes.
cfe/trunk/test/CodeGenObjCXX/objc-struct-cxx-abi.mm