1 //===- CXType.cpp - Implements 'CXTypes' aspect of libclang ---------------===// 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 // This file implements the 'CXTypes' API hooks in the Clang-C library. 10 // 11 //===--------------------------------------------------------------------===// 12 13 #include "CIndexer.h" 14 #include "CXCursor.h" 15 #include "CXString.h" 16 #include "CXTranslationUnit.h" 17 #include "CXType.h" 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/DeclObjC.h" 20 #include "clang/AST/DeclTemplate.h" 21 #include "clang/AST/Expr.h" 22 #include "clang/AST/Type.h" 23 #include "clang/Basic/AddressSpaces.h" 24 #include "clang/Frontend/ASTUnit.h" 25 26 using namespace clang; 27 28 static CXTypeKind GetBuiltinTypeKind(const BuiltinType *BT) { 29 #define BTCASE(K) case BuiltinType::K: return CXType_##K 30 switch (BT->getKind()) { 31 BTCASE(Void); 32 BTCASE(Bool); 33 BTCASE(Char_U); 34 BTCASE(UChar); 35 BTCASE(Char16); 36 BTCASE(Char32); 37 BTCASE(UShort); 38 BTCASE(UInt); 39 BTCASE(ULong); 40 BTCASE(ULongLong); 41 BTCASE(UInt128); 42 BTCASE(Char_S); 43 BTCASE(SChar); 44 case BuiltinType::WChar_S: return CXType_WChar; 45 case BuiltinType::WChar_U: return CXType_WChar; 46 BTCASE(Short); 47 BTCASE(Int); 48 BTCASE(Long); 49 BTCASE(LongLong); 50 BTCASE(Int128); 51 BTCASE(Half); 52 BTCASE(Float); 53 BTCASE(Double); 54 BTCASE(LongDouble); 55 BTCASE(ShortAccum); 56 BTCASE(Accum); 57 BTCASE(LongAccum); 58 BTCASE(UShortAccum); 59 BTCASE(UAccum); 60 BTCASE(ULongAccum); 61 BTCASE(Float16); 62 BTCASE(Float128); 63 BTCASE(Ibm128); 64 BTCASE(NullPtr); 65 BTCASE(Overload); 66 BTCASE(Dependent); 67 BTCASE(ObjCId); 68 BTCASE(ObjCClass); 69 BTCASE(ObjCSel); 70 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) BTCASE(Id); 71 #include "clang/Basic/OpenCLImageTypes.def" 72 #undef IMAGE_TYPE 73 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) BTCASE(Id); 74 #include "clang/Basic/OpenCLExtensionTypes.def" 75 BTCASE(OCLSampler); 76 BTCASE(OCLEvent); 77 BTCASE(OCLQueue); 78 BTCASE(OCLReserveID); 79 default: 80 return CXType_Unexposed; 81 } 82 #undef BTCASE 83 } 84 85 static CXTypeKind GetTypeKind(QualType T) { 86 const Type *TP = T.getTypePtrOrNull(); 87 if (!TP) 88 return CXType_Invalid; 89 90 #define TKCASE(K) case Type::K: return CXType_##K 91 switch (TP->getTypeClass()) { 92 case Type::Builtin: 93 return GetBuiltinTypeKind(cast<BuiltinType>(TP)); 94 TKCASE(Complex); 95 TKCASE(Pointer); 96 TKCASE(BlockPointer); 97 TKCASE(LValueReference); 98 TKCASE(RValueReference); 99 TKCASE(Record); 100 TKCASE(Enum); 101 TKCASE(Typedef); 102 TKCASE(ObjCInterface); 103 TKCASE(ObjCObject); 104 TKCASE(ObjCObjectPointer); 105 TKCASE(ObjCTypeParam); 106 TKCASE(FunctionNoProto); 107 TKCASE(FunctionProto); 108 TKCASE(ConstantArray); 109 TKCASE(IncompleteArray); 110 TKCASE(VariableArray); 111 TKCASE(DependentSizedArray); 112 TKCASE(Vector); 113 TKCASE(ExtVector); 114 TKCASE(MemberPointer); 115 TKCASE(Auto); 116 TKCASE(Elaborated); 117 TKCASE(Pipe); 118 TKCASE(Attributed); 119 TKCASE(BTFTagAttributed); 120 TKCASE(Atomic); 121 default: 122 return CXType_Unexposed; 123 } 124 #undef TKCASE 125 } 126 127 128 CXType cxtype::MakeCXType(QualType T, CXTranslationUnit TU) { 129 CXTypeKind TK = CXType_Invalid; 130 131 if (TU && !T.isNull()) { 132 // Handle attributed types as the original type 133 if (auto *ATT = T->getAs<AttributedType>()) { 134 if (!(TU->ParsingOptions & CXTranslationUnit_IncludeAttributedTypes)) { 135 // Return the equivalent type which represents the canonically 136 // equivalent type. 137 return MakeCXType(ATT->getEquivalentType(), TU); 138 } 139 } 140 if (auto *ATT = T->getAs<BTFTagAttributedType>()) { 141 if (!(TU->ParsingOptions & CXTranslationUnit_IncludeAttributedTypes)) 142 return MakeCXType(ATT->getWrappedType(), TU); 143 } 144 // Handle paren types as the original type 145 if (auto *PTT = T->getAs<ParenType>()) { 146 return MakeCXType(PTT->getInnerType(), TU); 147 } 148 149 ASTContext &Ctx = cxtu::getASTUnit(TU)->getASTContext(); 150 if (Ctx.getLangOpts().ObjC) { 151 QualType UnqualT = T.getUnqualifiedType(); 152 if (Ctx.isObjCIdType(UnqualT)) 153 TK = CXType_ObjCId; 154 else if (Ctx.isObjCClassType(UnqualT)) 155 TK = CXType_ObjCClass; 156 else if (Ctx.isObjCSelType(UnqualT)) 157 TK = CXType_ObjCSel; 158 } 159 160 /* Handle decayed types as the original type */ 161 if (const DecayedType *DT = T->getAs<DecayedType>()) { 162 return MakeCXType(DT->getOriginalType(), TU); 163 } 164 } 165 if (TK == CXType_Invalid) 166 TK = GetTypeKind(T); 167 168 CXType CT = { TK, { TK == CXType_Invalid ? nullptr 169 : T.getAsOpaquePtr(), TU } }; 170 return CT; 171 } 172 173 using cxtype::MakeCXType; 174 175 static inline QualType GetQualType(CXType CT) { 176 return QualType::getFromOpaquePtr(CT.data[0]); 177 } 178 179 static inline CXTranslationUnit GetTU(CXType CT) { 180 return static_cast<CXTranslationUnit>(CT.data[1]); 181 } 182 183 static Optional<ArrayRef<TemplateArgument>> 184 GetTemplateArguments(QualType Type) { 185 assert(!Type.isNull()); 186 if (const auto *Specialization = Type->getAs<TemplateSpecializationType>()) 187 return Specialization->template_arguments(); 188 189 if (const auto *RecordDecl = Type->getAsCXXRecordDecl()) { 190 const auto *TemplateDecl = 191 dyn_cast<ClassTemplateSpecializationDecl>(RecordDecl); 192 if (TemplateDecl) 193 return TemplateDecl->getTemplateArgs().asArray(); 194 } 195 196 return None; 197 } 198 199 static Optional<QualType> TemplateArgumentToQualType(const TemplateArgument &A) { 200 if (A.getKind() == TemplateArgument::Type) 201 return A.getAsType(); 202 return None; 203 } 204 205 static Optional<QualType> 206 FindTemplateArgumentTypeAt(ArrayRef<TemplateArgument> TA, unsigned index) { 207 unsigned current = 0; 208 for (const auto &A : TA) { 209 if (A.getKind() == TemplateArgument::Pack) { 210 if (index < current + A.pack_size()) 211 return TemplateArgumentToQualType(A.getPackAsArray()[index - current]); 212 current += A.pack_size(); 213 continue; 214 } 215 if (current == index) 216 return TemplateArgumentToQualType(A); 217 current++; 218 } 219 return None; 220 } 221 222 CXType clang_getCursorType(CXCursor C) { 223 using namespace cxcursor; 224 225 CXTranslationUnit TU = cxcursor::getCursorTU(C); 226 if (!TU) 227 return MakeCXType(QualType(), TU); 228 229 ASTContext &Context = cxtu::getASTUnit(TU)->getASTContext(); 230 if (clang_isExpression(C.kind)) { 231 QualType T = cxcursor::getCursorExpr(C)->getType(); 232 return MakeCXType(T, TU); 233 } 234 235 if (clang_isDeclaration(C.kind)) { 236 const Decl *D = cxcursor::getCursorDecl(C); 237 if (!D) 238 return MakeCXType(QualType(), TU); 239 240 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D)) 241 return MakeCXType(Context.getTypeDeclType(TD), TU); 242 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) 243 return MakeCXType(Context.getObjCInterfaceType(ID), TU); 244 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) 245 return MakeCXType(DD->getType(), TU); 246 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) 247 return MakeCXType(VD->getType(), TU); 248 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) 249 return MakeCXType(PD->getType(), TU); 250 if (const FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D)) 251 return MakeCXType(FTD->getTemplatedDecl()->getType(), TU); 252 return MakeCXType(QualType(), TU); 253 } 254 255 if (clang_isReference(C.kind)) { 256 switch (C.kind) { 257 case CXCursor_ObjCSuperClassRef: { 258 QualType T 259 = Context.getObjCInterfaceType(getCursorObjCSuperClassRef(C).first); 260 return MakeCXType(T, TU); 261 } 262 263 case CXCursor_ObjCClassRef: { 264 QualType T = Context.getObjCInterfaceType(getCursorObjCClassRef(C).first); 265 return MakeCXType(T, TU); 266 } 267 268 case CXCursor_TypeRef: { 269 QualType T = Context.getTypeDeclType(getCursorTypeRef(C).first); 270 return MakeCXType(T, TU); 271 272 } 273 274 case CXCursor_CXXBaseSpecifier: 275 return cxtype::MakeCXType(getCursorCXXBaseSpecifier(C)->getType(), TU); 276 277 case CXCursor_MemberRef: 278 return cxtype::MakeCXType(getCursorMemberRef(C).first->getType(), TU); 279 280 case CXCursor_VariableRef: 281 return cxtype::MakeCXType(getCursorVariableRef(C).first->getType(), TU); 282 283 case CXCursor_ObjCProtocolRef: 284 case CXCursor_TemplateRef: 285 case CXCursor_NamespaceRef: 286 case CXCursor_OverloadedDeclRef: 287 default: 288 break; 289 } 290 291 return MakeCXType(QualType(), TU); 292 } 293 294 return MakeCXType(QualType(), TU); 295 } 296 297 CXString clang_getTypeSpelling(CXType CT) { 298 QualType T = GetQualType(CT); 299 if (T.isNull()) 300 return cxstring::createEmpty(); 301 302 CXTranslationUnit TU = GetTU(CT); 303 SmallString<64> Str; 304 llvm::raw_svector_ostream OS(Str); 305 PrintingPolicy PP(cxtu::getASTUnit(TU)->getASTContext().getLangOpts()); 306 307 T.print(OS, PP); 308 309 return cxstring::createDup(OS.str()); 310 } 311 312 CXType clang_getTypedefDeclUnderlyingType(CXCursor C) { 313 using namespace cxcursor; 314 CXTranslationUnit TU = cxcursor::getCursorTU(C); 315 316 if (clang_isDeclaration(C.kind)) { 317 const Decl *D = cxcursor::getCursorDecl(C); 318 319 if (const TypedefNameDecl *TD = dyn_cast_or_null<TypedefNameDecl>(D)) { 320 QualType T = TD->getUnderlyingType(); 321 return MakeCXType(T, TU); 322 } 323 324 return MakeCXType(QualType(), TU); 325 } 326 327 return MakeCXType(QualType(), TU); 328 } 329 330 CXType clang_getEnumDeclIntegerType(CXCursor C) { 331 using namespace cxcursor; 332 CXTranslationUnit TU = cxcursor::getCursorTU(C); 333 334 if (clang_isDeclaration(C.kind)) { 335 const Decl *D = cxcursor::getCursorDecl(C); 336 337 if (const EnumDecl *TD = dyn_cast_or_null<EnumDecl>(D)) { 338 QualType T = TD->getIntegerType(); 339 return MakeCXType(T, TU); 340 } 341 342 return MakeCXType(QualType(), TU); 343 } 344 345 return MakeCXType(QualType(), TU); 346 } 347 348 long long clang_getEnumConstantDeclValue(CXCursor C) { 349 using namespace cxcursor; 350 351 if (clang_isDeclaration(C.kind)) { 352 const Decl *D = cxcursor::getCursorDecl(C); 353 354 if (const EnumConstantDecl *TD = dyn_cast_or_null<EnumConstantDecl>(D)) { 355 return TD->getInitVal().getSExtValue(); 356 } 357 358 return LLONG_MIN; 359 } 360 361 return LLONG_MIN; 362 } 363 364 unsigned long long clang_getEnumConstantDeclUnsignedValue(CXCursor C) { 365 using namespace cxcursor; 366 367 if (clang_isDeclaration(C.kind)) { 368 const Decl *D = cxcursor::getCursorDecl(C); 369 370 if (const EnumConstantDecl *TD = dyn_cast_or_null<EnumConstantDecl>(D)) { 371 return TD->getInitVal().getZExtValue(); 372 } 373 374 return ULLONG_MAX; 375 } 376 377 return ULLONG_MAX; 378 } 379 380 int clang_getFieldDeclBitWidth(CXCursor C) { 381 using namespace cxcursor; 382 383 if (clang_isDeclaration(C.kind)) { 384 const Decl *D = getCursorDecl(C); 385 386 if (const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D)) { 387 if (FD->isBitField()) 388 return FD->getBitWidthValue(getCursorContext(C)); 389 } 390 } 391 392 return -1; 393 } 394 395 CXType clang_getCanonicalType(CXType CT) { 396 if (CT.kind == CXType_Invalid) 397 return CT; 398 399 QualType T = GetQualType(CT); 400 CXTranslationUnit TU = GetTU(CT); 401 402 if (T.isNull()) 403 return MakeCXType(QualType(), GetTU(CT)); 404 405 return MakeCXType(cxtu::getASTUnit(TU)->getASTContext() 406 .getCanonicalType(T), 407 TU); 408 } 409 410 unsigned clang_isConstQualifiedType(CXType CT) { 411 QualType T = GetQualType(CT); 412 return T.isLocalConstQualified(); 413 } 414 415 unsigned clang_isVolatileQualifiedType(CXType CT) { 416 QualType T = GetQualType(CT); 417 return T.isLocalVolatileQualified(); 418 } 419 420 unsigned clang_isRestrictQualifiedType(CXType CT) { 421 QualType T = GetQualType(CT); 422 return T.isLocalRestrictQualified(); 423 } 424 425 unsigned clang_getAddressSpace(CXType CT) { 426 QualType T = GetQualType(CT); 427 428 // For non language-specific address space, use separate helper function. 429 if (T.getAddressSpace() >= LangAS::FirstTargetAddressSpace) { 430 return T.getQualifiers().getAddressSpaceAttributePrintValue(); 431 } 432 // FIXME: this function returns either a LangAS or a target AS 433 // Those values can overlap which makes this function rather unpredictable 434 // for any caller 435 return (unsigned)T.getAddressSpace(); 436 } 437 438 CXString clang_getTypedefName(CXType CT) { 439 QualType T = GetQualType(CT); 440 const TypedefType *TT = T->getAs<TypedefType>(); 441 if (TT) { 442 TypedefNameDecl *TD = TT->getDecl(); 443 if (TD) 444 return cxstring::createDup(TD->getNameAsString().c_str()); 445 } 446 return cxstring::createEmpty(); 447 } 448 449 CXType clang_getPointeeType(CXType CT) { 450 QualType T = GetQualType(CT); 451 const Type *TP = T.getTypePtrOrNull(); 452 453 if (!TP) 454 return MakeCXType(QualType(), GetTU(CT)); 455 456 try_again: 457 switch (TP->getTypeClass()) { 458 case Type::Pointer: 459 T = cast<PointerType>(TP)->getPointeeType(); 460 break; 461 case Type::BlockPointer: 462 T = cast<BlockPointerType>(TP)->getPointeeType(); 463 break; 464 case Type::LValueReference: 465 case Type::RValueReference: 466 T = cast<ReferenceType>(TP)->getPointeeType(); 467 break; 468 case Type::ObjCObjectPointer: 469 T = cast<ObjCObjectPointerType>(TP)->getPointeeType(); 470 break; 471 case Type::MemberPointer: 472 T = cast<MemberPointerType>(TP)->getPointeeType(); 473 break; 474 case Type::Auto: 475 case Type::DeducedTemplateSpecialization: 476 TP = cast<DeducedType>(TP)->getDeducedType().getTypePtrOrNull(); 477 if (TP) 478 goto try_again; 479 break; 480 default: 481 T = QualType(); 482 break; 483 } 484 return MakeCXType(T, GetTU(CT)); 485 } 486 487 CXCursor clang_getTypeDeclaration(CXType CT) { 488 if (CT.kind == CXType_Invalid) 489 return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound); 490 491 QualType T = GetQualType(CT); 492 const Type *TP = T.getTypePtrOrNull(); 493 494 if (!TP) 495 return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound); 496 497 Decl *D = nullptr; 498 499 try_again: 500 switch (TP->getTypeClass()) { 501 case Type::Typedef: 502 D = cast<TypedefType>(TP)->getDecl(); 503 break; 504 case Type::ObjCObject: 505 D = cast<ObjCObjectType>(TP)->getInterface(); 506 break; 507 case Type::ObjCInterface: 508 D = cast<ObjCInterfaceType>(TP)->getDecl(); 509 break; 510 case Type::Record: 511 case Type::Enum: 512 D = cast<TagType>(TP)->getDecl(); 513 break; 514 case Type::TemplateSpecialization: 515 if (const RecordType *Record = TP->getAs<RecordType>()) 516 D = Record->getDecl(); 517 else 518 D = cast<TemplateSpecializationType>(TP)->getTemplateName() 519 .getAsTemplateDecl(); 520 break; 521 522 case Type::Auto: 523 case Type::DeducedTemplateSpecialization: 524 TP = cast<DeducedType>(TP)->getDeducedType().getTypePtrOrNull(); 525 if (TP) 526 goto try_again; 527 break; 528 529 case Type::InjectedClassName: 530 D = cast<InjectedClassNameType>(TP)->getDecl(); 531 break; 532 533 // FIXME: Template type parameters! 534 535 case Type::Elaborated: 536 TP = cast<ElaboratedType>(TP)->getNamedType().getTypePtrOrNull(); 537 goto try_again; 538 539 default: 540 break; 541 } 542 543 if (!D) 544 return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound); 545 546 return cxcursor::MakeCXCursor(D, GetTU(CT)); 547 } 548 549 CXString clang_getTypeKindSpelling(enum CXTypeKind K) { 550 const char *s = nullptr; 551 #define TKIND(X) case CXType_##X: s = "" #X ""; break 552 switch (K) { 553 TKIND(Invalid); 554 TKIND(Unexposed); 555 TKIND(Void); 556 TKIND(Bool); 557 TKIND(Char_U); 558 TKIND(UChar); 559 TKIND(Char16); 560 TKIND(Char32); 561 TKIND(UShort); 562 TKIND(UInt); 563 TKIND(ULong); 564 TKIND(ULongLong); 565 TKIND(UInt128); 566 TKIND(Char_S); 567 TKIND(SChar); 568 case CXType_WChar: s = "WChar"; break; 569 TKIND(Short); 570 TKIND(Int); 571 TKIND(Long); 572 TKIND(LongLong); 573 TKIND(Int128); 574 TKIND(Half); 575 TKIND(Float); 576 TKIND(Double); 577 TKIND(LongDouble); 578 TKIND(ShortAccum); 579 TKIND(Accum); 580 TKIND(LongAccum); 581 TKIND(UShortAccum); 582 TKIND(UAccum); 583 TKIND(ULongAccum); 584 TKIND(Float16); 585 TKIND(Float128); 586 TKIND(Ibm128); 587 TKIND(NullPtr); 588 TKIND(Overload); 589 TKIND(Dependent); 590 TKIND(ObjCId); 591 TKIND(ObjCClass); 592 TKIND(ObjCSel); 593 TKIND(Complex); 594 TKIND(Pointer); 595 TKIND(BlockPointer); 596 TKIND(LValueReference); 597 TKIND(RValueReference); 598 TKIND(Record); 599 TKIND(Enum); 600 TKIND(Typedef); 601 TKIND(ObjCInterface); 602 TKIND(ObjCObject); 603 TKIND(ObjCObjectPointer); 604 TKIND(ObjCTypeParam); 605 TKIND(FunctionNoProto); 606 TKIND(FunctionProto); 607 TKIND(ConstantArray); 608 TKIND(IncompleteArray); 609 TKIND(VariableArray); 610 TKIND(DependentSizedArray); 611 TKIND(Vector); 612 TKIND(ExtVector); 613 TKIND(MemberPointer); 614 TKIND(Auto); 615 TKIND(Elaborated); 616 TKIND(Pipe); 617 TKIND(Attributed); 618 TKIND(BTFTagAttributed); 619 TKIND(BFloat16); 620 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) TKIND(Id); 621 #include "clang/Basic/OpenCLImageTypes.def" 622 #undef IMAGE_TYPE 623 #define EXT_OPAQUE_TYPE(ExtTYpe, Id, Ext) TKIND(Id); 624 #include "clang/Basic/OpenCLExtensionTypes.def" 625 TKIND(OCLSampler); 626 TKIND(OCLEvent); 627 TKIND(OCLQueue); 628 TKIND(OCLReserveID); 629 TKIND(Atomic); 630 } 631 #undef TKIND 632 return cxstring::createRef(s); 633 } 634 635 unsigned clang_equalTypes(CXType A, CXType B) { 636 return A.data[0] == B.data[0] && A.data[1] == B.data[1]; 637 } 638 639 unsigned clang_isFunctionTypeVariadic(CXType X) { 640 QualType T = GetQualType(X); 641 if (T.isNull()) 642 return 0; 643 644 if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>()) 645 return (unsigned)FD->isVariadic(); 646 647 if (T->getAs<FunctionNoProtoType>()) 648 return 1; 649 650 return 0; 651 } 652 653 CXCallingConv clang_getFunctionTypeCallingConv(CXType X) { 654 QualType T = GetQualType(X); 655 if (T.isNull()) 656 return CXCallingConv_Invalid; 657 658 if (const FunctionType *FD = T->getAs<FunctionType>()) { 659 #define TCALLINGCONV(X) case CC_##X: return CXCallingConv_##X 660 switch (FD->getCallConv()) { 661 TCALLINGCONV(C); 662 TCALLINGCONV(X86StdCall); 663 TCALLINGCONV(X86FastCall); 664 TCALLINGCONV(X86ThisCall); 665 TCALLINGCONV(X86Pascal); 666 TCALLINGCONV(X86RegCall); 667 TCALLINGCONV(X86VectorCall); 668 TCALLINGCONV(AArch64VectorCall); 669 TCALLINGCONV(Win64); 670 TCALLINGCONV(X86_64SysV); 671 TCALLINGCONV(AAPCS); 672 TCALLINGCONV(AAPCS_VFP); 673 TCALLINGCONV(IntelOclBicc); 674 TCALLINGCONV(Swift); 675 TCALLINGCONV(SwiftAsync); 676 TCALLINGCONV(PreserveMost); 677 TCALLINGCONV(PreserveAll); 678 case CC_SpirFunction: return CXCallingConv_Unexposed; 679 case CC_OpenCLKernel: return CXCallingConv_Unexposed; 680 break; 681 } 682 #undef TCALLINGCONV 683 } 684 685 return CXCallingConv_Invalid; 686 } 687 688 int clang_getNumArgTypes(CXType X) { 689 QualType T = GetQualType(X); 690 if (T.isNull()) 691 return -1; 692 693 if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>()) { 694 return FD->getNumParams(); 695 } 696 697 if (T->getAs<FunctionNoProtoType>()) { 698 return 0; 699 } 700 701 return -1; 702 } 703 704 CXType clang_getArgType(CXType X, unsigned i) { 705 QualType T = GetQualType(X); 706 if (T.isNull()) 707 return MakeCXType(QualType(), GetTU(X)); 708 709 if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>()) { 710 unsigned numParams = FD->getNumParams(); 711 if (i >= numParams) 712 return MakeCXType(QualType(), GetTU(X)); 713 714 return MakeCXType(FD->getParamType(i), GetTU(X)); 715 } 716 717 return MakeCXType(QualType(), GetTU(X)); 718 } 719 720 CXType clang_getResultType(CXType X) { 721 QualType T = GetQualType(X); 722 if (T.isNull()) 723 return MakeCXType(QualType(), GetTU(X)); 724 725 if (const FunctionType *FD = T->getAs<FunctionType>()) 726 return MakeCXType(FD->getReturnType(), GetTU(X)); 727 728 return MakeCXType(QualType(), GetTU(X)); 729 } 730 731 CXType clang_getCursorResultType(CXCursor C) { 732 if (clang_isDeclaration(C.kind)) { 733 const Decl *D = cxcursor::getCursorDecl(C); 734 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) 735 return MakeCXType(MD->getReturnType(), cxcursor::getCursorTU(C)); 736 737 return clang_getResultType(clang_getCursorType(C)); 738 } 739 740 return MakeCXType(QualType(), cxcursor::getCursorTU(C)); 741 } 742 743 // FIXME: We should expose the canThrow(...) result instead of the EST. 744 static CXCursor_ExceptionSpecificationKind 745 getExternalExceptionSpecificationKind(ExceptionSpecificationType EST) { 746 switch (EST) { 747 case EST_None: 748 return CXCursor_ExceptionSpecificationKind_None; 749 case EST_DynamicNone: 750 return CXCursor_ExceptionSpecificationKind_DynamicNone; 751 case EST_Dynamic: 752 return CXCursor_ExceptionSpecificationKind_Dynamic; 753 case EST_MSAny: 754 return CXCursor_ExceptionSpecificationKind_MSAny; 755 case EST_BasicNoexcept: 756 return CXCursor_ExceptionSpecificationKind_BasicNoexcept; 757 case EST_NoThrow: 758 return CXCursor_ExceptionSpecificationKind_NoThrow; 759 case EST_NoexceptFalse: 760 case EST_NoexceptTrue: 761 case EST_DependentNoexcept: 762 return CXCursor_ExceptionSpecificationKind_ComputedNoexcept; 763 case EST_Unevaluated: 764 return CXCursor_ExceptionSpecificationKind_Unevaluated; 765 case EST_Uninstantiated: 766 return CXCursor_ExceptionSpecificationKind_Uninstantiated; 767 case EST_Unparsed: 768 return CXCursor_ExceptionSpecificationKind_Unparsed; 769 } 770 llvm_unreachable("invalid EST value"); 771 } 772 773 int clang_getExceptionSpecificationType(CXType X) { 774 QualType T = GetQualType(X); 775 if (T.isNull()) 776 return -1; 777 778 if (const auto *FD = T->getAs<FunctionProtoType>()) 779 return getExternalExceptionSpecificationKind(FD->getExceptionSpecType()); 780 781 return -1; 782 } 783 784 int clang_getCursorExceptionSpecificationType(CXCursor C) { 785 if (clang_isDeclaration(C.kind)) 786 return clang_getExceptionSpecificationType(clang_getCursorType(C)); 787 788 return -1; 789 } 790 791 unsigned clang_isPODType(CXType X) { 792 QualType T = GetQualType(X); 793 if (T.isNull()) 794 return 0; 795 796 CXTranslationUnit TU = GetTU(X); 797 798 return T.isPODType(cxtu::getASTUnit(TU)->getASTContext()) ? 1 : 0; 799 } 800 801 CXType clang_getElementType(CXType CT) { 802 QualType ET = QualType(); 803 QualType T = GetQualType(CT); 804 const Type *TP = T.getTypePtrOrNull(); 805 806 if (TP) { 807 switch (TP->getTypeClass()) { 808 case Type::ConstantArray: 809 ET = cast<ConstantArrayType> (TP)->getElementType(); 810 break; 811 case Type::IncompleteArray: 812 ET = cast<IncompleteArrayType> (TP)->getElementType(); 813 break; 814 case Type::VariableArray: 815 ET = cast<VariableArrayType> (TP)->getElementType(); 816 break; 817 case Type::DependentSizedArray: 818 ET = cast<DependentSizedArrayType> (TP)->getElementType(); 819 break; 820 case Type::Vector: 821 ET = cast<VectorType> (TP)->getElementType(); 822 break; 823 case Type::ExtVector: 824 ET = cast<ExtVectorType>(TP)->getElementType(); 825 break; 826 case Type::Complex: 827 ET = cast<ComplexType> (TP)->getElementType(); 828 break; 829 default: 830 break; 831 } 832 } 833 return MakeCXType(ET, GetTU(CT)); 834 } 835 836 long long clang_getNumElements(CXType CT) { 837 long long result = -1; 838 QualType T = GetQualType(CT); 839 const Type *TP = T.getTypePtrOrNull(); 840 841 if (TP) { 842 switch (TP->getTypeClass()) { 843 case Type::ConstantArray: 844 result = cast<ConstantArrayType> (TP)->getSize().getSExtValue(); 845 break; 846 case Type::Vector: 847 result = cast<VectorType> (TP)->getNumElements(); 848 break; 849 case Type::ExtVector: 850 result = cast<ExtVectorType>(TP)->getNumElements(); 851 break; 852 default: 853 break; 854 } 855 } 856 return result; 857 } 858 859 CXType clang_getArrayElementType(CXType CT) { 860 QualType ET = QualType(); 861 QualType T = GetQualType(CT); 862 const Type *TP = T.getTypePtrOrNull(); 863 864 if (TP) { 865 switch (TP->getTypeClass()) { 866 case Type::ConstantArray: 867 ET = cast<ConstantArrayType> (TP)->getElementType(); 868 break; 869 case Type::IncompleteArray: 870 ET = cast<IncompleteArrayType> (TP)->getElementType(); 871 break; 872 case Type::VariableArray: 873 ET = cast<VariableArrayType> (TP)->getElementType(); 874 break; 875 case Type::DependentSizedArray: 876 ET = cast<DependentSizedArrayType> (TP)->getElementType(); 877 break; 878 default: 879 break; 880 } 881 } 882 return MakeCXType(ET, GetTU(CT)); 883 } 884 885 long long clang_getArraySize(CXType CT) { 886 long long result = -1; 887 QualType T = GetQualType(CT); 888 const Type *TP = T.getTypePtrOrNull(); 889 890 if (TP) { 891 switch (TP->getTypeClass()) { 892 case Type::ConstantArray: 893 result = cast<ConstantArrayType> (TP)->getSize().getSExtValue(); 894 break; 895 default: 896 break; 897 } 898 } 899 return result; 900 } 901 902 static bool isIncompleteTypeWithAlignment(QualType QT) { 903 return QT->isIncompleteArrayType() || !QT->isIncompleteType(); 904 } 905 906 long long clang_Type_getAlignOf(CXType T) { 907 if (T.kind == CXType_Invalid) 908 return CXTypeLayoutError_Invalid; 909 ASTContext &Ctx = cxtu::getASTUnit(GetTU(T))->getASTContext(); 910 QualType QT = GetQualType(T); 911 // [expr.alignof] p1: return size_t value for complete object type, reference 912 // or array. 913 // [expr.alignof] p3: if reference type, return size of referenced type 914 if (QT->isReferenceType()) 915 QT = QT.getNonReferenceType(); 916 if (!isIncompleteTypeWithAlignment(QT)) 917 return CXTypeLayoutError_Incomplete; 918 if (QT->isDependentType()) 919 return CXTypeLayoutError_Dependent; 920 if (const auto *Deduced = dyn_cast<DeducedType>(QT)) 921 if (Deduced->getDeducedType().isNull()) 922 return CXTypeLayoutError_Undeduced; 923 // Exceptions by GCC extension - see ASTContext.cpp:1313 getTypeInfoImpl 924 // if (QT->isFunctionType()) return 4; // Bug #15511 - should be 1 925 // if (QT->isVoidType()) return 1; 926 return Ctx.getTypeAlignInChars(QT).getQuantity(); 927 } 928 929 CXType clang_Type_getClassType(CXType CT) { 930 QualType ET = QualType(); 931 QualType T = GetQualType(CT); 932 const Type *TP = T.getTypePtrOrNull(); 933 934 if (TP && TP->getTypeClass() == Type::MemberPointer) { 935 ET = QualType(cast<MemberPointerType> (TP)->getClass(), 0); 936 } 937 return MakeCXType(ET, GetTU(CT)); 938 } 939 940 long long clang_Type_getSizeOf(CXType T) { 941 if (T.kind == CXType_Invalid) 942 return CXTypeLayoutError_Invalid; 943 ASTContext &Ctx = cxtu::getASTUnit(GetTU(T))->getASTContext(); 944 QualType QT = GetQualType(T); 945 // [expr.sizeof] p2: if reference type, return size of referenced type 946 if (QT->isReferenceType()) 947 QT = QT.getNonReferenceType(); 948 // [expr.sizeof] p1: return -1 on: func, incomplete, bitfield, incomplete 949 // enumeration 950 // Note: We get the cxtype, not the cxcursor, so we can't call 951 // FieldDecl->isBitField() 952 // [expr.sizeof] p3: pointer ok, function not ok. 953 // [gcc extension] lib/AST/ExprConstant.cpp:1372 HandleSizeof : vla == error 954 if (QT->isIncompleteType()) 955 return CXTypeLayoutError_Incomplete; 956 if (QT->isDependentType()) 957 return CXTypeLayoutError_Dependent; 958 if (!QT->isConstantSizeType()) 959 return CXTypeLayoutError_NotConstantSize; 960 if (const auto *Deduced = dyn_cast<DeducedType>(QT)) 961 if (Deduced->getDeducedType().isNull()) 962 return CXTypeLayoutError_Undeduced; 963 // [gcc extension] lib/AST/ExprConstant.cpp:1372 964 // HandleSizeof : {voidtype,functype} == 1 965 // not handled by ASTContext.cpp:1313 getTypeInfoImpl 966 if (QT->isVoidType() || QT->isFunctionType()) 967 return 1; 968 return Ctx.getTypeSizeInChars(QT).getQuantity(); 969 } 970 971 static bool isTypeIncompleteForLayout(QualType QT) { 972 return QT->isIncompleteType() && !QT->isIncompleteArrayType(); 973 } 974 975 static long long visitRecordForValidation(const RecordDecl *RD) { 976 for (const auto *I : RD->fields()){ 977 QualType FQT = I->getType(); 978 if (isTypeIncompleteForLayout(FQT)) 979 return CXTypeLayoutError_Incomplete; 980 if (FQT->isDependentType()) 981 return CXTypeLayoutError_Dependent; 982 // recurse 983 if (const RecordType *ChildType = I->getType()->getAs<RecordType>()) { 984 if (const RecordDecl *Child = ChildType->getDecl()) { 985 long long ret = visitRecordForValidation(Child); 986 if (ret < 0) 987 return ret; 988 } 989 } 990 // else try next field 991 } 992 return 0; 993 } 994 995 static long long validateFieldParentType(CXCursor PC, CXType PT){ 996 if (clang_isInvalid(PC.kind)) 997 return CXTypeLayoutError_Invalid; 998 const RecordDecl *RD = 999 dyn_cast_or_null<RecordDecl>(cxcursor::getCursorDecl(PC)); 1000 // validate parent declaration 1001 if (!RD || RD->isInvalidDecl()) 1002 return CXTypeLayoutError_Invalid; 1003 RD = RD->getDefinition(); 1004 if (!RD) 1005 return CXTypeLayoutError_Incomplete; 1006 if (RD->isInvalidDecl()) 1007 return CXTypeLayoutError_Invalid; 1008 // validate parent type 1009 QualType RT = GetQualType(PT); 1010 if (RT->isIncompleteType()) 1011 return CXTypeLayoutError_Incomplete; 1012 if (RT->isDependentType()) 1013 return CXTypeLayoutError_Dependent; 1014 // We recurse into all record fields to detect incomplete and dependent types. 1015 long long Error = visitRecordForValidation(RD); 1016 if (Error < 0) 1017 return Error; 1018 return 0; 1019 } 1020 1021 long long clang_Type_getOffsetOf(CXType PT, const char *S) { 1022 // check that PT is not incomplete/dependent 1023 CXCursor PC = clang_getTypeDeclaration(PT); 1024 long long Error = validateFieldParentType(PC,PT); 1025 if (Error < 0) 1026 return Error; 1027 if (!S) 1028 return CXTypeLayoutError_InvalidFieldName; 1029 // lookup field 1030 ASTContext &Ctx = cxtu::getASTUnit(GetTU(PT))->getASTContext(); 1031 IdentifierInfo *II = &Ctx.Idents.get(S); 1032 DeclarationName FieldName(II); 1033 const RecordDecl *RD = 1034 dyn_cast_or_null<RecordDecl>(cxcursor::getCursorDecl(PC)); 1035 // verified in validateFieldParentType 1036 RD = RD->getDefinition(); 1037 RecordDecl::lookup_result Res = RD->lookup(FieldName); 1038 // If a field of the parent record is incomplete, lookup will fail. 1039 // and we would return InvalidFieldName instead of Incomplete. 1040 // But this erroneous results does protects again a hidden assertion failure 1041 // in the RecordLayoutBuilder 1042 if (!Res.isSingleResult()) 1043 return CXTypeLayoutError_InvalidFieldName; 1044 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Res.front())) 1045 return Ctx.getFieldOffset(FD); 1046 if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(Res.front())) 1047 return Ctx.getFieldOffset(IFD); 1048 // we don't want any other Decl Type. 1049 return CXTypeLayoutError_InvalidFieldName; 1050 } 1051 1052 CXType clang_Type_getModifiedType(CXType CT) { 1053 QualType T = GetQualType(CT); 1054 if (T.isNull()) 1055 return MakeCXType(QualType(), GetTU(CT)); 1056 1057 if (auto *ATT = T->getAs<AttributedType>()) 1058 return MakeCXType(ATT->getModifiedType(), GetTU(CT)); 1059 1060 if (auto *ATT = T->getAs<BTFTagAttributedType>()) 1061 return MakeCXType(ATT->getWrappedType(), GetTU(CT)); 1062 1063 return MakeCXType(QualType(), GetTU(CT)); 1064 } 1065 1066 long long clang_Cursor_getOffsetOfField(CXCursor C) { 1067 if (clang_isDeclaration(C.kind)) { 1068 // we need to validate the parent type 1069 CXCursor PC = clang_getCursorSemanticParent(C); 1070 CXType PT = clang_getCursorType(PC); 1071 long long Error = validateFieldParentType(PC,PT); 1072 if (Error < 0) 1073 return Error; 1074 // proceed with the offset calculation 1075 const Decl *D = cxcursor::getCursorDecl(C); 1076 ASTContext &Ctx = cxcursor::getCursorContext(C); 1077 if (const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D)) 1078 return Ctx.getFieldOffset(FD); 1079 if (const IndirectFieldDecl *IFD = dyn_cast_or_null<IndirectFieldDecl>(D)) 1080 return Ctx.getFieldOffset(IFD); 1081 } 1082 return -1; 1083 } 1084 1085 enum CXRefQualifierKind clang_Type_getCXXRefQualifier(CXType T) { 1086 QualType QT = GetQualType(T); 1087 if (QT.isNull()) 1088 return CXRefQualifier_None; 1089 const FunctionProtoType *FD = QT->getAs<FunctionProtoType>(); 1090 if (!FD) 1091 return CXRefQualifier_None; 1092 switch (FD->getRefQualifier()) { 1093 case RQ_None: 1094 return CXRefQualifier_None; 1095 case RQ_LValue: 1096 return CXRefQualifier_LValue; 1097 case RQ_RValue: 1098 return CXRefQualifier_RValue; 1099 } 1100 return CXRefQualifier_None; 1101 } 1102 1103 unsigned clang_Cursor_isBitField(CXCursor C) { 1104 if (!clang_isDeclaration(C.kind)) 1105 return 0; 1106 const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(cxcursor::getCursorDecl(C)); 1107 if (!FD) 1108 return 0; 1109 return FD->isBitField(); 1110 } 1111 1112 CXString clang_getDeclObjCTypeEncoding(CXCursor C) { 1113 if (!clang_isDeclaration(C.kind)) 1114 return cxstring::createEmpty(); 1115 1116 const Decl *D = cxcursor::getCursorDecl(C); 1117 ASTContext &Ctx = cxcursor::getCursorContext(C); 1118 std::string encoding; 1119 1120 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) { 1121 encoding = Ctx.getObjCEncodingForMethodDecl(OMD); 1122 } else if (const ObjCPropertyDecl *OPD = dyn_cast<ObjCPropertyDecl>(D)) 1123 encoding = Ctx.getObjCEncodingForPropertyDecl(OPD, nullptr); 1124 else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 1125 encoding = Ctx.getObjCEncodingForFunctionDecl(FD); 1126 else { 1127 QualType Ty; 1128 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D)) 1129 Ty = Ctx.getTypeDeclType(TD); 1130 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) 1131 Ty = VD->getType(); 1132 else return cxstring::createRef("?"); 1133 Ctx.getObjCEncodingForType(Ty, encoding); 1134 } 1135 1136 return cxstring::createDup(encoding); 1137 } 1138 1139 static unsigned GetTemplateArgumentArraySize(ArrayRef<TemplateArgument> TA) { 1140 unsigned size = TA.size(); 1141 for (const auto &Arg : TA) 1142 if (Arg.getKind() == TemplateArgument::Pack) 1143 size += Arg.pack_size() - 1; 1144 return size; 1145 } 1146 1147 int clang_Type_getNumTemplateArguments(CXType CT) { 1148 QualType T = GetQualType(CT); 1149 if (T.isNull()) 1150 return -1; 1151 1152 auto TA = GetTemplateArguments(T); 1153 if (!TA) 1154 return -1; 1155 1156 return GetTemplateArgumentArraySize(TA.getValue()); 1157 } 1158 1159 CXType clang_Type_getTemplateArgumentAsType(CXType CT, unsigned index) { 1160 QualType T = GetQualType(CT); 1161 if (T.isNull()) 1162 return MakeCXType(QualType(), GetTU(CT)); 1163 1164 auto TA = GetTemplateArguments(T); 1165 if (!TA) 1166 return MakeCXType(QualType(), GetTU(CT)); 1167 1168 Optional<QualType> QT = FindTemplateArgumentTypeAt(TA.getValue(), index); 1169 return MakeCXType(QT.getValueOr(QualType()), GetTU(CT)); 1170 } 1171 1172 CXType clang_Type_getObjCObjectBaseType(CXType CT) { 1173 QualType T = GetQualType(CT); 1174 if (T.isNull()) 1175 return MakeCXType(QualType(), GetTU(CT)); 1176 1177 const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T); 1178 if (!OT) 1179 return MakeCXType(QualType(), GetTU(CT)); 1180 1181 return MakeCXType(OT->getBaseType(), GetTU(CT)); 1182 } 1183 1184 unsigned clang_Type_getNumObjCProtocolRefs(CXType CT) { 1185 QualType T = GetQualType(CT); 1186 if (T.isNull()) 1187 return 0; 1188 1189 const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T); 1190 if (!OT) 1191 return 0; 1192 1193 return OT->getNumProtocols(); 1194 } 1195 1196 CXCursor clang_Type_getObjCProtocolDecl(CXType CT, unsigned i) { 1197 QualType T = GetQualType(CT); 1198 if (T.isNull()) 1199 return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound); 1200 1201 const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T); 1202 if (!OT) 1203 return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound); 1204 1205 const ObjCProtocolDecl *PD = OT->getProtocol(i); 1206 if (!PD) 1207 return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound); 1208 1209 return cxcursor::MakeCXCursor(PD, GetTU(CT)); 1210 } 1211 1212 unsigned clang_Type_getNumObjCTypeArgs(CXType CT) { 1213 QualType T = GetQualType(CT); 1214 if (T.isNull()) 1215 return 0; 1216 1217 const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T); 1218 if (!OT) 1219 return 0; 1220 1221 return OT->getTypeArgs().size(); 1222 } 1223 1224 CXType clang_Type_getObjCTypeArg(CXType CT, unsigned i) { 1225 QualType T = GetQualType(CT); 1226 if (T.isNull()) 1227 return MakeCXType(QualType(), GetTU(CT)); 1228 1229 const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T); 1230 if (!OT) 1231 return MakeCXType(QualType(), GetTU(CT)); 1232 1233 const ArrayRef<QualType> TA = OT->getTypeArgs(); 1234 if ((size_t)i >= TA.size()) 1235 return MakeCXType(QualType(), GetTU(CT)); 1236 1237 return MakeCXType(TA[i], GetTU(CT)); 1238 } 1239 1240 unsigned clang_Type_visitFields(CXType PT, 1241 CXFieldVisitor visitor, 1242 CXClientData client_data){ 1243 CXCursor PC = clang_getTypeDeclaration(PT); 1244 if (clang_isInvalid(PC.kind)) 1245 return false; 1246 const RecordDecl *RD = 1247 dyn_cast_or_null<RecordDecl>(cxcursor::getCursorDecl(PC)); 1248 if (!RD || RD->isInvalidDecl()) 1249 return false; 1250 RD = RD->getDefinition(); 1251 if (!RD || RD->isInvalidDecl()) 1252 return false; 1253 1254 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); 1255 I != E; ++I){ 1256 const FieldDecl *FD = dyn_cast_or_null<FieldDecl>((*I)); 1257 // Callback to the client. 1258 switch (visitor(cxcursor::MakeCXCursor(FD, GetTU(PT)), client_data)){ 1259 case CXVisit_Break: 1260 return true; 1261 case CXVisit_Continue: 1262 break; 1263 } 1264 } 1265 return true; 1266 } 1267 1268 unsigned clang_Cursor_isAnonymous(CXCursor C){ 1269 if (!clang_isDeclaration(C.kind)) 1270 return 0; 1271 const Decl *D = cxcursor::getCursorDecl(C); 1272 if (const NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(D)) { 1273 return ND->isAnonymousNamespace(); 1274 } else if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(D)) { 1275 return TD->getTypedefNameForAnonDecl() == nullptr && 1276 TD->getIdentifier() == nullptr; 1277 } 1278 1279 return 0; 1280 } 1281 1282 unsigned clang_Cursor_isAnonymousRecordDecl(CXCursor C){ 1283 if (!clang_isDeclaration(C.kind)) 1284 return 0; 1285 const Decl *D = cxcursor::getCursorDecl(C); 1286 if (const RecordDecl *FD = dyn_cast_or_null<RecordDecl>(D)) 1287 return FD->isAnonymousStructOrUnion(); 1288 return 0; 1289 } 1290 1291 unsigned clang_Cursor_isInlineNamespace(CXCursor C) { 1292 if (!clang_isDeclaration(C.kind)) 1293 return 0; 1294 const Decl *D = cxcursor::getCursorDecl(C); 1295 const NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(D); 1296 return ND ? ND->isInline() : 0; 1297 } 1298 1299 CXType clang_Type_getNamedType(CXType CT){ 1300 QualType T = GetQualType(CT); 1301 const Type *TP = T.getTypePtrOrNull(); 1302 1303 if (TP && TP->getTypeClass() == Type::Elaborated) 1304 return MakeCXType(cast<ElaboratedType>(TP)->getNamedType(), GetTU(CT)); 1305 1306 return MakeCXType(QualType(), GetTU(CT)); 1307 } 1308 1309 unsigned clang_Type_isTransparentTagTypedef(CXType TT){ 1310 QualType T = GetQualType(TT); 1311 if (auto *TT = dyn_cast_or_null<TypedefType>(T.getTypePtrOrNull())) { 1312 if (auto *D = TT->getDecl()) 1313 return D->isTransparentTag(); 1314 } 1315 return false; 1316 } 1317 1318 enum CXTypeNullabilityKind clang_Type_getNullability(CXType CT) { 1319 QualType T = GetQualType(CT); 1320 if (T.isNull()) 1321 return CXTypeNullability_Invalid; 1322 1323 ASTContext &Ctx = cxtu::getASTUnit(GetTU(CT))->getASTContext(); 1324 if (auto nullability = T->getNullability(Ctx)) { 1325 switch (*nullability) { 1326 case NullabilityKind::NonNull: 1327 return CXTypeNullability_NonNull; 1328 case NullabilityKind::Nullable: 1329 return CXTypeNullability_Nullable; 1330 case NullabilityKind::NullableResult: 1331 return CXTypeNullability_NullableResult; 1332 case NullabilityKind::Unspecified: 1333 return CXTypeNullability_Unspecified; 1334 } 1335 } 1336 return CXTypeNullability_Invalid; 1337 } 1338 1339 CXType clang_Type_getValueType(CXType CT) { 1340 QualType T = GetQualType(CT); 1341 1342 if (T.isNull() || !T->isAtomicType()) 1343 return MakeCXType(QualType(), GetTU(CT)); 1344 1345 const auto *AT = T->castAs<AtomicType>(); 1346 return MakeCXType(AT->getValueType(), GetTU(CT)); 1347 } 1348