Debian Codegraph @

clang/test/SemaObjCXX/literals.mm

raw
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x -fblocks %s
2
3 typedef signed char BOOL;
4
5 void y(BOOL (^foo)());
6
7 void 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
55 template<typename T>
56 struct ConvertibleTo {
57 operator T();
58 };
59
60 template<typename T>
61 struct ExplicitlyConvertibleTo {
62 explicit operator T();
63 };
64
65 template<typename T>
66 class PrivateConvertibleTo {
67 private:
68 operator T(); // expected-note{{declared private here}}
69 };
70
71 template<typename T> ConvertibleTo<T> makeConvertible();
72
73 struct X {
74 ConvertibleTo<id> x;
75 ConvertibleTo<id> get();
76 };
77
78 template<typename T> T test_numeric_instantiation() {
79 return @-17.42;
80 }
81
82 template id test_numeric_instantiation();
83
84 void 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
108 template<typename T>
109 void 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
113 template void test_array_literals(id);
114 template void test_array_literals(NSArray*);
115 template void test_array_literals(int); // expected-note{{in instantiation of function template specialization 'test_array_literals<int>' requested here}}
116
117 template<typename T, typename U>
118 void 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
130 template void test_dictionary_literals(id, NSArray*);
131 template void test_dictionary_literals(NSArray*, id);
132 template void test_dictionary_literals(int, id); // expected-note{{in instantiation of function template specialization 'test_dictionary_literals<int, id>' requested here}}
133 template void test_dictionary_literals(id, int); // expected-note{{in instantiation of function template specialization 'test_dictionary_literals<id, int>' requested here}}
134
135 template<typename ...Args>
136 void test_bad_variadic_array_literal(Args ...args) {
137 id arr1 = @[ args ]; // expected-error{{initializer contains unexpanded parameter pack 'args'}}
138 }
139
140 template<typename ...Args>
141 void test_variadic_array_literal(Args ...args) {
142 id arr1 = @[ args... ]; // expected-error{{collection element of type 'int' is not an Objective-C object}}
143 }
144 template void test_variadic_array_literal(id);
145 template void test_variadic_array_literal(id, NSArray*);
146 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}}
147
148 template<typename ...Args>
149 void 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.
154 template<typename T, typename U>
155 struct pair {
156 T first;
157 U second;
158 };
159
160 template<typename T, typename ...Ts, typename ... Us>
161 void 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
169 template void test_variadic_dictionary_expansion(id,
170 pair<NSNumber*, id>,
171 pair<id, ConvertibleTo<id>>);
172 template void test_variadic_dictionary_expansion(NSNumber *, // expected-note{{in instantiation of function template specialization}}
173 pair<NSNumber*, int>,
174 pair<id, ConvertibleTo<id>>);
175 template 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
180 struct key {
181 static id value;
182 };
183
184 id key;
185 id value;
186
187 void test_dictionary_colon() {
188 id dict = @{ key : value };
189 }
190
191 void testConstExpr() {
192 constexpr NSString *t0 = @"abc";
193 constexpr NSString *t1 = @("abc");
194 }