clang: lib/AST/NSAPI.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
13#include "llvm/ADT/StringSwitch.h"
14#include
15
16using namespace clang;
17
19 : Ctx(ctx), ClassIds(), BOOLId(nullptr), NSIntegerId(nullptr),
20 NSUIntegerId(nullptr), NSASCIIStringEncodingId(nullptr),
21 NSUTF8StringEncodingId(nullptr) {}
22
24 static const char *ClassName[NumClassIds] = {
25 "NSObject",
26 "NSString",
27 "NSArray",
28 "NSMutableArray",
29 "NSDictionary",
30 "NSMutableDictionary",
31 "NSNumber",
32 "NSMutableSet",
33 "NSMutableOrderedSet",
34 "NSValue"
35 };
36
37 if (!ClassIds[K])
38 return (ClassIds[K] = &Ctx.Idents.get(ClassName[K]));
39
40 return ClassIds[K];
41}
42
44 if (NSStringSelectors[MK].isNull()) {
46 switch (MK) {
49 break;
52 &Ctx.Idents.get("stringWithUTF8String"));
53 break;
56 &Ctx.Idents.get("initWithUTF8String"));
57 break;
62 break;
63 }
66 break;
69 break;
70 }
71 return (NSStringSelectors[MK] = Sel);
72 }
73
74 return NSStringSelectors[MK];
75}
76
78 if (NSArraySelectors[MK].isNull()) {
80 switch (MK) {
83 break;
86 break;
89 break;
92 break;
97 break;
98 }
101 break;
104 break;
107 break;
110 &Ctx.Idents.get("replaceObjectAtIndex"),
113 break;
114 }
117 break;
122 break;
123 }
126 &Ctx.Idents.get("setObject"), &Ctx.Idents.get("atIndexedSubscript")};
128 break;
129 }
130 }
131 return (NSArraySelectors[MK] = Sel);
132 }
133
134 return NSArraySelectors[MK];
135}
136
137std::optionalNSAPI::NSArrayMethodKind
142 return MK;
143 }
144
145 return std::nullopt;
146}
147
150 if (NSDictionarySelectors[MK].isNull()) {
152 switch (MK) {
155 break;
158 &Ctx.Idents.get("dictionaryWithDictionary"));
159 break;
164 break;
165 }
168 &Ctx.Idents.get("dictionaryWithObjects"), &Ctx.Idents.get("forKeys")};
170 break;
171 }
174 &Ctx.Idents.get("dictionaryWithObjects"), &Ctx.Idents.get("forKeys"),
177 break;
178 }
181 &Ctx.Idents.get("dictionaryWithObjectsAndKeys"));
182 break;
185 &Ctx.Idents.get("initWithDictionary"));
186 break;
189 &Ctx.Idents.get("initWithObjectsAndKeys"));
190 break;
195 break;
196 }
199 break;
204 break;
205 }
210 break;
211 }
216 break;
217 }
218 }
219 return (NSDictionarySelectors[MK] = Sel);
220 }
221
222 return NSDictionarySelectors[MK];
223}
224
225std::optionalNSAPI::NSDictionaryMethodKind
230 return MK;
231 }
232
233 return std::nullopt;
234}
235
237 if (NSSetSelectors[MK].isNull()) {
239 switch (MK) {
242 break;
247 break;
248 }
253 break;
254 }
257 &Ctx.Idents.get("setObject"), &Ctx.Idents.get("atIndexedSubscript")};
259 break;
260 }
263 &Ctx.Idents.get("replaceObjectAtIndex"),
266 break;
267 }
268 }
269 return (NSSetSelectors[MK] = Sel);
270 }
271
272 return NSSetSelectors[MK];
273}
274
279 return MK;
280 }
281
282 return std::nullopt;
283}
284
286 bool Instance) const {
288 "numberWithChar",
289 "numberWithUnsignedChar",
290 "numberWithShort",
291 "numberWithUnsignedShort",
292 "numberWithInt",
293 "numberWithUnsignedInt",
294 "numberWithLong",
295 "numberWithUnsignedLong",
296 "numberWithLongLong",
297 "numberWithUnsignedLongLong",
298 "numberWithFloat",
299 "numberWithDouble",
300 "numberWithBool",
301 "numberWithInteger",
302 "numberWithUnsignedInteger"
303 };
305 "initWithChar",
306 "initWithUnsignedChar",
307 "initWithShort",
308 "initWithUnsignedShort",
309 "initWithInt",
310 "initWithUnsignedInt",
311 "initWithLong",
312 "initWithUnsignedLong",
313 "initWithLongLong",
314 "initWithUnsignedLongLong",
315 "initWithFloat",
316 "initWithDouble",
317 "initWithBool",
318 "initWithInteger",
319 "initWithUnsignedInteger"
320 };
321
323 const char **Names;
324 if (Instance) {
325 Sels = NSNumberInstanceSelectors;
326 Names = InstanceSelectorName;
327 } else {
328 Sels = NSNumberClassSelectors;
329 Names = ClassSelectorName;
330 }
331
332 if (Sels[MK].isNull())
334 return Sels[MK];
335}
336
337std::optionalNSAPI::NSNumberLiteralMethodKind
342 return MK;
343 }
344
345 return std::nullopt;
346}
347
348std::optionalNSAPI::NSNumberLiteralMethodKind
351 if (!BT)
352 return std::nullopt;
353
355 if (TDT) {
363 }
364
366 case BuiltinType::Char_S:
367 case BuiltinType::SChar:
369 case BuiltinType::Char_U:
370 case BuiltinType::UChar:
372 case BuiltinType::Short:
374 case BuiltinType::UShort:
376 case BuiltinType::Int:
378 case BuiltinType::UInt:
380 case BuiltinType::Long:
382 case BuiltinType::ULong:
384 case BuiltinType::LongLong:
386 case BuiltinType::ULongLong:
388 case BuiltinType::Float:
390 case BuiltinType::Double:
392 case BuiltinType::Bool:
394
395 case BuiltinType::Void:
396 case BuiltinType::WChar_U:
397 case BuiltinType::WChar_S:
398 case BuiltinType::Char8:
399 case BuiltinType::Char16:
400 case BuiltinType::Char32:
401 case BuiltinType::Int128:
402 case BuiltinType::LongDouble:
403 case BuiltinType::ShortAccum:
404 case BuiltinType::Accum:
405 case BuiltinType::LongAccum:
406 case BuiltinType::UShortAccum:
407 case BuiltinType::UAccum:
408 case BuiltinType::ULongAccum:
409 case BuiltinType::ShortFract:
410 case BuiltinType::Fract:
411 case BuiltinType::LongFract:
412 case BuiltinType::UShortFract:
413 case BuiltinType::UFract:
414 case BuiltinType::ULongFract:
415 case BuiltinType::SatShortAccum:
416 case BuiltinType::SatAccum:
417 case BuiltinType::SatLongAccum:
418 case BuiltinType::SatUShortAccum:
419 case BuiltinType::SatUAccum:
420 case BuiltinType::SatULongAccum:
421 case BuiltinType::SatShortFract:
422 case BuiltinType::SatFract:
423 case BuiltinType::SatLongFract:
424 case BuiltinType::SatUShortFract:
425 case BuiltinType::SatUFract:
426 case BuiltinType::SatULongFract:
427 case BuiltinType::UInt128:
428 case BuiltinType::Float16:
429 case BuiltinType::Float128:
430 case BuiltinType::Ibm128:
431 case BuiltinType::NullPtr:
432 case BuiltinType::ObjCClass:
433 case BuiltinType::ObjCId:
434 case BuiltinType::ObjCSel:
435#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
436 case BuiltinType::Id:
437#include "clang/Basic/OpenCLImageTypes.def"
438#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
439 case BuiltinType::Id:
440#include "clang/Basic/OpenCLExtensionTypes.def"
441 case BuiltinType::OCLSampler:
442 case BuiltinType::OCLEvent:
443 case BuiltinType::OCLClkEvent:
444 case BuiltinType::OCLQueue:
445 case BuiltinType::OCLReserveID:
446#define SVE_TYPE(Name, Id, SingletonId) \
447 case BuiltinType::Id:
448#include "clang/Basic/AArch64SVEACLETypes.def"
449#define PPC_VECTOR_TYPE(Name, Id, Size) \
450 case BuiltinType::Id:
451#include "clang/Basic/PPCTypes.def"
452#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
453#include "clang/Basic/RISCVVTypes.def"
454#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
455#include "clang/Basic/WebAssemblyReferenceTypes.def"
456#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:
457#include "clang/Basic/AMDGPUTypes.def"
458#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
459#include "clang/Basic/HLSLIntangibleTypes.def"
460 case BuiltinType::BoundMember:
461 case BuiltinType::UnresolvedTemplate:
462 case BuiltinType::Dependent:
463 case BuiltinType::Overload:
464 case BuiltinType::UnknownAny:
465 case BuiltinType::ARCUnbridgedCast:
466 case BuiltinType::Half:
467 case BuiltinType::PseudoObject:
468 case BuiltinType::BuiltinFn:
469 case BuiltinType::IncompleteMatrixIdx:
470 case BuiltinType::ArraySection:
471 case BuiltinType::OMPArrayShaping:
472 case BuiltinType::OMPIterator:
473 case BuiltinType::BFloat16:
474 break;
475 }
476
477 return std::nullopt;
478}
479
480
482 return isObjCTypedef(T, "BOOL", BOOLId);
483}
484
486 return isObjCTypedef(T, "NSInteger", NSIntegerId);
487}
488
490 return isObjCTypedef(T, "NSUInteger", NSUIntegerId);
491}
492
495 return StringRef();
496
498 StringRef NSIntegralResust =
499 llvm::StringSwitch(
500 TDT->getDecl()->getDeclName().getAsIdentifierInfo()->getName())
501 .Case("int8_t", "int8_t")
502 .Case("int16_t", "int16_t")
503 .Case("int32_t", "int32_t")
504 .Case("NSInteger", "NSInteger")
505 .Case("int64_t", "int64_t")
506 .Case("uint8_t", "uint8_t")
507 .Case("uint16_t", "uint16_t")
508 .Case("uint32_t", "uint32_t")
509 .Case("NSUInteger", "NSUInteger")
510 .Case("uint64_t", "uint64_t")
511 .Default(StringRef());
512 if (!NSIntegralResust.empty())
513 return NSIntegralResust;
515 }
516 return StringRef();
517}
518
520
522}
523
526 if (!InterfaceDecl) {
527 return false;
528 }
529
531
532 bool IsSubclass = false;
533 do {
534 IsSubclass = NSClassID == InterfaceDecl->getIdentifier();
535
536 if (IsSubclass) {
537 break;
538 }
539 } while ((InterfaceDecl = InterfaceDecl->getSuperClass()));
540
541 return IsSubclass;
542}
543
544bool NSAPI::isObjCTypedef(QualType T,
547 return false;
548 if (T.isNull())
549 return false;
550
551 if (!II)
553
555 if (TDT->getDecl()->getDeclName().getAsIdentifierInfo() == II)
556 return true;
558 }
559
560 return false;
561}
562
563bool NSAPI::isObjCEnumerator(const Expr *E,
566 return false;
567 if ()
568 return false;
569
570 if (!II)
572
575 EnumD = dyn_cast_or_null(DRE->getDecl()))
576 return EnumD->getIdentifier() == II;
577
578 return false;
579}
580
586 I = Ids.begin(), E = Ids.end(); I != E; ++I)
587 Idents.push_back(&Ctx.Idents.get(*I));
589 }
590 return Sel;
591}
592
593Selector NSAPI::getOrInitNullarySelector(StringRef Id, Selector &Sel) const {
597 }
598 return Sel;
599}
Defines the clang::ASTContext interface.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
const LangOptions & getLangOpts() const
SelectorTable & Selectors
This class is used for builtin types like 'int'.
A reference to a declared variable, function, enum, etc.
An instance of this object exists for each enum constant that is defined.
This represents one expression.
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
One of these records is kept for each identifier that is lexed.
bool hasMacroDefinition() const
Return true if this identifier is #defined to some other value.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
std::optional< NSNumberLiteralMethodKind > getNSNumberLiteralMethodKind(Selector Sel) const
Return NSNumberLiteralMethodKind if Sel is such a selector.
bool isObjCNSIntegerType(QualType T) const
Returns true if.
@ NSStr_initWithUTF8String
@ NSStr_stringWithCString
@ NSStr_stringWithCStringEncoding
@ NSStr_stringWithUTF8String
Selector getNSDictionarySelector(NSDictionaryMethodKind MK) const
The Objective-C NSDictionary selectors.
static const unsigned NumNSArrayMethods
Selector getNSNumberLiteralSelector(NSNumberLiteralMethodKind MK, bool Instance) const
The Objective-C NSNumber selectors used to create NSNumber literals.
bool isMacroDefined(StringRef Id) const
Returns true if Id is currently defined as a macro.
std::optional< NSNumberLiteralMethodKind > getNSNumberFactoryMethodKind(QualType T) const
Determine the appropriate NSNumber factory method kind for a literal of the given type.
std::optional< NSDictionaryMethodKind > getNSDictionaryMethodKind(Selector Sel)
Return NSDictionaryMethodKind if Sel is such a selector.
static const unsigned NumClassIds
NSSetMethodKind
Enumerates the NSMutableSet/NSOrderedSet methods used to apply some checks.
@ NSOrderedSet_setObjectAtIndex
@ NSOrderedSet_replaceObjectAtIndexWithObject
@ NSOrderedSet_setObjectAtIndexedSubscript
@ NSOrderedSet_insertObjectAtIndex
NSDictionaryMethodKind
Enumerates the NSDictionary/NSMutableDictionary methods used to generate literals and to apply some c...
@ NSMutableDict_setValueForKey
@ NSDict_dictionaryWithObjectsForKeys
@ NSDict_dictionaryWithDictionary
@ NSMutableDict_setObjectForKey
@ NSDict_initWithObjectsForKeys
@ NSDict_dictionaryWithObjectForKey
@ NSDict_initWithDictionary
@ NSDict_dictionaryWithObjectsForKeysCount
@ NSDict_initWithObjectsAndKeys
@ NSMutableDict_setObjectForKeyedSubscript
@ NSDict_dictionaryWithObjectsAndKeys
Selector getNSArraySelector(NSArrayMethodKind MK) const
The Objective-C NSArray selectors.
std::optional< NSArrayMethodKind > getNSArrayMethodKind(Selector Sel)
Return NSArrayMethodKind if Sel is such a selector.
NSArrayMethodKind
Enumerates the NSArray/NSMutableArray methods used to generate literals and to apply some checks.
@ NSMutableArr_setObjectAtIndexedSubscript
@ NSMutableArr_insertObjectAtIndex
@ NSArr_arrayWithObjectsCount
@ NSMutableArr_replaceObjectAtIndex
bool isSubclassOfNSClass(ObjCInterfaceDecl *InterfaceDecl, NSClassIdKindKind NSClassKind) const
Returns true if InterfaceDecl is subclass of NSClassKind.
std::optional< NSSetMethodKind > getNSSetMethodKind(Selector Sel)
Return NSSetMethodKind if Sel is such a selector.
bool isNSNumberLiteralSelector(NSNumberLiteralMethodKind MK, Selector Sel) const
static const unsigned NumNSNumberLiteralMethods
bool isObjCNSUIntegerType(QualType T) const
Returns true if.
NSNumberLiteralMethodKind
Enumerates the NSNumber methods used to generate literals.
@ NSNumberWithUnsignedChar
@ NSNumberWithUnsignedLongLong
@ NSNumberWithUnsignedInt
@ NSNumberWithUnsignedLong
@ NSNumberWithUnsignedInteger
@ NSNumberWithUnsignedShort
Selector getNSStringSelector(NSStringMethodKind MK) const
The Objective-C NSString selectors.
static const unsigned NumNSDictionaryMethods
bool isObjCBOOLType(QualType T) const
Returns true if.
static const unsigned NumNSSetMethods
StringRef GetNSIntegralKind(QualType T) const
Returns one of NSIntegral typedef names if.
Selector getNSSetSelector(NSSetMethodKind MK) const
The Objective-C NSSet selectors.
IdentifierInfo * getNSClassId(NSClassIdKindKind K) const
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Represents an ObjC class declaration.
ObjCInterfaceDecl * getSuperClass() const
A (possibly-)qualified type.
Selector getNullarySelector(const IdentifierInfo *ID)
Selector getSelector(unsigned NumArgs, const IdentifierInfo **IIV)
Can create any sort of selector.
Selector getUnarySelector(const IdentifierInfo *ID)
Smart pointer class that efficiently represents Objective-C method names.
bool isNull() const
Determine whether this is the empty selector.
const T * getAs() const
Member-template getAs'.
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T