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(AArch64SVEPCS);
670       TCALLINGCONV(Win64);
671       TCALLINGCONV(X86_64SysV);
672       TCALLINGCONV(AAPCS);
673       TCALLINGCONV(AAPCS_VFP);
674       TCALLINGCONV(IntelOclBicc);
675       TCALLINGCONV(Swift);
676       TCALLINGCONV(SwiftAsync);
677       TCALLINGCONV(PreserveMost);
678       TCALLINGCONV(PreserveAll);
679     case CC_SpirFunction: return CXCallingConv_Unexposed;
680     case CC_OpenCLKernel: return CXCallingConv_Unexposed;
681       break;
682     }
683 #undef TCALLINGCONV
684   }
685 
686   return CXCallingConv_Invalid;
687 }
688 
689 int clang_getNumArgTypes(CXType X) {
690   QualType T = GetQualType(X);
691   if (T.isNull())
692     return -1;
693 
694   if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>()) {
695     return FD->getNumParams();
696   }
697 
698   if (T->getAs<FunctionNoProtoType>()) {
699     return 0;
700   }
701 
702   return -1;
703 }
704 
705 CXType clang_getArgType(CXType X, unsigned i) {
706   QualType T = GetQualType(X);
707   if (T.isNull())
708     return MakeCXType(QualType(), GetTU(X));
709 
710   if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>()) {
711     unsigned numParams = FD->getNumParams();
712     if (i >= numParams)
713       return MakeCXType(QualType(), GetTU(X));
714 
715     return MakeCXType(FD->getParamType(i), GetTU(X));
716   }
717 
718   return MakeCXType(QualType(), GetTU(X));
719 }
720 
721 CXType clang_getResultType(CXType X) {
722   QualType T = GetQualType(X);
723   if (T.isNull())
724     return MakeCXType(QualType(), GetTU(X));
725 
726   if (const FunctionType *FD = T->getAs<FunctionType>())
727     return MakeCXType(FD->getReturnType(), GetTU(X));
728 
729   return MakeCXType(QualType(), GetTU(X));
730 }
731 
732 CXType clang_getCursorResultType(CXCursor C) {
733   if (clang_isDeclaration(C.kind)) {
734     const Decl *D = cxcursor::getCursorDecl(C);
735     if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
736       return MakeCXType(MD->getReturnType(), cxcursor::getCursorTU(C));
737 
738     return clang_getResultType(clang_getCursorType(C));
739   }
740 
741   return MakeCXType(QualType(), cxcursor::getCursorTU(C));
742 }
743 
744 // FIXME: We should expose the canThrow(...) result instead of the EST.
745 static CXCursor_ExceptionSpecificationKind
746 getExternalExceptionSpecificationKind(ExceptionSpecificationType EST) {
747   switch (EST) {
748   case EST_None:
749     return CXCursor_ExceptionSpecificationKind_None;
750   case EST_DynamicNone:
751     return CXCursor_ExceptionSpecificationKind_DynamicNone;
752   case EST_Dynamic:
753     return CXCursor_ExceptionSpecificationKind_Dynamic;
754   case EST_MSAny:
755     return CXCursor_ExceptionSpecificationKind_MSAny;
756   case EST_BasicNoexcept:
757     return CXCursor_ExceptionSpecificationKind_BasicNoexcept;
758   case EST_NoThrow:
759     return CXCursor_ExceptionSpecificationKind_NoThrow;
760   case EST_NoexceptFalse:
761   case EST_NoexceptTrue:
762   case EST_DependentNoexcept:
763     return CXCursor_ExceptionSpecificationKind_ComputedNoexcept;
764   case EST_Unevaluated:
765     return CXCursor_ExceptionSpecificationKind_Unevaluated;
766   case EST_Uninstantiated:
767     return CXCursor_ExceptionSpecificationKind_Uninstantiated;
768   case EST_Unparsed:
769     return CXCursor_ExceptionSpecificationKind_Unparsed;
770   }
771   llvm_unreachable("invalid EST value");
772 }
773 
774 int clang_getExceptionSpecificationType(CXType X) {
775   QualType T = GetQualType(X);
776   if (T.isNull())
777     return -1;
778 
779   if (const auto *FD = T->getAs<FunctionProtoType>())
780     return getExternalExceptionSpecificationKind(FD->getExceptionSpecType());
781 
782   return -1;
783 }
784 
785 int clang_getCursorExceptionSpecificationType(CXCursor C) {
786   if (clang_isDeclaration(C.kind))
787     return clang_getExceptionSpecificationType(clang_getCursorType(C));
788 
789   return -1;
790 }
791 
792 unsigned clang_isPODType(CXType X) {
793   QualType T = GetQualType(X);
794   if (T.isNull())
795     return 0;
796 
797   CXTranslationUnit TU = GetTU(X);
798 
799   return T.isPODType(cxtu::getASTUnit(TU)->getASTContext()) ? 1 : 0;
800 }
801 
802 CXType clang_getElementType(CXType CT) {
803   QualType ET = QualType();
804   QualType T = GetQualType(CT);
805   const Type *TP = T.getTypePtrOrNull();
806 
807   if (TP) {
808     switch (TP->getTypeClass()) {
809     case Type::ConstantArray:
810       ET = cast<ConstantArrayType> (TP)->getElementType();
811       break;
812     case Type::IncompleteArray:
813       ET = cast<IncompleteArrayType> (TP)->getElementType();
814       break;
815     case Type::VariableArray:
816       ET = cast<VariableArrayType> (TP)->getElementType();
817       break;
818     case Type::DependentSizedArray:
819       ET = cast<DependentSizedArrayType> (TP)->getElementType();
820       break;
821     case Type::Vector:
822       ET = cast<VectorType> (TP)->getElementType();
823       break;
824     case Type::ExtVector:
825       ET = cast<ExtVectorType>(TP)->getElementType();
826       break;
827     case Type::Complex:
828       ET = cast<ComplexType> (TP)->getElementType();
829       break;
830     default:
831       break;
832     }
833   }
834   return MakeCXType(ET, GetTU(CT));
835 }
836 
837 long long clang_getNumElements(CXType CT) {
838   long long result = -1;
839   QualType T = GetQualType(CT);
840   const Type *TP = T.getTypePtrOrNull();
841 
842   if (TP) {
843     switch (TP->getTypeClass()) {
844     case Type::ConstantArray:
845       result = cast<ConstantArrayType> (TP)->getSize().getSExtValue();
846       break;
847     case Type::Vector:
848       result = cast<VectorType> (TP)->getNumElements();
849       break;
850     case Type::ExtVector:
851       result = cast<ExtVectorType>(TP)->getNumElements();
852       break;
853     default:
854       break;
855     }
856   }
857   return result;
858 }
859 
860 CXType clang_getArrayElementType(CXType CT) {
861   QualType ET = QualType();
862   QualType T = GetQualType(CT);
863   const Type *TP = T.getTypePtrOrNull();
864 
865   if (TP) {
866     switch (TP->getTypeClass()) {
867     case Type::ConstantArray:
868       ET = cast<ConstantArrayType> (TP)->getElementType();
869       break;
870     case Type::IncompleteArray:
871       ET = cast<IncompleteArrayType> (TP)->getElementType();
872       break;
873     case Type::VariableArray:
874       ET = cast<VariableArrayType> (TP)->getElementType();
875       break;
876     case Type::DependentSizedArray:
877       ET = cast<DependentSizedArrayType> (TP)->getElementType();
878       break;
879     default:
880       break;
881     }
882   }
883   return MakeCXType(ET, GetTU(CT));
884 }
885 
886 long long clang_getArraySize(CXType CT) {
887   long long result = -1;
888   QualType T = GetQualType(CT);
889   const Type *TP = T.getTypePtrOrNull();
890 
891   if (TP) {
892     switch (TP->getTypeClass()) {
893     case Type::ConstantArray:
894       result = cast<ConstantArrayType> (TP)->getSize().getSExtValue();
895       break;
896     default:
897       break;
898     }
899   }
900   return result;
901 }
902 
903 static bool isIncompleteTypeWithAlignment(QualType QT) {
904   return QT->isIncompleteArrayType() || !QT->isIncompleteType();
905 }
906 
907 long long clang_Type_getAlignOf(CXType T) {
908   if (T.kind == CXType_Invalid)
909     return CXTypeLayoutError_Invalid;
910   ASTContext &Ctx = cxtu::getASTUnit(GetTU(T))->getASTContext();
911   QualType QT = GetQualType(T);
912   // [expr.alignof] p1: return size_t value for complete object type, reference
913   //                    or array.
914   // [expr.alignof] p3: if reference type, return size of referenced type
915   if (QT->isReferenceType())
916     QT = QT.getNonReferenceType();
917   if (!isIncompleteTypeWithAlignment(QT))
918     return CXTypeLayoutError_Incomplete;
919   if (QT->isDependentType())
920     return CXTypeLayoutError_Dependent;
921   if (const auto *Deduced = dyn_cast<DeducedType>(QT))
922     if (Deduced->getDeducedType().isNull())
923       return CXTypeLayoutError_Undeduced;
924   // Exceptions by GCC extension - see ASTContext.cpp:1313 getTypeInfoImpl
925   // if (QT->isFunctionType()) return 4; // Bug #15511 - should be 1
926   // if (QT->isVoidType()) return 1;
927   return Ctx.getTypeAlignInChars(QT).getQuantity();
928 }
929 
930 CXType clang_Type_getClassType(CXType CT) {
931   QualType ET = QualType();
932   QualType T = GetQualType(CT);
933   const Type *TP = T.getTypePtrOrNull();
934 
935   if (TP && TP->getTypeClass() == Type::MemberPointer) {
936     ET = QualType(cast<MemberPointerType> (TP)->getClass(), 0);
937   }
938   return MakeCXType(ET, GetTU(CT));
939 }
940 
941 long long clang_Type_getSizeOf(CXType T) {
942   if (T.kind == CXType_Invalid)
943     return CXTypeLayoutError_Invalid;
944   ASTContext &Ctx = cxtu::getASTUnit(GetTU(T))->getASTContext();
945   QualType QT = GetQualType(T);
946   // [expr.sizeof] p2: if reference type, return size of referenced type
947   if (QT->isReferenceType())
948     QT = QT.getNonReferenceType();
949   // [expr.sizeof] p1: return -1 on: func, incomplete, bitfield, incomplete
950   //                   enumeration
951   // Note: We get the cxtype, not the cxcursor, so we can't call
952   //       FieldDecl->isBitField()
953   // [expr.sizeof] p3: pointer ok, function not ok.
954   // [gcc extension] lib/AST/ExprConstant.cpp:1372 HandleSizeof : vla == error
955   if (QT->isIncompleteType())
956     return CXTypeLayoutError_Incomplete;
957   if (QT->isDependentType())
958     return CXTypeLayoutError_Dependent;
959   if (!QT->isConstantSizeType())
960     return CXTypeLayoutError_NotConstantSize;
961   if (const auto *Deduced = dyn_cast<DeducedType>(QT))
962     if (Deduced->getDeducedType().isNull())
963       return CXTypeLayoutError_Undeduced;
964   // [gcc extension] lib/AST/ExprConstant.cpp:1372
965   //                 HandleSizeof : {voidtype,functype} == 1
966   // not handled by ASTContext.cpp:1313 getTypeInfoImpl
967   if (QT->isVoidType() || QT->isFunctionType())
968     return 1;
969   return Ctx.getTypeSizeInChars(QT).getQuantity();
970 }
971 
972 static bool isTypeIncompleteForLayout(QualType QT) {
973   return QT->isIncompleteType() && !QT->isIncompleteArrayType();
974 }
975 
976 static long long visitRecordForValidation(const RecordDecl *RD) {
977   for (const auto *I : RD->fields()){
978     QualType FQT = I->getType();
979     if (isTypeIncompleteForLayout(FQT))
980       return CXTypeLayoutError_Incomplete;
981     if (FQT->isDependentType())
982       return CXTypeLayoutError_Dependent;
983     // recurse
984     if (const RecordType *ChildType = I->getType()->getAs<RecordType>()) {
985       if (const RecordDecl *Child = ChildType->getDecl()) {
986         long long ret = visitRecordForValidation(Child);
987         if (ret < 0)
988           return ret;
989       }
990     }
991     // else try next field
992   }
993   return 0;
994 }
995 
996 static long long validateFieldParentType(CXCursor PC, CXType PT){
997   if (clang_isInvalid(PC.kind))
998     return CXTypeLayoutError_Invalid;
999   const RecordDecl *RD =
1000         dyn_cast_or_null<RecordDecl>(cxcursor::getCursorDecl(PC));
1001   // validate parent declaration
1002   if (!RD || RD->isInvalidDecl())
1003     return CXTypeLayoutError_Invalid;
1004   RD = RD->getDefinition();
1005   if (!RD)
1006     return CXTypeLayoutError_Incomplete;
1007   if (RD->isInvalidDecl())
1008     return CXTypeLayoutError_Invalid;
1009   // validate parent type
1010   QualType RT = GetQualType(PT);
1011   if (RT->isIncompleteType())
1012     return CXTypeLayoutError_Incomplete;
1013   if (RT->isDependentType())
1014     return CXTypeLayoutError_Dependent;
1015   // We recurse into all record fields to detect incomplete and dependent types.
1016   long long Error = visitRecordForValidation(RD);
1017   if (Error < 0)
1018     return Error;
1019   return 0;
1020 }
1021 
1022 long long clang_Type_getOffsetOf(CXType PT, const char *S) {
1023   // check that PT is not incomplete/dependent
1024   CXCursor PC = clang_getTypeDeclaration(PT);
1025   long long Error = validateFieldParentType(PC,PT);
1026   if (Error < 0)
1027     return Error;
1028   if (!S)
1029     return CXTypeLayoutError_InvalidFieldName;
1030   // lookup field
1031   ASTContext &Ctx = cxtu::getASTUnit(GetTU(PT))->getASTContext();
1032   IdentifierInfo *II = &Ctx.Idents.get(S);
1033   DeclarationName FieldName(II);
1034   const RecordDecl *RD =
1035         dyn_cast_or_null<RecordDecl>(cxcursor::getCursorDecl(PC));
1036   // verified in validateFieldParentType
1037   RD = RD->getDefinition();
1038   RecordDecl::lookup_result Res = RD->lookup(FieldName);
1039   // If a field of the parent record is incomplete, lookup will fail.
1040   // and we would return InvalidFieldName instead of Incomplete.
1041   // But this erroneous results does protects again a hidden assertion failure
1042   // in the RecordLayoutBuilder
1043   if (!Res.isSingleResult())
1044     return CXTypeLayoutError_InvalidFieldName;
1045   if (const FieldDecl *FD = dyn_cast<FieldDecl>(Res.front()))
1046     return Ctx.getFieldOffset(FD);
1047   if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(Res.front()))
1048     return Ctx.getFieldOffset(IFD);
1049   // we don't want any other Decl Type.
1050   return CXTypeLayoutError_InvalidFieldName;
1051 }
1052 
1053 CXType clang_Type_getModifiedType(CXType CT) {
1054   QualType T = GetQualType(CT);
1055   if (T.isNull())
1056     return MakeCXType(QualType(), GetTU(CT));
1057 
1058   if (auto *ATT = T->getAs<AttributedType>())
1059     return MakeCXType(ATT->getModifiedType(), GetTU(CT));
1060 
1061   if (auto *ATT = T->getAs<BTFTagAttributedType>())
1062     return MakeCXType(ATT->getWrappedType(), GetTU(CT));
1063 
1064   return MakeCXType(QualType(), GetTU(CT));
1065 }
1066 
1067 long long clang_Cursor_getOffsetOfField(CXCursor C) {
1068   if (clang_isDeclaration(C.kind)) {
1069     // we need to validate the parent type
1070     CXCursor PC = clang_getCursorSemanticParent(C);
1071     CXType PT = clang_getCursorType(PC);
1072     long long Error = validateFieldParentType(PC,PT);
1073     if (Error < 0)
1074       return Error;
1075     // proceed with the offset calculation
1076     const Decl *D = cxcursor::getCursorDecl(C);
1077     ASTContext &Ctx = cxcursor::getCursorContext(C);
1078     if (const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D))
1079       return Ctx.getFieldOffset(FD);
1080     if (const IndirectFieldDecl *IFD = dyn_cast_or_null<IndirectFieldDecl>(D))
1081       return Ctx.getFieldOffset(IFD);
1082   }
1083   return -1;
1084 }
1085 
1086 enum CXRefQualifierKind clang_Type_getCXXRefQualifier(CXType T) {
1087   QualType QT = GetQualType(T);
1088   if (QT.isNull())
1089     return CXRefQualifier_None;
1090   const FunctionProtoType *FD = QT->getAs<FunctionProtoType>();
1091   if (!FD)
1092     return CXRefQualifier_None;
1093   switch (FD->getRefQualifier()) {
1094     case RQ_None:
1095       return CXRefQualifier_None;
1096     case RQ_LValue:
1097       return CXRefQualifier_LValue;
1098     case RQ_RValue:
1099       return CXRefQualifier_RValue;
1100   }
1101   return CXRefQualifier_None;
1102 }
1103 
1104 unsigned clang_Cursor_isBitField(CXCursor C) {
1105   if (!clang_isDeclaration(C.kind))
1106     return 0;
1107   const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(cxcursor::getCursorDecl(C));
1108   if (!FD)
1109     return 0;
1110   return FD->isBitField();
1111 }
1112 
1113 CXString clang_getDeclObjCTypeEncoding(CXCursor C) {
1114   if (!clang_isDeclaration(C.kind))
1115     return cxstring::createEmpty();
1116 
1117   const Decl *D = cxcursor::getCursorDecl(C);
1118   ASTContext &Ctx = cxcursor::getCursorContext(C);
1119   std::string encoding;
1120 
1121   if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))  {
1122     encoding = Ctx.getObjCEncodingForMethodDecl(OMD);
1123   } else if (const ObjCPropertyDecl *OPD = dyn_cast<ObjCPropertyDecl>(D))
1124     encoding = Ctx.getObjCEncodingForPropertyDecl(OPD, nullptr);
1125   else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1126     encoding = Ctx.getObjCEncodingForFunctionDecl(FD);
1127   else {
1128     QualType Ty;
1129     if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
1130       Ty = Ctx.getTypeDeclType(TD);
1131     if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
1132       Ty = VD->getType();
1133     else return cxstring::createRef("?");
1134     Ctx.getObjCEncodingForType(Ty, encoding);
1135   }
1136 
1137   return cxstring::createDup(encoding);
1138 }
1139 
1140 static unsigned GetTemplateArgumentArraySize(ArrayRef<TemplateArgument> TA) {
1141   unsigned size = TA.size();
1142   for (const auto &Arg : TA)
1143     if (Arg.getKind() == TemplateArgument::Pack)
1144       size += Arg.pack_size() - 1;
1145   return size;
1146 }
1147 
1148 int clang_Type_getNumTemplateArguments(CXType CT) {
1149   QualType T = GetQualType(CT);
1150   if (T.isNull())
1151     return -1;
1152 
1153   auto TA = GetTemplateArguments(T);
1154   if (!TA)
1155     return -1;
1156 
1157   return GetTemplateArgumentArraySize(TA.getValue());
1158 }
1159 
1160 CXType clang_Type_getTemplateArgumentAsType(CXType CT, unsigned index) {
1161   QualType T = GetQualType(CT);
1162   if (T.isNull())
1163     return MakeCXType(QualType(), GetTU(CT));
1164 
1165   auto TA = GetTemplateArguments(T);
1166   if (!TA)
1167     return MakeCXType(QualType(), GetTU(CT));
1168 
1169   Optional<QualType> QT = FindTemplateArgumentTypeAt(TA.getValue(), index);
1170   return MakeCXType(QT.getValueOr(QualType()), GetTU(CT));
1171 }
1172 
1173 CXType clang_Type_getObjCObjectBaseType(CXType CT) {
1174   QualType T = GetQualType(CT);
1175   if (T.isNull())
1176     return MakeCXType(QualType(), GetTU(CT));
1177 
1178   const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T);
1179   if (!OT)
1180     return MakeCXType(QualType(), GetTU(CT));
1181 
1182   return MakeCXType(OT->getBaseType(), GetTU(CT));
1183 }
1184 
1185 unsigned clang_Type_getNumObjCProtocolRefs(CXType CT) {
1186   QualType T = GetQualType(CT);
1187   if (T.isNull())
1188     return 0;
1189 
1190   const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T);
1191   if (!OT)
1192     return 0;
1193 
1194   return OT->getNumProtocols();
1195 }
1196 
1197 CXCursor clang_Type_getObjCProtocolDecl(CXType CT, unsigned i) {
1198   QualType T = GetQualType(CT);
1199   if (T.isNull())
1200     return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
1201 
1202   const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T);
1203   if (!OT)
1204     return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
1205 
1206   const ObjCProtocolDecl *PD = OT->getProtocol(i);
1207   if (!PD)
1208     return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
1209 
1210   return cxcursor::MakeCXCursor(PD, GetTU(CT));
1211 }
1212 
1213 unsigned clang_Type_getNumObjCTypeArgs(CXType CT) {
1214   QualType T = GetQualType(CT);
1215   if (T.isNull())
1216     return 0;
1217 
1218   const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T);
1219   if (!OT)
1220     return 0;
1221 
1222   return OT->getTypeArgs().size();
1223 }
1224 
1225 CXType clang_Type_getObjCTypeArg(CXType CT, unsigned i) {
1226   QualType T = GetQualType(CT);
1227   if (T.isNull())
1228     return MakeCXType(QualType(), GetTU(CT));
1229 
1230   const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T);
1231   if (!OT)
1232     return MakeCXType(QualType(), GetTU(CT));
1233 
1234   const ArrayRef<QualType> TA = OT->getTypeArgs();
1235   if ((size_t)i >= TA.size())
1236     return MakeCXType(QualType(), GetTU(CT));
1237 
1238   return MakeCXType(TA[i], GetTU(CT));
1239 }
1240 
1241 unsigned clang_Type_visitFields(CXType PT,
1242                                 CXFieldVisitor visitor,
1243                                 CXClientData client_data){
1244   CXCursor PC = clang_getTypeDeclaration(PT);
1245   if (clang_isInvalid(PC.kind))
1246     return false;
1247   const RecordDecl *RD =
1248         dyn_cast_or_null<RecordDecl>(cxcursor::getCursorDecl(PC));
1249   if (!RD || RD->isInvalidDecl())
1250     return false;
1251   RD = RD->getDefinition();
1252   if (!RD || RD->isInvalidDecl())
1253     return false;
1254 
1255   for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
1256        I != E; ++I){
1257     const FieldDecl *FD = dyn_cast_or_null<FieldDecl>((*I));
1258     // Callback to the client.
1259     switch (visitor(cxcursor::MakeCXCursor(FD, GetTU(PT)), client_data)){
1260     case CXVisit_Break:
1261       return true;
1262     case CXVisit_Continue:
1263       break;
1264     }
1265   }
1266   return true;
1267 }
1268 
1269 unsigned clang_Cursor_isAnonymous(CXCursor C){
1270   if (!clang_isDeclaration(C.kind))
1271     return 0;
1272   const Decl *D = cxcursor::getCursorDecl(C);
1273   if (const NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(D)) {
1274     return ND->isAnonymousNamespace();
1275   } else if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(D)) {
1276     return TD->getTypedefNameForAnonDecl() == nullptr &&
1277            TD->getIdentifier() == nullptr;
1278   }
1279 
1280   return 0;
1281 }
1282 
1283 unsigned clang_Cursor_isAnonymousRecordDecl(CXCursor C){
1284   if (!clang_isDeclaration(C.kind))
1285     return 0;
1286   const Decl *D = cxcursor::getCursorDecl(C);
1287   if (const RecordDecl *FD = dyn_cast_or_null<RecordDecl>(D))
1288     return FD->isAnonymousStructOrUnion();
1289   return 0;
1290 }
1291 
1292 unsigned clang_Cursor_isInlineNamespace(CXCursor C) {
1293   if (!clang_isDeclaration(C.kind))
1294     return 0;
1295   const Decl *D = cxcursor::getCursorDecl(C);
1296   const NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(D);
1297   return ND ? ND->isInline() : 0;
1298 }
1299 
1300 CXType clang_Type_getNamedType(CXType CT){
1301   QualType T = GetQualType(CT);
1302   const Type *TP = T.getTypePtrOrNull();
1303 
1304   if (TP && TP->getTypeClass() == Type::Elaborated)
1305     return MakeCXType(cast<ElaboratedType>(TP)->getNamedType(), GetTU(CT));
1306 
1307   return MakeCXType(QualType(), GetTU(CT));
1308 }
1309 
1310 unsigned clang_Type_isTransparentTagTypedef(CXType TT){
1311   QualType T = GetQualType(TT);
1312   if (auto *TT = dyn_cast_or_null<TypedefType>(T.getTypePtrOrNull())) {
1313     if (auto *D = TT->getDecl())
1314       return D->isTransparentTag();
1315   }
1316   return false;
1317 }
1318 
1319 enum CXTypeNullabilityKind clang_Type_getNullability(CXType CT) {
1320   QualType T = GetQualType(CT);
1321   if (T.isNull())
1322     return CXTypeNullability_Invalid;
1323 
1324   ASTContext &Ctx = cxtu::getASTUnit(GetTU(CT))->getASTContext();
1325   if (auto nullability = T->getNullability(Ctx)) {
1326     switch (*nullability) {
1327       case NullabilityKind::NonNull:
1328         return CXTypeNullability_NonNull;
1329       case NullabilityKind::Nullable:
1330         return CXTypeNullability_Nullable;
1331       case NullabilityKind::NullableResult:
1332         return CXTypeNullability_NullableResult;
1333       case NullabilityKind::Unspecified:
1334         return CXTypeNullability_Unspecified;
1335     }
1336   }
1337   return CXTypeNullability_Invalid;
1338 }
1339 
1340 CXType clang_Type_getValueType(CXType CT) {
1341   QualType T = GetQualType(CT);
1342 
1343   if (T.isNull() || !T->isAtomicType())
1344       return MakeCXType(QualType(), GetTU(CT));
1345 
1346   const auto *AT = T->castAs<AtomicType>();
1347   return MakeCXType(AT->getValueType(), GetTU(CT));
1348 }
1349