1 //===--- NSAPI.cpp - NSFoundation APIs ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/AST/NSAPI.h"
11 #include "clang/AST/ASTContext.h"
13 using namespace clang;
15 NSAPI::NSAPI(ASTContext &ctx)
16 : Ctx(ctx), ClassIds() {
19 IdentifierInfo *NSAPI::getNSClassId(NSClassIdKindKind K) const {
20 static const char *ClassName[NumClassIds] = {
26 "NSMutableDictionary",
31 return (ClassIds[K] = &Ctx.Idents.get(ClassName[K]));
36 Selector NSAPI::getNSStringSelector(NSStringMethodKind MK) const {
37 if (NSStringSelectors[MK].isNull()) {
40 case NSStr_stringWithString:
41 Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("stringWithString"));
43 case NSStr_initWithString:
44 Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("initWithString"));
47 return (NSStringSelectors[MK] = Sel);
50 return NSStringSelectors[MK];
53 Selector NSAPI::getNSArraySelector(NSArrayMethodKind MK) const {
54 if (NSArraySelectors[MK].isNull()) {
58 Sel = Ctx.Selectors.getNullarySelector(&Ctx.Idents.get("array"));
60 case NSArr_arrayWithArray:
61 Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("arrayWithArray"));
63 case NSArr_arrayWithObject:
64 Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("arrayWithObject"));
66 case NSArr_arrayWithObjects:
67 Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("arrayWithObjects"));
69 case NSArr_arrayWithObjectsCount: {
70 IdentifierInfo *KeyIdents[] = {
71 &Ctx.Idents.get("arrayWithObjects"),
72 &Ctx.Idents.get("count")
74 Sel = Ctx.Selectors.getSelector(2, KeyIdents);
77 case NSArr_initWithArray:
78 Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("initWithArray"));
80 case NSArr_initWithObjects:
81 Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("initWithObjects"));
83 case NSArr_objectAtIndex:
84 Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("objectAtIndex"));
86 case NSMutableArr_replaceObjectAtIndex: {
87 IdentifierInfo *KeyIdents[] = {
88 &Ctx.Idents.get("replaceObjectAtIndex"),
89 &Ctx.Idents.get("withObject")
91 Sel = Ctx.Selectors.getSelector(2, KeyIdents);
95 return (NSArraySelectors[MK] = Sel);
98 return NSArraySelectors[MK];
101 llvm::Optional<NSAPI::NSArrayMethodKind>
102 NSAPI::getNSArrayMethodKind(Selector Sel) {
103 for (unsigned i = 0; i != NumNSArrayMethods; ++i) {
104 NSArrayMethodKind MK = NSArrayMethodKind(i);
105 if (Sel == getNSArraySelector(MK))
109 return llvm::Optional<NSArrayMethodKind>();
112 Selector NSAPI::getNSDictionarySelector(
113 NSDictionaryMethodKind MK) const {
114 if (NSDictionarySelectors[MK].isNull()) {
117 case NSDict_dictionary:
118 Sel = Ctx.Selectors.getNullarySelector(&Ctx.Idents.get("dictionary"));
120 case NSDict_dictionaryWithDictionary:
121 Sel = Ctx.Selectors.getUnarySelector(
122 &Ctx.Idents.get("dictionaryWithDictionary"));
124 case NSDict_dictionaryWithObjectForKey: {
125 IdentifierInfo *KeyIdents[] = {
126 &Ctx.Idents.get("dictionaryWithObject"),
127 &Ctx.Idents.get("forKey")
129 Sel = Ctx.Selectors.getSelector(2, KeyIdents);
132 case NSDict_dictionaryWithObjectsForKeys: {
133 IdentifierInfo *KeyIdents[] = {
134 &Ctx.Idents.get("dictionaryWithObjects"),
135 &Ctx.Idents.get("forKeys")
137 Sel = Ctx.Selectors.getSelector(2, KeyIdents);
140 case NSDict_dictionaryWithObjectsForKeysCount: {
141 IdentifierInfo *KeyIdents[] = {
142 &Ctx.Idents.get("dictionaryWithObjects"),
143 &Ctx.Idents.get("forKeys"),
144 &Ctx.Idents.get("count")
146 Sel = Ctx.Selectors.getSelector(3, KeyIdents);
149 case NSDict_dictionaryWithObjectsAndKeys:
150 Sel = Ctx.Selectors.getUnarySelector(
151 &Ctx.Idents.get("dictionaryWithObjectsAndKeys"));
153 case NSDict_initWithDictionary:
154 Sel = Ctx.Selectors.getUnarySelector(
155 &Ctx.Idents.get("initWithDictionary"));
157 case NSDict_initWithObjectsAndKeys:
158 Sel = Ctx.Selectors.getUnarySelector(
159 &Ctx.Idents.get("initWithObjectsAndKeys"));
161 case NSDict_objectForKey:
162 Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("objectForKey"));
164 case NSMutableDict_setObjectForKey: {
165 IdentifierInfo *KeyIdents[] = {
166 &Ctx.Idents.get("setObject"),
167 &Ctx.Idents.get("forKey")
169 Sel = Ctx.Selectors.getSelector(2, KeyIdents);
173 return (NSDictionarySelectors[MK] = Sel);
176 return NSDictionarySelectors[MK];
179 llvm::Optional<NSAPI::NSDictionaryMethodKind>
180 NSAPI::getNSDictionaryMethodKind(Selector Sel) {
181 for (unsigned i = 0; i != NumNSDictionaryMethods; ++i) {
182 NSDictionaryMethodKind MK = NSDictionaryMethodKind(i);
183 if (Sel == getNSDictionarySelector(MK))
187 return llvm::Optional<NSDictionaryMethodKind>();
190 Selector NSAPI::getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
191 bool Instance) const {
192 static const char *ClassSelectorName[NumNSNumberLiteralMethods] = {
194 "numberWithUnsignedChar",
196 "numberWithUnsignedShort",
198 "numberWithUnsignedInt",
200 "numberWithUnsignedLong",
201 "numberWithLongLong",
202 "numberWithUnsignedLongLong",
207 "numberWithUnsignedInteger"
209 static const char *InstanceSelectorName[NumNSNumberLiteralMethods] = {
211 "initWithUnsignedChar",
213 "initWithUnsignedShort",
215 "initWithUnsignedInt",
217 "initWithUnsignedLong",
219 "initWithUnsignedLongLong",
224 "initWithUnsignedInteger"
230 Sels = NSNumberInstanceSelectors;
231 Names = InstanceSelectorName;
233 Sels = NSNumberClassSelectors;
234 Names = ClassSelectorName;
237 if (Sels[MK].isNull())
238 Sels[MK] = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get(Names[MK]));
242 llvm::Optional<NSAPI::NSNumberLiteralMethodKind>
243 NSAPI::getNSNumberLiteralMethodKind(Selector Sel) const {
244 for (unsigned i = 0; i != NumNSNumberLiteralMethods; ++i) {
245 NSNumberLiteralMethodKind MK = NSNumberLiteralMethodKind(i);
246 if (isNSNumberLiteralSelector(MK, Sel))
250 return llvm::Optional<NSNumberLiteralMethodKind>();
253 llvm::Optional<NSAPI::NSNumberLiteralMethodKind>
254 NSAPI::getNSNumberFactoryMethodKind(QualType T) {
255 const BuiltinType *BT = T->getAs<BuiltinType>();
257 return llvm::Optional<NSAPI::NSNumberLiteralMethodKind>();
259 switch (BT->getKind()) {
260 case BuiltinType::Char_S:
261 case BuiltinType::SChar:
262 return NSAPI::NSNumberWithChar;
263 case BuiltinType::Char_U:
264 case BuiltinType::UChar:
265 return NSAPI::NSNumberWithUnsignedChar;
266 case BuiltinType::Short:
267 return NSAPI::NSNumberWithShort;
268 case BuiltinType::UShort:
269 return NSAPI::NSNumberWithUnsignedShort;
270 case BuiltinType::Int:
271 return NSAPI::NSNumberWithInt;
272 case BuiltinType::UInt:
273 return NSAPI::NSNumberWithUnsignedInt;
274 case BuiltinType::Long:
275 return NSAPI::NSNumberWithLong;
276 case BuiltinType::ULong:
277 return NSAPI::NSNumberWithUnsignedLong;
278 case BuiltinType::LongLong:
279 return NSAPI::NSNumberWithLongLong;
280 case BuiltinType::ULongLong:
281 return NSAPI::NSNumberWithUnsignedLongLong;
282 case BuiltinType::Float:
283 return NSAPI::NSNumberWithFloat;
284 case BuiltinType::Double:
285 return NSAPI::NSNumberWithDouble;
286 case BuiltinType::Bool:
287 return NSAPI::NSNumberWithBool;
289 case BuiltinType::Void:
290 case BuiltinType::WChar_U:
291 case BuiltinType::WChar_S:
292 case BuiltinType::Char16:
293 case BuiltinType::Char32:
294 case BuiltinType::Int128:
295 case BuiltinType::LongDouble:
296 case BuiltinType::UInt128:
297 case BuiltinType::NullPtr:
298 case BuiltinType::ObjCClass:
299 case BuiltinType::ObjCId:
300 case BuiltinType::ObjCSel:
301 case BuiltinType::BoundMember:
302 case BuiltinType::Dependent:
303 case BuiltinType::Overload:
304 case BuiltinType::UnknownAny:
305 case BuiltinType::ARCUnbridgedCast:
306 case BuiltinType::Half:
307 case BuiltinType::PseudoObject:
311 return llvm::Optional<NSAPI::NSNumberLiteralMethodKind>();