1 //===--- NSAPI.h - NSFoundation APIs ----------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_AST_NSAPI_H
10 #define LLVM_CLANG_AST_NSAPI_H
12 #include "clang/Basic/IdentifierTable.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/Optional.h"
18 class ObjCInterfaceDecl;
22 // 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 ClassId_NSMutableOrderedSet,
41 static const unsigned NumClassIds = 10;
43 enum NSStringMethodKind {
44 NSStr_stringWithString,
45 NSStr_stringWithUTF8String,
46 NSStr_stringWithCStringEncoding,
47 NSStr_stringWithCString,
49 NSStr_initWithUTF8String
51 static const unsigned NumNSStringMethods = 6;
53 IdentifierInfo *getNSClassId(NSClassIdKindKind K) const;
55 /// The Objective-C NSString selectors.
56 Selector getNSStringSelector(NSStringMethodKind MK) const;
58 /// Return NSStringMethodKind if \param Sel is such a selector.
59 Optional<NSStringMethodKind> getNSStringMethodKind(Selector Sel) const;
61 /// Returns true if the expression \param E is a reference of
62 /// "NSUTF8StringEncoding" enum constant.
63 bool isNSUTF8StringEncodingConstant(const Expr *E) const {
64 return isObjCEnumerator(E, "NSUTF8StringEncoding", NSUTF8StringEncodingId);
67 /// Returns true if the expression \param E is a reference of
68 /// "NSASCIIStringEncoding" enum constant.
69 bool isNSASCIIStringEncodingConstant(const Expr *E) const {
70 return isObjCEnumerator(E, "NSASCIIStringEncoding",NSASCIIStringEncodingId);
73 /// Enumerates the NSArray/NSMutableArray methods used to generate
74 /// literals and to apply some checks.
75 enum NSArrayMethodKind {
78 NSArr_arrayWithObject,
79 NSArr_arrayWithObjects,
80 NSArr_arrayWithObjectsCount,
82 NSArr_initWithObjects,
84 NSMutableArr_replaceObjectAtIndex,
85 NSMutableArr_addObject,
86 NSMutableArr_insertObjectAtIndex,
87 NSMutableArr_setObjectAtIndexedSubscript
89 static const unsigned NumNSArrayMethods = 12;
91 /// The Objective-C NSArray selectors.
92 Selector getNSArraySelector(NSArrayMethodKind MK) const;
94 /// Return NSArrayMethodKind if \p Sel is such a selector.
95 Optional<NSArrayMethodKind> getNSArrayMethodKind(Selector Sel);
97 /// Enumerates the NSDictionary/NSMutableDictionary methods used
98 /// to generate literals and to apply some checks.
99 enum NSDictionaryMethodKind {
101 NSDict_dictionaryWithDictionary,
102 NSDict_dictionaryWithObjectForKey,
103 NSDict_dictionaryWithObjectsForKeys,
104 NSDict_dictionaryWithObjectsForKeysCount,
105 NSDict_dictionaryWithObjectsAndKeys,
106 NSDict_initWithDictionary,
107 NSDict_initWithObjectsAndKeys,
108 NSDict_initWithObjectsForKeys,
110 NSMutableDict_setObjectForKey,
111 NSMutableDict_setObjectForKeyedSubscript,
112 NSMutableDict_setValueForKey
114 static const unsigned NumNSDictionaryMethods = 13;
116 /// The Objective-C NSDictionary selectors.
117 Selector getNSDictionarySelector(NSDictionaryMethodKind MK) const;
119 /// Return NSDictionaryMethodKind if \p Sel is such a selector.
120 Optional<NSDictionaryMethodKind> getNSDictionaryMethodKind(Selector Sel);
122 /// Enumerates the NSMutableSet/NSOrderedSet methods used
123 /// to apply some checks.
124 enum NSSetMethodKind {
125 NSMutableSet_addObject,
126 NSOrderedSet_insertObjectAtIndex,
127 NSOrderedSet_setObjectAtIndex,
128 NSOrderedSet_setObjectAtIndexedSubscript,
129 NSOrderedSet_replaceObjectAtIndexWithObject
131 static const unsigned NumNSSetMethods = 5;
133 /// The Objective-C NSSet selectors.
134 Selector getNSSetSelector(NSSetMethodKind MK) const;
136 /// Return NSSetMethodKind if \p Sel is such a selector.
137 Optional<NSSetMethodKind> getNSSetMethodKind(Selector Sel);
139 /// Returns selector for "objectForKeyedSubscript:".
140 Selector getObjectForKeyedSubscriptSelector() const {
141 return getOrInitSelector(StringRef("objectForKeyedSubscript"),
142 objectForKeyedSubscriptSel);
145 /// Returns selector for "objectAtIndexedSubscript:".
146 Selector getObjectAtIndexedSubscriptSelector() const {
147 return getOrInitSelector(StringRef("objectAtIndexedSubscript"),
148 objectAtIndexedSubscriptSel);
151 /// Returns selector for "setObject:forKeyedSubscript".
152 Selector getSetObjectForKeyedSubscriptSelector() const {
153 StringRef Ids[] = { "setObject", "forKeyedSubscript" };
154 return getOrInitSelector(Ids, setObjectForKeyedSubscriptSel);
157 /// Returns selector for "setObject:atIndexedSubscript".
158 Selector getSetObjectAtIndexedSubscriptSelector() const {
159 StringRef Ids[] = { "setObject", "atIndexedSubscript" };
160 return getOrInitSelector(Ids, setObjectAtIndexedSubscriptSel);
163 /// Returns selector for "isEqual:".
164 Selector getIsEqualSelector() const {
165 return getOrInitSelector(StringRef("isEqual"), isEqualSel);
168 Selector getNewSelector() const {
169 return getOrInitNullarySelector("new", NewSel);
172 Selector getInitSelector() const {
173 return getOrInitNullarySelector("init", InitSel);
176 /// Enumerates the NSNumber methods used to generate literals.
177 enum NSNumberLiteralMethodKind {
179 NSNumberWithUnsignedChar,
181 NSNumberWithUnsignedShort,
183 NSNumberWithUnsignedInt,
185 NSNumberWithUnsignedLong,
186 NSNumberWithLongLong,
187 NSNumberWithUnsignedLongLong,
192 NSNumberWithUnsignedInteger
194 static const unsigned NumNSNumberLiteralMethods = 15;
196 /// The Objective-C NSNumber selectors used to create NSNumber literals.
197 /// \param Instance if true it will return the selector for the init* method
198 /// otherwise it will return the selector for the number* method.
199 Selector getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
200 bool Instance) const;
202 bool isNSNumberLiteralSelector(NSNumberLiteralMethodKind MK,
203 Selector Sel) const {
204 return Sel == getNSNumberLiteralSelector(MK, false) ||
205 Sel == getNSNumberLiteralSelector(MK, true);
208 /// Return NSNumberLiteralMethodKind if \p Sel is such a selector.
209 Optional<NSNumberLiteralMethodKind>
210 getNSNumberLiteralMethodKind(Selector Sel) const;
212 /// Determine the appropriate NSNumber factory method kind for a
213 /// literal of the given type.
214 Optional<NSNumberLiteralMethodKind>
215 getNSNumberFactoryMethodKind(QualType T) const;
217 /// Returns true if \param T is a typedef of "BOOL" in objective-c.
218 bool isObjCBOOLType(QualType T) const;
219 /// Returns true if \param T is a typedef of "NSInteger" in objective-c.
220 bool isObjCNSIntegerType(QualType T) const;
221 /// Returns true if \param T is a typedef of "NSUInteger" in objective-c.
222 bool isObjCNSUIntegerType(QualType T) const;
223 /// Returns one of NSIntegral typedef names if \param T is a typedef
224 /// of that name in objective-c.
225 StringRef GetNSIntegralKind(QualType T) const;
227 /// Returns \c true if \p Id is currently defined as a macro.
228 bool isMacroDefined(StringRef Id) const;
230 /// Returns \c true if \p InterfaceDecl is subclass of \p NSClassKind
231 bool isSubclassOfNSClass(ObjCInterfaceDecl *InterfaceDecl,
232 NSClassIdKindKind NSClassKind) const;
235 bool isObjCTypedef(QualType T, StringRef name, IdentifierInfo *&II) const;
236 bool isObjCEnumerator(const Expr *E,
237 StringRef name, IdentifierInfo *&II) const;
238 Selector getOrInitSelector(ArrayRef<StringRef> Ids, Selector &Sel) const;
239 Selector getOrInitNullarySelector(StringRef Id, Selector &Sel) const;
243 mutable IdentifierInfo *ClassIds[NumClassIds];
245 mutable Selector NSStringSelectors[NumNSStringMethods];
247 /// The selectors for Objective-C NSArray methods.
248 mutable Selector NSArraySelectors[NumNSArrayMethods];
250 /// The selectors for Objective-C NSDictionary methods.
251 mutable Selector NSDictionarySelectors[NumNSDictionaryMethods];
253 /// The selectors for Objective-C NSSet methods.
254 mutable Selector NSSetSelectors[NumNSSetMethods];
256 /// The Objective-C NSNumber selectors used to create NSNumber literals.
257 mutable Selector NSNumberClassSelectors[NumNSNumberLiteralMethods];
258 mutable Selector NSNumberInstanceSelectors[NumNSNumberLiteralMethods];
260 mutable Selector objectForKeyedSubscriptSel, objectAtIndexedSubscriptSel,
261 setObjectForKeyedSubscriptSel,setObjectAtIndexedSubscriptSel,
262 isEqualSel, InitSel, NewSel;
264 mutable IdentifierInfo *BOOLId, *NSIntegerId, *NSUIntegerId;
265 mutable IdentifierInfo *NSASCIIStringEncodingId, *NSUTF8StringEncodingId;
268 } // end namespace clang
270 #endif // LLVM_CLANG_AST_NSAPI_H