xref: /minix3/external/bsd/llvm/dist/clang/test/SemaObjCXX/objc-container-subscripting.mm (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc// RUN: %clang_cc1 -fblocks -triple x86_64-apple-darwin11 -fsyntax-only -std=c++11 -verify %s
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuc@class NSArray;
4*f4a2713aSLionel Sambuc
5*f4a2713aSLionel Sambuc@interface NSMutableDictionary
6*f4a2713aSLionel Sambuc- (id)objectForKeyedSubscript:(id)key;
7*f4a2713aSLionel Sambuc- (void)setObject:(id)object forKeyedSubscript:(id)key; // expected-note {{passing argument to parameter 'object' here}}
8*f4a2713aSLionel Sambuc@end
9*f4a2713aSLionel Sambuc
10*f4a2713aSLionel Sambuctemplate<typename T, typename U, typename O>
11*f4a2713aSLionel Sambucvoid test_dictionary_subscripts(T base, U key, O obj) {
12*f4a2713aSLionel Sambuc  base[key] = obj; // expected-error {{expected method to write array element not found on object of type 'NSMutableDictionary *'}} \
13*f4a2713aSLionel Sambuc                   // expected-error {{cannot initialize a parameter of type 'id' with an lvalue of type 'int'}}
14*f4a2713aSLionel Sambuc  obj = base[key];  // expected-error {{expected method to read array element not found on object of type 'NSMutableDictionary *'}} \
15*f4a2713aSLionel Sambuc                    // expected-error {{assigning to 'int' from incompatible type 'id'}}
16*f4a2713aSLionel Sambuc
17*f4a2713aSLionel Sambuc}
18*f4a2713aSLionel Sambuc
19*f4a2713aSLionel Sambuctemplate void test_dictionary_subscripts(NSMutableDictionary*, id, NSArray *ns);
20*f4a2713aSLionel Sambuc
21*f4a2713aSLionel Sambuctemplate void test_dictionary_subscripts(NSMutableDictionary*, NSArray *ns, id);
22*f4a2713aSLionel Sambuc
23*f4a2713aSLionel Sambuctemplate void test_dictionary_subscripts(NSMutableDictionary*, int, id); // expected-note {{in instantiation of function template specialization 'test_dictionary_subscripts<NSMutableDictionary *, int, id>' requested here}}
24*f4a2713aSLionel Sambuc
25*f4a2713aSLionel Sambuctemplate void test_dictionary_subscripts(NSMutableDictionary*, id, int); // expected-note {{in instantiation of function template specialization 'test_dictionary_subscripts<NSMutableDictionary *, id, int>' requested here}}
26*f4a2713aSLionel Sambuc
27*f4a2713aSLionel Sambuc
28*f4a2713aSLionel Sambuc@interface NSMutableArray
29*f4a2713aSLionel Sambuc- (id)objectAtIndexedSubscript:(int)index;
30*f4a2713aSLionel Sambuc- (void)setObject:(id)object atIndexedSubscript:(int)index;
31*f4a2713aSLionel Sambuc@end
32*f4a2713aSLionel Sambuc
33*f4a2713aSLionel Sambuctemplate<typename T, typename U, typename O>
34*f4a2713aSLionel Sambucvoid test_array_subscripts(T base, U index, O obj) {
35*f4a2713aSLionel Sambuc  base[index] = obj; // expected-error {{indexing expression is invalid because subscript type 'double' is not an integral or Objective-C pointer type}}
36*f4a2713aSLionel Sambuc  obj = base[index]; // expected-error {{indexing expression is invalid because subscript type 'double' is not an integral or Objective-C pointer type}}
37*f4a2713aSLionel Sambuc}
38*f4a2713aSLionel Sambuc
39*f4a2713aSLionel Sambuctemplate void  test_array_subscripts(NSMutableArray *, int, id);
40*f4a2713aSLionel Sambuctemplate void  test_array_subscripts(NSMutableArray *, short, id);
41*f4a2713aSLionel Sambucenum E { e };
42*f4a2713aSLionel Sambuc
43*f4a2713aSLionel Sambuctemplate void  test_array_subscripts(NSMutableArray *, E, id);
44*f4a2713aSLionel Sambuc
45*f4a2713aSLionel Sambuctemplate void  test_array_subscripts(NSMutableArray *, double, id); // expected-note {{in instantiation of function template specialization 'test_array_subscripts<NSMutableArray *, double, id>' requested here}}
46*f4a2713aSLionel Sambuc
47*f4a2713aSLionel Sambuctemplate<typename T>
48*f4a2713aSLionel Sambucstruct ConvertibleTo {
49*f4a2713aSLionel Sambuc  operator T();
50*f4a2713aSLionel Sambuc};
51*f4a2713aSLionel Sambuc
52*f4a2713aSLionel Sambuctemplate<typename T>
53*f4a2713aSLionel Sambucstruct ExplicitlyConvertibleTo {
54*f4a2713aSLionel Sambuc  explicit operator T();
55*f4a2713aSLionel Sambuc};
56*f4a2713aSLionel Sambuc
57*f4a2713aSLionel Sambuctemplate<typename T> ConvertibleTo<T> makeConvertible();
58*f4a2713aSLionel Sambuc
59*f4a2713aSLionel Sambucstruct X {
60*f4a2713aSLionel Sambuc  ConvertibleTo<id> x;
61*f4a2713aSLionel Sambuc  ConvertibleTo<id> get();
62*f4a2713aSLionel Sambuc};
63*f4a2713aSLionel Sambuc
64*f4a2713aSLionel SambucNSMutableArray *test_array_convertibility(ConvertibleTo<NSMutableArray*> toArray,
65*f4a2713aSLionel Sambuc                         ConvertibleTo<id> toId,
66*f4a2713aSLionel Sambuc                         ConvertibleTo<int (^)(int)> toBlock,
67*f4a2713aSLionel Sambuc                         ConvertibleTo<int> toInt,
68*f4a2713aSLionel Sambuc                         ExplicitlyConvertibleTo<NSMutableArray *> toArrayExplicit) {
69*f4a2713aSLionel Sambuc  id array;
70*f4a2713aSLionel Sambuc
71*f4a2713aSLionel Sambuc  array[1] = toArray;
72*f4a2713aSLionel Sambuc
73*f4a2713aSLionel Sambuc  array[4] = array[1];
74*f4a2713aSLionel Sambuc
75*f4a2713aSLionel Sambuc  toArrayExplicit[2] = toId; // expected-error {{type 'ExplicitlyConvertibleTo<NSMutableArray *>' does not provide a subscript operator}}
76*f4a2713aSLionel Sambuc
77*f4a2713aSLionel Sambuc  return array[toInt];
78*f4a2713aSLionel Sambuc
79*f4a2713aSLionel Sambuc}
80*f4a2713aSLionel Sambuc
81*f4a2713aSLionel Sambucid test_dict_convertibility(ConvertibleTo<NSMutableDictionary*> toDict,
82*f4a2713aSLionel Sambuc                         ConvertibleTo<id> toId,
83*f4a2713aSLionel Sambuc                         ConvertibleTo<int (^)(int)> toBlock,
84*f4a2713aSLionel Sambuc                         ConvertibleTo<int> toInt,
85*f4a2713aSLionel Sambuc                         ExplicitlyConvertibleTo<NSMutableDictionary *> toDictExplicit) {
86*f4a2713aSLionel Sambuc
87*f4a2713aSLionel Sambuc
88*f4a2713aSLionel Sambuc  NSMutableDictionary *Dict;
89*f4a2713aSLionel Sambuc  id Id;
90*f4a2713aSLionel Sambuc  Dict[toId] = toBlock;
91*f4a2713aSLionel Sambuc
92*f4a2713aSLionel Sambuc  Dict[toBlock] = toBlock;
93*f4a2713aSLionel Sambuc
94*f4a2713aSLionel Sambuc  Dict[toBlock] = Dict[toId] = Dict[toBlock];
95*f4a2713aSLionel Sambuc
96*f4a2713aSLionel Sambuc  Id = toDictExplicit[toId] = Id; // expected-error {{no viable overloaded operator[] for type 'ExplicitlyConvertibleTo<NSMutableDictionary *>'}}
97*f4a2713aSLionel Sambuc
98*f4a2713aSLionel Sambuc  return Dict[toBlock];
99*f4a2713aSLionel Sambuc}
100*f4a2713aSLionel Sambuc
101*f4a2713aSLionel Sambuc
102*f4a2713aSLionel Sambuctemplate<typename ...Args>
103*f4a2713aSLionel Sambucvoid test_bad_variadic_array_subscripting(Args ...args) {
104*f4a2713aSLionel Sambuc  id arr1;
105*f4a2713aSLionel Sambuc  arr1[3] = args; // expected-error {{expression contains unexpanded parameter pack 'args'}}
106*f4a2713aSLionel Sambuc}
107*f4a2713aSLionel Sambuc
108*f4a2713aSLionel Sambuctemplate<typename ...Args>
109*f4a2713aSLionel Sambucvoid test_variadic_array_subscripting(Args ...args) {
110*f4a2713aSLionel Sambuc  id arr[] = {args[3]...}; // which means: {a[3], b[3], c[3]};
111*f4a2713aSLionel Sambuc}
112*f4a2713aSLionel Sambuc
113*f4a2713aSLionel Sambuctemplate void test_variadic_array_subscripting(id arg1, NSMutableArray* arg2, id arg3);
114*f4a2713aSLionel Sambuc
115*f4a2713aSLionel Sambuc@class Key;
116*f4a2713aSLionel Sambuc
117*f4a2713aSLionel Sambuctemplate<typename Index, typename ...Args>
118*f4a2713aSLionel Sambucvoid test_variadic_dictionary_subscripting(Index I, Args ...args) {
119*f4a2713aSLionel Sambuc  id arr[] = {args[I]...}; // which means: {a[3], b[3], c[3]};
120*f4a2713aSLionel Sambuc}
121*f4a2713aSLionel Sambuc
122*f4a2713aSLionel Sambuctemplate void test_variadic_dictionary_subscripting(Key *key, id arg1, NSMutableDictionary* arg2, id arg3);
123*f4a2713aSLionel Sambuc
124*f4a2713aSLionel Sambuctemplate<int N>
125*f4a2713aSLionel Sambucid get(NSMutableArray *array) {
126*f4a2713aSLionel Sambuc return array[N]; // array[N] should be a value- and instantiation-dependent ObjCSubscriptRefExpr
127*f4a2713aSLionel Sambuc}
128*f4a2713aSLionel Sambuc
129*f4a2713aSLionel Sambucstruct WeirdIndex {
130*f4a2713aSLionel Sambuc   operator int(); // expected-note {{type conversion function declared here}}
131*f4a2713aSLionel Sambuc   operator id(); // expected-note {{type conversion function declared here}}
132*f4a2713aSLionel Sambuc};
133*f4a2713aSLionel Sambuc
134*f4a2713aSLionel Sambucid FUNC(WeirdIndex w) {
135*f4a2713aSLionel Sambuc  NSMutableArray *array;
136*f4a2713aSLionel Sambuc  return array[w]; // expected-error {{indexing expression is invalid because subscript type 'WeirdIndex' has multiple type conversion functions}}
137*f4a2713aSLionel Sambuc}
138*f4a2713aSLionel Sambuc
139