clang  14.0.0git
NSAPI.h
Go to the documentation of this file.
1 //===--- NSAPI.h - NSFoundation APIs ----------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_AST_NSAPI_H
10 #define LLVM_CLANG_AST_NSAPI_H
11 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/Optional.h"
15 
16 namespace clang {
17  class ASTContext;
18  class ObjCInterfaceDecl;
19  class QualType;
20  class Expr;
21 
22 // Provides info and caches identifiers/selectors for NSFoundation API.
23 class NSAPI {
24 public:
25  explicit NSAPI(ASTContext &Ctx);
26 
27  ASTContext &getASTContext() const { return Ctx; }
28 
40  };
41  static const unsigned NumClassIds = 10;
42 
50  };
51  static const unsigned NumNSStringMethods = 6;
52 
54 
55  /// The Objective-C NSString selectors.
57 
58  /// 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);
62  }
63 
64  /// 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);
68  }
69 
70  /// Enumerates the NSArray/NSMutableArray methods used to generate
71  /// literals and to apply some checks.
85  };
86  static const unsigned NumNSArrayMethods = 12;
87 
88  /// The Objective-C NSArray selectors.
90 
91  /// Return NSArrayMethodKind if \p Sel is such a selector.
93 
94  /// Enumerates the NSDictionary/NSMutableDictionary methods used
95  /// to generate literals and to apply some checks.
110  };
111  static const unsigned NumNSDictionaryMethods = 13;
112 
113  /// The Objective-C NSDictionary selectors.
115 
116  /// Return NSDictionaryMethodKind if \p Sel is such a selector.
118 
119  /// Enumerates the NSMutableSet/NSOrderedSet methods used
120  /// to apply some checks.
127  };
128  static const unsigned NumNSSetMethods = 5;
129 
130  /// The Objective-C NSSet selectors.
132 
133  /// Return NSSetMethodKind if \p Sel is such a selector.
135 
136  /// Returns selector for "objectForKeyedSubscript:".
138  return getOrInitSelector(StringRef("objectForKeyedSubscript"),
139  objectForKeyedSubscriptSel);
140  }
141 
142  /// Returns selector for "objectAtIndexedSubscript:".
144  return getOrInitSelector(StringRef("objectAtIndexedSubscript"),
145  objectAtIndexedSubscriptSel);
146  }
147 
148  /// Returns selector for "setObject:forKeyedSubscript".
150  StringRef Ids[] = { "setObject", "forKeyedSubscript" };
151  return getOrInitSelector(Ids, setObjectForKeyedSubscriptSel);
152  }
153 
154  /// Returns selector for "setObject:atIndexedSubscript".
156  StringRef Ids[] = { "setObject", "atIndexedSubscript" };
157  return getOrInitSelector(Ids, setObjectAtIndexedSubscriptSel);
158  }
159 
160  /// Returns selector for "isEqual:".
162  return getOrInitSelector(StringRef("isEqual"), isEqualSel);
163  }
164 
166  return getOrInitNullarySelector("new", NewSel);
167  }
168 
170  return getOrInitNullarySelector("init", InitSel);
171  }
172 
173  /// Enumerates the NSNumber methods used to generate literals.
190  };
191  static const unsigned NumNSNumberLiteralMethods = 15;
192 
193  /// The Objective-C NSNumber selectors used to create NSNumber literals.
194  /// \param Instance if true it will return the selector for the init* method
195  /// otherwise it will return the selector for the number* method.
197  bool Instance) const;
198 
200  Selector Sel) const {
201  return Sel == getNSNumberLiteralSelector(MK, false) ||
202  Sel == getNSNumberLiteralSelector(MK, true);
203  }
204 
205  /// Return NSNumberLiteralMethodKind if \p Sel is such a selector.
208 
209  /// Determine the appropriate NSNumber factory method kind for a
210  /// literal of the given type.
213 
214  /// Returns true if \param T is a typedef of "BOOL" in objective-c.
215  bool isObjCBOOLType(QualType T) const;
216  /// Returns true if \param T is a typedef of "NSInteger" in objective-c.
217  bool isObjCNSIntegerType(QualType T) const;
218  /// Returns true if \param T is a typedef of "NSUInteger" in objective-c.
219  bool isObjCNSUIntegerType(QualType T) const;
220  /// Returns one of NSIntegral typedef names if \param T is a typedef
221  /// of that name in objective-c.
222  StringRef GetNSIntegralKind(QualType T) const;
223 
224  /// Returns \c true if \p Id is currently defined as a macro.
225  bool isMacroDefined(StringRef Id) const;
226 
227  /// Returns \c true if \p InterfaceDecl is subclass of \p NSClassKind
228  bool isSubclassOfNSClass(ObjCInterfaceDecl *InterfaceDecl,
229  NSClassIdKindKind NSClassKind) const;
230 
231 private:
232  bool isObjCTypedef(QualType T, StringRef name, IdentifierInfo *&II) const;
233  bool isObjCEnumerator(const Expr *E,
234  StringRef name, IdentifierInfo *&II) const;
235  Selector getOrInitSelector(ArrayRef<StringRef> Ids, Selector &Sel) const;
236  Selector getOrInitNullarySelector(StringRef Id, Selector &Sel) const;
237 
238  ASTContext &Ctx;
239 
240  mutable IdentifierInfo *ClassIds[NumClassIds];
241 
242  mutable Selector NSStringSelectors[NumNSStringMethods];
243 
244  /// The selectors for Objective-C NSArray methods.
245  mutable Selector NSArraySelectors[NumNSArrayMethods];
246 
247  /// The selectors for Objective-C NSDictionary methods.
248  mutable Selector NSDictionarySelectors[NumNSDictionaryMethods];
249 
250  /// The selectors for Objective-C NSSet methods.
251  mutable Selector NSSetSelectors[NumNSSetMethods];
252 
253  /// The Objective-C NSNumber selectors used to create NSNumber literals.
254  mutable Selector NSNumberClassSelectors[NumNSNumberLiteralMethods];
255  mutable Selector NSNumberInstanceSelectors[NumNSNumberLiteralMethods];
256 
257  mutable Selector objectForKeyedSubscriptSel, objectAtIndexedSubscriptSel,
258  setObjectForKeyedSubscriptSel,setObjectAtIndexedSubscriptSel,
259  isEqualSel, InitSel, NewSel;
260 
261  mutable IdentifierInfo *BOOLId, *NSIntegerId, *NSUIntegerId;
262  mutable IdentifierInfo *NSASCIIStringEncodingId, *NSUTF8StringEncodingId;
263 };
264 
265 } // end namespace clang
266 
267 #endif // LLVM_CLANG_AST_NSAPI_H
clang::NSAPI::ClassId_NSMutableOrderedSet
@ ClassId_NSMutableOrderedSet
Definition: NSAPI.h:38
clang::ObjCInterfaceDecl
Represents an ObjC class declaration.
Definition: DeclObjC.h:1151
clang::NSAPI::NSNumberWithInteger
@ NSNumberWithInteger
Definition: NSAPI.h:188
clang::NSAPI::NSDict_objectForKey
@ NSDict_objectForKey
Definition: NSAPI.h:106
clang::NSAPI::NSStr_stringWithCString
@ NSStr_stringWithCString
Definition: NSAPI.h:47
clang::NSAPI::NSAPI
NSAPI(ASTContext &Ctx)
Definition: NSAPI.cpp:17
clang::NSAPI::getNSNumberLiteralMethodKind
Optional< NSNumberLiteralMethodKind > getNSNumberLiteralMethodKind(Selector Sel) const
Return NSNumberLiteralMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:367
clang::NSAPI::NSDict_initWithObjectsAndKeys
@ NSDict_initWithObjectsAndKeys
Definition: NSAPI.h:104
clang::NSAPI::getNSSetSelector
Selector getNSSetSelector(NSSetMethodKind MK) const
The Objective-C NSSet selectors.
Definition: NSAPI.cpp:257
clang::NSAPI::isNSNumberLiteralSelector
bool isNSNumberLiteralSelector(NSNumberLiteralMethodKind MK, Selector Sel) const
Definition: NSAPI.h:199
clang::NSAPI::NSOrderedSet_setObjectAtIndex
@ NSOrderedSet_setObjectAtIndex
Definition: NSAPI.h:124
clang::NSAPI::ClassId_NSNumber
@ ClassId_NSNumber
Definition: NSAPI.h:36
clang::NSAPI::getNSClassId
IdentifierInfo * getNSClassId(NSClassIdKindKind K) const
Definition: NSAPI.cpp:22
clang::NSAPI::NumClassIds
static const unsigned NumClassIds
Definition: NSAPI.h:41
clang::NSAPI::NSArrayMethodKind
NSArrayMethodKind
Enumerates the NSArray/NSMutableArray methods used to generate literals and to apply some checks.
Definition: NSAPI.h:72
clang::NSAPI::NSOrderedSet_insertObjectAtIndex
@ NSOrderedSet_insertObjectAtIndex
Definition: NSAPI.h:123
clang::NSAPI::ClassId_NSMutableArray
@ ClassId_NSMutableArray
Definition: NSAPI.h:33
clang::NSAPI::NSOrderedSet_replaceObjectAtIndexWithObject
@ NSOrderedSet_replaceObjectAtIndexWithObject
Definition: NSAPI.h:126
clang::NSAPI::NumNSArrayMethods
static const unsigned NumNSArrayMethods
Definition: NSAPI.h:86
clang::NSAPI::NSNumberWithUnsignedLongLong
@ NSNumberWithUnsignedLongLong
Definition: NSAPI.h:184
clang::NSAPI::NSStr_stringWithCStringEncoding
@ NSStr_stringWithCStringEncoding
Definition: NSAPI.h:46
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
clang::NSAPI::NSMutableDict_setObjectForKey
@ NSMutableDict_setObjectForKey
Definition: NSAPI.h:107
clang::NSAPI::NSNumberWithShort
@ NSNumberWithShort
Definition: NSAPI.h:177
clang::NSAPI::NSStr_stringWithString
@ NSStr_stringWithString
Definition: NSAPI.h:44
llvm::Optional
Definition: LLVM.h:40
clang::NSAPI::NumNSSetMethods
static const unsigned NumNSSetMethods
Definition: NSAPI.h:128
clang::NSAPI::isObjCBOOLType
bool isObjCBOOLType(QualType T) const
Returns true if.
Definition: NSAPI.cpp:503
clang::NSAPI::NSArr_array
@ NSArr_array
Definition: NSAPI.h:73
clang::NSAPI::getInitSelector
Selector getInitSelector() const
Definition: NSAPI.h:169
clang::NSAPI::NSArr_objectAtIndex
@ NSArr_objectAtIndex
Definition: NSAPI.h:80
clang::NSAPI::getNSArraySelector
Selector getNSArraySelector(NSArrayMethodKind MK) const
The Objective-C NSArray selectors.
Definition: NSAPI.cpp:78
clang::NSAPI::getASTContext
ASTContext & getASTContext() const
Definition: NSAPI.h:27
clang::NSAPI::NSOrderedSet_setObjectAtIndexedSubscript
@ NSOrderedSet_setObjectAtIndexedSubscript
Definition: NSAPI.h:125
clang::NSAPI::isSubclassOfNSClass
bool isSubclassOfNSClass(ObjCInterfaceDecl *InterfaceDecl, NSClassIdKindKind NSClassKind) const
Returns true if InterfaceDecl is subclass of NSClassKind.
Definition: NSAPI.cpp:546
clang::NSAPI::NSNumberWithDouble
@ NSNumberWithDouble
Definition: NSAPI.h:186
clang::NSAPI::NSDict_dictionary
@ NSDict_dictionary
Definition: NSAPI.h:97
clang::NSAPI::ClassId_NSValue
@ ClassId_NSValue
Definition: NSAPI.h:39
clang::NSAPI::NSNumberWithUnsignedInteger
@ NSNumberWithUnsignedInteger
Definition: NSAPI.h:189
clang::NSAPI::NSStr_initWithUTF8String
@ NSStr_initWithUTF8String
Definition: NSAPI.h:49
clang::NSAPI::NSStr_initWithString
@ NSStr_initWithString
Definition: NSAPI.h:48
clang::NSAPI::NSNumberWithLongLong
@ NSNumberWithLongLong
Definition: NSAPI.h:183
clang::NSAPI::NSDict_dictionaryWithObjectForKey
@ NSDict_dictionaryWithObjectForKey
Definition: NSAPI.h:99
clang::NSAPI::isNSUTF8StringEncodingConstant
bool isNSUTF8StringEncodingConstant(const Expr *E) const
Returns true if the expression.
Definition: NSAPI.h:60
clang::NSAPI::NSArr_initWithObjects
@ NSArr_initWithObjects
Definition: NSAPI.h:79
clang::NSAPI::NSMutableArr_replaceObjectAtIndex
@ NSMutableArr_replaceObjectAtIndex
Definition: NSAPI.h:81
clang::NSAPI::ClassId_NSObject
@ ClassId_NSObject
Definition: NSAPI.h:30
clang::NSAPI::NumNSDictionaryMethods
static const unsigned NumNSDictionaryMethods
Definition: NSAPI.h:111
Id
int Id
Definition: ASTDiff.cpp:191
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:212
clang::NSAPI::NumNSStringMethods
static const unsigned NumNSStringMethods
Definition: NSAPI.h:51
clang::NSAPI::NSDict_initWithObjectsForKeys
@ NSDict_initWithObjectsForKeys
Definition: NSAPI.h:105
clang::NSAPI
Definition: NSAPI.h:23
clang::NSAPI::NSNumberWithUnsignedShort
@ NSNumberWithUnsignedShort
Definition: NSAPI.h:178
IdentifierTable.h
clang::NSAPI::ClassId_NSString
@ ClassId_NSString
Definition: NSAPI.h:31
clang::NSAPI::NSNumberWithFloat
@ NSNumberWithFloat
Definition: NSAPI.h:185
clang::NSAPI::isObjCNSIntegerType
bool isObjCNSIntegerType(QualType T) const
Returns true if.
Definition: NSAPI.cpp:507
clang::NSAPI::NSArr_arrayWithArray
@ NSArr_arrayWithArray
Definition: NSAPI.h:74
clang::NSAPI::NSArr_initWithArray
@ NSArr_initWithArray
Definition: NSAPI.h:78
clang::NSAPI::NSStringMethodKind
NSStringMethodKind
Definition: NSAPI.h:43
clang::NSAPI::NSDict_dictionaryWithObjectsAndKeys
@ NSDict_dictionaryWithObjectsAndKeys
Definition: NSAPI.h:102
clang::NSAPI::NSNumberWithUnsignedInt
@ NSNumberWithUnsignedInt
Definition: NSAPI.h:180
clang::NSAPI::NSDict_dictionaryWithObjectsForKeysCount
@ NSDict_dictionaryWithObjectsForKeysCount
Definition: NSAPI.h:101
clang::NSAPI::NSMutableArr_addObject
@ NSMutableArr_addObject
Definition: NSAPI.h:82
clang::NSAPI::NSNumberWithLong
@ NSNumberWithLong
Definition: NSAPI.h:181
clang::NSAPI::ClassId_NSDictionary
@ ClassId_NSDictionary
Definition: NSAPI.h:34
clang::NSAPI::ClassId_NSMutableSet
@ ClassId_NSMutableSet
Definition: NSAPI.h:37
clang::NSAPI::getNSDictionarySelector
Selector getNSDictionarySelector(NSDictionaryMethodKind MK) const
The Objective-C NSDictionary selectors.
Definition: NSAPI.cpp:155
clang::NSAPI::NSNumberWithUnsignedLong
@ NSNumberWithUnsignedLong
Definition: NSAPI.h:182
clang::NSAPI::getObjectForKeyedSubscriptSelector
Selector getObjectForKeyedSubscriptSelector() const
Returns selector for "objectForKeyedSubscript:".
Definition: NSAPI.h:137
clang::NSAPI::NumNSNumberLiteralMethods
static const unsigned NumNSNumberLiteralMethods
Definition: NSAPI.h:191
clang::NSAPI::isNSASCIIStringEncodingConstant
bool isNSASCIIStringEncodingConstant(const Expr *E) const
Returns true if the expression.
Definition: NSAPI.h:66
clang::NSAPI::getSetObjectAtIndexedSubscriptSelector
Selector getSetObjectAtIndexedSubscriptSelector() const
Returns selector for "setObject:atIndexedSubscript".
Definition: NSAPI.h:155
clang::NSAPI::getObjectAtIndexedSubscriptSelector
Selector getObjectAtIndexedSubscriptSelector() const
Returns selector for "objectAtIndexedSubscript:".
Definition: NSAPI.h:143
clang::NSAPI::NSStr_stringWithUTF8String
@ NSStr_stringWithUTF8String
Definition: NSAPI.h:45
clang::NSAPI::getNSNumberFactoryMethodKind
Optional< NSNumberLiteralMethodKind > getNSNumberFactoryMethodKind(QualType T) const
Determine the appropriate NSNumber factory method kind for a literal of the given type.
Definition: NSAPI.cpp:378
clang::NSAPI::NSDict_dictionaryWithObjectsForKeys
@ NSDict_dictionaryWithObjectsForKeys
Definition: NSAPI.h:100
clang::NSAPI::getIsEqualSelector
Selector getIsEqualSelector() const
Returns selector for "isEqual:".
Definition: NSAPI.h:161
clang::NSAPI::NSDictionaryMethodKind
NSDictionaryMethodKind
Enumerates the NSDictionary/NSMutableDictionary methods used to generate literals and to apply some c...
Definition: NSAPI.h:96
clang::NSAPI::NSArr_arrayWithObjects
@ NSArr_arrayWithObjects
Definition: NSAPI.h:76
clang::NSAPI::NSNumberLiteralMethodKind
NSNumberLiteralMethodKind
Enumerates the NSNumber methods used to generate literals.
Definition: NSAPI.h:174
llvm::ArrayRef
Definition: LLVM.h:34
clang::NSAPI::NSNumberWithInt
@ NSNumberWithInt
Definition: NSAPI.h:179
clang::NSAPI::getNSNumberLiteralSelector
Selector getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK, bool Instance) const
The Objective-C NSNumber selectors used to create NSNumber literals.
Definition: NSAPI.cpp:314
clang::NSAPI::NSDict_dictionaryWithDictionary
@ NSDict_dictionaryWithDictionary
Definition: NSAPI.h:98
clang::NSAPI::getNSArrayMethodKind
Optional< NSArrayMethodKind > getNSArrayMethodKind(Selector Sel)
Return NSArrayMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:145
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:84
clang::NSAPI::NSMutableDict_setValueForKey
@ NSMutableDict_setValueForKey
Definition: NSAPI.h:109
clang::NSAPI::getSetObjectForKeyedSubscriptSelector
Selector getSetObjectForKeyedSubscriptSelector() const
Returns selector for "setObject:forKeyedSubscript".
Definition: NSAPI.h:149
clang::NSAPI::getNSSetMethodKind
Optional< NSSetMethodKind > getNSSetMethodKind(Selector Sel)
Return NSSetMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:304
clang::NSAPI::getNSStringSelector
Selector getNSStringSelector(NSStringMethodKind MK) const
The Objective-C NSString selectors.
Definition: NSAPI.cpp:42
clang::NSAPI::NSClassIdKindKind
NSClassIdKindKind
Definition: NSAPI.h:29
clang::NSAPI::NSNumberWithUnsignedChar
@ NSNumberWithUnsignedChar
Definition: NSAPI.h:176
clang::NSAPI::NSSetMethodKind
NSSetMethodKind
Enumerates the NSMutableSet/NSOrderedSet methods used to apply some checks.
Definition: NSAPI.h:121
clang::NSAPI::NSDict_initWithDictionary
@ NSDict_initWithDictionary
Definition: NSAPI.h:103
clang
Definition: CalledOnceCheck.h:17
clang::Selector
Smart pointer class that efficiently represents Objective-C method names.
Definition: IdentifierTable.h:748
clang::NSAPI::NSMutableDict_setObjectForKeyedSubscript
@ NSMutableDict_setObjectForKeyedSubscript
Definition: NSAPI.h:108
clang::NSAPI::NSMutableArr_setObjectAtIndexedSubscript
@ NSMutableArr_setObjectAtIndexedSubscript
Definition: NSAPI.h:84
clang::NSAPI::ClassId_NSArray
@ ClassId_NSArray
Definition: NSAPI.h:32
clang::NSAPI::getNewSelector
Selector getNewSelector() const
Definition: NSAPI.h:165
clang::NSAPI::NSMutableArr_insertObjectAtIndex
@ NSMutableArr_insertObjectAtIndex
Definition: NSAPI.h:83
clang::NSAPI::getNSDictionaryMethodKind
Optional< NSDictionaryMethodKind > getNSDictionaryMethodKind(Selector Sel)
Return NSDictionaryMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:247
clang::NSAPI::ClassId_NSMutableDictionary
@ ClassId_NSMutableDictionary
Definition: NSAPI.h:35
clang::NSAPI::NSNumberWithChar
@ NSNumberWithChar
Definition: NSAPI.h:175
clang::NSAPI::NSNumberWithBool
@ NSNumberWithBool
Definition: NSAPI.h:187
clang::NSAPI::NSArr_arrayWithObjectsCount
@ NSArr_arrayWithObjectsCount
Definition: NSAPI.h:77
clang::NSAPI::NSMutableSet_addObject
@ NSMutableSet_addObject
Definition: NSAPI.h:122
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::transformer::name
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr, CxxCtorInitializer, and TypeLoc) selects th...
Definition: RangeSelector.cpp:200
clang::NSAPI::GetNSIntegralKind
StringRef GetNSIntegralKind(QualType T) const
Returns one of NSIntegral typedef names if.
Definition: NSAPI.cpp:515
clang::NSAPI::isObjCNSUIntegerType
bool isObjCNSUIntegerType(QualType T) const
Returns true if.
Definition: NSAPI.cpp:511
clang::NSAPI::isMacroDefined
bool isMacroDefined(StringRef Id) const
Returns true if Id is currently defined as a macro.
Definition: NSAPI.cpp:541
clang::NSAPI::NSArr_arrayWithObject
@ NSArr_arrayWithObject
Definition: NSAPI.h:75