clang  9.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  /// Return NSStringMethodKind if \param Sel is such a selector.
60 
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);
65  }
66 
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);
71  }
72 
73  /// Enumerates the NSArray/NSMutableArray methods used to generate
74  /// literals and to apply some checks.
88  };
89  static const unsigned NumNSArrayMethods = 12;
90 
91  /// The Objective-C NSArray selectors.
93 
94  /// Return NSArrayMethodKind if \p Sel is such a selector.
96 
97  /// Enumerates the NSDictionary/NSMutableDictionary methods used
98  /// to generate literals and to apply some checks.
113  };
114  static const unsigned NumNSDictionaryMethods = 13;
115 
116  /// The Objective-C NSDictionary selectors.
118 
119  /// Return NSDictionaryMethodKind if \p Sel is such a selector.
121 
122  /// Enumerates the NSMutableSet/NSOrderedSet methods used
123  /// to apply some checks.
130  };
131  static const unsigned NumNSSetMethods = 5;
132 
133  /// The Objective-C NSSet selectors.
135 
136  /// Return NSSetMethodKind if \p Sel is such a selector.
138 
139  /// Returns selector for "objectForKeyedSubscript:".
141  return getOrInitSelector(StringRef("objectForKeyedSubscript"),
142  objectForKeyedSubscriptSel);
143  }
144 
145  /// Returns selector for "objectAtIndexedSubscript:".
147  return getOrInitSelector(StringRef("objectAtIndexedSubscript"),
148  objectAtIndexedSubscriptSel);
149  }
150 
151  /// Returns selector for "setObject:forKeyedSubscript".
153  StringRef Ids[] = { "setObject", "forKeyedSubscript" };
154  return getOrInitSelector(Ids, setObjectForKeyedSubscriptSel);
155  }
156 
157  /// Returns selector for "setObject:atIndexedSubscript".
159  StringRef Ids[] = { "setObject", "atIndexedSubscript" };
160  return getOrInitSelector(Ids, setObjectAtIndexedSubscriptSel);
161  }
162 
163  /// Returns selector for "isEqual:".
165  return getOrInitSelector(StringRef("isEqual"), isEqualSel);
166  }
167 
169  return getOrInitNullarySelector("new", NewSel);
170  }
171 
173  return getOrInitNullarySelector("init", InitSel);
174  }
175 
176  /// Enumerates the NSNumber methods used to generate literals.
193  };
194  static const unsigned NumNSNumberLiteralMethods = 15;
195 
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.
200  bool Instance) const;
201 
203  Selector Sel) const {
204  return Sel == getNSNumberLiteralSelector(MK, false) ||
205  Sel == getNSNumberLiteralSelector(MK, true);
206  }
207 
208  /// Return NSNumberLiteralMethodKind if \p Sel is such a selector.
211 
212  /// Determine the appropriate NSNumber factory method kind for a
213  /// literal of the given type.
216 
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;
226 
227  /// Returns \c true if \p Id is currently defined as a macro.
228  bool isMacroDefined(StringRef Id) const;
229 
230  /// Returns \c true if \p InterfaceDecl is subclass of \p NSClassKind
231  bool isSubclassOfNSClass(ObjCInterfaceDecl *InterfaceDecl,
232  NSClassIdKindKind NSClassKind) const;
233 
234 private:
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;
240 
241  ASTContext &Ctx;
242 
243  mutable IdentifierInfo *ClassIds[NumClassIds];
244 
245  mutable Selector NSStringSelectors[NumNSStringMethods];
246 
247  /// The selectors for Objective-C NSArray methods.
248  mutable Selector NSArraySelectors[NumNSArrayMethods];
249 
250  /// The selectors for Objective-C NSDictionary methods.
251  mutable Selector NSDictionarySelectors[NumNSDictionaryMethods];
252 
253  /// The selectors for Objective-C NSSet methods.
254  mutable Selector NSSetSelectors[NumNSSetMethods];
255 
256  /// The Objective-C NSNumber selectors used to create NSNumber literals.
257  mutable Selector NSNumberClassSelectors[NumNSNumberLiteralMethods];
258  mutable Selector NSNumberInstanceSelectors[NumNSNumberLiteralMethods];
259 
260  mutable Selector objectForKeyedSubscriptSel, objectAtIndexedSubscriptSel,
261  setObjectForKeyedSubscriptSel,setObjectAtIndexedSubscriptSel,
262  isEqualSel, InitSel, NewSel;
263 
264  mutable IdentifierInfo *BOOLId, *NSIntegerId, *NSUIntegerId;
265  mutable IdentifierInfo *NSASCIIStringEncodingId, *NSUTF8StringEncodingId;
266 };
267 
268 } // end namespace clang
269 
270 #endif // LLVM_CLANG_AST_NSAPI_H
Selector getObjectAtIndexedSubscriptSelector() const
Returns selector for "objectAtIndexedSubscript:".
Definition: NSAPI.h:146
Optional< NSNumberLiteralMethodKind > getNSNumberFactoryMethodKind(QualType T) const
Determine the appropriate NSNumber factory method kind for a literal of the given type...
Definition: NSAPI.cpp:389
Smart pointer class that efficiently represents Objective-C method names.
A (possibly-)qualified type.
Definition: Type.h:634
Selector getNewSelector() const
Definition: NSAPI.h:168
Optional< NSDictionaryMethodKind > getNSDictionaryMethodKind(Selector Sel)
Return NSDictionaryMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:258
NSNumberLiteralMethodKind
Enumerates the NSNumber methods used to generate literals.
Definition: NSAPI.h:177
Selector getIsEqualSelector() const
Returns selector for "isEqual:".
Definition: NSAPI.h:164
NSDictionaryMethodKind
Enumerates the NSDictionary/NSMutableDictionary methods used to generate literals and to apply some c...
Definition: NSAPI.h:99
One of these records is kept for each identifier that is lexed.
Selector getSetObjectForKeyedSubscriptSelector() const
Returns selector for "setObject:forKeyedSubscript".
Definition: NSAPI.h:152
Optional< NSSetMethodKind > getNSSetMethodKind(Selector Sel)
Return NSSetMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:315
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
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:325
Optional< NSNumberLiteralMethodKind > getNSNumberLiteralMethodKind(Selector Sel) const
Return NSNumberLiteralMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:378
static const unsigned NumClassIds
Definition: NSAPI.h:41
static const unsigned NumNSNumberLiteralMethods
Definition: NSAPI.h:194
NSSetMethodKind
Enumerates the NSMutableSet/NSOrderedSet methods used to apply some checks.
Definition: NSAPI.h:124
NSAPI(ASTContext &Ctx)
Definition: NSAPI.cpp:17
Represents an ObjC class declaration.
Definition: DeclObjC.h:1171
static const unsigned NumNSSetMethods
Definition: NSAPI.h:131
NSClassIdKindKind
Definition: NSAPI.h:29
NSArrayMethodKind
Enumerates the NSArray/NSMutableArray methods used to generate literals and to apply some checks...
Definition: NSAPI.h:75
This represents one expression.
Definition: Expr.h:106
Selector getNSArraySelector(NSArrayMethodKind MK) const
The Objective-C NSArray selectors.
Definition: NSAPI.cpp:89
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:513
bool isNSUTF8StringEncodingConstant(const Expr *E) const
Returns true if the expression.
Definition: NSAPI.h:63
bool isObjCBOOLType(QualType T) const
Returns true if.
Definition: NSAPI.cpp:501
bool isObjCNSUIntegerType(QualType T) const
Returns true if.
Definition: NSAPI.cpp:509
Selector getNSSetSelector(NSSetMethodKind MK) const
The Objective-C NSSet selectors.
Definition: NSAPI.cpp:268
bool isObjCNSIntegerType(QualType T) const
Returns true if.
Definition: NSAPI.cpp:505
bool isSubclassOfNSClass(ObjCInterfaceDecl *InterfaceDecl, NSClassIdKindKind NSClassKind) const
Returns true if InterfaceDecl is subclass of NSClassKind.
Definition: NSAPI.cpp:544
IdentifierInfo * getNSClassId(NSClassIdKindKind K) const
Definition: NSAPI.cpp:22
NSStringMethodKind
Definition: NSAPI.h:43
Selector getInitSelector() const
Definition: NSAPI.h:172
Optional< NSStringMethodKind > getNSStringMethodKind(Selector Sel) const
Return NSStringMethodKind if.
Definition: NSAPI.cpp:79
Dataflow Directional Tag Classes.
bool isMacroDefined(StringRef Id) const
Returns true if Id is currently defined as a macro.
Definition: NSAPI.cpp:539
bool isNSNumberLiteralSelector(NSNumberLiteralMethodKind MK, Selector Sel) const
Definition: NSAPI.h:202
static const unsigned NumNSArrayMethods
Definition: NSAPI.h:89
Selector getNSDictionarySelector(NSDictionaryMethodKind MK) const
The Objective-C NSDictionary selectors.
Definition: NSAPI.cpp:166
static const unsigned NumNSDictionaryMethods
Definition: NSAPI.h:114
Optional< NSArrayMethodKind > getNSArrayMethodKind(Selector Sel)
Return NSArrayMethodKind if Sel is such a selector.
Definition: NSAPI.cpp:156
Selector getObjectForKeyedSubscriptSelector() const
Returns selector for "objectForKeyedSubscript:".
Definition: NSAPI.h:140
static const unsigned NumNSStringMethods
Definition: NSAPI.h:51
bool isNSASCIIStringEncodingConstant(const Expr *E) const
Returns true if the expression.
Definition: NSAPI.h:69
Selector getSetObjectAtIndexedSubscriptSelector() const
Returns selector for "setObject:atIndexedSubscript".
Definition: NSAPI.h:158