Skip to content

[IRGen] Do not emit ObjC property ivar field when getters and setters are non-trivial. #21976

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
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/IRGen/GenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,7 @@ namespace {
llvm::raw_svector_ostream outs(out);

auto propTy = prop->getValueInterfaceType();
auto propDC = prop->getDeclContext();

// Emit the type encoding for the property.
outs << 'T';
Expand All @@ -1884,13 +1885,13 @@ namespace {
if (prop->getAttrs().hasAttribute<NSManagedAttr>())
outs << ",D";

auto isObject = prop->getDeclContext()->mapTypeIntoContext(propTy)
auto isObject = propDC->mapTypeIntoContext(propTy)
->hasRetainablePointerRepresentation();
auto hasObjectEncoding = typeEnc[0] == '@';

// Determine the assignment semantics.
// Get-only properties are (readonly).
if (!prop->isSettable(prop->getDeclContext()))
if (!prop->isSettable(propDC))
outs << ",R";
// Weak and Unowned properties are (weak).
else if (prop->getAttrs().hasAttribute<ReferenceOwnershipAttr>())
Expand All @@ -1908,9 +1909,12 @@ namespace {
else
(void)0;

// If the property is an instance property and has storage, emit the ivar
// name last.
if (!prop->isStatic() && prop->hasStorage())
// If the property is an instance property and has storage, and meanwhile
// its type is trivially representable in ObjC, emit the ivar name last.
bool isTriviallyRepresentable =
propTy->isTriviallyRepresentableIn(ForeignLanguage::ObjectiveC,
propDC);
if (!prop->isStatic() && prop->hasStorage() && isTriviallyRepresentable)
outs << ",V" << prop->getName();
}

Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/objc_bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ import Foundation

// CHECK: @_PROPERTIES__TtC11objc_bridge3Bas = private constant { i32, i32, [5 x { i8*, i8* }] } {

// CHECK: [[OBJC_BLOCK_PROPERTY:@.*]] = private unnamed_addr constant [11 x i8] c"T@?,N,C,Vx\00"
// CHECK: [[OBJC_BLOCK_PROPERTY:@.*]] = private unnamed_addr constant [8 x i8] c"T@?,N,C\00"
// CHECK: @_PROPERTIES__TtC11objc_bridge21OptionalBlockProperty = private constant {{.*}} [[OBJC_BLOCK_PROPERTY]]

func getDescription(_ o: NSObject) -> String {
Expand Down
8 changes: 4 additions & 4 deletions test/IRGen/objc_property_attrs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ class Foo: NSManagedObject {

// -- Bridged value types

// nonatomic, copy, ivar k
// CHECK: private unnamed_addr constant {{.*}} c"T@\22NSString\22,N,C,Vk\00"
// nonatomic, copy
// CHECK: private unnamed_addr constant {{.*}} c"T@\22NSString\22,N,C\00"
@objc var k: String = ""
// nonatomic, readonly, ivar l
// CHECK: private unnamed_addr constant {{.*}} c"T@\22NSString\22,N,R,Vl\00"
// nonatomic, readonly
// CHECK: private unnamed_addr constant {{.*}} c"T@\22NSString\22,N,R\00"
@objc let l: String? = nil

// -- Protocol types:
Expand Down