NSAPI.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- NSAPI.h - NSFoundation APIs ----------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_AST_NSAPI_H
  14. #define LLVM_CLANG_AST_NSAPI_H
  15. #include "clang/Basic/IdentifierTable.h"
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/Optional.h"
  18. namespace clang {
  19. class ASTContext;
  20. class ObjCInterfaceDecl;
  21. class QualType;
  22. class Expr;
  23. // Provides info and caches identifiers/selectors for NSFoundation API.
  24. class NSAPI {
  25. public:
  26. explicit NSAPI(ASTContext &Ctx);
  27. ASTContext &getASTContext() const { return Ctx; }
  28. enum NSClassIdKindKind {
  29. ClassId_NSObject,
  30. ClassId_NSString,
  31. ClassId_NSArray,
  32. ClassId_NSMutableArray,
  33. ClassId_NSDictionary,
  34. ClassId_NSMutableDictionary,
  35. ClassId_NSNumber,
  36. ClassId_NSMutableSet,
  37. ClassId_NSMutableOrderedSet,
  38. ClassId_NSValue
  39. };
  40. static const unsigned NumClassIds = 10;
  41. enum NSStringMethodKind {
  42. NSStr_stringWithString,
  43. NSStr_stringWithUTF8String,
  44. NSStr_stringWithCStringEncoding,
  45. NSStr_stringWithCString,
  46. NSStr_initWithString,
  47. NSStr_initWithUTF8String
  48. };
  49. static const unsigned NumNSStringMethods = 6;
  50. IdentifierInfo *getNSClassId(NSClassIdKindKind K) const;
  51. /// The Objective-C NSString selectors.
  52. Selector getNSStringSelector(NSStringMethodKind MK) const;
  53. /// Returns true if the expression \param E is a reference of
  54. /// "NSUTF8StringEncoding" enum constant.
  55. bool isNSUTF8StringEncodingConstant(const Expr *E) const {
  56. return isObjCEnumerator(E, "NSUTF8StringEncoding", NSUTF8StringEncodingId);
  57. }
  58. /// Returns true if the expression \param E is a reference of
  59. /// "NSASCIIStringEncoding" enum constant.
  60. bool isNSASCIIStringEncodingConstant(const Expr *E) const {
  61. return isObjCEnumerator(E, "NSASCIIStringEncoding",NSASCIIStringEncodingId);
  62. }
  63. /// Enumerates the NSArray/NSMutableArray methods used to generate
  64. /// literals and to apply some checks.
  65. enum NSArrayMethodKind {
  66. NSArr_array,
  67. NSArr_arrayWithArray,
  68. NSArr_arrayWithObject,
  69. NSArr_arrayWithObjects,
  70. NSArr_arrayWithObjectsCount,
  71. NSArr_initWithArray,
  72. NSArr_initWithObjects,
  73. NSArr_objectAtIndex,
  74. NSMutableArr_replaceObjectAtIndex,
  75. NSMutableArr_addObject,
  76. NSMutableArr_insertObjectAtIndex,
  77. NSMutableArr_setObjectAtIndexedSubscript
  78. };
  79. static const unsigned NumNSArrayMethods = 12;
  80. /// The Objective-C NSArray selectors.
  81. Selector getNSArraySelector(NSArrayMethodKind MK) const;
  82. /// Return NSArrayMethodKind if \p Sel is such a selector.
  83. Optional<NSArrayMethodKind> getNSArrayMethodKind(Selector Sel);
  84. /// Enumerates the NSDictionary/NSMutableDictionary methods used
  85. /// to generate literals and to apply some checks.
  86. enum NSDictionaryMethodKind {
  87. NSDict_dictionary,
  88. NSDict_dictionaryWithDictionary,
  89. NSDict_dictionaryWithObjectForKey,
  90. NSDict_dictionaryWithObjectsForKeys,
  91. NSDict_dictionaryWithObjectsForKeysCount,
  92. NSDict_dictionaryWithObjectsAndKeys,
  93. NSDict_initWithDictionary,
  94. NSDict_initWithObjectsAndKeys,
  95. NSDict_initWithObjectsForKeys,
  96. NSDict_objectForKey,
  97. NSMutableDict_setObjectForKey,
  98. NSMutableDict_setObjectForKeyedSubscript,
  99. NSMutableDict_setValueForKey
  100. };
  101. static const unsigned NumNSDictionaryMethods = 13;
  102. /// The Objective-C NSDictionary selectors.
  103. Selector getNSDictionarySelector(NSDictionaryMethodKind MK) const;
  104. /// Return NSDictionaryMethodKind if \p Sel is such a selector.
  105. Optional<NSDictionaryMethodKind> getNSDictionaryMethodKind(Selector Sel);
  106. /// Enumerates the NSMutableSet/NSOrderedSet methods used
  107. /// to apply some checks.
  108. enum NSSetMethodKind {
  109. NSMutableSet_addObject,
  110. NSOrderedSet_insertObjectAtIndex,
  111. NSOrderedSet_setObjectAtIndex,
  112. NSOrderedSet_setObjectAtIndexedSubscript,
  113. NSOrderedSet_replaceObjectAtIndexWithObject
  114. };
  115. static const unsigned NumNSSetMethods = 5;
  116. /// The Objective-C NSSet selectors.
  117. Selector getNSSetSelector(NSSetMethodKind MK) const;
  118. /// Return NSSetMethodKind if \p Sel is such a selector.
  119. Optional<NSSetMethodKind> getNSSetMethodKind(Selector Sel);
  120. /// Returns selector for "objectForKeyedSubscript:".
  121. Selector getObjectForKeyedSubscriptSelector() const {
  122. return getOrInitSelector(StringRef("objectForKeyedSubscript"),
  123. objectForKeyedSubscriptSel);
  124. }
  125. /// Returns selector for "objectAtIndexedSubscript:".
  126. Selector getObjectAtIndexedSubscriptSelector() const {
  127. return getOrInitSelector(StringRef("objectAtIndexedSubscript"),
  128. objectAtIndexedSubscriptSel);
  129. }
  130. /// Returns selector for "setObject:forKeyedSubscript".
  131. Selector getSetObjectForKeyedSubscriptSelector() const {
  132. StringRef Ids[] = { "setObject", "forKeyedSubscript" };
  133. return getOrInitSelector(Ids, setObjectForKeyedSubscriptSel);
  134. }
  135. /// Returns selector for "setObject:atIndexedSubscript".
  136. Selector getSetObjectAtIndexedSubscriptSelector() const {
  137. StringRef Ids[] = { "setObject", "atIndexedSubscript" };
  138. return getOrInitSelector(Ids, setObjectAtIndexedSubscriptSel);
  139. }
  140. /// Returns selector for "isEqual:".
  141. Selector getIsEqualSelector() const {
  142. return getOrInitSelector(StringRef("isEqual"), isEqualSel);
  143. }
  144. Selector getNewSelector() const {
  145. return getOrInitNullarySelector("new", NewSel);
  146. }
  147. Selector getInitSelector() const {
  148. return getOrInitNullarySelector("init", InitSel);
  149. }
  150. /// Enumerates the NSNumber methods used to generate literals.
  151. enum NSNumberLiteralMethodKind {
  152. NSNumberWithChar,
  153. NSNumberWithUnsignedChar,
  154. NSNumberWithShort,
  155. NSNumberWithUnsignedShort,
  156. NSNumberWithInt,
  157. NSNumberWithUnsignedInt,
  158. NSNumberWithLong,
  159. NSNumberWithUnsignedLong,
  160. NSNumberWithLongLong,
  161. NSNumberWithUnsignedLongLong,
  162. NSNumberWithFloat,
  163. NSNumberWithDouble,
  164. NSNumberWithBool,
  165. NSNumberWithInteger,
  166. NSNumberWithUnsignedInteger
  167. };
  168. static const unsigned NumNSNumberLiteralMethods = 15;
  169. /// The Objective-C NSNumber selectors used to create NSNumber literals.
  170. /// \param Instance if true it will return the selector for the init* method
  171. /// otherwise it will return the selector for the number* method.
  172. Selector getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
  173. bool Instance) const;
  174. bool isNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
  175. Selector Sel) const {
  176. return Sel == getNSNumberLiteralSelector(MK, false) ||
  177. Sel == getNSNumberLiteralSelector(MK, true);
  178. }
  179. /// Return NSNumberLiteralMethodKind if \p Sel is such a selector.
  180. Optional<NSNumberLiteralMethodKind>
  181. getNSNumberLiteralMethodKind(Selector Sel) const;
  182. /// Determine the appropriate NSNumber factory method kind for a
  183. /// literal of the given type.
  184. Optional<NSNumberLiteralMethodKind>
  185. getNSNumberFactoryMethodKind(QualType T) const;
  186. /// Returns true if \param T is a typedef of "BOOL" in objective-c.
  187. bool isObjCBOOLType(QualType T) const;
  188. /// Returns true if \param T is a typedef of "NSInteger" in objective-c.
  189. bool isObjCNSIntegerType(QualType T) const;
  190. /// Returns true if \param T is a typedef of "NSUInteger" in objective-c.
  191. bool isObjCNSUIntegerType(QualType T) const;
  192. /// Returns one of NSIntegral typedef names if \param T is a typedef
  193. /// of that name in objective-c.
  194. StringRef GetNSIntegralKind(QualType T) const;
  195. /// Returns \c true if \p Id is currently defined as a macro.
  196. bool isMacroDefined(StringRef Id) const;
  197. /// Returns \c true if \p InterfaceDecl is subclass of \p NSClassKind
  198. bool isSubclassOfNSClass(ObjCInterfaceDecl *InterfaceDecl,
  199. NSClassIdKindKind NSClassKind) const;
  200. private:
  201. bool isObjCTypedef(QualType T, StringRef name, IdentifierInfo *&II) const;
  202. bool isObjCEnumerator(const Expr *E,
  203. StringRef name, IdentifierInfo *&II) const;
  204. Selector getOrInitSelector(ArrayRef<StringRef> Ids, Selector &Sel) const;
  205. Selector getOrInitNullarySelector(StringRef Id, Selector &Sel) const;
  206. ASTContext &Ctx;
  207. mutable IdentifierInfo *ClassIds[NumClassIds];
  208. mutable Selector NSStringSelectors[NumNSStringMethods];
  209. /// The selectors for Objective-C NSArray methods.
  210. mutable Selector NSArraySelectors[NumNSArrayMethods];
  211. /// The selectors for Objective-C NSDictionary methods.
  212. mutable Selector NSDictionarySelectors[NumNSDictionaryMethods];
  213. /// The selectors for Objective-C NSSet methods.
  214. mutable Selector NSSetSelectors[NumNSSetMethods];
  215. /// The Objective-C NSNumber selectors used to create NSNumber literals.
  216. mutable Selector NSNumberClassSelectors[NumNSNumberLiteralMethods];
  217. mutable Selector NSNumberInstanceSelectors[NumNSNumberLiteralMethods];
  218. mutable Selector objectForKeyedSubscriptSel, objectAtIndexedSubscriptSel,
  219. setObjectForKeyedSubscriptSel,setObjectAtIndexedSubscriptSel,
  220. isEqualSel, InitSel, NewSel;
  221. mutable IdentifierInfo *BOOLId, *NSIntegerId, *NSUIntegerId;
  222. mutable IdentifierInfo *NSASCIIStringEncodingId, *NSUTF8StringEncodingId;
  223. };
  224. } // end namespace clang
  225. #endif // LLVM_CLANG_AST_NSAPI_H
  226. #ifdef __GNUC__
  227. #pragma GCC diagnostic pop
  228. #endif