xref: /llvm-project/clang/test/SemaObjCXX/literals.mm (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x -fblocks %s
2
3typedef signed char BOOL;
4
5void y(BOOL (^foo)());
6
7void x() {
8    y(^{
9        return __objc_yes;
10    });
11}
12
13@protocol NSCopying
14- copy;
15@end
16
17@interface NSObject
18@end
19
20@interface NSNumber : NSObject <NSCopying>
21-copy;
22@end
23
24@interface NSNumber (NSNumberCreation)
25+ (NSNumber *)numberWithChar:(char)value;
26+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
27+ (NSNumber *)numberWithShort:(short)value;
28+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
29+ (NSNumber *)numberWithInt:(int)value;
30+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
31+ (NSNumber *)numberWithLong:(long)value;
32+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
33+ (NSNumber *)numberWithLongLong:(long long)value;
34+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
35+ (NSNumber *)numberWithFloat:(float)value;
36+ (NSNumber *)numberWithDouble:(double)value;
37+ (NSNumber *)numberWithBool:(BOOL)value;
38@end
39
40@interface NSArray : NSObject <NSCopying>
41-copy;
42@end
43
44@interface NSArray (NSArrayCreation)
45+ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
46@end
47
48@interface NSDictionary
49+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id<NSCopying> [])keys count:(unsigned long)cnt;
50@end
51
52@interface NSString
53@end
54
55template<typename T>
56struct ConvertibleTo {
57  operator T();
58};
59
60template<typename T>
61struct ExplicitlyConvertibleTo {
62  explicit operator T();
63};
64
65template<typename T>
66class PrivateConvertibleTo {
67private:
68  operator T(); // expected-note{{declared private here}}
69};
70
71template<typename T> ConvertibleTo<T> makeConvertible();
72
73struct X {
74  ConvertibleTo<id> x;
75  ConvertibleTo<id> get();
76};
77
78template<typename T> T test_numeric_instantiation() {
79  return @-17.42;
80}
81
82template id test_numeric_instantiation();
83
84void test_convertibility(ConvertibleTo<NSArray*> toArray,
85                         ConvertibleTo<id> toId,
86                         ConvertibleTo<int (^)(int)> toBlock,
87                         ConvertibleTo<int> toInt,
88                         ExplicitlyConvertibleTo<NSArray *> toArrayExplicit) {
89  id array = @[
90               toArray,
91               toId,
92               toBlock,
93               toInt // expected-error{{collection element of type 'ConvertibleTo<int>' is not an Objective-C object}}
94              ];
95  id array2 = @[ toArrayExplicit ]; // expected-error{{collection element of type 'ExplicitlyConvertibleTo<NSArray *>' is not an Objective-C object}}
96
97  id array3 = @[
98                makeConvertible<id>(),
99                               makeConvertible<id>, // expected-error{{collection element of type 'ConvertibleTo<id> ()' is not an Objective-C object}}
100               ];
101
102  X x;
103  id array4 = @[ x.x ];
104  id array5 = @[ x.get ]; // expected-error{{reference to non-static member function must be called}}
105  id array6 = @[ PrivateConvertibleTo<NSArray*>() ]; // expected-error{{operator NSArray *' is a private member of 'PrivateConvertibleTo<NSArray *>'}}
106}
107
108template<typename T>
109void test_array_literals(T t) {
110  id arr = @[ @17, t ]; // expected-error{{collection element of type 'int' is not an Objective-C object}}
111}
112
113template void test_array_literals(id);
114template void test_array_literals(NSArray*);
115template void test_array_literals(int); // expected-note{{in instantiation of function template specialization 'test_array_literals<int>' requested here}}
116
117template<typename T, typename U>
118void test_dictionary_literals(T t, U u) {
119  NSObject *object;
120  id dict = @{
121    @17 : t, // expected-error{{collection element of type 'int' is not an Objective-C object}}
122    u : @42 // expected-error{{collection element of type 'int' is not an Objective-C object}}
123  };
124
125  id dict2 = @{
126    object : @"object" // expected-error{{cannot initialize a parameter of type 'const id<NSCopying>' with an rvalue of type 'NSObject *'}}
127  };
128}
129
130template void test_dictionary_literals(id, NSArray*);
131template void test_dictionary_literals(NSArray*, id);
132template void test_dictionary_literals(int, id); // expected-note{{in instantiation of function template specialization 'test_dictionary_literals<int, id>' requested here}}
133template void test_dictionary_literals(id, int); // expected-note{{in instantiation of function template specialization 'test_dictionary_literals<id, int>' requested here}}
134
135template<typename ...Args>
136void test_bad_variadic_array_literal(Args ...args) {
137  id arr1 = @[ args ]; // expected-error{{initializer contains unexpanded parameter pack 'args'}}
138}
139
140template<typename ...Args>
141void test_variadic_array_literal(Args ...args) {
142  id arr1 = @[ args... ]; // expected-error{{collection element of type 'int' is not an Objective-C object}}
143}
144template void test_variadic_array_literal(id);
145template void test_variadic_array_literal(id, NSArray*);
146template 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}}
147
148template<typename ...Args>
149void test_bad_variadic_dictionary_literal(Args ...args) {
150  id dict = @{ args : @17 }; // expected-error{{initializer contains unexpanded parameter pack 'args'}}
151}
152
153// Test array literal pack expansions.
154template<typename T, typename U>
155struct pair {
156  T first;
157  U second;
158};
159
160template<typename T, typename ...Ts, typename ... Us>
161void test_variadic_dictionary_expansion(T t, pair<Ts, Us>... key_values) {
162  id dict = @{
163    t : key_values.second ..., // expected-error{{collection element of type 'int' is not an Objective-C object}}
164    key_values.first : key_values.second ..., // expected-error{{collection element of type 'float' is not an Objective-C object}}
165    key_values.second : t ...
166  };
167}
168
169template void test_variadic_dictionary_expansion(id,
170                                                 pair<NSNumber*, id>,
171                                                 pair<id, ConvertibleTo<id>>);
172template void test_variadic_dictionary_expansion(NSNumber *, // expected-note{{in instantiation of function template specialization}}
173                                                 pair<NSNumber*, int>,
174                                                 pair<id, ConvertibleTo<id>>);
175template void test_variadic_dictionary_expansion(NSNumber *, // expected-note{{in instantiation of function template specialization}}
176                                                 pair<NSNumber*, id>,
177                                                 pair<float, ConvertibleTo<id>>);
178
179// Test parsing
180struct key {
181  static id value;
182};
183
184id key;
185id value;
186
187void test_dictionary_colon() {
188  id dict = @{ key : value };
189}
190
191void testConstExpr() {
192  constexpr NSString *t0 = @"abc";
193  constexpr NSString *t1 = @("abc");
194}
195