diff options
author | Ted Kremenek <kremenek@apple.com> | 2012-03-06 20:05:56 +0000 |
---|---|---|
committer | Ted Kremenek <kremenek@apple.com> | 2012-03-06 20:05:56 +0000 |
commit | ebcb57a8d298862c65043e88b2429591ab3c58d3 (patch) | |
tree | adbe4e0a60340ec2858e4b8d74d7bb0f23887d67 /test/SemaObjCXX | |
parent | 9f86af897458b3b44e26b9e06a857f626f71a692 (diff) |
Add clang support for new Objective-C literal syntax for NSDictionary, NSArray,
NSNumber, and boolean literals. This includes both Sema and Codegen support.
Included is also support for new Objective-C container subscripting.
My apologies for the large patch. It was very difficult to break apart.
The patch introduces changes to the driver as well to cause clang to link
in additional runtime support when needed to support the new language features.
Docs are forthcoming to document the implementation and behavior of these features.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@152137 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/SemaObjCXX')
-rw-r--r-- | test/SemaObjCXX/literals.mm | 178 | ||||
-rw-r--r-- | test/SemaObjCXX/objc-container-subscripting.mm | 138 |
2 files changed, 316 insertions, 0 deletions
diff --git a/test/SemaObjCXX/literals.mm b/test/SemaObjCXX/literals.mm new file mode 100644 index 0000000000..1f6782abbe --- /dev/null +++ b/test/SemaObjCXX/literals.mm @@ -0,0 +1,178 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x -fblocks %s + +typedef unsigned char BOOL; + +@protocol NSCopying +- copy; +@end + +@interface NSObject +@end + +@interface NSNumber : NSObject <NSCopying> +-copy; +@end + +@interface NSNumber (NSNumberCreation) ++ (NSNumber *)numberWithChar:(char)value; ++ (NSNumber *)numberWithUnsignedChar:(unsigned char)value; ++ (NSNumber *)numberWithShort:(short)value; ++ (NSNumber *)numberWithUnsignedShort:(unsigned short)value; ++ (NSNumber *)numberWithInt:(int)value; ++ (NSNumber *)numberWithUnsignedInt:(unsigned int)value; ++ (NSNumber *)numberWithLong:(long)value; ++ (NSNumber *)numberWithUnsignedLong:(unsigned long)value; ++ (NSNumber *)numberWithLongLong:(long long)value; ++ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value; ++ (NSNumber *)numberWithFloat:(float)value; ++ (NSNumber *)numberWithDouble:(double)value; ++ (NSNumber *)numberWithBool:(BOOL)value; +@end + +@interface NSArray : NSObject <NSCopying> +-copy; +@end + +@interface NSArray (NSArrayCreation) ++ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt; +@end + +@interface NSDictionary ++ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id<NSCopying> [])keys count:(unsigned long)cnt; +@end + +template<typename T> +struct ConvertibleTo { + operator T(); +}; + +template<typename T> +struct ExplicitlyConvertibleTo { + explicit operator T(); +}; + +template<typename T> +class PrivateConvertibleTo { +private: + operator T(); // expected-note{{declared private here}} +}; + +template<typename T> ConvertibleTo<T> makeConvertible(); + +struct X { + ConvertibleTo<id> x; + ConvertibleTo<id> get(); +}; + +template<typename T> T test_numeric_instantiation() { + return @-17.42; +} + +template id test_numeric_instantiation(); + +void test_convertibility(ConvertibleTo<NSArray*> toArray, + ConvertibleTo<id> toId, + ConvertibleTo<int (^)(int)> toBlock, + ConvertibleTo<int> toInt, + ExplicitlyConvertibleTo<NSArray *> toArrayExplicit) { + id array = @[ + toArray, + toId, + toBlock, + toInt // expected-error{{collection element of type 'ConvertibleTo<int>' is not an Objective-C object}} + ]; + id array2 = @[ toArrayExplicit ]; // expected-error{{collection element of type 'ExplicitlyConvertibleTo<NSArray *>' is not an Objective-C object}} + + id array3 = @[ + makeConvertible<id>(), + makeConvertible<id>, // expected-error{{collection element of type 'ConvertibleTo<id> ()' is not an Objective-C object}} + ]; + + X x; + id array4 = @[ x.x ]; + id array5 = @[ x.get ]; // expected-error{{reference to non-static member function must be called}} + id array6 = @[ PrivateConvertibleTo<NSArray*>() ]; // expected-error{{operator NSArray *' is a private member of 'PrivateConvertibleTo<NSArray *>'}} +} + +template<typename T> +void test_array_literals(T t) { + id arr = @[ @17, t ]; // expected-error{{collection element of type 'int' is not an Objective-C object}} +} + +template void test_array_literals(id); +template void test_array_literals(NSArray*); +template void test_array_literals(int); // expected-note{{in instantiation of function template specialization 'test_array_literals<int>' requested here}} + +template<typename T, typename U> +void test_dictionary_literals(T t, U u) { + NSObject *object; + id dict = @{ + @17 : t, // expected-error{{collection element of type 'int' is not an Objective-C object}} + u : @42 // expected-error{{collection element of type 'int' is not an Objective-C object}} + }; + + id dict2 = @{ + object : @"object" // expected-error{{cannot initialize a parameter of type 'const id<NSCopying>' with an rvalue of type 'NSObject *'}} + }; +} + +template void test_dictionary_literals(id, NSArray*); +template void test_dictionary_literals(NSArray*, id); +template void test_dictionary_literals(int, id); // expected-note{{in instantiation of function template specialization 'test_dictionary_literals<int, id>' requested here}} +template void test_dictionary_literals(id, int); // expected-note{{in instantiation of function template specialization 'test_dictionary_literals<id, int>' requested here}} + +template<typename ...Args> +void test_bad_variadic_array_literal(Args ...args) { + id arr1 = @[ args ]; // expected-error{{initializer contains unexpanded parameter pack 'args'}} +} + +template<typename ...Args> +void test_variadic_array_literal(Args ...args) { + id arr1 = @[ args... ]; // expected-error{{collection element of type 'int' is not an Objective-C object}} +} +template void test_variadic_array_literal(id); +template void test_variadic_array_literal(id, NSArray*); +template void test_variadic_array_literal(id, int, NSArray*); // expected-note{{in instantiation of function template specialization 'test_variadic_array_literal<id, int, NSArray *>' requested here}} + +template<typename ...Args> +void test_bad_variadic_dictionary_literal(Args ...args) { + id dict = @{ args : @17 }; // expected-error{{initializer contains unexpanded parameter pack 'args'}} +} + +// Test array literal pack expansions. +template<typename T, typename U> +struct pair { + T first; + U second; +}; + +template<typename T, typename ...Ts, typename ... Us> +void test_variadic_dictionary_expansion(T t, pair<Ts, Us>... key_values) { + id dict = @{ + t : key_values.second ..., // expected-error{{collection element of type 'int' is not an Objective-C object}} + key_values.first : key_values.second ..., // expected-error{{collection element of type 'float' is not an Objective-C object}} + key_values.second : t ... + }; +} + +template void test_variadic_dictionary_expansion(id, + pair<NSNumber*, id>, + pair<id, ConvertibleTo<id>>); +template void test_variadic_dictionary_expansion(NSNumber *, // expected-note{{in instantiation of function template specialization}} + pair<NSNumber*, int>, + pair<id, ConvertibleTo<id>>); +template void test_variadic_dictionary_expansion(NSNumber *, // expected-note{{in instantiation of function template specialization}} + pair<NSNumber*, id>, + pair<float, ConvertibleTo<id>>); + +// Test parsing +struct key { + static id value; +}; + +id key; +id value; + +void test_dictionary_colon() { + id dict = @{ key : value }; +} diff --git a/test/SemaObjCXX/objc-container-subscripting.mm b/test/SemaObjCXX/objc-container-subscripting.mm new file mode 100644 index 0000000000..ccbc45e3b4 --- /dev/null +++ b/test/SemaObjCXX/objc-container-subscripting.mm @@ -0,0 +1,138 @@ +// RUN: %clang_cc1 -fblocks -triple x86_64-apple-darwin11 -fsyntax-only -std=c++11 -verify %s + +@class NSArray; + +@interface NSMutableDictionary +- (id)objectForKeyedSubscript:(id)key; +- (void)setObject:(id)object forKeyedSubscript:(id)key; // expected-note {{passing argument to parameter 'object' here}} +@end + +template<typename T, typename U, typename O> +void test_dictionary_subscripts(T base, U key, O obj) { + base[key] = obj; // expected-error {{expected method to write array element not found on object of type 'NSMutableDictionary *'}} \ + // expected-error {{cannot initialize a parameter of type 'id' with an lvalue of type 'int'}} + obj = base[key]; // expected-error {{expected method to read array element not found on object of type 'NSMutableDictionary *'}} \ + // expected-error {{assigning to 'int' from incompatible type 'id'}} + +} + +template void test_dictionary_subscripts(NSMutableDictionary*, id, NSArray *ns); + +template void test_dictionary_subscripts(NSMutableDictionary*, NSArray *ns, id); + +template void test_dictionary_subscripts(NSMutableDictionary*, int, id); // expected-note {{in instantiation of function template specialization 'test_dictionary_subscripts<NSMutableDictionary *, int, id>' requested here}} + +template void test_dictionary_subscripts(NSMutableDictionary*, id, int); // expected-note {{in instantiation of function template specialization 'test_dictionary_subscripts<NSMutableDictionary *, id, int>' requested here}} + + +@interface NSMutableArray +- (id)objectAtIndexedSubscript:(int)index; +- (void)setObject:(id)object atIndexedSubscript:(int)index; +@end + +template<typename T, typename U, typename O> +void test_array_subscripts(T base, U index, O obj) { + base[index] = obj; // expected-error {{expected method to write dictionary element not found on object of type 'NSMutableArray *'}} + obj = base[index]; // expected-error {{expected method to read dictionary element not found on object of type 'NSMutableArray *'}} +} + +template void test_array_subscripts(NSMutableArray *, int, id); +template void test_array_subscripts(NSMutableArray *, short, id); +enum E { e }; + +template void test_array_subscripts(NSMutableArray *, E, id); + +template void test_array_subscripts(NSMutableArray *, double, id); // expected-note {{in instantiation of function template specialization 'test_array_subscripts<NSMutableArray *, double, id>' requested here}} + +template<typename T> +struct ConvertibleTo { + operator T(); +}; + +template<typename T> +struct ExplicitlyConvertibleTo { + explicit operator T(); +}; + +template<typename T> ConvertibleTo<T> makeConvertible(); + +struct X { + ConvertibleTo<id> x; + ConvertibleTo<id> get(); +}; + +NSMutableArray *test_array_convertibility(ConvertibleTo<NSMutableArray*> toArray, + ConvertibleTo<id> toId, + ConvertibleTo<int (^)(int)> toBlock, + ConvertibleTo<int> toInt, + ExplicitlyConvertibleTo<NSMutableArray *> toArrayExplicit) { + id array; + + array[1] = toArray; + + array[4] = array[1]; + + toArrayExplicit[2] = toId; // expected-error {{type 'ExplicitlyConvertibleTo<NSMutableArray *>' does not provide a subscript operator}} + + return array[toInt]; + +} + +id test_dict_convertibility(ConvertibleTo<NSMutableDictionary*> toDict, + ConvertibleTo<id> toId, + ConvertibleTo<int (^)(int)> toBlock, + ConvertibleTo<int> toInt, + ExplicitlyConvertibleTo<NSMutableDictionary *> toDictExplicit) { + + + NSMutableDictionary *Dict; + id Id; + Dict[toId] = toBlock; + + Dict[toBlock] = toBlock; + + Dict[toBlock] = Dict[toId] = Dict[toBlock]; + + Id = toDictExplicit[toId] = Id; // expected-error {{no viable overloaded operator[] for type 'ExplicitlyConvertibleTo<NSMutableDictionary *>'}} + + return Dict[toBlock]; +} + + +template<typename ...Args> +void test_bad_variadic_array_subscripting(Args ...args) { + id arr1; + arr1[3] = args; // expected-error {{expression contains unexpanded parameter pack 'args'}} +} + +template<typename ...Args> +void test_variadic_array_subscripting(Args ...args) { + id arr[] = {args[3]...}; // which means: {a[3], b[3], c[3]}; +} + +template void test_variadic_array_subscripting(id arg1, NSMutableArray* arg2, id arg3); + +@class Key; + +template<typename Index, typename ...Args> +void test_variadic_dictionary_subscripting(Index I, Args ...args) { + id arr[] = {args[I]...}; // which means: {a[3], b[3], c[3]}; +} + +template void test_variadic_dictionary_subscripting(Key *key, id arg1, NSMutableDictionary* arg2, id arg3); + +template<int N> +id get(NSMutableArray *array) { + return array[N]; // array[N] should be a value- and instantiation-dependent ObjCSubscriptRefExpr +} + +struct WeirdIndex { + operator int(); // expected-note {{type conversion function declared here}} + operator id(); // expected-note {{type conversion function declared here}} +}; + +id FUNC(WeirdIndex w) { + NSMutableArray *array; + return array[w]; // expected-error {{indexing expression is invalid because subscript type 'WeirdIndex' has multiple type conversion functions}} +} + |