Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:38

0001 //===--- NSAPI.h - NSFoundation APIs ----------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_AST_NSAPI_H
0010 #define LLVM_CLANG_AST_NSAPI_H
0011 
0012 #include "clang/Basic/IdentifierTable.h"
0013 #include "llvm/ADT/ArrayRef.h"
0014 #include <optional>
0015 
0016 namespace clang {
0017   class ASTContext;
0018   class ObjCInterfaceDecl;
0019   class QualType;
0020   class Expr;
0021 
0022 // Provides info and caches identifiers/selectors for NSFoundation API.
0023 class NSAPI {
0024 public:
0025   explicit NSAPI(ASTContext &Ctx);
0026 
0027   ASTContext &getASTContext() const { return Ctx; }
0028 
0029   enum NSClassIdKindKind {
0030     ClassId_NSObject,
0031     ClassId_NSString,
0032     ClassId_NSArray,
0033     ClassId_NSMutableArray,
0034     ClassId_NSDictionary,
0035     ClassId_NSMutableDictionary,
0036     ClassId_NSNumber,
0037     ClassId_NSMutableSet,
0038     ClassId_NSMutableOrderedSet,
0039     ClassId_NSValue
0040   };
0041   static const unsigned NumClassIds = 10;
0042 
0043   enum NSStringMethodKind {
0044     NSStr_stringWithString,
0045     NSStr_stringWithUTF8String,
0046     NSStr_stringWithCStringEncoding,
0047     NSStr_stringWithCString,
0048     NSStr_initWithString,
0049     NSStr_initWithUTF8String
0050   };
0051   static const unsigned NumNSStringMethods = 6;
0052 
0053   IdentifierInfo *getNSClassId(NSClassIdKindKind K) const;
0054 
0055   /// The Objective-C NSString selectors.
0056   Selector getNSStringSelector(NSStringMethodKind MK) const;
0057 
0058   /// Returns true if the expression \param E is a reference of
0059   /// "NSUTF8StringEncoding" enum constant.
0060   bool isNSUTF8StringEncodingConstant(const Expr *E) const {
0061     return isObjCEnumerator(E, "NSUTF8StringEncoding", NSUTF8StringEncodingId);
0062   }
0063 
0064   /// Returns true if the expression \param E is a reference of
0065   /// "NSASCIIStringEncoding" enum constant.
0066   bool isNSASCIIStringEncodingConstant(const Expr *E) const {
0067     return isObjCEnumerator(E, "NSASCIIStringEncoding",NSASCIIStringEncodingId);
0068   }
0069 
0070   /// Enumerates the NSArray/NSMutableArray methods used to generate
0071   /// literals and to apply some checks.
0072   enum NSArrayMethodKind {
0073     NSArr_array,
0074     NSArr_arrayWithArray,
0075     NSArr_arrayWithObject,
0076     NSArr_arrayWithObjects,
0077     NSArr_arrayWithObjectsCount,
0078     NSArr_initWithArray,
0079     NSArr_initWithObjects,
0080     NSArr_objectAtIndex,
0081     NSMutableArr_replaceObjectAtIndex,
0082     NSMutableArr_addObject,
0083     NSMutableArr_insertObjectAtIndex,
0084     NSMutableArr_setObjectAtIndexedSubscript
0085   };
0086   static const unsigned NumNSArrayMethods = 12;
0087 
0088   /// The Objective-C NSArray selectors.
0089   Selector getNSArraySelector(NSArrayMethodKind MK) const;
0090 
0091   /// Return NSArrayMethodKind if \p Sel is such a selector.
0092   std::optional<NSArrayMethodKind> getNSArrayMethodKind(Selector Sel);
0093 
0094   /// Enumerates the NSDictionary/NSMutableDictionary methods used
0095   /// to generate literals and to apply some checks.
0096   enum NSDictionaryMethodKind {
0097     NSDict_dictionary,
0098     NSDict_dictionaryWithDictionary,
0099     NSDict_dictionaryWithObjectForKey,
0100     NSDict_dictionaryWithObjectsForKeys,
0101     NSDict_dictionaryWithObjectsForKeysCount,
0102     NSDict_dictionaryWithObjectsAndKeys,
0103     NSDict_initWithDictionary,
0104     NSDict_initWithObjectsAndKeys,
0105     NSDict_initWithObjectsForKeys,
0106     NSDict_objectForKey,
0107     NSMutableDict_setObjectForKey,
0108     NSMutableDict_setObjectForKeyedSubscript,
0109     NSMutableDict_setValueForKey
0110   };
0111   static const unsigned NumNSDictionaryMethods = 13;
0112 
0113   /// The Objective-C NSDictionary selectors.
0114   Selector getNSDictionarySelector(NSDictionaryMethodKind MK) const;
0115 
0116   /// Return NSDictionaryMethodKind if \p Sel is such a selector.
0117   std::optional<NSDictionaryMethodKind> getNSDictionaryMethodKind(Selector Sel);
0118 
0119   /// Enumerates the NSMutableSet/NSOrderedSet methods used
0120   /// to apply some checks.
0121   enum NSSetMethodKind {
0122     NSMutableSet_addObject,
0123     NSOrderedSet_insertObjectAtIndex,
0124     NSOrderedSet_setObjectAtIndex,
0125     NSOrderedSet_setObjectAtIndexedSubscript,
0126     NSOrderedSet_replaceObjectAtIndexWithObject
0127   };
0128   static const unsigned NumNSSetMethods = 5;
0129 
0130   /// The Objective-C NSSet selectors.
0131   Selector getNSSetSelector(NSSetMethodKind MK) const;
0132 
0133   /// Return NSSetMethodKind if \p Sel is such a selector.
0134   std::optional<NSSetMethodKind> getNSSetMethodKind(Selector Sel);
0135 
0136   /// Returns selector for "objectForKeyedSubscript:".
0137   Selector getObjectForKeyedSubscriptSelector() const {
0138     return getOrInitSelector(StringRef("objectForKeyedSubscript"),
0139                              objectForKeyedSubscriptSel);
0140   }
0141 
0142   /// Returns selector for "objectAtIndexedSubscript:".
0143   Selector getObjectAtIndexedSubscriptSelector() const {
0144     return getOrInitSelector(StringRef("objectAtIndexedSubscript"),
0145                              objectAtIndexedSubscriptSel);
0146   }
0147 
0148   /// Returns selector for "setObject:forKeyedSubscript".
0149   Selector getSetObjectForKeyedSubscriptSelector() const {
0150     StringRef Ids[] = { "setObject", "forKeyedSubscript" };
0151     return getOrInitSelector(Ids, setObjectForKeyedSubscriptSel);
0152   }
0153 
0154   /// Returns selector for "setObject:atIndexedSubscript".
0155   Selector getSetObjectAtIndexedSubscriptSelector() const {
0156     StringRef Ids[] = { "setObject", "atIndexedSubscript" };
0157     return getOrInitSelector(Ids, setObjectAtIndexedSubscriptSel);
0158   }
0159 
0160   /// Returns selector for "isEqual:".
0161   Selector getIsEqualSelector() const {
0162     return getOrInitSelector(StringRef("isEqual"), isEqualSel);
0163   }
0164 
0165   Selector getNewSelector() const {
0166     return getOrInitNullarySelector("new", NewSel);
0167   }
0168 
0169   Selector getInitSelector() const {
0170     return getOrInitNullarySelector("init", InitSel);
0171   }
0172 
0173   /// Enumerates the NSNumber methods used to generate literals.
0174   enum NSNumberLiteralMethodKind {
0175     NSNumberWithChar,
0176     NSNumberWithUnsignedChar,
0177     NSNumberWithShort,
0178     NSNumberWithUnsignedShort,
0179     NSNumberWithInt,
0180     NSNumberWithUnsignedInt,
0181     NSNumberWithLong,
0182     NSNumberWithUnsignedLong,
0183     NSNumberWithLongLong,
0184     NSNumberWithUnsignedLongLong,
0185     NSNumberWithFloat,
0186     NSNumberWithDouble,
0187     NSNumberWithBool,
0188     NSNumberWithInteger,
0189     NSNumberWithUnsignedInteger
0190   };
0191   static const unsigned NumNSNumberLiteralMethods = 15;
0192 
0193   /// The Objective-C NSNumber selectors used to create NSNumber literals.
0194   /// \param Instance if true it will return the selector for the init* method
0195   /// otherwise it will return the selector for the number* method.
0196   Selector getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
0197                                       bool Instance) const;
0198 
0199   bool isNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
0200                                  Selector Sel) const {
0201     return Sel == getNSNumberLiteralSelector(MK, false) ||
0202            Sel == getNSNumberLiteralSelector(MK, true);
0203   }
0204 
0205   /// Return NSNumberLiteralMethodKind if \p Sel is such a selector.
0206   std::optional<NSNumberLiteralMethodKind>
0207   getNSNumberLiteralMethodKind(Selector Sel) const;
0208 
0209   /// Determine the appropriate NSNumber factory method kind for a
0210   /// literal of the given type.
0211   std::optional<NSNumberLiteralMethodKind>
0212   getNSNumberFactoryMethodKind(QualType T) const;
0213 
0214   /// Returns true if \param T is a typedef of "BOOL" in objective-c.
0215   bool isObjCBOOLType(QualType T) const;
0216   /// Returns true if \param T is a typedef of "NSInteger" in objective-c.
0217   bool isObjCNSIntegerType(QualType T) const;
0218   /// Returns true if \param T is a typedef of "NSUInteger" in objective-c.
0219   bool isObjCNSUIntegerType(QualType T) const;
0220   /// Returns one of NSIntegral typedef names if \param T is a typedef
0221   /// of that name in objective-c.
0222   StringRef GetNSIntegralKind(QualType T) const;
0223 
0224   /// Returns \c true if \p Id is currently defined as a macro.
0225   bool isMacroDefined(StringRef Id) const;
0226 
0227   /// Returns \c true if \p InterfaceDecl is subclass of \p NSClassKind
0228   bool isSubclassOfNSClass(ObjCInterfaceDecl *InterfaceDecl,
0229                            NSClassIdKindKind NSClassKind) const;
0230 
0231 private:
0232   bool isObjCTypedef(QualType T, StringRef name, IdentifierInfo *&II) const;
0233   bool isObjCEnumerator(const Expr *E,
0234                         StringRef name, IdentifierInfo *&II) const;
0235   Selector getOrInitSelector(ArrayRef<StringRef> Ids, Selector &Sel) const;
0236   Selector getOrInitNullarySelector(StringRef Id, Selector &Sel) const;
0237 
0238   ASTContext &Ctx;
0239 
0240   mutable IdentifierInfo *ClassIds[NumClassIds];
0241 
0242   mutable Selector NSStringSelectors[NumNSStringMethods];
0243 
0244   /// The selectors for Objective-C NSArray methods.
0245   mutable Selector NSArraySelectors[NumNSArrayMethods];
0246 
0247   /// The selectors for Objective-C NSDictionary methods.
0248   mutable Selector NSDictionarySelectors[NumNSDictionaryMethods];
0249 
0250   /// The selectors for Objective-C NSSet methods.
0251   mutable Selector NSSetSelectors[NumNSSetMethods];
0252 
0253   /// The Objective-C NSNumber selectors used to create NSNumber literals.
0254   mutable Selector NSNumberClassSelectors[NumNSNumberLiteralMethods];
0255   mutable Selector NSNumberInstanceSelectors[NumNSNumberLiteralMethods];
0256 
0257   mutable Selector objectForKeyedSubscriptSel, objectAtIndexedSubscriptSel,
0258                    setObjectForKeyedSubscriptSel,setObjectAtIndexedSubscriptSel,
0259                    isEqualSel, InitSel, NewSel;
0260 
0261   mutable IdentifierInfo *BOOLId, *NSIntegerId, *NSUIntegerId;
0262   mutable IdentifierInfo *NSASCIIStringEncodingId, *NSUTF8StringEncodingId;
0263 };
0264 
0265 }  // end namespace clang
0266 
0267 #endif // LLVM_CLANG_AST_NSAPI_H