xref: /aosp_15_r20/external/clang/lib/AST/NSAPI.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //===--- NSAPI.cpp - NSFoundation APIs ------------------------------------===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li 
10*67e74705SXin Li #include "clang/AST/NSAPI.h"
11*67e74705SXin Li #include "clang/AST/ASTContext.h"
12*67e74705SXin Li #include "clang/AST/DeclObjC.h"
13*67e74705SXin Li #include "clang/AST/Expr.h"
14*67e74705SXin Li #include "llvm/ADT/StringSwitch.h"
15*67e74705SXin Li 
16*67e74705SXin Li using namespace clang;
17*67e74705SXin Li 
NSAPI(ASTContext & ctx)18*67e74705SXin Li NSAPI::NSAPI(ASTContext &ctx)
19*67e74705SXin Li   : Ctx(ctx), ClassIds(), BOOLId(nullptr), NSIntegerId(nullptr),
20*67e74705SXin Li     NSUIntegerId(nullptr), NSASCIIStringEncodingId(nullptr),
21*67e74705SXin Li     NSUTF8StringEncodingId(nullptr) {}
22*67e74705SXin Li 
getNSClassId(NSClassIdKindKind K) const23*67e74705SXin Li IdentifierInfo *NSAPI::getNSClassId(NSClassIdKindKind K) const {
24*67e74705SXin Li   static const char *ClassName[NumClassIds] = {
25*67e74705SXin Li     "NSObject",
26*67e74705SXin Li     "NSString",
27*67e74705SXin Li     "NSArray",
28*67e74705SXin Li     "NSMutableArray",
29*67e74705SXin Li     "NSDictionary",
30*67e74705SXin Li     "NSMutableDictionary",
31*67e74705SXin Li     "NSNumber",
32*67e74705SXin Li     "NSMutableSet",
33*67e74705SXin Li     "NSMutableOrderedSet",
34*67e74705SXin Li     "NSValue"
35*67e74705SXin Li   };
36*67e74705SXin Li 
37*67e74705SXin Li   if (!ClassIds[K])
38*67e74705SXin Li     return (ClassIds[K] = &Ctx.Idents.get(ClassName[K]));
39*67e74705SXin Li 
40*67e74705SXin Li   return ClassIds[K];
41*67e74705SXin Li }
42*67e74705SXin Li 
getNSStringSelector(NSStringMethodKind MK) const43*67e74705SXin Li Selector NSAPI::getNSStringSelector(NSStringMethodKind MK) const {
44*67e74705SXin Li   if (NSStringSelectors[MK].isNull()) {
45*67e74705SXin Li     Selector Sel;
46*67e74705SXin Li     switch (MK) {
47*67e74705SXin Li     case NSStr_stringWithString:
48*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("stringWithString"));
49*67e74705SXin Li       break;
50*67e74705SXin Li     case NSStr_stringWithUTF8String:
51*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(
52*67e74705SXin Li                                        &Ctx.Idents.get("stringWithUTF8String"));
53*67e74705SXin Li       break;
54*67e74705SXin Li     case NSStr_initWithUTF8String:
55*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(
56*67e74705SXin Li                                        &Ctx.Idents.get("initWithUTF8String"));
57*67e74705SXin Li       break;
58*67e74705SXin Li     case NSStr_stringWithCStringEncoding: {
59*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
60*67e74705SXin Li         &Ctx.Idents.get("stringWithCString"),
61*67e74705SXin Li         &Ctx.Idents.get("encoding")
62*67e74705SXin Li       };
63*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
64*67e74705SXin Li       break;
65*67e74705SXin Li     }
66*67e74705SXin Li     case NSStr_stringWithCString:
67*67e74705SXin Li       Sel= Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("stringWithCString"));
68*67e74705SXin Li       break;
69*67e74705SXin Li     case NSStr_initWithString:
70*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("initWithString"));
71*67e74705SXin Li       break;
72*67e74705SXin Li     }
73*67e74705SXin Li     return (NSStringSelectors[MK] = Sel);
74*67e74705SXin Li   }
75*67e74705SXin Li 
76*67e74705SXin Li   return NSStringSelectors[MK];
77*67e74705SXin Li }
78*67e74705SXin Li 
79*67e74705SXin Li Optional<NSAPI::NSStringMethodKind>
getNSStringMethodKind(Selector Sel) const80*67e74705SXin Li NSAPI::getNSStringMethodKind(Selector Sel) const {
81*67e74705SXin Li   for (unsigned i = 0; i != NumNSStringMethods; ++i) {
82*67e74705SXin Li     NSStringMethodKind MK = NSStringMethodKind(i);
83*67e74705SXin Li     if (Sel == getNSStringSelector(MK))
84*67e74705SXin Li       return MK;
85*67e74705SXin Li   }
86*67e74705SXin Li 
87*67e74705SXin Li   return None;
88*67e74705SXin Li }
89*67e74705SXin Li 
getNSArraySelector(NSArrayMethodKind MK) const90*67e74705SXin Li Selector NSAPI::getNSArraySelector(NSArrayMethodKind MK) const {
91*67e74705SXin Li   if (NSArraySelectors[MK].isNull()) {
92*67e74705SXin Li     Selector Sel;
93*67e74705SXin Li     switch (MK) {
94*67e74705SXin Li     case NSArr_array:
95*67e74705SXin Li       Sel = Ctx.Selectors.getNullarySelector(&Ctx.Idents.get("array"));
96*67e74705SXin Li       break;
97*67e74705SXin Li     case NSArr_arrayWithArray:
98*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("arrayWithArray"));
99*67e74705SXin Li       break;
100*67e74705SXin Li     case NSArr_arrayWithObject:
101*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("arrayWithObject"));
102*67e74705SXin Li       break;
103*67e74705SXin Li     case NSArr_arrayWithObjects:
104*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("arrayWithObjects"));
105*67e74705SXin Li       break;
106*67e74705SXin Li     case NSArr_arrayWithObjectsCount: {
107*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
108*67e74705SXin Li         &Ctx.Idents.get("arrayWithObjects"),
109*67e74705SXin Li         &Ctx.Idents.get("count")
110*67e74705SXin Li       };
111*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
112*67e74705SXin Li       break;
113*67e74705SXin Li     }
114*67e74705SXin Li     case NSArr_initWithArray:
115*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("initWithArray"));
116*67e74705SXin Li       break;
117*67e74705SXin Li     case NSArr_initWithObjects:
118*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("initWithObjects"));
119*67e74705SXin Li       break;
120*67e74705SXin Li     case NSArr_objectAtIndex:
121*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("objectAtIndex"));
122*67e74705SXin Li       break;
123*67e74705SXin Li     case NSMutableArr_replaceObjectAtIndex: {
124*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
125*67e74705SXin Li         &Ctx.Idents.get("replaceObjectAtIndex"),
126*67e74705SXin Li         &Ctx.Idents.get("withObject")
127*67e74705SXin Li       };
128*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
129*67e74705SXin Li       break;
130*67e74705SXin Li     }
131*67e74705SXin Li     case NSMutableArr_addObject:
132*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("addObject"));
133*67e74705SXin Li       break;
134*67e74705SXin Li     case NSMutableArr_insertObjectAtIndex: {
135*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
136*67e74705SXin Li         &Ctx.Idents.get("insertObject"),
137*67e74705SXin Li         &Ctx.Idents.get("atIndex")
138*67e74705SXin Li       };
139*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
140*67e74705SXin Li       break;
141*67e74705SXin Li     }
142*67e74705SXin Li     case NSMutableArr_setObjectAtIndexedSubscript: {
143*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
144*67e74705SXin Li         &Ctx.Idents.get("setObject"),
145*67e74705SXin Li         &Ctx.Idents.get("atIndexedSubscript")
146*67e74705SXin Li       };
147*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
148*67e74705SXin Li       break;
149*67e74705SXin Li     }
150*67e74705SXin Li     }
151*67e74705SXin Li     return (NSArraySelectors[MK] = Sel);
152*67e74705SXin Li   }
153*67e74705SXin Li 
154*67e74705SXin Li   return NSArraySelectors[MK];
155*67e74705SXin Li }
156*67e74705SXin Li 
getNSArrayMethodKind(Selector Sel)157*67e74705SXin Li Optional<NSAPI::NSArrayMethodKind> NSAPI::getNSArrayMethodKind(Selector Sel) {
158*67e74705SXin Li   for (unsigned i = 0; i != NumNSArrayMethods; ++i) {
159*67e74705SXin Li     NSArrayMethodKind MK = NSArrayMethodKind(i);
160*67e74705SXin Li     if (Sel == getNSArraySelector(MK))
161*67e74705SXin Li       return MK;
162*67e74705SXin Li   }
163*67e74705SXin Li 
164*67e74705SXin Li   return None;
165*67e74705SXin Li }
166*67e74705SXin Li 
getNSDictionarySelector(NSDictionaryMethodKind MK) const167*67e74705SXin Li Selector NSAPI::getNSDictionarySelector(
168*67e74705SXin Li                                        NSDictionaryMethodKind MK) const {
169*67e74705SXin Li   if (NSDictionarySelectors[MK].isNull()) {
170*67e74705SXin Li     Selector Sel;
171*67e74705SXin Li     switch (MK) {
172*67e74705SXin Li     case NSDict_dictionary:
173*67e74705SXin Li       Sel = Ctx.Selectors.getNullarySelector(&Ctx.Idents.get("dictionary"));
174*67e74705SXin Li       break;
175*67e74705SXin Li     case NSDict_dictionaryWithDictionary:
176*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(
177*67e74705SXin Li                                    &Ctx.Idents.get("dictionaryWithDictionary"));
178*67e74705SXin Li       break;
179*67e74705SXin Li     case NSDict_dictionaryWithObjectForKey: {
180*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
181*67e74705SXin Li         &Ctx.Idents.get("dictionaryWithObject"),
182*67e74705SXin Li         &Ctx.Idents.get("forKey")
183*67e74705SXin Li       };
184*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
185*67e74705SXin Li       break;
186*67e74705SXin Li     }
187*67e74705SXin Li     case NSDict_dictionaryWithObjectsForKeys: {
188*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
189*67e74705SXin Li         &Ctx.Idents.get("dictionaryWithObjects"),
190*67e74705SXin Li         &Ctx.Idents.get("forKeys")
191*67e74705SXin Li       };
192*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
193*67e74705SXin Li       break;
194*67e74705SXin Li     }
195*67e74705SXin Li     case NSDict_dictionaryWithObjectsForKeysCount: {
196*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
197*67e74705SXin Li         &Ctx.Idents.get("dictionaryWithObjects"),
198*67e74705SXin Li         &Ctx.Idents.get("forKeys"),
199*67e74705SXin Li         &Ctx.Idents.get("count")
200*67e74705SXin Li       };
201*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(3, KeyIdents);
202*67e74705SXin Li       break;
203*67e74705SXin Li     }
204*67e74705SXin Li     case NSDict_dictionaryWithObjectsAndKeys:
205*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(
206*67e74705SXin Li                                &Ctx.Idents.get("dictionaryWithObjectsAndKeys"));
207*67e74705SXin Li       break;
208*67e74705SXin Li     case NSDict_initWithDictionary:
209*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(
210*67e74705SXin Li                                          &Ctx.Idents.get("initWithDictionary"));
211*67e74705SXin Li       break;
212*67e74705SXin Li     case NSDict_initWithObjectsAndKeys:
213*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(
214*67e74705SXin Li                                      &Ctx.Idents.get("initWithObjectsAndKeys"));
215*67e74705SXin Li       break;
216*67e74705SXin Li     case NSDict_initWithObjectsForKeys: {
217*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
218*67e74705SXin Li         &Ctx.Idents.get("initWithObjects"),
219*67e74705SXin Li         &Ctx.Idents.get("forKeys")
220*67e74705SXin Li       };
221*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
222*67e74705SXin Li       break;
223*67e74705SXin Li     }
224*67e74705SXin Li     case NSDict_objectForKey:
225*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("objectForKey"));
226*67e74705SXin Li       break;
227*67e74705SXin Li     case NSMutableDict_setObjectForKey: {
228*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
229*67e74705SXin Li         &Ctx.Idents.get("setObject"),
230*67e74705SXin Li         &Ctx.Idents.get("forKey")
231*67e74705SXin Li       };
232*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
233*67e74705SXin Li       break;
234*67e74705SXin Li     }
235*67e74705SXin Li     case NSMutableDict_setObjectForKeyedSubscript: {
236*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
237*67e74705SXin Li         &Ctx.Idents.get("setObject"),
238*67e74705SXin Li         &Ctx.Idents.get("forKeyedSubscript")
239*67e74705SXin Li       };
240*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
241*67e74705SXin Li       break;
242*67e74705SXin Li     }
243*67e74705SXin Li     case NSMutableDict_setValueForKey: {
244*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
245*67e74705SXin Li         &Ctx.Idents.get("setValue"),
246*67e74705SXin Li         &Ctx.Idents.get("forKey")
247*67e74705SXin Li       };
248*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
249*67e74705SXin Li       break;
250*67e74705SXin Li     }
251*67e74705SXin Li     }
252*67e74705SXin Li     return (NSDictionarySelectors[MK] = Sel);
253*67e74705SXin Li   }
254*67e74705SXin Li 
255*67e74705SXin Li   return NSDictionarySelectors[MK];
256*67e74705SXin Li }
257*67e74705SXin Li 
258*67e74705SXin Li Optional<NSAPI::NSDictionaryMethodKind>
getNSDictionaryMethodKind(Selector Sel)259*67e74705SXin Li NSAPI::getNSDictionaryMethodKind(Selector Sel) {
260*67e74705SXin Li   for (unsigned i = 0; i != NumNSDictionaryMethods; ++i) {
261*67e74705SXin Li     NSDictionaryMethodKind MK = NSDictionaryMethodKind(i);
262*67e74705SXin Li     if (Sel == getNSDictionarySelector(MK))
263*67e74705SXin Li       return MK;
264*67e74705SXin Li   }
265*67e74705SXin Li 
266*67e74705SXin Li   return None;
267*67e74705SXin Li }
268*67e74705SXin Li 
getNSSetSelector(NSSetMethodKind MK) const269*67e74705SXin Li Selector NSAPI::getNSSetSelector(NSSetMethodKind MK) const {
270*67e74705SXin Li   if (NSSetSelectors[MK].isNull()) {
271*67e74705SXin Li     Selector Sel;
272*67e74705SXin Li     switch (MK) {
273*67e74705SXin Li     case NSMutableSet_addObject:
274*67e74705SXin Li       Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("addObject"));
275*67e74705SXin Li       break;
276*67e74705SXin Li     case NSOrderedSet_insertObjectAtIndex: {
277*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
278*67e74705SXin Li         &Ctx.Idents.get("insertObject"),
279*67e74705SXin Li         &Ctx.Idents.get("atIndex")
280*67e74705SXin Li       };
281*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
282*67e74705SXin Li       break;
283*67e74705SXin Li     }
284*67e74705SXin Li     case NSOrderedSet_setObjectAtIndex: {
285*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
286*67e74705SXin Li         &Ctx.Idents.get("setObject"),
287*67e74705SXin Li         &Ctx.Idents.get("atIndex")
288*67e74705SXin Li       };
289*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
290*67e74705SXin Li       break;
291*67e74705SXin Li     }
292*67e74705SXin Li     case NSOrderedSet_setObjectAtIndexedSubscript: {
293*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
294*67e74705SXin Li         &Ctx.Idents.get("setObject"),
295*67e74705SXin Li         &Ctx.Idents.get("atIndexedSubscript")
296*67e74705SXin Li       };
297*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
298*67e74705SXin Li       break;
299*67e74705SXin Li     }
300*67e74705SXin Li     case NSOrderedSet_replaceObjectAtIndexWithObject: {
301*67e74705SXin Li       IdentifierInfo *KeyIdents[] = {
302*67e74705SXin Li         &Ctx.Idents.get("replaceObjectAtIndex"),
303*67e74705SXin Li         &Ctx.Idents.get("withObject")
304*67e74705SXin Li       };
305*67e74705SXin Li       Sel = Ctx.Selectors.getSelector(2, KeyIdents);
306*67e74705SXin Li       break;
307*67e74705SXin Li     }
308*67e74705SXin Li     }
309*67e74705SXin Li     return (NSSetSelectors[MK] = Sel);
310*67e74705SXin Li   }
311*67e74705SXin Li 
312*67e74705SXin Li   return NSSetSelectors[MK];
313*67e74705SXin Li }
314*67e74705SXin Li 
315*67e74705SXin Li Optional<NSAPI::NSSetMethodKind>
getNSSetMethodKind(Selector Sel)316*67e74705SXin Li NSAPI::getNSSetMethodKind(Selector Sel) {
317*67e74705SXin Li   for (unsigned i = 0; i != NumNSSetMethods; ++i) {
318*67e74705SXin Li     NSSetMethodKind MK = NSSetMethodKind(i);
319*67e74705SXin Li     if (Sel == getNSSetSelector(MK))
320*67e74705SXin Li       return MK;
321*67e74705SXin Li   }
322*67e74705SXin Li 
323*67e74705SXin Li   return None;
324*67e74705SXin Li }
325*67e74705SXin Li 
getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,bool Instance) const326*67e74705SXin Li Selector NSAPI::getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
327*67e74705SXin Li                                            bool Instance) const {
328*67e74705SXin Li   static const char *ClassSelectorName[NumNSNumberLiteralMethods] = {
329*67e74705SXin Li     "numberWithChar",
330*67e74705SXin Li     "numberWithUnsignedChar",
331*67e74705SXin Li     "numberWithShort",
332*67e74705SXin Li     "numberWithUnsignedShort",
333*67e74705SXin Li     "numberWithInt",
334*67e74705SXin Li     "numberWithUnsignedInt",
335*67e74705SXin Li     "numberWithLong",
336*67e74705SXin Li     "numberWithUnsignedLong",
337*67e74705SXin Li     "numberWithLongLong",
338*67e74705SXin Li     "numberWithUnsignedLongLong",
339*67e74705SXin Li     "numberWithFloat",
340*67e74705SXin Li     "numberWithDouble",
341*67e74705SXin Li     "numberWithBool",
342*67e74705SXin Li     "numberWithInteger",
343*67e74705SXin Li     "numberWithUnsignedInteger"
344*67e74705SXin Li   };
345*67e74705SXin Li   static const char *InstanceSelectorName[NumNSNumberLiteralMethods] = {
346*67e74705SXin Li     "initWithChar",
347*67e74705SXin Li     "initWithUnsignedChar",
348*67e74705SXin Li     "initWithShort",
349*67e74705SXin Li     "initWithUnsignedShort",
350*67e74705SXin Li     "initWithInt",
351*67e74705SXin Li     "initWithUnsignedInt",
352*67e74705SXin Li     "initWithLong",
353*67e74705SXin Li     "initWithUnsignedLong",
354*67e74705SXin Li     "initWithLongLong",
355*67e74705SXin Li     "initWithUnsignedLongLong",
356*67e74705SXin Li     "initWithFloat",
357*67e74705SXin Li     "initWithDouble",
358*67e74705SXin Li     "initWithBool",
359*67e74705SXin Li     "initWithInteger",
360*67e74705SXin Li     "initWithUnsignedInteger"
361*67e74705SXin Li   };
362*67e74705SXin Li 
363*67e74705SXin Li   Selector *Sels;
364*67e74705SXin Li   const char **Names;
365*67e74705SXin Li   if (Instance) {
366*67e74705SXin Li     Sels = NSNumberInstanceSelectors;
367*67e74705SXin Li     Names = InstanceSelectorName;
368*67e74705SXin Li   } else {
369*67e74705SXin Li     Sels = NSNumberClassSelectors;
370*67e74705SXin Li     Names = ClassSelectorName;
371*67e74705SXin Li   }
372*67e74705SXin Li 
373*67e74705SXin Li   if (Sels[MK].isNull())
374*67e74705SXin Li     Sels[MK] = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get(Names[MK]));
375*67e74705SXin Li   return Sels[MK];
376*67e74705SXin Li }
377*67e74705SXin Li 
378*67e74705SXin Li Optional<NSAPI::NSNumberLiteralMethodKind>
getNSNumberLiteralMethodKind(Selector Sel) const379*67e74705SXin Li NSAPI::getNSNumberLiteralMethodKind(Selector Sel) const {
380*67e74705SXin Li   for (unsigned i = 0; i != NumNSNumberLiteralMethods; ++i) {
381*67e74705SXin Li     NSNumberLiteralMethodKind MK = NSNumberLiteralMethodKind(i);
382*67e74705SXin Li     if (isNSNumberLiteralSelector(MK, Sel))
383*67e74705SXin Li       return MK;
384*67e74705SXin Li   }
385*67e74705SXin Li 
386*67e74705SXin Li   return None;
387*67e74705SXin Li }
388*67e74705SXin Li 
389*67e74705SXin Li Optional<NSAPI::NSNumberLiteralMethodKind>
getNSNumberFactoryMethodKind(QualType T) const390*67e74705SXin Li NSAPI::getNSNumberFactoryMethodKind(QualType T) const {
391*67e74705SXin Li   const BuiltinType *BT = T->getAs<BuiltinType>();
392*67e74705SXin Li   if (!BT)
393*67e74705SXin Li     return None;
394*67e74705SXin Li 
395*67e74705SXin Li   const TypedefType *TDT = T->getAs<TypedefType>();
396*67e74705SXin Li   if (TDT) {
397*67e74705SXin Li     QualType TDTTy = QualType(TDT, 0);
398*67e74705SXin Li     if (isObjCBOOLType(TDTTy))
399*67e74705SXin Li       return NSAPI::NSNumberWithBool;
400*67e74705SXin Li     if (isObjCNSIntegerType(TDTTy))
401*67e74705SXin Li       return NSAPI::NSNumberWithInteger;
402*67e74705SXin Li     if (isObjCNSUIntegerType(TDTTy))
403*67e74705SXin Li       return NSAPI::NSNumberWithUnsignedInteger;
404*67e74705SXin Li   }
405*67e74705SXin Li 
406*67e74705SXin Li   switch (BT->getKind()) {
407*67e74705SXin Li   case BuiltinType::Char_S:
408*67e74705SXin Li   case BuiltinType::SChar:
409*67e74705SXin Li     return NSAPI::NSNumberWithChar;
410*67e74705SXin Li   case BuiltinType::Char_U:
411*67e74705SXin Li   case BuiltinType::UChar:
412*67e74705SXin Li     return NSAPI::NSNumberWithUnsignedChar;
413*67e74705SXin Li   case BuiltinType::Short:
414*67e74705SXin Li     return NSAPI::NSNumberWithShort;
415*67e74705SXin Li   case BuiltinType::UShort:
416*67e74705SXin Li     return NSAPI::NSNumberWithUnsignedShort;
417*67e74705SXin Li   case BuiltinType::Int:
418*67e74705SXin Li     return NSAPI::NSNumberWithInt;
419*67e74705SXin Li   case BuiltinType::UInt:
420*67e74705SXin Li     return NSAPI::NSNumberWithUnsignedInt;
421*67e74705SXin Li   case BuiltinType::Long:
422*67e74705SXin Li     return NSAPI::NSNumberWithLong;
423*67e74705SXin Li   case BuiltinType::ULong:
424*67e74705SXin Li     return NSAPI::NSNumberWithUnsignedLong;
425*67e74705SXin Li   case BuiltinType::LongLong:
426*67e74705SXin Li     return NSAPI::NSNumberWithLongLong;
427*67e74705SXin Li   case BuiltinType::ULongLong:
428*67e74705SXin Li     return NSAPI::NSNumberWithUnsignedLongLong;
429*67e74705SXin Li   case BuiltinType::Float:
430*67e74705SXin Li     return NSAPI::NSNumberWithFloat;
431*67e74705SXin Li   case BuiltinType::Double:
432*67e74705SXin Li     return NSAPI::NSNumberWithDouble;
433*67e74705SXin Li   case BuiltinType::Bool:
434*67e74705SXin Li     return NSAPI::NSNumberWithBool;
435*67e74705SXin Li 
436*67e74705SXin Li   case BuiltinType::Void:
437*67e74705SXin Li   case BuiltinType::WChar_U:
438*67e74705SXin Li   case BuiltinType::WChar_S:
439*67e74705SXin Li   case BuiltinType::Char16:
440*67e74705SXin Li   case BuiltinType::Char32:
441*67e74705SXin Li   case BuiltinType::Int128:
442*67e74705SXin Li   case BuiltinType::LongDouble:
443*67e74705SXin Li   case BuiltinType::UInt128:
444*67e74705SXin Li   case BuiltinType::Float128:
445*67e74705SXin Li   case BuiltinType::NullPtr:
446*67e74705SXin Li   case BuiltinType::ObjCClass:
447*67e74705SXin Li   case BuiltinType::ObjCId:
448*67e74705SXin Li   case BuiltinType::ObjCSel:
449*67e74705SXin Li #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
450*67e74705SXin Li   case BuiltinType::Id:
451*67e74705SXin Li #include "clang/Basic/OpenCLImageTypes.def"
452*67e74705SXin Li   case BuiltinType::OCLSampler:
453*67e74705SXin Li   case BuiltinType::OCLEvent:
454*67e74705SXin Li   case BuiltinType::OCLClkEvent:
455*67e74705SXin Li   case BuiltinType::OCLQueue:
456*67e74705SXin Li   case BuiltinType::OCLNDRange:
457*67e74705SXin Li   case BuiltinType::OCLReserveID:
458*67e74705SXin Li   case BuiltinType::BoundMember:
459*67e74705SXin Li   case BuiltinType::Dependent:
460*67e74705SXin Li   case BuiltinType::Overload:
461*67e74705SXin Li   case BuiltinType::UnknownAny:
462*67e74705SXin Li   case BuiltinType::ARCUnbridgedCast:
463*67e74705SXin Li   case BuiltinType::Half:
464*67e74705SXin Li   case BuiltinType::PseudoObject:
465*67e74705SXin Li   case BuiltinType::BuiltinFn:
466*67e74705SXin Li   case BuiltinType::OMPArraySection:
467*67e74705SXin Li     break;
468*67e74705SXin Li   }
469*67e74705SXin Li 
470*67e74705SXin Li   return None;
471*67e74705SXin Li }
472*67e74705SXin Li 
473*67e74705SXin Li /// \brief Returns true if \param T is a typedef of "BOOL" in objective-c.
isObjCBOOLType(QualType T) const474*67e74705SXin Li bool NSAPI::isObjCBOOLType(QualType T) const {
475*67e74705SXin Li   return isObjCTypedef(T, "BOOL", BOOLId);
476*67e74705SXin Li }
477*67e74705SXin Li /// \brief Returns true if \param T is a typedef of "NSInteger" in objective-c.
isObjCNSIntegerType(QualType T) const478*67e74705SXin Li bool NSAPI::isObjCNSIntegerType(QualType T) const {
479*67e74705SXin Li   return isObjCTypedef(T, "NSInteger", NSIntegerId);
480*67e74705SXin Li }
481*67e74705SXin Li /// \brief Returns true if \param T is a typedef of "NSUInteger" in objective-c.
isObjCNSUIntegerType(QualType T) const482*67e74705SXin Li bool NSAPI::isObjCNSUIntegerType(QualType T) const {
483*67e74705SXin Li   return isObjCTypedef(T, "NSUInteger", NSUIntegerId);
484*67e74705SXin Li }
485*67e74705SXin Li 
GetNSIntegralKind(QualType T) const486*67e74705SXin Li StringRef NSAPI::GetNSIntegralKind(QualType T) const {
487*67e74705SXin Li   if (!Ctx.getLangOpts().ObjC1 || T.isNull())
488*67e74705SXin Li     return StringRef();
489*67e74705SXin Li 
490*67e74705SXin Li   while (const TypedefType *TDT = T->getAs<TypedefType>()) {
491*67e74705SXin Li     StringRef NSIntegralResust =
492*67e74705SXin Li       llvm::StringSwitch<StringRef>(
493*67e74705SXin Li         TDT->getDecl()->getDeclName().getAsIdentifierInfo()->getName())
494*67e74705SXin Li     .Case("int8_t", "int8_t")
495*67e74705SXin Li     .Case("int16_t", "int16_t")
496*67e74705SXin Li     .Case("int32_t", "int32_t")
497*67e74705SXin Li     .Case("NSInteger", "NSInteger")
498*67e74705SXin Li     .Case("int64_t", "int64_t")
499*67e74705SXin Li     .Case("uint8_t", "uint8_t")
500*67e74705SXin Li     .Case("uint16_t", "uint16_t")
501*67e74705SXin Li     .Case("uint32_t", "uint32_t")
502*67e74705SXin Li     .Case("NSUInteger", "NSUInteger")
503*67e74705SXin Li     .Case("uint64_t", "uint64_t")
504*67e74705SXin Li     .Default(StringRef());
505*67e74705SXin Li     if (!NSIntegralResust.empty())
506*67e74705SXin Li       return NSIntegralResust;
507*67e74705SXin Li     T = TDT->desugar();
508*67e74705SXin Li   }
509*67e74705SXin Li   return StringRef();
510*67e74705SXin Li }
511*67e74705SXin Li 
isMacroDefined(StringRef Id) const512*67e74705SXin Li bool NSAPI::isMacroDefined(StringRef Id) const {
513*67e74705SXin Li   // FIXME: Check whether the relevant module macros are visible.
514*67e74705SXin Li   return Ctx.Idents.get(Id).hasMacroDefinition();
515*67e74705SXin Li }
516*67e74705SXin Li 
isSubclassOfNSClass(ObjCInterfaceDecl * InterfaceDecl,NSClassIdKindKind NSClassKind) const517*67e74705SXin Li bool NSAPI::isSubclassOfNSClass(ObjCInterfaceDecl *InterfaceDecl,
518*67e74705SXin Li                                 NSClassIdKindKind NSClassKind) const {
519*67e74705SXin Li   if (!InterfaceDecl) {
520*67e74705SXin Li     return false;
521*67e74705SXin Li   }
522*67e74705SXin Li 
523*67e74705SXin Li   IdentifierInfo *NSClassID = getNSClassId(NSClassKind);
524*67e74705SXin Li 
525*67e74705SXin Li   bool IsSubclass = false;
526*67e74705SXin Li   do {
527*67e74705SXin Li     IsSubclass = NSClassID == InterfaceDecl->getIdentifier();
528*67e74705SXin Li 
529*67e74705SXin Li     if (IsSubclass) {
530*67e74705SXin Li       break;
531*67e74705SXin Li     }
532*67e74705SXin Li   } while ((InterfaceDecl = InterfaceDecl->getSuperClass()));
533*67e74705SXin Li 
534*67e74705SXin Li   return IsSubclass;
535*67e74705SXin Li }
536*67e74705SXin Li 
isObjCTypedef(QualType T,StringRef name,IdentifierInfo * & II) const537*67e74705SXin Li bool NSAPI::isObjCTypedef(QualType T,
538*67e74705SXin Li                           StringRef name, IdentifierInfo *&II) const {
539*67e74705SXin Li   if (!Ctx.getLangOpts().ObjC1)
540*67e74705SXin Li     return false;
541*67e74705SXin Li   if (T.isNull())
542*67e74705SXin Li     return false;
543*67e74705SXin Li 
544*67e74705SXin Li   if (!II)
545*67e74705SXin Li     II = &Ctx.Idents.get(name);
546*67e74705SXin Li 
547*67e74705SXin Li   while (const TypedefType *TDT = T->getAs<TypedefType>()) {
548*67e74705SXin Li     if (TDT->getDecl()->getDeclName().getAsIdentifierInfo() == II)
549*67e74705SXin Li       return true;
550*67e74705SXin Li     T = TDT->desugar();
551*67e74705SXin Li   }
552*67e74705SXin Li 
553*67e74705SXin Li   return false;
554*67e74705SXin Li }
555*67e74705SXin Li 
isObjCEnumerator(const Expr * E,StringRef name,IdentifierInfo * & II) const556*67e74705SXin Li bool NSAPI::isObjCEnumerator(const Expr *E,
557*67e74705SXin Li                              StringRef name, IdentifierInfo *&II) const {
558*67e74705SXin Li   if (!Ctx.getLangOpts().ObjC1)
559*67e74705SXin Li     return false;
560*67e74705SXin Li   if (!E)
561*67e74705SXin Li     return false;
562*67e74705SXin Li 
563*67e74705SXin Li   if (!II)
564*67e74705SXin Li     II = &Ctx.Idents.get(name);
565*67e74705SXin Li 
566*67e74705SXin Li   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
567*67e74705SXin Li     if (const EnumConstantDecl *
568*67e74705SXin Li           EnumD = dyn_cast_or_null<EnumConstantDecl>(DRE->getDecl()))
569*67e74705SXin Li       return EnumD->getIdentifier() == II;
570*67e74705SXin Li 
571*67e74705SXin Li   return false;
572*67e74705SXin Li }
573*67e74705SXin Li 
getOrInitSelector(ArrayRef<StringRef> Ids,Selector & Sel) const574*67e74705SXin Li Selector NSAPI::getOrInitSelector(ArrayRef<StringRef> Ids,
575*67e74705SXin Li                                   Selector &Sel) const {
576*67e74705SXin Li   if (Sel.isNull()) {
577*67e74705SXin Li     SmallVector<IdentifierInfo *, 4> Idents;
578*67e74705SXin Li     for (ArrayRef<StringRef>::const_iterator
579*67e74705SXin Li            I = Ids.begin(), E = Ids.end(); I != E; ++I)
580*67e74705SXin Li       Idents.push_back(&Ctx.Idents.get(*I));
581*67e74705SXin Li     Sel = Ctx.Selectors.getSelector(Idents.size(), Idents.data());
582*67e74705SXin Li   }
583*67e74705SXin Li   return Sel;
584*67e74705SXin Li }
585