xref: /minix3/external/bsd/llvm/dist/clang/lib/AST/NSAPI.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- NSAPI.cpp - NSFoundation APIs ------------------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "clang/AST/NSAPI.h"
11f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
12f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
13*0a6a1f1dSLionel Sambuc #include "llvm/ADT/StringSwitch.h"
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc using namespace clang;
16f4a2713aSLionel Sambuc 
NSAPI(ASTContext & ctx)17f4a2713aSLionel Sambuc NSAPI::NSAPI(ASTContext &ctx)
18*0a6a1f1dSLionel Sambuc   : Ctx(ctx), ClassIds(), BOOLId(nullptr), NSIntegerId(nullptr),
19*0a6a1f1dSLionel Sambuc     NSUIntegerId(nullptr), NSASCIIStringEncodingId(nullptr),
20*0a6a1f1dSLionel Sambuc     NSUTF8StringEncodingId(nullptr) {}
21f4a2713aSLionel Sambuc 
getNSClassId(NSClassIdKindKind K) const22f4a2713aSLionel Sambuc IdentifierInfo *NSAPI::getNSClassId(NSClassIdKindKind K) const {
23f4a2713aSLionel Sambuc   static const char *ClassName[NumClassIds] = {
24f4a2713aSLionel Sambuc     "NSObject",
25f4a2713aSLionel Sambuc     "NSString",
26f4a2713aSLionel Sambuc     "NSArray",
27f4a2713aSLionel Sambuc     "NSMutableArray",
28f4a2713aSLionel Sambuc     "NSDictionary",
29f4a2713aSLionel Sambuc     "NSMutableDictionary",
30f4a2713aSLionel Sambuc     "NSNumber"
31f4a2713aSLionel Sambuc   };
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc   if (!ClassIds[K])
34f4a2713aSLionel Sambuc     return (ClassIds[K] = &Ctx.Idents.get(ClassName[K]));
35f4a2713aSLionel Sambuc 
36f4a2713aSLionel Sambuc   return ClassIds[K];
37f4a2713aSLionel Sambuc }
38f4a2713aSLionel Sambuc 
getNSStringSelector(NSStringMethodKind MK) const39f4a2713aSLionel Sambuc Selector NSAPI::getNSStringSelector(NSStringMethodKind MK) const {
40f4a2713aSLionel Sambuc   if (NSStringSelectors[MK].isNull()) {
41f4a2713aSLionel Sambuc     Selector Sel;
42f4a2713aSLionel Sambuc     switch (MK) {
43f4a2713aSLionel Sambuc     case NSStr_stringWithString:
44f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("stringWithString"));
45f4a2713aSLionel Sambuc       break;
46f4a2713aSLionel Sambuc     case NSStr_stringWithUTF8String:
47f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(
48f4a2713aSLionel Sambuc                                        &Ctx.Idents.get("stringWithUTF8String"));
49f4a2713aSLionel Sambuc       break;
50*0a6a1f1dSLionel Sambuc     case NSStr_initWithUTF8String:
51*0a6a1f1dSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(
52*0a6a1f1dSLionel Sambuc                                        &Ctx.Idents.get("initWithUTF8String"));
53*0a6a1f1dSLionel Sambuc       break;
54f4a2713aSLionel Sambuc     case NSStr_stringWithCStringEncoding: {
55f4a2713aSLionel Sambuc       IdentifierInfo *KeyIdents[] = {
56f4a2713aSLionel Sambuc         &Ctx.Idents.get("stringWithCString"),
57f4a2713aSLionel Sambuc         &Ctx.Idents.get("encoding")
58f4a2713aSLionel Sambuc       };
59f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
60f4a2713aSLionel Sambuc       break;
61f4a2713aSLionel Sambuc     }
62f4a2713aSLionel Sambuc     case NSStr_stringWithCString:
63f4a2713aSLionel Sambuc       Sel= Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("stringWithCString"));
64f4a2713aSLionel Sambuc       break;
65f4a2713aSLionel Sambuc     case NSStr_initWithString:
66f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("initWithString"));
67f4a2713aSLionel Sambuc       break;
68f4a2713aSLionel Sambuc     }
69f4a2713aSLionel Sambuc     return (NSStringSelectors[MK] = Sel);
70f4a2713aSLionel Sambuc   }
71f4a2713aSLionel Sambuc 
72f4a2713aSLionel Sambuc   return NSStringSelectors[MK];
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc Optional<NSAPI::NSStringMethodKind>
getNSStringMethodKind(Selector Sel) const76f4a2713aSLionel Sambuc NSAPI::getNSStringMethodKind(Selector Sel) const {
77f4a2713aSLionel Sambuc   for (unsigned i = 0; i != NumNSStringMethods; ++i) {
78f4a2713aSLionel Sambuc     NSStringMethodKind MK = NSStringMethodKind(i);
79f4a2713aSLionel Sambuc     if (Sel == getNSStringSelector(MK))
80f4a2713aSLionel Sambuc       return MK;
81f4a2713aSLionel Sambuc   }
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc   return None;
84f4a2713aSLionel Sambuc }
85f4a2713aSLionel Sambuc 
getNSArraySelector(NSArrayMethodKind MK) const86f4a2713aSLionel Sambuc Selector NSAPI::getNSArraySelector(NSArrayMethodKind MK) const {
87f4a2713aSLionel Sambuc   if (NSArraySelectors[MK].isNull()) {
88f4a2713aSLionel Sambuc     Selector Sel;
89f4a2713aSLionel Sambuc     switch (MK) {
90f4a2713aSLionel Sambuc     case NSArr_array:
91f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getNullarySelector(&Ctx.Idents.get("array"));
92f4a2713aSLionel Sambuc       break;
93f4a2713aSLionel Sambuc     case NSArr_arrayWithArray:
94f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("arrayWithArray"));
95f4a2713aSLionel Sambuc       break;
96f4a2713aSLionel Sambuc     case NSArr_arrayWithObject:
97f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("arrayWithObject"));
98f4a2713aSLionel Sambuc       break;
99f4a2713aSLionel Sambuc     case NSArr_arrayWithObjects:
100f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("arrayWithObjects"));
101f4a2713aSLionel Sambuc       break;
102f4a2713aSLionel Sambuc     case NSArr_arrayWithObjectsCount: {
103f4a2713aSLionel Sambuc       IdentifierInfo *KeyIdents[] = {
104f4a2713aSLionel Sambuc         &Ctx.Idents.get("arrayWithObjects"),
105f4a2713aSLionel Sambuc         &Ctx.Idents.get("count")
106f4a2713aSLionel Sambuc       };
107f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
108f4a2713aSLionel Sambuc       break;
109f4a2713aSLionel Sambuc     }
110f4a2713aSLionel Sambuc     case NSArr_initWithArray:
111f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("initWithArray"));
112f4a2713aSLionel Sambuc       break;
113f4a2713aSLionel Sambuc     case NSArr_initWithObjects:
114f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("initWithObjects"));
115f4a2713aSLionel Sambuc       break;
116f4a2713aSLionel Sambuc     case NSArr_objectAtIndex:
117f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("objectAtIndex"));
118f4a2713aSLionel Sambuc       break;
119f4a2713aSLionel Sambuc     case NSMutableArr_replaceObjectAtIndex: {
120f4a2713aSLionel Sambuc       IdentifierInfo *KeyIdents[] = {
121f4a2713aSLionel Sambuc         &Ctx.Idents.get("replaceObjectAtIndex"),
122f4a2713aSLionel Sambuc         &Ctx.Idents.get("withObject")
123f4a2713aSLionel Sambuc       };
124f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
125f4a2713aSLionel Sambuc       break;
126f4a2713aSLionel Sambuc     }
127f4a2713aSLionel Sambuc     }
128f4a2713aSLionel Sambuc     return (NSArraySelectors[MK] = Sel);
129f4a2713aSLionel Sambuc   }
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc   return NSArraySelectors[MK];
132f4a2713aSLionel Sambuc }
133f4a2713aSLionel Sambuc 
getNSArrayMethodKind(Selector Sel)134f4a2713aSLionel Sambuc Optional<NSAPI::NSArrayMethodKind> NSAPI::getNSArrayMethodKind(Selector Sel) {
135f4a2713aSLionel Sambuc   for (unsigned i = 0; i != NumNSArrayMethods; ++i) {
136f4a2713aSLionel Sambuc     NSArrayMethodKind MK = NSArrayMethodKind(i);
137f4a2713aSLionel Sambuc     if (Sel == getNSArraySelector(MK))
138f4a2713aSLionel Sambuc       return MK;
139f4a2713aSLionel Sambuc   }
140f4a2713aSLionel Sambuc 
141f4a2713aSLionel Sambuc   return None;
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc 
getNSDictionarySelector(NSDictionaryMethodKind MK) const144f4a2713aSLionel Sambuc Selector NSAPI::getNSDictionarySelector(
145f4a2713aSLionel Sambuc                                        NSDictionaryMethodKind MK) const {
146f4a2713aSLionel Sambuc   if (NSDictionarySelectors[MK].isNull()) {
147f4a2713aSLionel Sambuc     Selector Sel;
148f4a2713aSLionel Sambuc     switch (MK) {
149f4a2713aSLionel Sambuc     case NSDict_dictionary:
150f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getNullarySelector(&Ctx.Idents.get("dictionary"));
151f4a2713aSLionel Sambuc       break;
152f4a2713aSLionel Sambuc     case NSDict_dictionaryWithDictionary:
153f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(
154f4a2713aSLionel Sambuc                                    &Ctx.Idents.get("dictionaryWithDictionary"));
155f4a2713aSLionel Sambuc       break;
156f4a2713aSLionel Sambuc     case NSDict_dictionaryWithObjectForKey: {
157f4a2713aSLionel Sambuc       IdentifierInfo *KeyIdents[] = {
158f4a2713aSLionel Sambuc         &Ctx.Idents.get("dictionaryWithObject"),
159f4a2713aSLionel Sambuc         &Ctx.Idents.get("forKey")
160f4a2713aSLionel Sambuc       };
161f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
162f4a2713aSLionel Sambuc       break;
163f4a2713aSLionel Sambuc     }
164f4a2713aSLionel Sambuc     case NSDict_dictionaryWithObjectsForKeys: {
165f4a2713aSLionel Sambuc       IdentifierInfo *KeyIdents[] = {
166f4a2713aSLionel Sambuc         &Ctx.Idents.get("dictionaryWithObjects"),
167f4a2713aSLionel Sambuc         &Ctx.Idents.get("forKeys")
168f4a2713aSLionel Sambuc       };
169f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
170f4a2713aSLionel Sambuc       break;
171f4a2713aSLionel Sambuc     }
172f4a2713aSLionel Sambuc     case NSDict_dictionaryWithObjectsForKeysCount: {
173f4a2713aSLionel Sambuc       IdentifierInfo *KeyIdents[] = {
174f4a2713aSLionel Sambuc         &Ctx.Idents.get("dictionaryWithObjects"),
175f4a2713aSLionel Sambuc         &Ctx.Idents.get("forKeys"),
176f4a2713aSLionel Sambuc         &Ctx.Idents.get("count")
177f4a2713aSLionel Sambuc       };
178f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getSelector(3, KeyIdents);
179f4a2713aSLionel Sambuc       break;
180f4a2713aSLionel Sambuc     }
181f4a2713aSLionel Sambuc     case NSDict_dictionaryWithObjectsAndKeys:
182f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(
183f4a2713aSLionel Sambuc                                &Ctx.Idents.get("dictionaryWithObjectsAndKeys"));
184f4a2713aSLionel Sambuc       break;
185f4a2713aSLionel Sambuc     case NSDict_initWithDictionary:
186f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(
187f4a2713aSLionel Sambuc                                          &Ctx.Idents.get("initWithDictionary"));
188f4a2713aSLionel Sambuc       break;
189f4a2713aSLionel Sambuc     case NSDict_initWithObjectsAndKeys:
190f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(
191f4a2713aSLionel Sambuc                                      &Ctx.Idents.get("initWithObjectsAndKeys"));
192f4a2713aSLionel Sambuc       break;
193f4a2713aSLionel Sambuc     case NSDict_initWithObjectsForKeys: {
194f4a2713aSLionel Sambuc       IdentifierInfo *KeyIdents[] = {
195f4a2713aSLionel Sambuc         &Ctx.Idents.get("initWithObjects"),
196f4a2713aSLionel Sambuc         &Ctx.Idents.get("forKeys")
197f4a2713aSLionel Sambuc       };
198f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
199f4a2713aSLionel Sambuc       break;
200f4a2713aSLionel Sambuc     }
201f4a2713aSLionel Sambuc     case NSDict_objectForKey:
202f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("objectForKey"));
203f4a2713aSLionel Sambuc       break;
204f4a2713aSLionel Sambuc     case NSMutableDict_setObjectForKey: {
205f4a2713aSLionel Sambuc       IdentifierInfo *KeyIdents[] = {
206f4a2713aSLionel Sambuc         &Ctx.Idents.get("setObject"),
207f4a2713aSLionel Sambuc         &Ctx.Idents.get("forKey")
208f4a2713aSLionel Sambuc       };
209f4a2713aSLionel Sambuc       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
210f4a2713aSLionel Sambuc       break;
211f4a2713aSLionel Sambuc     }
212f4a2713aSLionel Sambuc     }
213f4a2713aSLionel Sambuc     return (NSDictionarySelectors[MK] = Sel);
214f4a2713aSLionel Sambuc   }
215f4a2713aSLionel Sambuc 
216f4a2713aSLionel Sambuc   return NSDictionarySelectors[MK];
217f4a2713aSLionel Sambuc }
218f4a2713aSLionel Sambuc 
219f4a2713aSLionel Sambuc Optional<NSAPI::NSDictionaryMethodKind>
getNSDictionaryMethodKind(Selector Sel)220f4a2713aSLionel Sambuc NSAPI::getNSDictionaryMethodKind(Selector Sel) {
221f4a2713aSLionel Sambuc   for (unsigned i = 0; i != NumNSDictionaryMethods; ++i) {
222f4a2713aSLionel Sambuc     NSDictionaryMethodKind MK = NSDictionaryMethodKind(i);
223f4a2713aSLionel Sambuc     if (Sel == getNSDictionarySelector(MK))
224f4a2713aSLionel Sambuc       return MK;
225f4a2713aSLionel Sambuc   }
226f4a2713aSLionel Sambuc 
227f4a2713aSLionel Sambuc   return None;
228f4a2713aSLionel Sambuc }
229f4a2713aSLionel Sambuc 
getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,bool Instance) const230f4a2713aSLionel Sambuc Selector NSAPI::getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
231f4a2713aSLionel Sambuc                                            bool Instance) const {
232f4a2713aSLionel Sambuc   static const char *ClassSelectorName[NumNSNumberLiteralMethods] = {
233f4a2713aSLionel Sambuc     "numberWithChar",
234f4a2713aSLionel Sambuc     "numberWithUnsignedChar",
235f4a2713aSLionel Sambuc     "numberWithShort",
236f4a2713aSLionel Sambuc     "numberWithUnsignedShort",
237f4a2713aSLionel Sambuc     "numberWithInt",
238f4a2713aSLionel Sambuc     "numberWithUnsignedInt",
239f4a2713aSLionel Sambuc     "numberWithLong",
240f4a2713aSLionel Sambuc     "numberWithUnsignedLong",
241f4a2713aSLionel Sambuc     "numberWithLongLong",
242f4a2713aSLionel Sambuc     "numberWithUnsignedLongLong",
243f4a2713aSLionel Sambuc     "numberWithFloat",
244f4a2713aSLionel Sambuc     "numberWithDouble",
245f4a2713aSLionel Sambuc     "numberWithBool",
246f4a2713aSLionel Sambuc     "numberWithInteger",
247f4a2713aSLionel Sambuc     "numberWithUnsignedInteger"
248f4a2713aSLionel Sambuc   };
249f4a2713aSLionel Sambuc   static const char *InstanceSelectorName[NumNSNumberLiteralMethods] = {
250f4a2713aSLionel Sambuc     "initWithChar",
251f4a2713aSLionel Sambuc     "initWithUnsignedChar",
252f4a2713aSLionel Sambuc     "initWithShort",
253f4a2713aSLionel Sambuc     "initWithUnsignedShort",
254f4a2713aSLionel Sambuc     "initWithInt",
255f4a2713aSLionel Sambuc     "initWithUnsignedInt",
256f4a2713aSLionel Sambuc     "initWithLong",
257f4a2713aSLionel Sambuc     "initWithUnsignedLong",
258f4a2713aSLionel Sambuc     "initWithLongLong",
259f4a2713aSLionel Sambuc     "initWithUnsignedLongLong",
260f4a2713aSLionel Sambuc     "initWithFloat",
261f4a2713aSLionel Sambuc     "initWithDouble",
262f4a2713aSLionel Sambuc     "initWithBool",
263f4a2713aSLionel Sambuc     "initWithInteger",
264f4a2713aSLionel Sambuc     "initWithUnsignedInteger"
265f4a2713aSLionel Sambuc   };
266f4a2713aSLionel Sambuc 
267f4a2713aSLionel Sambuc   Selector *Sels;
268f4a2713aSLionel Sambuc   const char **Names;
269f4a2713aSLionel Sambuc   if (Instance) {
270f4a2713aSLionel Sambuc     Sels = NSNumberInstanceSelectors;
271f4a2713aSLionel Sambuc     Names = InstanceSelectorName;
272f4a2713aSLionel Sambuc   } else {
273f4a2713aSLionel Sambuc     Sels = NSNumberClassSelectors;
274f4a2713aSLionel Sambuc     Names = ClassSelectorName;
275f4a2713aSLionel Sambuc   }
276f4a2713aSLionel Sambuc 
277f4a2713aSLionel Sambuc   if (Sels[MK].isNull())
278f4a2713aSLionel Sambuc     Sels[MK] = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get(Names[MK]));
279f4a2713aSLionel Sambuc   return Sels[MK];
280f4a2713aSLionel Sambuc }
281f4a2713aSLionel Sambuc 
282f4a2713aSLionel Sambuc Optional<NSAPI::NSNumberLiteralMethodKind>
getNSNumberLiteralMethodKind(Selector Sel) const283f4a2713aSLionel Sambuc NSAPI::getNSNumberLiteralMethodKind(Selector Sel) const {
284f4a2713aSLionel Sambuc   for (unsigned i = 0; i != NumNSNumberLiteralMethods; ++i) {
285f4a2713aSLionel Sambuc     NSNumberLiteralMethodKind MK = NSNumberLiteralMethodKind(i);
286f4a2713aSLionel Sambuc     if (isNSNumberLiteralSelector(MK, Sel))
287f4a2713aSLionel Sambuc       return MK;
288f4a2713aSLionel Sambuc   }
289f4a2713aSLionel Sambuc 
290f4a2713aSLionel Sambuc   return None;
291f4a2713aSLionel Sambuc }
292f4a2713aSLionel Sambuc 
293f4a2713aSLionel Sambuc Optional<NSAPI::NSNumberLiteralMethodKind>
getNSNumberFactoryMethodKind(QualType T) const294f4a2713aSLionel Sambuc NSAPI::getNSNumberFactoryMethodKind(QualType T) const {
295f4a2713aSLionel Sambuc   const BuiltinType *BT = T->getAs<BuiltinType>();
296f4a2713aSLionel Sambuc   if (!BT)
297f4a2713aSLionel Sambuc     return None;
298f4a2713aSLionel Sambuc 
299f4a2713aSLionel Sambuc   const TypedefType *TDT = T->getAs<TypedefType>();
300f4a2713aSLionel Sambuc   if (TDT) {
301f4a2713aSLionel Sambuc     QualType TDTTy = QualType(TDT, 0);
302f4a2713aSLionel Sambuc     if (isObjCBOOLType(TDTTy))
303f4a2713aSLionel Sambuc       return NSAPI::NSNumberWithBool;
304f4a2713aSLionel Sambuc     if (isObjCNSIntegerType(TDTTy))
305f4a2713aSLionel Sambuc       return NSAPI::NSNumberWithInteger;
306f4a2713aSLionel Sambuc     if (isObjCNSUIntegerType(TDTTy))
307f4a2713aSLionel Sambuc       return NSAPI::NSNumberWithUnsignedInteger;
308f4a2713aSLionel Sambuc   }
309f4a2713aSLionel Sambuc 
310f4a2713aSLionel Sambuc   switch (BT->getKind()) {
311f4a2713aSLionel Sambuc   case BuiltinType::Char_S:
312f4a2713aSLionel Sambuc   case BuiltinType::SChar:
313f4a2713aSLionel Sambuc     return NSAPI::NSNumberWithChar;
314f4a2713aSLionel Sambuc   case BuiltinType::Char_U:
315f4a2713aSLionel Sambuc   case BuiltinType::UChar:
316f4a2713aSLionel Sambuc     return NSAPI::NSNumberWithUnsignedChar;
317f4a2713aSLionel Sambuc   case BuiltinType::Short:
318f4a2713aSLionel Sambuc     return NSAPI::NSNumberWithShort;
319f4a2713aSLionel Sambuc   case BuiltinType::UShort:
320f4a2713aSLionel Sambuc     return NSAPI::NSNumberWithUnsignedShort;
321f4a2713aSLionel Sambuc   case BuiltinType::Int:
322f4a2713aSLionel Sambuc     return NSAPI::NSNumberWithInt;
323f4a2713aSLionel Sambuc   case BuiltinType::UInt:
324f4a2713aSLionel Sambuc     return NSAPI::NSNumberWithUnsignedInt;
325f4a2713aSLionel Sambuc   case BuiltinType::Long:
326f4a2713aSLionel Sambuc     return NSAPI::NSNumberWithLong;
327f4a2713aSLionel Sambuc   case BuiltinType::ULong:
328f4a2713aSLionel Sambuc     return NSAPI::NSNumberWithUnsignedLong;
329f4a2713aSLionel Sambuc   case BuiltinType::LongLong:
330f4a2713aSLionel Sambuc     return NSAPI::NSNumberWithLongLong;
331f4a2713aSLionel Sambuc   case BuiltinType::ULongLong:
332f4a2713aSLionel Sambuc     return NSAPI::NSNumberWithUnsignedLongLong;
333f4a2713aSLionel Sambuc   case BuiltinType::Float:
334f4a2713aSLionel Sambuc     return NSAPI::NSNumberWithFloat;
335f4a2713aSLionel Sambuc   case BuiltinType::Double:
336f4a2713aSLionel Sambuc     return NSAPI::NSNumberWithDouble;
337f4a2713aSLionel Sambuc   case BuiltinType::Bool:
338f4a2713aSLionel Sambuc     return NSAPI::NSNumberWithBool;
339f4a2713aSLionel Sambuc 
340f4a2713aSLionel Sambuc   case BuiltinType::Void:
341f4a2713aSLionel Sambuc   case BuiltinType::WChar_U:
342f4a2713aSLionel Sambuc   case BuiltinType::WChar_S:
343f4a2713aSLionel Sambuc   case BuiltinType::Char16:
344f4a2713aSLionel Sambuc   case BuiltinType::Char32:
345f4a2713aSLionel Sambuc   case BuiltinType::Int128:
346f4a2713aSLionel Sambuc   case BuiltinType::LongDouble:
347f4a2713aSLionel Sambuc   case BuiltinType::UInt128:
348f4a2713aSLionel Sambuc   case BuiltinType::NullPtr:
349f4a2713aSLionel Sambuc   case BuiltinType::ObjCClass:
350f4a2713aSLionel Sambuc   case BuiltinType::ObjCId:
351f4a2713aSLionel Sambuc   case BuiltinType::ObjCSel:
352f4a2713aSLionel Sambuc   case BuiltinType::OCLImage1d:
353f4a2713aSLionel Sambuc   case BuiltinType::OCLImage1dArray:
354f4a2713aSLionel Sambuc   case BuiltinType::OCLImage1dBuffer:
355f4a2713aSLionel Sambuc   case BuiltinType::OCLImage2d:
356f4a2713aSLionel Sambuc   case BuiltinType::OCLImage2dArray:
357f4a2713aSLionel Sambuc   case BuiltinType::OCLImage3d:
358f4a2713aSLionel Sambuc   case BuiltinType::OCLSampler:
359f4a2713aSLionel Sambuc   case BuiltinType::OCLEvent:
360f4a2713aSLionel Sambuc   case BuiltinType::BoundMember:
361f4a2713aSLionel Sambuc   case BuiltinType::Dependent:
362f4a2713aSLionel Sambuc   case BuiltinType::Overload:
363f4a2713aSLionel Sambuc   case BuiltinType::UnknownAny:
364f4a2713aSLionel Sambuc   case BuiltinType::ARCUnbridgedCast:
365f4a2713aSLionel Sambuc   case BuiltinType::Half:
366f4a2713aSLionel Sambuc   case BuiltinType::PseudoObject:
367f4a2713aSLionel Sambuc   case BuiltinType::BuiltinFn:
368f4a2713aSLionel Sambuc     break;
369f4a2713aSLionel Sambuc   }
370f4a2713aSLionel Sambuc 
371f4a2713aSLionel Sambuc   return None;
372f4a2713aSLionel Sambuc }
373f4a2713aSLionel Sambuc 
374f4a2713aSLionel Sambuc /// \brief Returns true if \param T is a typedef of "BOOL" in objective-c.
isObjCBOOLType(QualType T) const375f4a2713aSLionel Sambuc bool NSAPI::isObjCBOOLType(QualType T) const {
376f4a2713aSLionel Sambuc   return isObjCTypedef(T, "BOOL", BOOLId);
377f4a2713aSLionel Sambuc }
378f4a2713aSLionel Sambuc /// \brief Returns true if \param T is a typedef of "NSInteger" in objective-c.
isObjCNSIntegerType(QualType T) const379f4a2713aSLionel Sambuc bool NSAPI::isObjCNSIntegerType(QualType T) const {
380f4a2713aSLionel Sambuc   return isObjCTypedef(T, "NSInteger", NSIntegerId);
381f4a2713aSLionel Sambuc }
382f4a2713aSLionel Sambuc /// \brief Returns true if \param T is a typedef of "NSUInteger" in objective-c.
isObjCNSUIntegerType(QualType T) const383f4a2713aSLionel Sambuc bool NSAPI::isObjCNSUIntegerType(QualType T) const {
384f4a2713aSLionel Sambuc   return isObjCTypedef(T, "NSUInteger", NSUIntegerId);
385f4a2713aSLionel Sambuc }
386f4a2713aSLionel Sambuc 
GetNSIntegralKind(QualType T) const387*0a6a1f1dSLionel Sambuc StringRef NSAPI::GetNSIntegralKind(QualType T) const {
388*0a6a1f1dSLionel Sambuc   if (!Ctx.getLangOpts().ObjC1 || T.isNull())
389*0a6a1f1dSLionel Sambuc     return StringRef();
390*0a6a1f1dSLionel Sambuc 
391*0a6a1f1dSLionel Sambuc   while (const TypedefType *TDT = T->getAs<TypedefType>()) {
392*0a6a1f1dSLionel Sambuc     StringRef NSIntegralResust =
393*0a6a1f1dSLionel Sambuc       llvm::StringSwitch<StringRef>(
394*0a6a1f1dSLionel Sambuc         TDT->getDecl()->getDeclName().getAsIdentifierInfo()->getName())
395*0a6a1f1dSLionel Sambuc     .Case("int8_t", "int8_t")
396*0a6a1f1dSLionel Sambuc     .Case("int16_t", "int16_t")
397*0a6a1f1dSLionel Sambuc     .Case("int32_t", "int32_t")
398*0a6a1f1dSLionel Sambuc     .Case("NSInteger", "NSInteger")
399*0a6a1f1dSLionel Sambuc     .Case("int64_t", "int64_t")
400*0a6a1f1dSLionel Sambuc     .Case("uint8_t", "uint8_t")
401*0a6a1f1dSLionel Sambuc     .Case("uint16_t", "uint16_t")
402*0a6a1f1dSLionel Sambuc     .Case("uint32_t", "uint32_t")
403*0a6a1f1dSLionel Sambuc     .Case("NSUInteger", "NSUInteger")
404*0a6a1f1dSLionel Sambuc     .Case("uint64_t", "uint64_t")
405*0a6a1f1dSLionel Sambuc     .Default(StringRef());
406*0a6a1f1dSLionel Sambuc     if (!NSIntegralResust.empty())
407*0a6a1f1dSLionel Sambuc       return NSIntegralResust;
408*0a6a1f1dSLionel Sambuc     T = TDT->desugar();
409*0a6a1f1dSLionel Sambuc   }
410*0a6a1f1dSLionel Sambuc   return StringRef();
411*0a6a1f1dSLionel Sambuc }
412*0a6a1f1dSLionel Sambuc 
isObjCTypedef(QualType T,StringRef name,IdentifierInfo * & II) const413f4a2713aSLionel Sambuc bool NSAPI::isObjCTypedef(QualType T,
414f4a2713aSLionel Sambuc                           StringRef name, IdentifierInfo *&II) const {
415f4a2713aSLionel Sambuc   if (!Ctx.getLangOpts().ObjC1)
416f4a2713aSLionel Sambuc     return false;
417f4a2713aSLionel Sambuc   if (T.isNull())
418f4a2713aSLionel Sambuc     return false;
419f4a2713aSLionel Sambuc 
420f4a2713aSLionel Sambuc   if (!II)
421f4a2713aSLionel Sambuc     II = &Ctx.Idents.get(name);
422f4a2713aSLionel Sambuc 
423f4a2713aSLionel Sambuc   while (const TypedefType *TDT = T->getAs<TypedefType>()) {
424f4a2713aSLionel Sambuc     if (TDT->getDecl()->getDeclName().getAsIdentifierInfo() == II)
425f4a2713aSLionel Sambuc       return true;
426f4a2713aSLionel Sambuc     T = TDT->desugar();
427f4a2713aSLionel Sambuc   }
428f4a2713aSLionel Sambuc 
429f4a2713aSLionel Sambuc   return false;
430f4a2713aSLionel Sambuc }
431f4a2713aSLionel Sambuc 
isObjCEnumerator(const Expr * E,StringRef name,IdentifierInfo * & II) const432f4a2713aSLionel Sambuc bool NSAPI::isObjCEnumerator(const Expr *E,
433f4a2713aSLionel Sambuc                              StringRef name, IdentifierInfo *&II) const {
434f4a2713aSLionel Sambuc   if (!Ctx.getLangOpts().ObjC1)
435f4a2713aSLionel Sambuc     return false;
436f4a2713aSLionel Sambuc   if (!E)
437f4a2713aSLionel Sambuc     return false;
438f4a2713aSLionel Sambuc 
439f4a2713aSLionel Sambuc   if (!II)
440f4a2713aSLionel Sambuc     II = &Ctx.Idents.get(name);
441f4a2713aSLionel Sambuc 
442f4a2713aSLionel Sambuc   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
443f4a2713aSLionel Sambuc     if (const EnumConstantDecl *
444f4a2713aSLionel Sambuc           EnumD = dyn_cast_or_null<EnumConstantDecl>(DRE->getDecl()))
445f4a2713aSLionel Sambuc       return EnumD->getIdentifier() == II;
446f4a2713aSLionel Sambuc 
447f4a2713aSLionel Sambuc   return false;
448f4a2713aSLionel Sambuc }
449f4a2713aSLionel Sambuc 
getOrInitSelector(ArrayRef<StringRef> Ids,Selector & Sel) const450f4a2713aSLionel Sambuc Selector NSAPI::getOrInitSelector(ArrayRef<StringRef> Ids,
451f4a2713aSLionel Sambuc                                   Selector &Sel) const {
452f4a2713aSLionel Sambuc   if (Sel.isNull()) {
453f4a2713aSLionel Sambuc     SmallVector<IdentifierInfo *, 4> Idents;
454f4a2713aSLionel Sambuc     for (ArrayRef<StringRef>::const_iterator
455f4a2713aSLionel Sambuc            I = Ids.begin(), E = Ids.end(); I != E; ++I)
456f4a2713aSLionel Sambuc       Idents.push_back(&Ctx.Idents.get(*I));
457f4a2713aSLionel Sambuc     Sel = Ctx.Selectors.getSelector(Idents.size(), Idents.data());
458f4a2713aSLionel Sambuc   }
459f4a2713aSLionel Sambuc   return Sel;
460f4a2713aSLionel Sambuc }
461