clang  6.0.0svn
NSAPI.cpp
Go to the documentation of this file.
1 //===--- NSAPI.cpp - NSFoundation APIs ------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/AST/NSAPI.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/DeclObjC.h"
13 #include "clang/AST/Expr.h"
14 #include "llvm/ADT/StringSwitch.h"
15 
16 using namespace clang;
17 
19  : Ctx(ctx), ClassIds(), BOOLId(nullptr), NSIntegerId(nullptr),
20  NSUIntegerId(nullptr), NSASCIIStringEncodingId(nullptr),
21  NSUTF8StringEncodingId(nullptr) {}
22 
24  static const char *ClassName[NumClassIds] = {
25  "NSObject",
26  "NSString",
27  "NSArray",
28  "NSMutableArray",
29  "NSDictionary",
30  "NSMutableDictionary",
31  "NSNumber",
32  "NSMutableSet",
33  "NSMutableOrderedSet",
34  "NSValue"
35  };
36 
37  if (!ClassIds[K])
38  return (ClassIds[K] = &Ctx.Idents.get(ClassName[K]));
39 
40  return ClassIds[K];
41 }
42 
44  if (NSStringSelectors[MK].isNull()) {
45  Selector Sel;
46  switch (MK) {
48  Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("stringWithString"));
49  break;
51  Sel = Ctx.Selectors.getUnarySelector(
52  &Ctx.Idents.get("stringWithUTF8String"));
53  break;
55  Sel = Ctx.Selectors.getUnarySelector(
56  &Ctx.Idents.get("initWithUTF8String"));
57  break;
59  IdentifierInfo *KeyIdents[] = {
60  &Ctx.Idents.get("stringWithCString"),
61  &Ctx.Idents.get("encoding")
62  };
63  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
64  break;
65  }
67  Sel= Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("stringWithCString"));
68  break;
70  Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("initWithString"));
71  break;
72  }
73  return (NSStringSelectors[MK] = Sel);
74  }
75 
76  return NSStringSelectors[MK];
77 }
78 
81  for (unsigned i = 0; i != NumNSStringMethods; ++i) {
83  if (Sel == getNSStringSelector(MK))
84  return MK;
85  }
86 
87  return None;
88 }
89 
91  if (NSArraySelectors[MK].isNull()) {
92  Selector Sel;
93  switch (MK) {
94  case NSArr_array:
95  Sel = Ctx.Selectors.getNullarySelector(&Ctx.Idents.get("array"));
96  break;
98  Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("arrayWithArray"));
99  break;
101  Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("arrayWithObject"));
102  break;
104  Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("arrayWithObjects"));
105  break;
107  IdentifierInfo *KeyIdents[] = {
108  &Ctx.Idents.get("arrayWithObjects"),
109  &Ctx.Idents.get("count")
110  };
111  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
112  break;
113  }
114  case NSArr_initWithArray:
115  Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("initWithArray"));
116  break;
118  Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("initWithObjects"));
119  break;
120  case NSArr_objectAtIndex:
121  Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("objectAtIndex"));
122  break;
124  IdentifierInfo *KeyIdents[] = {
125  &Ctx.Idents.get("replaceObjectAtIndex"),
126  &Ctx.Idents.get("withObject")
127  };
128  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
129  break;
130  }
132  Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("addObject"));
133  break;
135  IdentifierInfo *KeyIdents[] = {
136  &Ctx.Idents.get("insertObject"),
137  &Ctx.Idents.get("atIndex")
138  };
139  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
140  break;
141  }
143  IdentifierInfo *KeyIdents[] = {
144  &Ctx.Idents.get("setObject"),
145  &Ctx.Idents.get("atIndexedSubscript")
146  };
147  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
148  break;
149  }
150  }
151  return (NSArraySelectors[MK] = Sel);
152  }
153 
154  return NSArraySelectors[MK];
155 }
156 
158  for (unsigned i = 0; i != NumNSArrayMethods; ++i) {
160  if (Sel == getNSArraySelector(MK))
161  return MK;
162  }
163 
164  return None;
165 }
166 
168  NSDictionaryMethodKind MK) const {
169  if (NSDictionarySelectors[MK].isNull()) {
170  Selector Sel;
171  switch (MK) {
172  case NSDict_dictionary:
173  Sel = Ctx.Selectors.getNullarySelector(&Ctx.Idents.get("dictionary"));
174  break;
176  Sel = Ctx.Selectors.getUnarySelector(
177  &Ctx.Idents.get("dictionaryWithDictionary"));
178  break;
180  IdentifierInfo *KeyIdents[] = {
181  &Ctx.Idents.get("dictionaryWithObject"),
182  &Ctx.Idents.get("forKey")
183  };
184  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
185  break;
186  }
188  IdentifierInfo *KeyIdents[] = {
189  &Ctx.Idents.get("dictionaryWithObjects"),
190  &Ctx.Idents.get("forKeys")
191  };
192  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
193  break;
194  }
196  IdentifierInfo *KeyIdents[] = {
197  &Ctx.Idents.get("dictionaryWithObjects"),
198  &Ctx.Idents.get("forKeys"),
199  &Ctx.Idents.get("count")
200  };
201  Sel = Ctx.Selectors.getSelector(3, KeyIdents);
202  break;
203  }
205  Sel = Ctx.Selectors.getUnarySelector(
206  &Ctx.Idents.get("dictionaryWithObjectsAndKeys"));
207  break;
209  Sel = Ctx.Selectors.getUnarySelector(
210  &Ctx.Idents.get("initWithDictionary"));
211  break;
213  Sel = Ctx.Selectors.getUnarySelector(
214  &Ctx.Idents.get("initWithObjectsAndKeys"));
215  break;
217  IdentifierInfo *KeyIdents[] = {
218  &Ctx.Idents.get("initWithObjects"),
219  &Ctx.Idents.get("forKeys")
220  };
221  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
222  break;
223  }
224  case NSDict_objectForKey:
225  Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("objectForKey"));
226  break;
228  IdentifierInfo *KeyIdents[] = {
229  &Ctx.Idents.get("setObject"),
230  &Ctx.Idents.get("forKey")
231  };
232  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
233  break;
234  }
236  IdentifierInfo *KeyIdents[] = {
237  &Ctx.Idents.get("setObject"),
238  &Ctx.Idents.get("forKeyedSubscript")
239  };
240  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
241  break;
242  }
244  IdentifierInfo *KeyIdents[] = {
245  &Ctx.Idents.get("setValue"),
246  &Ctx.Idents.get("forKey")
247  };
248  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
249  break;
250  }
251  }
252  return (NSDictionarySelectors[MK] = Sel);
253  }
254 
255  return NSDictionarySelectors[MK];
256 }
257 
260  for (unsigned i = 0; i != NumNSDictionaryMethods; ++i) {
262  if (Sel == getNSDictionarySelector(MK))
263  return MK;
264  }
265 
266  return None;
267 }
268 
270  if (NSSetSelectors[MK].isNull()) {
271  Selector Sel;
272  switch (MK) {
274  Sel = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get("addObject"));
275  break;
277  IdentifierInfo *KeyIdents[] = {
278  &Ctx.Idents.get("insertObject"),
279  &Ctx.Idents.get("atIndex")
280  };
281  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
282  break;
283  }
285  IdentifierInfo *KeyIdents[] = {
286  &Ctx.Idents.get("setObject"),
287  &Ctx.Idents.get("atIndex")
288  };
289  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
290  break;
291  }
293  IdentifierInfo *KeyIdents[] = {
294  &Ctx.Idents.get("setObject"),
295  &Ctx.Idents.get("atIndexedSubscript")
296  };
297  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
298  break;
299  }
301  IdentifierInfo *KeyIdents[] = {
302  &Ctx.Idents.get("replaceObjectAtIndex"),
303  &Ctx.Idents.get("withObject")
304  };
305  Sel = Ctx.Selectors.getSelector(2, KeyIdents);
306  break;
307  }
308  }
309  return (NSSetSelectors[MK] = Sel);
310  }
311 
312  return NSSetSelectors[MK];
313 }
314 
317  for (unsigned i = 0; i != NumNSSetMethods; ++i) {
319  if (Sel == getNSSetSelector(MK))
320  return MK;
321  }
322 
323  return None;
324 }
325 
327  bool Instance) const {
328  static const char *ClassSelectorName[NumNSNumberLiteralMethods] = {
329  "numberWithChar",
330  "numberWithUnsignedChar",
331  "numberWithShort",
332  "numberWithUnsignedShort",
333  "numberWithInt",
334  "numberWithUnsignedInt",
335  "numberWithLong",
336  "numberWithUnsignedLong",
337  "numberWithLongLong",
338  "numberWithUnsignedLongLong",
339  "numberWithFloat",
340  "numberWithDouble",
341  "numberWithBool",
342  "numberWithInteger",
343  "numberWithUnsignedInteger"
344  };
345  static const char *InstanceSelectorName[NumNSNumberLiteralMethods] = {
346  "initWithChar",
347  "initWithUnsignedChar",
348  "initWithShort",
349  "initWithUnsignedShort",
350  "initWithInt",
351  "initWithUnsignedInt",
352  "initWithLong",
353  "initWithUnsignedLong",
354  "initWithLongLong",
355  "initWithUnsignedLongLong",
356  "initWithFloat",
357  "initWithDouble",
358  "initWithBool",
359  "initWithInteger",
360  "initWithUnsignedInteger"
361  };
362 
363  Selector *Sels;
364  const char **Names;
365  if (Instance) {
366  Sels = NSNumberInstanceSelectors;
367  Names = InstanceSelectorName;
368  } else {
369  Sels = NSNumberClassSelectors;
370  Names = ClassSelectorName;
371  }
372 
373  if (Sels[MK].isNull())
374  Sels[MK] = Ctx.Selectors.getUnarySelector(&Ctx.Idents.get(Names[MK]));
375  return Sels[MK];
376 }
377 
380  for (unsigned i = 0; i != NumNSNumberLiteralMethods; ++i) {
382  if (isNSNumberLiteralSelector(MK, Sel))
383  return MK;
384  }
385 
386  return None;
387 }
388 
391  const BuiltinType *BT = T->getAs<BuiltinType>();
392  if (!BT)
393  return None;
394 
395  const TypedefType *TDT = T->getAs<TypedefType>();
396  if (TDT) {
397  QualType TDTTy = QualType(TDT, 0);
398  if (isObjCBOOLType(TDTTy))
400  if (isObjCNSIntegerType(TDTTy))
402  if (isObjCNSUIntegerType(TDTTy))
404  }
405 
406  switch (BT->getKind()) {
407  case BuiltinType::Char_S:
408  case BuiltinType::SChar:
410  case BuiltinType::Char_U:
411  case BuiltinType::UChar:
413  case BuiltinType::Short:
415  case BuiltinType::UShort:
417  case BuiltinType::Int:
418  return NSAPI::NSNumberWithInt;
419  case BuiltinType::UInt:
421  case BuiltinType::Long:
423  case BuiltinType::ULong:
425  case BuiltinType::LongLong:
427  case BuiltinType::ULongLong:
429  case BuiltinType::Float:
431  case BuiltinType::Double:
433  case BuiltinType::Bool:
435 
436  case BuiltinType::Void:
437  case BuiltinType::WChar_U:
438  case BuiltinType::WChar_S:
439  case BuiltinType::Char16:
440  case BuiltinType::Char32:
441  case BuiltinType::Int128:
442  case BuiltinType::LongDouble:
443  case BuiltinType::UInt128:
444  case BuiltinType::Float16:
445  case BuiltinType::Float128:
446  case BuiltinType::NullPtr:
447  case BuiltinType::ObjCClass:
448  case BuiltinType::ObjCId:
449  case BuiltinType::ObjCSel:
450 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
451  case BuiltinType::Id:
452 #include "clang/Basic/OpenCLImageTypes.def"
453  case BuiltinType::OCLSampler:
454  case BuiltinType::OCLEvent:
455  case BuiltinType::OCLClkEvent:
456  case BuiltinType::OCLQueue:
457  case BuiltinType::OCLReserveID:
458  case BuiltinType::BoundMember:
459  case BuiltinType::Dependent:
460  case BuiltinType::Overload:
461  case BuiltinType::UnknownAny:
462  case BuiltinType::ARCUnbridgedCast:
463  case BuiltinType::Half:
464  case BuiltinType::PseudoObject:
465  case BuiltinType::BuiltinFn:
466  case BuiltinType::OMPArraySection:
467  break;
468  }
469 
470  return None;
471 }
472 
473 /// \brief Returns true if \param T is a typedef of "BOOL" in objective-c.
475  return isObjCTypedef(T, "BOOL", BOOLId);
476 }
477 /// \brief Returns true if \param T is a typedef of "NSInteger" in objective-c.
479  return isObjCTypedef(T, "NSInteger", NSIntegerId);
480 }
481 /// \brief Returns true if \param T is a typedef of "NSUInteger" in objective-c.
483  return isObjCTypedef(T, "NSUInteger", NSUIntegerId);
484 }
485 
487  if (!Ctx.getLangOpts().ObjC1 || T.isNull())
488  return StringRef();
489 
490  while (const TypedefType *TDT = T->getAs<TypedefType>()) {
491  StringRef NSIntegralResust =
492  llvm::StringSwitch<StringRef>(
493  TDT->getDecl()->getDeclName().getAsIdentifierInfo()->getName())
494  .Case("int8_t", "int8_t")
495  .Case("int16_t", "int16_t")
496  .Case("int32_t", "int32_t")
497  .Case("NSInteger", "NSInteger")
498  .Case("int64_t", "int64_t")
499  .Case("uint8_t", "uint8_t")
500  .Case("uint16_t", "uint16_t")
501  .Case("uint32_t", "uint32_t")
502  .Case("NSUInteger", "NSUInteger")
503  .Case("uint64_t", "uint64_t")
504  .Default(StringRef());
505  if (!NSIntegralResust.empty())
506  return NSIntegralResust;
507  T = TDT->desugar();
508  }
509  return StringRef();
510 }
511 
512 bool NSAPI::isMacroDefined(StringRef Id) const {
513  // FIXME: Check whether the relevant module macros are visible.
514  return Ctx.Idents.get(Id).hasMacroDefinition();
515 }
516 
518  NSClassIdKindKind NSClassKind) const {
519  if (!InterfaceDecl) {
520  return false;
521  }
522 
523  IdentifierInfo *NSClassID = getNSClassId(NSClassKind);
524 
525  bool IsSubclass = false;
526  do {
527  IsSubclass = NSClassID == InterfaceDecl->getIdentifier();
528 
529  if (IsSubclass) {
530  break;
531  }
532  } while ((InterfaceDecl = InterfaceDecl->getSuperClass()));
533 
534  return IsSubclass;
535 }
536 
537 bool NSAPI::isObjCTypedef(QualType T,
538  StringRef name, IdentifierInfo *&II) const {
539  if (!Ctx.getLangOpts().ObjC1)
540  return false;
541  if (T.isNull())
542  return false;
543 
544  if (!II)
545  II = &Ctx.Idents.get(name);
546 
547  while (const TypedefType *TDT = T->getAs<TypedefType>()) {
548  if (TDT->getDecl()->getDeclName().getAsIdentifierInfo() == II)
549  return true;
550  T = TDT->desugar();
551  }
552 
553  return false;
554 }
555 
556 bool NSAPI::isObjCEnumerator(const Expr *E,
557  StringRef name, IdentifierInfo *&II) const {
558  if (!Ctx.getLangOpts().ObjC1)
559  return false;
560  if (!E)
561  return false;
562 
563  if (!II)
564  II = &Ctx.Idents.get(name);
565 
566  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
567  if (const EnumConstantDecl *
568  EnumD = dyn_cast_or_null<EnumConstantDecl>(DRE->getDecl()))
569  return EnumD->getIdentifier() == II;
570 
571  return false;
572 }
573 
574 Selector NSAPI::getOrInitSelector(ArrayRef<StringRef> Ids,
575  Selector &Sel) const {
576  if (Sel.isNull()) {
579  I = Ids.begin(), E = Ids.end(); I != E; ++I)
580  Idents.push_back(&Ctx.Idents.get(*I));
581  Sel = Ctx.Selectors.getSelector(Idents.size(), Idents.data());
582  }
583  return Sel;
584 }
Optional< NSNumberLiteralMethodKind > getNSNumberFactoryMethodKind(QualType T) const
Determine the appropriate NSNumber factory method kind for a literal of the given type...
Definition: NSAPI.cpp:390
Defines the clang::ASTContext interface.
Smart pointer class that efficiently represents Objective-C method names.
A (possibly-)qualified type.
Definition: Type.h:614
Kind getKind() const
Definition: Type.h:2103
EnumConstantDecl - An instance of this object exists for each enum constant that is defined...
Definition: Decl.h:2584
Optional< NSDictionaryMethodKind > getNSDictionaryMethodKind(Selector Sel)
Return NSDictionaryMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:259
NSNumberLiteralMethodKind
Enumerates the NSNumber methods used to generate literals.
Definition: NSAPI.h:170
NSDictionaryMethodKind
Enumerates the NSDictionary/NSMutableDictionary methods used to generate literals and to apply some c...
Definition: NSAPI.h:100
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6099
IdentifierInfo * getIdentifier() const
getIdentifier - Get the identifier that names this declaration, if there is one.
Definition: Decl.h:232
Selector getUnarySelector(IdentifierInfo *ID)
One of these records is kept for each identifier that is lexed.
Optional< NSSetMethodKind > getNSSetMethodKind(Selector Sel)
Return NSSetMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:316
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:128
Selector getNSStringSelector(NSStringMethodKind MK) const
The Objective-C NSString selectors.
Definition: NSAPI.cpp:43
Selector getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK, bool Instance) const
The Objective-C NSNumber selectors used to create NSNumber literals.
Definition: NSAPI.cpp:326
IdentifierTable & Idents
Definition: ASTContext.h:515
Optional< NSNumberLiteralMethodKind > getNSNumberLiteralMethodKind(Selector Sel) const
Return NSNumberLiteralMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:379
Selector getNullarySelector(IdentifierInfo *ID)
static const unsigned NumClassIds
Definition: NSAPI.h:42
static const unsigned NumNSNumberLiteralMethods
Definition: NSAPI.h:187
NSSetMethodKind
Enumerates the NSMutableSet/NSOrderedSet methods used to apply some checks.
Definition: NSAPI.h:125
NSAPI(ASTContext &Ctx)
Definition: NSAPI.cpp:18
Represents an ObjC class declaration.
Definition: DeclObjC.h:1108
static const unsigned NumNSSetMethods
Definition: NSAPI.h:132
NSClassIdKindKind
Definition: NSAPI.h:30
NSArrayMethodKind
Enumerates the NSArray/NSMutableArray methods used to generate literals and to apply some checks...
Definition: NSAPI.h:76
Expr - This represents one expression.
Definition: Expr.h:106
Selector getNSArraySelector(NSArrayMethodKind MK) const
The Objective-C NSArray selectors.
Definition: NSAPI.cpp:90
int Id
Definition: ASTDiff.cpp:191
const FunctionProtoType * T
ObjCInterfaceDecl * getSuperClass() const
Definition: DeclObjC.cpp:314
StringRef GetNSIntegralKind(QualType T) const
Returns one of NSIntegral typedef names if.
Definition: NSAPI.cpp:486
bool isNull() const
Return true if this QualType doesn&#39;t point to a type yet.
Definition: Type.h:681
SelectorTable & Selectors
Definition: ASTContext.h:516
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
bool isObjCBOOLType(QualType T) const
Returns true if.
Definition: NSAPI.cpp:474
bool isObjCNSUIntegerType(QualType T) const
Returns true if.
Definition: NSAPI.cpp:482
Selector getNSSetSelector(NSSetMethodKind MK) const
The Objective-C NSSet selectors.
Definition: NSAPI.cpp:269
bool isObjCNSIntegerType(QualType T) const
Returns true if.
Definition: NSAPI.cpp:478
bool isSubclassOfNSClass(ObjCInterfaceDecl *InterfaceDecl, NSClassIdKindKind NSClassKind) const
Returns true if InterfaceDecl is subclass of NSClassKind.
Definition: NSAPI.cpp:517
NSStringMethodKind
Definition: NSAPI.h:44
IdentifierInfo * getNSClassId(NSClassIdKindKind K) const
Definition: NSAPI.cpp:23
Optional< NSStringMethodKind > getNSStringMethodKind(Selector Sel) const
Return NSStringMethodKind if.
Definition: NSAPI.cpp:80
Dataflow Directional Tag Classes.
bool isMacroDefined(StringRef Id) const
Returns true if Id is currently defined as a macro.
Definition: NSAPI.cpp:512
bool isNSNumberLiteralSelector(NSNumberLiteralMethodKind MK, Selector Sel) const
Definition: NSAPI.h:195
Expr * IgnoreParenImpCasts() LLVM_READONLY
IgnoreParenImpCasts - Ignore parentheses and implicit casts.
Definition: Expr.cpp:2549
Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV)
Can create any sort of selector.
static const unsigned NumNSArrayMethods
Definition: NSAPI.h:90
Selector getNSDictionarySelector(NSDictionaryMethodKind MK) const
The Objective-C NSDictionary selectors.
Definition: NSAPI.cpp:167
static const unsigned NumNSDictionaryMethods
Definition: NSAPI.h:115
bool isNull() const
Determine whether this is the empty selector.
This class is used for builtin types like &#39;int&#39;.
Definition: Type.h:2082
Optional< NSArrayMethodKind > getNSArrayMethodKind(Selector Sel)
Return NSArrayMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:157
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:956
bool hasMacroDefinition() const
Return true if this identifier is #defined to some other value.
const LangOptions & getLangOpts() const
Definition: ASTContext.h:661
static const unsigned NumNSStringMethods
Definition: NSAPI.h:52