NSAPI.h revision 360784
1238106Sdes//===--- NSAPI.h - NSFoundation APIs ----------------------------*- C++ -*-===// 2238106Sdes// 3238106Sdes// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4238106Sdes// See https://llvm.org/LICENSE.txt for license information. 5238106Sdes// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6238106Sdes// 7238106Sdes//===----------------------------------------------------------------------===// 8238106Sdes 9238106Sdes#ifndef LLVM_CLANG_AST_NSAPI_H 10238106Sdes#define LLVM_CLANG_AST_NSAPI_H 11238106Sdes 12238106Sdes#include "clang/Basic/IdentifierTable.h" 13238106Sdes#include "llvm/ADT/ArrayRef.h" 14238106Sdes#include "llvm/ADT/Optional.h" 15238106Sdes 16238106Sdesnamespace clang { 17238106Sdes class ASTContext; 18238106Sdes class ObjCInterfaceDecl; 19238106Sdes class QualType; 20238106Sdes class Expr; 21238106Sdes 22238106Sdes// Provides info and caches identifiers/selectors for NSFoundation API. 23238106Sdesclass NSAPI { 24266114Sdespublic: 25266114Sdes explicit NSAPI(ASTContext &Ctx); 26266114Sdes 27266114Sdes ASTContext &getASTContext() const { return Ctx; } 28266114Sdes 29266114Sdes enum NSClassIdKindKind { 30266114Sdes ClassId_NSObject, 31266114Sdes ClassId_NSString, 32266114Sdes ClassId_NSArray, 33266114Sdes ClassId_NSMutableArray, 34238106Sdes ClassId_NSDictionary, 35238106Sdes ClassId_NSMutableDictionary, 36238106Sdes ClassId_NSNumber, 37238106Sdes ClassId_NSMutableSet, 38238106Sdes ClassId_NSMutableOrderedSet, 39238106Sdes ClassId_NSValue 40238106Sdes }; 41238106Sdes static const unsigned NumClassIds = 10; 42238106Sdes 43238106Sdes enum NSStringMethodKind { 44238106Sdes NSStr_stringWithString, 45238106Sdes NSStr_stringWithUTF8String, 46238106Sdes NSStr_stringWithCStringEncoding, 47238106Sdes NSStr_stringWithCString, 48238106Sdes NSStr_initWithString, 49238106Sdes NSStr_initWithUTF8String 50238106Sdes }; 51238106Sdes static const unsigned NumNSStringMethods = 6; 52238106Sdes 53238106Sdes IdentifierInfo *getNSClassId(NSClassIdKindKind K) const; 54238106Sdes 55238106Sdes /// The Objective-C NSString selectors. 56255579Sdes Selector getNSStringSelector(NSStringMethodKind MK) const; 57238106Sdes 58238106Sdes /// Returns true if the expression \param E is a reference of 59238106Sdes /// "NSUTF8StringEncoding" enum constant. 60238106Sdes bool isNSUTF8StringEncodingConstant(const Expr *E) const { 61238106Sdes return isObjCEnumerator(E, "NSUTF8StringEncoding", NSUTF8StringEncodingId); 62238106Sdes } 63238106Sdes 64238106Sdes /// Returns true if the expression \param E is a reference of 65238106Sdes /// "NSASCIIStringEncoding" enum constant. 66238106Sdes bool isNSASCIIStringEncodingConstant(const Expr *E) const { 67238106Sdes return isObjCEnumerator(E, "NSASCIIStringEncoding",NSASCIIStringEncodingId); 68238106Sdes } 69238106Sdes 70266114Sdes /// Enumerates the NSArray/NSMutableArray methods used to generate 71266114Sdes /// literals and to apply some checks. 72238106Sdes enum NSArrayMethodKind { 73238106Sdes NSArr_array, 74238106Sdes NSArr_arrayWithArray, 75266114Sdes NSArr_arrayWithObject, 76238106Sdes NSArr_arrayWithObjects, 77238106Sdes NSArr_arrayWithObjectsCount, 78238106Sdes NSArr_initWithArray, 79238106Sdes NSArr_initWithObjects, 80238106Sdes NSArr_objectAtIndex, 81238106Sdes NSMutableArr_replaceObjectAtIndex, 82238106Sdes NSMutableArr_addObject, 83238106Sdes NSMutableArr_insertObjectAtIndex, 84238106Sdes NSMutableArr_setObjectAtIndexedSubscript 85238106Sdes }; 86238106Sdes static const unsigned NumNSArrayMethods = 12; 87276605Sdes 88276605Sdes /// The Objective-C NSArray selectors. 89276605Sdes Selector getNSArraySelector(NSArrayMethodKind MK) const; 90276605Sdes 91276605Sdes /// Return NSArrayMethodKind if \p Sel is such a selector. 92276605Sdes Optional<NSArrayMethodKind> getNSArrayMethodKind(Selector Sel); 93276605Sdes 94238106Sdes /// Enumerates the NSDictionary/NSMutableDictionary methods used 95238106Sdes /// to generate literals and to apply some checks. 96238106Sdes enum NSDictionaryMethodKind { 97238106Sdes NSDict_dictionary, 98238106Sdes NSDict_dictionaryWithDictionary, 99238106Sdes NSDict_dictionaryWithObjectForKey, 100249141Sdes NSDict_dictionaryWithObjectsForKeys, 101249141Sdes NSDict_dictionaryWithObjectsForKeysCount, 102249141Sdes NSDict_dictionaryWithObjectsAndKeys, 103249141Sdes NSDict_initWithDictionary, 104249141Sdes NSDict_initWithObjectsAndKeys, 105255579Sdes NSDict_initWithObjectsForKeys, 106238106Sdes NSDict_objectForKey, 107238106Sdes NSMutableDict_setObjectForKey, 108255579Sdes NSMutableDict_setObjectForKeyedSubscript, 109238106Sdes NSMutableDict_setValueForKey 110238106Sdes }; 111238106Sdes static const unsigned NumNSDictionaryMethods = 13; 112238106Sdes 113238106Sdes /// The Objective-C NSDictionary selectors. 114238106Sdes Selector getNSDictionarySelector(NSDictionaryMethodKind MK) const; 115238106Sdes 116238106Sdes /// Return NSDictionaryMethodKind if \p Sel is such a selector. 117238106Sdes Optional<NSDictionaryMethodKind> getNSDictionaryMethodKind(Selector Sel); 118238106Sdes 119238106Sdes /// Enumerates the NSMutableSet/NSOrderedSet methods used 120238106Sdes /// to apply some checks. 121238106Sdes enum NSSetMethodKind { 122238106Sdes NSMutableSet_addObject, 123238106Sdes NSOrderedSet_insertObjectAtIndex, 124238106Sdes NSOrderedSet_setObjectAtIndex, 125238106Sdes NSOrderedSet_setObjectAtIndexedSubscript, 126238106Sdes NSOrderedSet_replaceObjectAtIndexWithObject 127238106Sdes }; 128238106Sdes static const unsigned NumNSSetMethods = 5; 129238106Sdes 130238106Sdes /// The Objective-C NSSet selectors. 131238106Sdes Selector getNSSetSelector(NSSetMethodKind MK) const; 132238106Sdes 133238106Sdes /// Return NSSetMethodKind if \p Sel is such a selector. 134238106Sdes Optional<NSSetMethodKind> getNSSetMethodKind(Selector Sel); 135238106Sdes 136238106Sdes /// Returns selector for "objectForKeyedSubscript:". 137238106Sdes Selector getObjectForKeyedSubscriptSelector() const { 138238106Sdes return getOrInitSelector(StringRef("objectForKeyedSubscript"), 139238106Sdes objectForKeyedSubscriptSel); 140238106Sdes } 141238106Sdes 142238106Sdes /// Returns selector for "objectAtIndexedSubscript:". 143238106Sdes Selector getObjectAtIndexedSubscriptSelector() const { 144238106Sdes return getOrInitSelector(StringRef("objectAtIndexedSubscript"), 145238106Sdes objectAtIndexedSubscriptSel); 146238106Sdes } 147238106Sdes 148238106Sdes /// Returns selector for "setObject:forKeyedSubscript". 149238106Sdes Selector getSetObjectForKeyedSubscriptSelector() const { 150238106Sdes StringRef Ids[] = { "setObject", "forKeyedSubscript" }; 151238106Sdes return getOrInitSelector(Ids, setObjectForKeyedSubscriptSel); 152238106Sdes } 153238106Sdes 154238106Sdes /// Returns selector for "setObject:atIndexedSubscript". 155238106Sdes Selector getSetObjectAtIndexedSubscriptSelector() const { 156238106Sdes StringRef Ids[] = { "setObject", "atIndexedSubscript" }; 157238106Sdes return getOrInitSelector(Ids, setObjectAtIndexedSubscriptSel); 158238106Sdes } 159238106Sdes 160238106Sdes /// Returns selector for "isEqual:". 161238106Sdes Selector getIsEqualSelector() const { 162238106Sdes return getOrInitSelector(StringRef("isEqual"), isEqualSel); 163238106Sdes } 164238106Sdes 165238106Sdes Selector getNewSelector() const { 166238106Sdes return getOrInitNullarySelector("new", NewSel); 167238106Sdes } 168238106Sdes 169238106Sdes Selector getInitSelector() const { 170238106Sdes return getOrInitNullarySelector("init", InitSel); 171238106Sdes } 172238106Sdes 173238106Sdes /// Enumerates the NSNumber methods used to generate literals. 174238106Sdes enum NSNumberLiteralMethodKind { 175238106Sdes NSNumberWithChar, 176238106Sdes NSNumberWithUnsignedChar, 177266114Sdes NSNumberWithShort, 178266114Sdes NSNumberWithUnsignedShort, 179249141Sdes NSNumberWithInt, 180249141Sdes NSNumberWithUnsignedInt, 181249141Sdes NSNumberWithLong, 182249141Sdes NSNumberWithUnsignedLong, 183292206Sdes NSNumberWithLongLong, 184292206Sdes NSNumberWithUnsignedLongLong, 185249141Sdes NSNumberWithFloat, 186249141Sdes NSNumberWithDouble, 187238106Sdes NSNumberWithBool, 188238106Sdes NSNumberWithInteger, 189238106Sdes NSNumberWithUnsignedInteger 190238106Sdes }; 191238106Sdes static const unsigned NumNSNumberLiteralMethods = 15; 192238106Sdes 193238106Sdes /// The Objective-C NSNumber selectors used to create NSNumber literals. 194238106Sdes /// \param Instance if true it will return the selector for the init* method 195238106Sdes /// otherwise it will return the selector for the number* method. 196238106Sdes Selector getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK, 197238106Sdes bool Instance) const; 198238106Sdes 199238106Sdes bool isNSNumberLiteralSelector(NSNumberLiteralMethodKind MK, 200238106Sdes Selector Sel) const { 201238106Sdes return Sel == getNSNumberLiteralSelector(MK, false) || 202238106Sdes Sel == getNSNumberLiteralSelector(MK, true); 203238106Sdes } 204238106Sdes 205238106Sdes /// Return NSNumberLiteralMethodKind if \p Sel is such a selector. 206238106Sdes Optional<NSNumberLiteralMethodKind> 207266114Sdes getNSNumberLiteralMethodKind(Selector Sel) const; 208238106Sdes 209238106Sdes /// Determine the appropriate NSNumber factory method kind for a 210238106Sdes /// literal of the given type. 211238106Sdes Optional<NSNumberLiteralMethodKind> 212238106Sdes getNSNumberFactoryMethodKind(QualType T) const; 213238106Sdes 214238106Sdes /// Returns true if \param T is a typedef of "BOOL" in objective-c. 215238106Sdes bool isObjCBOOLType(QualType T) const; 216238106Sdes /// Returns true if \param T is a typedef of "NSInteger" in objective-c. 217238106Sdes bool isObjCNSIntegerType(QualType T) const; 218238106Sdes /// Returns true if \param T is a typedef of "NSUInteger" in objective-c. 219238106Sdes bool isObjCNSUIntegerType(QualType T) const; 220238106Sdes /// Returns one of NSIntegral typedef names if \param T is a typedef 221238106Sdes /// of that name in objective-c. 222238106Sdes StringRef GetNSIntegralKind(QualType T) const; 223238106Sdes 224238106Sdes /// Returns \c true if \p Id is currently defined as a macro. 225238106Sdes bool isMacroDefined(StringRef Id) const; 226238106Sdes 227238106Sdes /// Returns \c true if \p InterfaceDecl is subclass of \p NSClassKind 228238106Sdes bool isSubclassOfNSClass(ObjCInterfaceDecl *InterfaceDecl, 229238106Sdes NSClassIdKindKind NSClassKind) const; 230238106Sdes 231238106Sdesprivate: 232238106Sdes bool isObjCTypedef(QualType T, StringRef name, IdentifierInfo *&II) const; 233238106Sdes bool isObjCEnumerator(const Expr *E, 234238106Sdes StringRef name, IdentifierInfo *&II) const; 235238106Sdes Selector getOrInitSelector(ArrayRef<StringRef> Ids, Selector &Sel) const; 236238106Sdes Selector getOrInitNullarySelector(StringRef Id, Selector &Sel) const; 237238106Sdes 238238106Sdes ASTContext &Ctx; 239238106Sdes 240238106Sdes mutable IdentifierInfo *ClassIds[NumClassIds]; 241238106Sdes 242238106Sdes mutable Selector NSStringSelectors[NumNSStringMethods]; 243238106Sdes 244238106Sdes /// The selectors for Objective-C NSArray methods. 245238106Sdes mutable Selector NSArraySelectors[NumNSArrayMethods]; 246238106Sdes 247238106Sdes /// The selectors for Objective-C NSDictionary methods. 248238106Sdes mutable Selector NSDictionarySelectors[NumNSDictionaryMethods]; 249238106Sdes 250238106Sdes /// The selectors for Objective-C NSSet methods. 251238106Sdes mutable Selector NSSetSelectors[NumNSSetMethods]; 252238106Sdes 253238106Sdes /// The Objective-C NSNumber selectors used to create NSNumber literals. 254238106Sdes mutable Selector NSNumberClassSelectors[NumNSNumberLiteralMethods]; 255238106Sdes mutable Selector NSNumberInstanceSelectors[NumNSNumberLiteralMethods]; 256238106Sdes 257238106Sdes mutable Selector objectForKeyedSubscriptSel, objectAtIndexedSubscriptSel, 258238106Sdes setObjectForKeyedSubscriptSel,setObjectAtIndexedSubscriptSel, 259238106Sdes isEqualSel, InitSel, NewSel; 260238106Sdes 261238106Sdes mutable IdentifierInfo *BOOLId, *NSIntegerId, *NSUIntegerId; 262238106Sdes mutable IdentifierInfo *NSASCIIStringEncodingId, *NSUTF8StringEncodingId; 263238106Sdes}; 264238106Sdes 265238106Sdes} // end namespace clang 266238106Sdes 267238106Sdes#endif // LLVM_CLANG_AST_NSAPI_H 268238106Sdes