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