clang  10.0.0svn
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
Selector getObjectAtIndexedSubscriptSelector() const
Returns selector for "objectAtIndexedSubscript:".
Definition: NSAPI.h:143
Optional< NSNumberLiteralMethodKind > getNSNumberFactoryMethodKind(QualType T) const
Determine the appropriate NSNumber factory method kind for a literal of the given type...
Definition: NSAPI.cpp:378
Smart pointer class that efficiently represents Objective-C method names.
A (possibly-)qualified type.
Definition: Type.h:643
Selector getNewSelector() const
Definition: NSAPI.h:165
Optional< NSDictionaryMethodKind > getNSDictionaryMethodKind(Selector Sel)
Return NSDictionaryMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:247
NSNumberLiteralMethodKind
Enumerates the NSNumber methods used to generate literals.
Definition: NSAPI.h:174
Selector getIsEqualSelector() const
Returns selector for "isEqual:".
Definition: NSAPI.h:161
NSDictionaryMethodKind
Enumerates the NSDictionary/NSMutableDictionary methods used to generate literals and to apply some c...
Definition: NSAPI.h:96
One of these records is kept for each identifier that is lexed.
Selector getSetObjectForKeyedSubscriptSelector() const
Returns selector for "setObject:forKeyedSubscript".
Definition: NSAPI.h:149
Optional< NSSetMethodKind > getNSSetMethodKind(Selector Sel)
Return NSSetMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:304
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:160
Selector getNSStringSelector(NSStringMethodKind MK) const
The Objective-C NSString selectors.
Definition: NSAPI.cpp:42
Selector getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK, bool Instance) const
The Objective-C NSNumber selectors used to create NSNumber literals.
Definition: NSAPI.cpp:314
Optional< NSNumberLiteralMethodKind > getNSNumberLiteralMethodKind(Selector Sel) const
Return NSNumberLiteralMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:367
static const unsigned NumClassIds
Definition: NSAPI.h:41
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr or CxxCtorInitializer) selects the name&#39;s to...
static const unsigned NumNSNumberLiteralMethods
Definition: NSAPI.h:191
NSSetMethodKind
Enumerates the NSMutableSet/NSOrderedSet methods used to apply some checks.
Definition: NSAPI.h:121
NSAPI(ASTContext &Ctx)
Definition: NSAPI.cpp:17
Represents an ObjC class declaration.
Definition: DeclObjC.h:1171
static const unsigned NumNSSetMethods
Definition: NSAPI.h:128
NSClassIdKindKind
Definition: NSAPI.h:29
NSArrayMethodKind
Enumerates the NSArray/NSMutableArray methods used to generate literals and to apply some checks...
Definition: NSAPI.h:72
This represents one expression.
Definition: Expr.h:108
Selector getNSArraySelector(NSArrayMethodKind MK) const
The Objective-C NSArray selectors.
Definition: NSAPI.cpp:78
int Id
Definition: ASTDiff.cpp:190
ASTContext & getASTContext() const
Definition: NSAPI.h:27
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
StringRef GetNSIntegralKind(QualType T) const
Returns one of NSIntegral typedef names if.
Definition: NSAPI.cpp:505
bool isNSUTF8StringEncodingConstant(const Expr *E) const
Returns true if the expression.
Definition: NSAPI.h:60
bool isObjCBOOLType(QualType T) const
Returns true if.
Definition: NSAPI.cpp:493
bool isObjCNSUIntegerType(QualType T) const
Returns true if.
Definition: NSAPI.cpp:501
Selector getNSSetSelector(NSSetMethodKind MK) const
The Objective-C NSSet selectors.
Definition: NSAPI.cpp:257
bool isObjCNSIntegerType(QualType T) const
Returns true if.
Definition: NSAPI.cpp:497
bool isSubclassOfNSClass(ObjCInterfaceDecl *InterfaceDecl, NSClassIdKindKind NSClassKind) const
Returns true if InterfaceDecl is subclass of NSClassKind.
Definition: NSAPI.cpp:536
IdentifierInfo * getNSClassId(NSClassIdKindKind K) const
Definition: NSAPI.cpp:22
NSStringMethodKind
Definition: NSAPI.h:43
Selector getInitSelector() const
Definition: NSAPI.h:169
Dataflow Directional Tag Classes.
bool isMacroDefined(StringRef Id) const
Returns true if Id is currently defined as a macro.
Definition: NSAPI.cpp:531
bool isNSNumberLiteralSelector(NSNumberLiteralMethodKind MK, Selector Sel) const
Definition: NSAPI.h:199
static const unsigned NumNSArrayMethods
Definition: NSAPI.h:86
Selector getNSDictionarySelector(NSDictionaryMethodKind MK) const
The Objective-C NSDictionary selectors.
Definition: NSAPI.cpp:155
static const unsigned NumNSDictionaryMethods
Definition: NSAPI.h:111
Optional< NSArrayMethodKind > getNSArrayMethodKind(Selector Sel)
Return NSArrayMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:145
Selector getObjectForKeyedSubscriptSelector() const
Returns selector for "objectForKeyedSubscript:".
Definition: NSAPI.h:137
static const unsigned NumNSStringMethods
Definition: NSAPI.h:51
bool isNSASCIIStringEncodingConstant(const Expr *E) const
Returns true if the expression.
Definition: NSAPI.h:66
Selector getSetObjectAtIndexedSubscriptSelector() const
Returns selector for "setObject:atIndexedSubscript".
Definition: NSAPI.h:155