1 //===--- NSAPI.h - NSFoundation APIs ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_AST_NSAPI_H
11 #define LLVM_CLANG_AST_NSAPI_H
13 #include "clang/Basic/IdentifierTable.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/Optional.h"
22 // \brief Provides info and caches identifiers/selectors for NSFoundation API.
25 explicit NSAPI(ASTContext &Ctx);
27 ASTContext &getASTContext() const { return Ctx; }
29 enum NSClassIdKindKind {
33 ClassId_NSMutableArray,
35 ClassId_NSMutableDictionary,
38 static const unsigned NumClassIds = 7;
40 enum NSStringMethodKind {
41 NSStr_stringWithString,
42 NSStr_stringWithUTF8String,
43 NSStr_stringWithCStringEncoding,
44 NSStr_stringWithCString,
46 NSStr_initWithUTF8String
48 static const unsigned NumNSStringMethods = 5;
50 IdentifierInfo *getNSClassId(NSClassIdKindKind K) const;
52 /// \brief The Objective-C NSString selectors.
53 Selector getNSStringSelector(NSStringMethodKind MK) const;
55 /// \brief Return NSStringMethodKind if \param Sel is such a selector.
56 Optional<NSStringMethodKind> getNSStringMethodKind(Selector Sel) const;
58 /// \brief Returns true if the expression \param E is a reference of
59 /// "NSUTF8StringEncoding" enum constant.
60 bool isNSUTF8StringEncodingConstant(const Expr *E) const {
61 return isObjCEnumerator(E, "NSUTF8StringEncoding", NSUTF8StringEncodingId);
64 /// \brief Returns true if the expression \param E is a reference of
65 /// "NSASCIIStringEncoding" enum constant.
66 bool isNSASCIIStringEncodingConstant(const Expr *E) const {
67 return isObjCEnumerator(E, "NSASCIIStringEncoding",NSASCIIStringEncodingId);
70 /// \brief Enumerates the NSArray methods used to generate literals.
71 enum NSArrayMethodKind {
74 NSArr_arrayWithObject,
75 NSArr_arrayWithObjects,
76 NSArr_arrayWithObjectsCount,
78 NSArr_initWithObjects,
80 NSMutableArr_replaceObjectAtIndex
82 static const unsigned NumNSArrayMethods = 9;
84 /// \brief The Objective-C NSArray selectors.
85 Selector getNSArraySelector(NSArrayMethodKind MK) const;
87 /// \brief Return NSArrayMethodKind if \p Sel is such a selector.
88 Optional<NSArrayMethodKind> getNSArrayMethodKind(Selector Sel);
90 /// \brief Enumerates the NSDictionary methods used to generate literals.
91 enum NSDictionaryMethodKind {
93 NSDict_dictionaryWithDictionary,
94 NSDict_dictionaryWithObjectForKey,
95 NSDict_dictionaryWithObjectsForKeys,
96 NSDict_dictionaryWithObjectsForKeysCount,
97 NSDict_dictionaryWithObjectsAndKeys,
98 NSDict_initWithDictionary,
99 NSDict_initWithObjectsAndKeys,
100 NSDict_initWithObjectsForKeys,
102 NSMutableDict_setObjectForKey
104 static const unsigned NumNSDictionaryMethods = 12;
106 /// \brief The Objective-C NSDictionary selectors.
107 Selector getNSDictionarySelector(NSDictionaryMethodKind MK) const;
109 /// \brief Return NSDictionaryMethodKind if \p Sel is such a selector.
110 Optional<NSDictionaryMethodKind> getNSDictionaryMethodKind(Selector Sel);
112 /// \brief Returns selector for "objectForKeyedSubscript:".
113 Selector getObjectForKeyedSubscriptSelector() const {
114 return getOrInitSelector(StringRef("objectForKeyedSubscript"),
115 objectForKeyedSubscriptSel);
118 /// \brief Returns selector for "objectAtIndexedSubscript:".
119 Selector getObjectAtIndexedSubscriptSelector() const {
120 return getOrInitSelector(StringRef("objectAtIndexedSubscript"),
121 objectAtIndexedSubscriptSel);
124 /// \brief Returns selector for "setObject:forKeyedSubscript".
125 Selector getSetObjectForKeyedSubscriptSelector() const {
126 StringRef Ids[] = { "setObject", "forKeyedSubscript" };
127 return getOrInitSelector(Ids, setObjectForKeyedSubscriptSel);
130 /// \brief Returns selector for "setObject:atIndexedSubscript".
131 Selector getSetObjectAtIndexedSubscriptSelector() const {
132 StringRef Ids[] = { "setObject", "atIndexedSubscript" };
133 return getOrInitSelector(Ids, setObjectAtIndexedSubscriptSel);
136 /// \brief Returns selector for "isEqual:".
137 Selector getIsEqualSelector() const {
138 return getOrInitSelector(StringRef("isEqual"), isEqualSel);
141 /// \brief Enumerates the NSNumber methods used to generate literals.
142 enum NSNumberLiteralMethodKind {
144 NSNumberWithUnsignedChar,
146 NSNumberWithUnsignedShort,
148 NSNumberWithUnsignedInt,
150 NSNumberWithUnsignedLong,
151 NSNumberWithLongLong,
152 NSNumberWithUnsignedLongLong,
157 NSNumberWithUnsignedInteger
159 static const unsigned NumNSNumberLiteralMethods = 15;
161 /// \brief The Objective-C NSNumber selectors used to create NSNumber literals.
162 /// \param Instance if true it will return the selector for the init* method
163 /// otherwise it will return the selector for the number* method.
164 Selector getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
165 bool Instance) const;
167 bool isNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
168 Selector Sel) const {
169 return Sel == getNSNumberLiteralSelector(MK, false) ||
170 Sel == getNSNumberLiteralSelector(MK, true);
173 /// \brief Return NSNumberLiteralMethodKind if \p Sel is such a selector.
174 Optional<NSNumberLiteralMethodKind>
175 getNSNumberLiteralMethodKind(Selector Sel) const;
177 /// \brief Determine the appropriate NSNumber factory method kind for a
178 /// literal of the given type.
179 Optional<NSNumberLiteralMethodKind>
180 getNSNumberFactoryMethodKind(QualType T) const;
182 /// \brief Returns true if \param T is a typedef of "BOOL" in objective-c.
183 bool isObjCBOOLType(QualType T) const;
184 /// \brief Returns true if \param T is a typedef of "NSInteger" in objective-c.
185 bool isObjCNSIntegerType(QualType T) const;
186 /// \brief Returns true if \param T is a typedef of "NSUInteger" in objective-c.
187 bool isObjCNSUIntegerType(QualType T) const;
188 /// \brief Returns one of NSIntegral typedef names if \param T is a typedef
189 /// of that name in objective-c.
190 StringRef GetNSIntegralKind(QualType T) const;
193 bool isObjCTypedef(QualType T, StringRef name, IdentifierInfo *&II) const;
194 bool isObjCEnumerator(const Expr *E,
195 StringRef name, IdentifierInfo *&II) const;
196 Selector getOrInitSelector(ArrayRef<StringRef> Ids, Selector &Sel) const;
200 mutable IdentifierInfo *ClassIds[NumClassIds];
202 mutable Selector NSStringSelectors[NumNSStringMethods];
204 /// \brief The selectors for Objective-C NSArray methods.
205 mutable Selector NSArraySelectors[NumNSArrayMethods];
207 /// \brief The selectors for Objective-C NSDictionary methods.
208 mutable Selector NSDictionarySelectors[NumNSDictionaryMethods];
210 /// \brief The Objective-C NSNumber selectors used to create NSNumber literals.
211 mutable Selector NSNumberClassSelectors[NumNSNumberLiteralMethods];
212 mutable Selector NSNumberInstanceSelectors[NumNSNumberLiteralMethods];
214 mutable Selector objectForKeyedSubscriptSel, objectAtIndexedSubscriptSel,
215 setObjectForKeyedSubscriptSel,setObjectAtIndexedSubscriptSel,
218 mutable IdentifierInfo *BOOLId, *NSIntegerId, *NSUIntegerId;
219 mutable IdentifierInfo *NSASCIIStringEncodingId, *NSUTF8StringEncodingId;
222 } // end namespace clang
224 #endif // LLVM_CLANG_AST_NSAPI_H