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