clang API Documentation

NSAPI.h
Go to the documentation of this file.
00001 //===--- NSAPI.h - NSFoundation APIs ----------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_CLANG_AST_NSAPI_H
00011 #define LLVM_CLANG_AST_NSAPI_H
00012 
00013 #include "clang/Basic/IdentifierTable.h"
00014 #include "llvm/ADT/ArrayRef.h"
00015 #include "llvm/ADT/Optional.h"
00016 
00017 namespace clang {
00018   class ASTContext;
00019   class QualType;
00020   class Expr;
00021 
00022 // \brief Provides info and caches identifiers/selectors for NSFoundation API.
00023 class NSAPI {
00024 public:
00025   explicit NSAPI(ASTContext &Ctx);
00026 
00027   ASTContext &getASTContext() const { return Ctx; }
00028 
00029   enum NSClassIdKindKind {
00030     ClassId_NSObject,
00031     ClassId_NSString,
00032     ClassId_NSArray,
00033     ClassId_NSMutableArray,
00034     ClassId_NSDictionary,
00035     ClassId_NSMutableDictionary,
00036     ClassId_NSNumber
00037   };
00038   static const unsigned NumClassIds = 7;
00039 
00040   enum NSStringMethodKind {
00041     NSStr_stringWithString,
00042     NSStr_stringWithUTF8String,
00043     NSStr_stringWithCStringEncoding,
00044     NSStr_stringWithCString,
00045     NSStr_initWithString,
00046     NSStr_initWithUTF8String
00047   };
00048   static const unsigned NumNSStringMethods = 5;
00049 
00050   IdentifierInfo *getNSClassId(NSClassIdKindKind K) const;
00051 
00052   /// \brief The Objective-C NSString selectors.
00053   Selector getNSStringSelector(NSStringMethodKind MK) const;
00054 
00055   /// \brief Return NSStringMethodKind if \param Sel is such a selector.
00056   Optional<NSStringMethodKind> getNSStringMethodKind(Selector Sel) const;
00057 
00058   /// \brief Returns true if the expression \param E is a reference of
00059   /// "NSUTF8StringEncoding" enum constant.
00060   bool isNSUTF8StringEncodingConstant(const Expr *E) const {
00061     return isObjCEnumerator(E, "NSUTF8StringEncoding", NSUTF8StringEncodingId);
00062   }
00063 
00064   /// \brief Returns true if the expression \param E is a reference of
00065   /// "NSASCIIStringEncoding" enum constant.
00066   bool isNSASCIIStringEncodingConstant(const Expr *E) const {
00067     return isObjCEnumerator(E, "NSASCIIStringEncoding",NSASCIIStringEncodingId);
00068   }
00069 
00070   /// \brief Enumerates the NSArray methods used to generate literals.
00071   enum NSArrayMethodKind {
00072     NSArr_array,
00073     NSArr_arrayWithArray,
00074     NSArr_arrayWithObject,
00075     NSArr_arrayWithObjects,
00076     NSArr_arrayWithObjectsCount,
00077     NSArr_initWithArray,
00078     NSArr_initWithObjects,
00079     NSArr_objectAtIndex,
00080     NSMutableArr_replaceObjectAtIndex
00081   };
00082   static const unsigned NumNSArrayMethods = 9;
00083 
00084   /// \brief The Objective-C NSArray selectors.
00085   Selector getNSArraySelector(NSArrayMethodKind MK) const;
00086 
00087   /// \brief Return NSArrayMethodKind if \p Sel is such a selector.
00088   Optional<NSArrayMethodKind> getNSArrayMethodKind(Selector Sel);
00089 
00090   /// \brief Enumerates the NSDictionary methods used to generate literals.
00091   enum NSDictionaryMethodKind {
00092     NSDict_dictionary,
00093     NSDict_dictionaryWithDictionary,
00094     NSDict_dictionaryWithObjectForKey,
00095     NSDict_dictionaryWithObjectsForKeys,
00096     NSDict_dictionaryWithObjectsForKeysCount,
00097     NSDict_dictionaryWithObjectsAndKeys,
00098     NSDict_initWithDictionary,
00099     NSDict_initWithObjectsAndKeys,
00100     NSDict_initWithObjectsForKeys,
00101     NSDict_objectForKey,
00102     NSMutableDict_setObjectForKey
00103   };
00104   static const unsigned NumNSDictionaryMethods = 12;
00105   
00106   /// \brief The Objective-C NSDictionary selectors.
00107   Selector getNSDictionarySelector(NSDictionaryMethodKind MK) const;
00108 
00109   /// \brief Return NSDictionaryMethodKind if \p Sel is such a selector.
00110   Optional<NSDictionaryMethodKind> getNSDictionaryMethodKind(Selector Sel);
00111 
00112   /// \brief Returns selector for "objectForKeyedSubscript:".
00113   Selector getObjectForKeyedSubscriptSelector() const {
00114     return getOrInitSelector(StringRef("objectForKeyedSubscript"),
00115                              objectForKeyedSubscriptSel);
00116   }
00117 
00118   /// \brief Returns selector for "objectAtIndexedSubscript:".
00119   Selector getObjectAtIndexedSubscriptSelector() const {
00120     return getOrInitSelector(StringRef("objectAtIndexedSubscript"),
00121                              objectAtIndexedSubscriptSel);
00122   }
00123 
00124   /// \brief Returns selector for "setObject:forKeyedSubscript".
00125   Selector getSetObjectForKeyedSubscriptSelector() const {
00126     StringRef Ids[] = { "setObject", "forKeyedSubscript" };
00127     return getOrInitSelector(Ids, setObjectForKeyedSubscriptSel);
00128   }
00129 
00130   /// \brief Returns selector for "setObject:atIndexedSubscript".
00131   Selector getSetObjectAtIndexedSubscriptSelector() const {
00132     StringRef Ids[] = { "setObject", "atIndexedSubscript" };
00133     return getOrInitSelector(Ids, setObjectAtIndexedSubscriptSel);
00134   }
00135 
00136   /// \brief Returns selector for "isEqual:".
00137   Selector getIsEqualSelector() const {
00138     return getOrInitSelector(StringRef("isEqual"), isEqualSel);
00139   }
00140 
00141   /// \brief Enumerates the NSNumber methods used to generate literals.
00142   enum NSNumberLiteralMethodKind {
00143     NSNumberWithChar,
00144     NSNumberWithUnsignedChar,
00145     NSNumberWithShort,
00146     NSNumberWithUnsignedShort,
00147     NSNumberWithInt,
00148     NSNumberWithUnsignedInt,
00149     NSNumberWithLong,
00150     NSNumberWithUnsignedLong,
00151     NSNumberWithLongLong,
00152     NSNumberWithUnsignedLongLong,
00153     NSNumberWithFloat,
00154     NSNumberWithDouble,
00155     NSNumberWithBool,
00156     NSNumberWithInteger,
00157     NSNumberWithUnsignedInteger
00158   };
00159   static const unsigned NumNSNumberLiteralMethods = 15;
00160 
00161   /// \brief The Objective-C NSNumber selectors used to create NSNumber literals.
00162   /// \param Instance if true it will return the selector for the init* method
00163   /// otherwise it will return the selector for the number* method.
00164   Selector getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
00165                                       bool Instance) const;
00166 
00167   bool isNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
00168                                  Selector Sel) const {
00169     return Sel == getNSNumberLiteralSelector(MK, false) ||
00170            Sel == getNSNumberLiteralSelector(MK, true);
00171   }
00172 
00173   /// \brief Return NSNumberLiteralMethodKind if \p Sel is such a selector.
00174   Optional<NSNumberLiteralMethodKind>
00175       getNSNumberLiteralMethodKind(Selector Sel) const;
00176 
00177   /// \brief Determine the appropriate NSNumber factory method kind for a
00178   /// literal of the given type.
00179   Optional<NSNumberLiteralMethodKind>
00180       getNSNumberFactoryMethodKind(QualType T) const;
00181 
00182   /// \brief Returns true if \param T is a typedef of "BOOL" in objective-c.
00183   bool isObjCBOOLType(QualType T) const;
00184   /// \brief Returns true if \param T is a typedef of "NSInteger" in objective-c.
00185   bool isObjCNSIntegerType(QualType T) const;
00186   /// \brief Returns true if \param T is a typedef of "NSUInteger" in objective-c.
00187   bool isObjCNSUIntegerType(QualType T) const;
00188   /// \brief Returns one of NSIntegral typedef names if \param T is a typedef
00189   /// of that name in objective-c.
00190   StringRef GetNSIntegralKind(QualType T) const;
00191 
00192 private:
00193   bool isObjCTypedef(QualType T, StringRef name, IdentifierInfo *&II) const;
00194   bool isObjCEnumerator(const Expr *E,
00195                         StringRef name, IdentifierInfo *&II) const;
00196   Selector getOrInitSelector(ArrayRef<StringRef> Ids, Selector &Sel) const;
00197 
00198   ASTContext &Ctx;
00199 
00200   mutable IdentifierInfo *ClassIds[NumClassIds];
00201 
00202   mutable Selector NSStringSelectors[NumNSStringMethods];
00203 
00204   /// \brief The selectors for Objective-C NSArray methods.
00205   mutable Selector NSArraySelectors[NumNSArrayMethods];
00206 
00207   /// \brief The selectors for Objective-C NSDictionary methods.
00208   mutable Selector NSDictionarySelectors[NumNSDictionaryMethods];
00209 
00210   /// \brief The Objective-C NSNumber selectors used to create NSNumber literals.
00211   mutable Selector NSNumberClassSelectors[NumNSNumberLiteralMethods];
00212   mutable Selector NSNumberInstanceSelectors[NumNSNumberLiteralMethods];
00213 
00214   mutable Selector objectForKeyedSubscriptSel, objectAtIndexedSubscriptSel,
00215                    setObjectForKeyedSubscriptSel,setObjectAtIndexedSubscriptSel,
00216                    isEqualSel;
00217 
00218   mutable IdentifierInfo *BOOLId, *NSIntegerId, *NSUIntegerId;
00219   mutable IdentifierInfo *NSASCIIStringEncodingId, *NSUTF8StringEncodingId;
00220 };
00221 
00222 }  // end namespace clang
00223 
00224 #endif // LLVM_CLANG_AST_NSAPI_H