llvm-project/clang/test/SemaObjC/method-direct-properties.m

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

127 lines
5.2 KiB
Mathematica
Raw Normal View History

Implement __attribute__((objc_direct)), __attribute__((objc_direct_members)) __attribute__((objc_direct)) is an attribute on methods declaration, and __attribute__((objc_direct_members)) on implementation, categories or extensions. A `direct` property specifier is added (@property(direct) type name) These attributes / specifiers cause the method to have no associated Objective-C metadata (for the property or the method itself), and the calling convention to be a direct C function call. The symbol for the method has enforced hidden visibility and such direct calls are hence unreachable cross image. An explicit C function must be made if so desired to wrap them. The implicit `self` and `_cmd` arguments are preserved, however to maintain compatibility with the usual `objc_msgSend` semantics, 3 fundamental precautions are taken: 1) for instance methods, `self` is nil-checked. On arm64 backends this typically adds a single instruction (cbz x0, <closest-ret>) to the codegen, for the vast majority of the cases when the return type is a scalar. 2) for class methods, because the class may not be realized/initialized yet, a call to `[self self]` is emitted. When the proper deployment target is used, this is optimized to `objc_opt_self(self)`. However, long term we might want to emit something better that the optimizer can reason about. When inlining kicks in, these calls aren't optimized away as the optimizer has no idea that a single call is really necessary. 3) the calling convention for the `_cmd` argument is changed: the caller leaves the second argument to the call undefined, and the selector is loaded inside the body when it's referenced only. As far as error reporting goes, the compiler refuses: - making any overloads direct, - making an overload of a direct method, - implementations marked as direct when the declaration in the interface isn't (the other way around is allowed, as the direct attribute is inherited from the declaration), - marking methods required for protocol conformance as direct, - messaging an unqualified `id` with a direct method, - forming any @selector() expression with only direct selectors. As warnings: - any inconsistency of direct-related calling convention when @selector() or messaging is used, - forming any @selector() expression with a possibly direct selector. Lastly an `objc_direct_members` attribute is added that can decorate `@implementation` blocks and causes methods only declared there (and in no `@interface`) to be automatically direct. When decorating an `@interface` then all methods and properties declared in this block are marked direct. Radar-ID: rdar://problem/2684889 Differential Revision: https://reviews.llvm.org/D69991 Reviewed-By: John McCall
2019-11-08 15:14:58 +08:00
// RUN: %clang_cc1 -fsyntax-only -verify -Wselector-type-mismatch %s
@protocol ProtoDirectFail
@property(nonatomic, direct) int protoProperty; // expected-error {{'objc_direct' attribute cannot be applied to properties declared in an Objective-C protocol}}
@end
__attribute__((objc_root_class))
@interface Root
@property(nonatomic, direct) int propertyWithNonDirectGetter; // expected-note {{previous declaration is here}}
- (int)propertyWithNonDirectGetter;
- (int)propertyWithNonDirectGetter2;
- (int)propertyWithNonDirectGetterInParent;
- (int)propertyWithNonDirectGetterInParent2;
@property(nonatomic, readonly, direct) int getDirect_setDynamic; // expected-note {{previous declaration is here}}
@property(nonatomic, readonly, direct) int getDirect_setDirect; // expected-note {{previous declaration is here}}
@property(nonatomic, readonly, direct) int getDirect_setDirectMembers; // expected-note {{previous declaration is here}}
@property(nonatomic, readonly) int getDynamic_setDirect;
@property(nonatomic, readonly) int getDynamic_setDirectMembers;
@property(nonatomic, readonly) int dynamicProperty;
@property(nonatomic, readonly) int synthDynamicProperty;
@property(nonatomic, readonly, direct) int directProperty; // expected-note {{previous declaration is here}}
@property(nonatomic, readonly, direct) int synthDirectProperty; // expected-note {{previous declaration is here}}
@end
__attribute__((objc_direct_members))
@interface
Root()
@property(nonatomic) int propertyWithNonDirectGetter2; // expected-note {{previous declaration is here}}
@property(nonatomic, readwrite) int getDirect_setDirectMembers; // expected-note {{previous declaration is here}}
@property(nonatomic, readwrite) int getDynamic_setDirectMembers; // expected-note {{previous declaration is here}}
@end
@interface Root ()
@property(nonatomic, readwrite) int getDirect_setDynamic;
@property(nonatomic, readwrite, direct) int getDirect_setDirect; // expected-note {{previous declaration is here}}
@property(nonatomic, readwrite, direct) int getDynamic_setDirect; // expected-note {{previous declaration is here}}
@end
@interface Sub : Root
@property(nonatomic, direct) int propertyWithNonDirectGetterInParent; // expected-note {{previous declaration is here}}
- (int)propertyWithNonDirectGetter; // no error: legal override
- (int)propertyWithNonDirectGetter2; // no error: legal override
- (int)propertyWithNonDirectGetterInParent; // no error: legal override
- (int)propertyWithNonDirectGetterInParent2; // no error: legal override
@end
__attribute__((objc_direct_members))
@interface Sub ()
@property(nonatomic) int propertyWithNonDirectGetterInParent2; // expected-note {{previous declaration is here}}
@end
// make sure that the `directness` of methods stuck,
// by observing errors trying to override the setter
@interface SubWitness : Sub
- (int)setPropertyWithNonDirectGetter:(int)value; // expected-error {{cannot override a method that is declared direct by a superclass}}
- (int)setPropertyWithNonDirectGetter2:(int)value; // expected-error {{cannot override a method that is declared direct by a superclass}}
- (int)setPropertyWithNonDirectGetterInParent:(int)value; // expected-error {{cannot override a method that is declared direct by a superclass}}
- (int)setPropertyWithNonDirectGetterInParent2:(int)value; // expected-error {{cannot override a method that is declared direct by a superclass}}
- (int)getDirect_setDynamic; // expected-error {{cannot override a method that is declared direct by a superclass}}
- (int)setGetDirect_setDynamic:(int)value;
- (int)getDirect_setDirect; // expected-error {{cannot override a method that is declared direct by a superclass}}
- (int)setGetDirect_setDirect:(int)value; // expected-error {{cannot override a method that is declared direct by a superclass}}
- (int)getDirect_setDirectMembers; // expected-error {{cannot override a method that is declared direct by a superclass}}
- (int)setGetDirect_setDirectMembers:(int)value; // expected-error {{cannot override a method that is declared direct by a superclass}}
- (int)getDynamic_setDirect;
- (int)setGetDynamic_setDirect:(int)value; // expected-error {{cannot override a method that is declared direct by a superclass}}
- (int)getDynamic_setDirectMembers;
- (int)setGetDynamic_setDirectMembers:(int)value; // expected-error {{cannot override a method that is declared direct by a superclass}}
@end
__attribute__((objc_direct_members))
@implementation Root
- (int)propertyWithNonDirectGetter {
return 42;
}
- (int)propertyWithNonDirectGetter2 {
return 42;
}
- (int)propertyWithNonDirectGetterInParent {
return 42;
}
- (int)propertyWithNonDirectGetterInParent2 {
return 42;
}
- (int)dynamicProperty {
return 42;
}
- (int)directProperty {
return 42;
}
@end
@implementation Sub
- (int)propertyWithNonDirectGetter {
return 42;
}
- (int)propertyWithNonDirectGetter2 {
return 42;
}
- (int)dynamicProperty {
return 42;
}
- (int)synthDynamicProperty {
return 42;
}
- (int)directProperty { // expected-error {{cannot override a method that is declared direct by a superclass}}
return 42;
}
- (int)synthDirectProperty { // expected-error {{cannot override a method that is declared direct by a superclass}}
return 42;
}
@end