Implement reasonable conversion ranking for Objective-C pointer
conversions (<rdar://problem/8592139>) for overload resolution. The
conversion ranking mirrors C++'s conversion ranking fairly closely,
except that we use a same pseudo-subtyping relationship employed by
Objective-C pointer assignment rather than simple checking
derived-to-base conversions. This change covers:
- Conversions to pointers to a specific object type are better than
conversions to 'id', 'Class', qualified 'id', or qualified 'Class'
(note: GCC doesn't perform this ranking, but it matches C++'s rules
for ranking conversions to void*).
- Conversions to qualified 'id' or qualified 'Class' are better than
conversions to 'id' or 'Class', respectively.
- When two conversion sequences convert to the same type, rank the
conversions based on the relationship between the types we're
converting from.
- When two conversion sequences convert from the same non-id,
non-Class type, rank the conversions based on the relationship of
the types we're converting to. (note: GCC allows this ranking even
when converting from 'id', which is extremeley dangerous).
llvm-svn: 124591
2011-02-01 02:51:41 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify %s
|
2012-10-19 20:44:48 +08:00
|
|
|
// expected-no-diagnostics
|
Implement reasonable conversion ranking for Objective-C pointer
conversions (<rdar://problem/8592139>) for overload resolution. The
conversion ranking mirrors C++'s conversion ranking fairly closely,
except that we use a same pseudo-subtyping relationship employed by
Objective-C pointer assignment rather than simple checking
derived-to-base conversions. This change covers:
- Conversions to pointers to a specific object type are better than
conversions to 'id', 'Class', qualified 'id', or qualified 'Class'
(note: GCC doesn't perform this ranking, but it matches C++'s rules
for ranking conversions to void*).
- Conversions to qualified 'id' or qualified 'Class' are better than
conversions to 'id' or 'Class', respectively.
- When two conversion sequences convert to the same type, rank the
conversions based on the relationship between the types we're
converting from.
- When two conversion sequences convert from the same non-id,
non-Class type, rank the conversions based on the relationship of
the types we're converting to. (note: GCC allows this ranking even
when converting from 'id', which is extremeley dangerous).
llvm-svn: 124591
2011-02-01 02:51:41 +08:00
|
|
|
@protocol P1
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface A <P1>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface B : A
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface C : B
|
|
|
|
@end
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct ConvertsTo {
|
|
|
|
operator T() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// conversion of C* to B* is better than conversion of C* to A*.
|
|
|
|
int &f0(A*);
|
|
|
|
float &f0(B*);
|
|
|
|
|
|
|
|
void test_f0(C *c) {
|
|
|
|
float &fr1 = f0(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// conversion of B* to A* is better than conversion of C* to A*
|
|
|
|
void f1(A*);
|
|
|
|
|
|
|
|
struct ConvertsToBoth {
|
|
|
|
private:
|
|
|
|
operator C*() const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
operator B*() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
void test_f1(ConvertsTo<B*> toB, ConvertsTo<C*> toC, ConvertsToBoth toBoth) {
|
|
|
|
f1(toB);
|
|
|
|
f1(toC);
|
|
|
|
f1(toBoth);
|
|
|
|
};
|
|
|
|
|
|
|
|
// A conversion to an a non-id object pointer type is better than a
|
|
|
|
// conversion to 'id'.
|
|
|
|
int &f2(A*);
|
|
|
|
float &f2(id);
|
|
|
|
|
|
|
|
void test_f2(B *b) {
|
|
|
|
int &ir = f2(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A conversion to an a non-Class object pointer type is better than a
|
|
|
|
// conversion to 'Class'.
|
|
|
|
int &f3(A*);
|
|
|
|
float &f3(Class);
|
|
|
|
|
|
|
|
void test_f3(B *b) {
|
|
|
|
int &ir = f3(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// When both conversions convert to 'id' or 'Class', pick the most
|
|
|
|
// specific type to convert from.
|
|
|
|
void f4(id);
|
|
|
|
|
|
|
|
void test_f4(ConvertsTo<B*> toB, ConvertsTo<C*> toC, ConvertsToBoth toBoth) {
|
|
|
|
f4(toB);
|
|
|
|
f4(toC);
|
|
|
|
f4(toBoth);
|
|
|
|
}
|
|
|
|
|
|
|
|
void f5(id<P1>);
|
|
|
|
|
|
|
|
void test_f5(ConvertsTo<B*> toB, ConvertsTo<C*> toC, ConvertsToBoth toBoth) {
|
|
|
|
f5(toB);
|
|
|
|
f5(toC);
|
|
|
|
f5(toBoth);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// A conversion to an a non-id object pointer type is better than a
|
|
|
|
// conversion to qualified 'id'.
|
|
|
|
int &f6(A*);
|
|
|
|
float &f6(id<P1>);
|
|
|
|
|
|
|
|
void test_f6(B *b) {
|
|
|
|
int &ir = f6(b);
|
|
|
|
}
|