Home | History | Annotate | Download | only in AST
      1 //===--- NSAPI.h - NSFoundation APIs ----------------------------*- C++ -*-===//
      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 #ifndef LLVM_CLANG_AST_NSAPI_H
     11 #define LLVM_CLANG_AST_NSAPI_H
     12 
     13 #include "clang/Basic/IdentifierTable.h"
     14 #include "llvm/ADT/Optional.h"
     15 
     16 namespace clang {
     17   class ASTContext;
     18   class QualType;
     19 
     20 // \brief Provides info and caches identifiers/selectors for NSFoundation API.
     21 class NSAPI {
     22 public:
     23   explicit NSAPI(ASTContext &Ctx);
     24 
     25   ASTContext &getASTContext() const { return Ctx; }
     26 
     27   enum NSClassIdKindKind {
     28     ClassId_NSObject,
     29     ClassId_NSString,
     30     ClassId_NSArray,
     31     ClassId_NSMutableArray,
     32     ClassId_NSDictionary,
     33     ClassId_NSMutableDictionary,
     34     ClassId_NSNumber
     35   };
     36   static const unsigned NumClassIds = 7;
     37 
     38   enum NSStringMethodKind {
     39     NSStr_stringWithString,
     40     NSStr_initWithString
     41   };
     42   static const unsigned NumNSStringMethods = 2;
     43 
     44   IdentifierInfo *getNSClassId(NSClassIdKindKind K) const;
     45 
     46   /// \brief The Objective-C NSString selectors.
     47   Selector getNSStringSelector(NSStringMethodKind MK) const;
     48 
     49   /// \brief Enumerates the NSArray methods used to generate literals.
     50   enum NSArrayMethodKind {
     51     NSArr_array,
     52     NSArr_arrayWithArray,
     53     NSArr_arrayWithObject,
     54     NSArr_arrayWithObjects,
     55     NSArr_arrayWithObjectsCount,
     56     NSArr_initWithArray,
     57     NSArr_initWithObjects,
     58     NSArr_objectAtIndex,
     59     NSMutableArr_replaceObjectAtIndex
     60   };
     61   static const unsigned NumNSArrayMethods = 9;
     62 
     63   /// \brief The Objective-C NSArray selectors.
     64   Selector getNSArraySelector(NSArrayMethodKind MK) const;
     65 
     66   /// \brief Return NSArrayMethodKind if \arg Sel is such a selector.
     67   llvm::Optional<NSArrayMethodKind> getNSArrayMethodKind(Selector Sel);
     68 
     69   /// \brief Enumerates the NSDictionary methods used to generate literals.
     70   enum NSDictionaryMethodKind {
     71     NSDict_dictionary,
     72     NSDict_dictionaryWithDictionary,
     73     NSDict_dictionaryWithObjectForKey,
     74     NSDict_dictionaryWithObjectsForKeys,
     75     NSDict_dictionaryWithObjectsForKeysCount,
     76     NSDict_dictionaryWithObjectsAndKeys,
     77     NSDict_initWithDictionary,
     78     NSDict_initWithObjectsAndKeys,
     79     NSDict_objectForKey,
     80     NSMutableDict_setObjectForKey
     81   };
     82   static const unsigned NumNSDictionaryMethods = 10;
     83 
     84   /// \brief The Objective-C NSDictionary selectors.
     85   Selector getNSDictionarySelector(NSDictionaryMethodKind MK) const;
     86 
     87   /// \brief Return NSDictionaryMethodKind if \arg Sel is such a selector.
     88   llvm::Optional<NSDictionaryMethodKind>
     89       getNSDictionaryMethodKind(Selector Sel);
     90 
     91   /// \brief Enumerates the NSNumber methods used to generate literals.
     92   enum NSNumberLiteralMethodKind {
     93     NSNumberWithChar,
     94     NSNumberWithUnsignedChar,
     95     NSNumberWithShort,
     96     NSNumberWithUnsignedShort,
     97     NSNumberWithInt,
     98     NSNumberWithUnsignedInt,
     99     NSNumberWithLong,
    100     NSNumberWithUnsignedLong,
    101     NSNumberWithLongLong,
    102     NSNumberWithUnsignedLongLong,
    103     NSNumberWithFloat,
    104     NSNumberWithDouble,
    105     NSNumberWithBool,
    106     NSNumberWithInteger,
    107     NSNumberWithUnsignedInteger
    108   };
    109   static const unsigned NumNSNumberLiteralMethods = 15;
    110 
    111   /// \brief The Objective-C NSNumber selectors used to create NSNumber literals.
    112   /// \param Instance if true it will return the selector for the init* method
    113   /// otherwise it will return the selector for the number* method.
    114   Selector getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
    115                                       bool Instance) const;
    116 
    117   bool isNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
    118                                  Selector Sel) const {
    119     return Sel == getNSNumberLiteralSelector(MK, false) ||
    120            Sel == getNSNumberLiteralSelector(MK, true);
    121   }
    122 
    123   /// \brief Return NSNumberLiteralMethodKind if \arg Sel is such a selector.
    124   llvm::Optional<NSNumberLiteralMethodKind>
    125       getNSNumberLiteralMethodKind(Selector Sel) const;
    126 
    127   /// \brief Determine the appropriate NSNumber factory method kind for a
    128   /// literal of the given type.
    129   static llvm::Optional<NSNumberLiteralMethodKind>
    130       getNSNumberFactoryMethodKind(QualType T);
    131 
    132 private:
    133   ASTContext &Ctx;
    134 
    135   mutable IdentifierInfo *ClassIds[NumClassIds];
    136 
    137   mutable Selector NSStringSelectors[NumNSStringMethods];
    138 
    139   /// \brief The selectors for Objective-C NSArray methods.
    140   mutable Selector NSArraySelectors[NumNSArrayMethods];
    141 
    142   /// \brief The selectors for Objective-C NSDictionary methods.
    143   mutable Selector NSDictionarySelectors[NumNSDictionaryMethods];
    144 
    145   /// \brief The Objective-C NSNumber selectors used to create NSNumber literals.
    146   mutable Selector NSNumberClassSelectors[NumNSNumberLiteralMethods];
    147   mutable Selector NSNumberInstanceSelectors[NumNSNumberLiteralMethods];
    148 };
    149 
    150 }  // end namespace clang
    151 
    152 #endif // LLVM_CLANG_AST_NSAPI_H
    153