1 //===- CXTypes.cpp - Implements 'CXTypes' aspect of libclang ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===--------------------------------------------------------------------===//
10 // This file implements the 'CXTypes' API hooks in the Clang-C library.
12 //===--------------------------------------------------------------------===//
17 #include "CXTranslationUnit.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/Type.h"
24 #include "clang/Frontend/ASTUnit.h"
26 using namespace clang;
28 static CXTypeKind GetBuiltinTypeKind(const BuiltinType *BT) {
29 #define BTCASE(K) case BuiltinType::K: return CXType_##K
30 switch (BT->getKind()) {
44 case BuiltinType::WChar_S: return CXType_WChar;
45 case BuiltinType::WChar_U: return CXType_WChar;
61 return CXType_Unexposed;
66 static CXTypeKind GetTypeKind(QualType T) {
67 const Type *TP = T.getTypePtrOrNull();
69 return CXType_Invalid;
71 #define TKCASE(K) case Type::K: return CXType_##K
72 switch (TP->getTypeClass()) {
74 return GetBuiltinTypeKind(cast<BuiltinType>(TP));
78 TKCASE(LValueReference);
79 TKCASE(RValueReference);
83 TKCASE(ObjCInterface);
84 TKCASE(ObjCObjectPointer);
85 TKCASE(FunctionNoProto);
86 TKCASE(FunctionProto);
87 TKCASE(ConstantArray);
88 TKCASE(IncompleteArray);
89 TKCASE(VariableArray);
90 TKCASE(DependentSizedArray);
92 TKCASE(MemberPointer);
94 return CXType_Unexposed;
100 CXType cxtype::MakeCXType(QualType T, CXTranslationUnit TU) {
101 CXTypeKind TK = CXType_Invalid;
103 if (TU && !T.isNull()) {
104 ASTContext &Ctx = cxtu::getASTUnit(TU)->getASTContext();
105 if (Ctx.getLangOpts().ObjC1) {
106 QualType UnqualT = T.getUnqualifiedType();
107 if (Ctx.isObjCIdType(UnqualT))
109 else if (Ctx.isObjCClassType(UnqualT))
110 TK = CXType_ObjCClass;
111 else if (Ctx.isObjCSelType(UnqualT))
115 /* Handle decayed types as the original type */
116 if (const DecayedType *DT = T->getAs<DecayedType>()) {
117 return MakeCXType(DT->getOriginalType(), TU);
120 if (TK == CXType_Invalid)
123 CXType CT = { TK, { TK == CXType_Invalid ? nullptr
124 : T.getAsOpaquePtr(), TU } };
128 using cxtype::MakeCXType;
130 static inline QualType GetQualType(CXType CT) {
131 return QualType::getFromOpaquePtr(CT.data[0]);
134 static inline CXTranslationUnit GetTU(CXType CT) {
135 return static_cast<CXTranslationUnit>(CT.data[1]);
140 CXType clang_getCursorType(CXCursor C) {
141 using namespace cxcursor;
143 CXTranslationUnit TU = cxcursor::getCursorTU(C);
145 return MakeCXType(QualType(), TU);
147 ASTContext &Context = cxtu::getASTUnit(TU)->getASTContext();
148 if (clang_isExpression(C.kind)) {
149 QualType T = cxcursor::getCursorExpr(C)->getType();
150 return MakeCXType(T, TU);
153 if (clang_isDeclaration(C.kind)) {
154 const Decl *D = cxcursor::getCursorDecl(C);
156 return MakeCXType(QualType(), TU);
158 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
159 return MakeCXType(Context.getTypeDeclType(TD), TU);
160 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
161 return MakeCXType(Context.getObjCInterfaceType(ID), TU);
162 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
163 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
164 return MakeCXType(TSInfo->getType(), TU);
165 return MakeCXType(DD->getType(), TU);
167 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
168 return MakeCXType(VD->getType(), TU);
169 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
170 return MakeCXType(PD->getType(), TU);
171 if (const FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D)) {
172 if (TypeSourceInfo *TSInfo = FTD->getTemplatedDecl()->getTypeSourceInfo())
173 return MakeCXType(TSInfo->getType(), TU);
174 return MakeCXType(FTD->getTemplatedDecl()->getType(), TU);
176 return MakeCXType(QualType(), TU);
179 if (clang_isReference(C.kind)) {
181 case CXCursor_ObjCSuperClassRef: {
183 = Context.getObjCInterfaceType(getCursorObjCSuperClassRef(C).first);
184 return MakeCXType(T, TU);
187 case CXCursor_ObjCClassRef: {
188 QualType T = Context.getObjCInterfaceType(getCursorObjCClassRef(C).first);
189 return MakeCXType(T, TU);
192 case CXCursor_TypeRef: {
193 QualType T = Context.getTypeDeclType(getCursorTypeRef(C).first);
194 return MakeCXType(T, TU);
198 case CXCursor_CXXBaseSpecifier:
199 return cxtype::MakeCXType(getCursorCXXBaseSpecifier(C)->getType(), TU);
201 case CXCursor_MemberRef:
202 return cxtype::MakeCXType(getCursorMemberRef(C).first->getType(), TU);
204 case CXCursor_VariableRef:
205 return cxtype::MakeCXType(getCursorVariableRef(C).first->getType(), TU);
207 case CXCursor_ObjCProtocolRef:
208 case CXCursor_TemplateRef:
209 case CXCursor_NamespaceRef:
210 case CXCursor_OverloadedDeclRef:
215 return MakeCXType(QualType(), TU);
218 return MakeCXType(QualType(), TU);
221 CXString clang_getTypeSpelling(CXType CT) {
222 QualType T = GetQualType(CT);
224 return cxstring::createEmpty();
226 CXTranslationUnit TU = GetTU(CT);
228 llvm::raw_svector_ostream OS(Str);
229 PrintingPolicy PP(cxtu::getASTUnit(TU)->getASTContext().getLangOpts());
233 return cxstring::createDup(OS.str());
236 CXType clang_getTypedefDeclUnderlyingType(CXCursor C) {
237 using namespace cxcursor;
238 CXTranslationUnit TU = cxcursor::getCursorTU(C);
240 if (clang_isDeclaration(C.kind)) {
241 const Decl *D = cxcursor::getCursorDecl(C);
243 if (const TypedefNameDecl *TD = dyn_cast_or_null<TypedefNameDecl>(D)) {
244 QualType T = TD->getUnderlyingType();
245 return MakeCXType(T, TU);
248 return MakeCXType(QualType(), TU);
251 return MakeCXType(QualType(), TU);
254 CXType clang_getEnumDeclIntegerType(CXCursor C) {
255 using namespace cxcursor;
256 CXTranslationUnit TU = cxcursor::getCursorTU(C);
258 if (clang_isDeclaration(C.kind)) {
259 const Decl *D = cxcursor::getCursorDecl(C);
261 if (const EnumDecl *TD = dyn_cast_or_null<EnumDecl>(D)) {
262 QualType T = TD->getIntegerType();
263 return MakeCXType(T, TU);
266 return MakeCXType(QualType(), TU);
269 return MakeCXType(QualType(), TU);
272 long long clang_getEnumConstantDeclValue(CXCursor C) {
273 using namespace cxcursor;
275 if (clang_isDeclaration(C.kind)) {
276 const Decl *D = cxcursor::getCursorDecl(C);
278 if (const EnumConstantDecl *TD = dyn_cast_or_null<EnumConstantDecl>(D)) {
279 return TD->getInitVal().getSExtValue();
288 unsigned long long clang_getEnumConstantDeclUnsignedValue(CXCursor C) {
289 using namespace cxcursor;
291 if (clang_isDeclaration(C.kind)) {
292 const Decl *D = cxcursor::getCursorDecl(C);
294 if (const EnumConstantDecl *TD = dyn_cast_or_null<EnumConstantDecl>(D)) {
295 return TD->getInitVal().getZExtValue();
304 int clang_getFieldDeclBitWidth(CXCursor C) {
305 using namespace cxcursor;
307 if (clang_isDeclaration(C.kind)) {
308 const Decl *D = getCursorDecl(C);
310 if (const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D)) {
311 if (FD->isBitField())
312 return FD->getBitWidthValue(getCursorContext(C));
319 CXType clang_getCanonicalType(CXType CT) {
320 if (CT.kind == CXType_Invalid)
323 QualType T = GetQualType(CT);
324 CXTranslationUnit TU = GetTU(CT);
327 return MakeCXType(QualType(), GetTU(CT));
329 return MakeCXType(cxtu::getASTUnit(TU)->getASTContext()
330 .getCanonicalType(T),
334 unsigned clang_isConstQualifiedType(CXType CT) {
335 QualType T = GetQualType(CT);
336 return T.isLocalConstQualified();
339 unsigned clang_isVolatileQualifiedType(CXType CT) {
340 QualType T = GetQualType(CT);
341 return T.isLocalVolatileQualified();
344 unsigned clang_isRestrictQualifiedType(CXType CT) {
345 QualType T = GetQualType(CT);
346 return T.isLocalRestrictQualified();
349 CXType clang_getPointeeType(CXType CT) {
350 QualType T = GetQualType(CT);
351 const Type *TP = T.getTypePtrOrNull();
354 return MakeCXType(QualType(), GetTU(CT));
356 switch (TP->getTypeClass()) {
358 T = cast<PointerType>(TP)->getPointeeType();
360 case Type::BlockPointer:
361 T = cast<BlockPointerType>(TP)->getPointeeType();
363 case Type::LValueReference:
364 case Type::RValueReference:
365 T = cast<ReferenceType>(TP)->getPointeeType();
367 case Type::ObjCObjectPointer:
368 T = cast<ObjCObjectPointerType>(TP)->getPointeeType();
370 case Type::MemberPointer:
371 T = cast<MemberPointerType>(TP)->getPointeeType();
377 return MakeCXType(T, GetTU(CT));
380 CXCursor clang_getTypeDeclaration(CXType CT) {
381 if (CT.kind == CXType_Invalid)
382 return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
384 QualType T = GetQualType(CT);
385 const Type *TP = T.getTypePtrOrNull();
388 return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
393 switch (TP->getTypeClass()) {
395 D = cast<TypedefType>(TP)->getDecl();
397 case Type::ObjCObject:
398 D = cast<ObjCObjectType>(TP)->getInterface();
400 case Type::ObjCInterface:
401 D = cast<ObjCInterfaceType>(TP)->getDecl();
405 D = cast<TagType>(TP)->getDecl();
407 case Type::TemplateSpecialization:
408 if (const RecordType *Record = TP->getAs<RecordType>())
409 D = Record->getDecl();
411 D = cast<TemplateSpecializationType>(TP)->getTemplateName()
412 .getAsTemplateDecl();
415 case Type::InjectedClassName:
416 D = cast<InjectedClassNameType>(TP)->getDecl();
419 // FIXME: Template type parameters!
421 case Type::Elaborated:
422 TP = cast<ElaboratedType>(TP)->getNamedType().getTypePtrOrNull();
430 return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
432 return cxcursor::MakeCXCursor(D, GetTU(CT));
435 CXString clang_getTypeKindSpelling(enum CXTypeKind K) {
436 const char *s = nullptr;
437 #define TKIND(X) case CXType_##X: s = "" #X ""; break
454 case CXType_WChar: s = "WChar"; break;
472 TKIND(LValueReference);
473 TKIND(RValueReference);
477 TKIND(ObjCInterface);
478 TKIND(ObjCObjectPointer);
479 TKIND(FunctionNoProto);
480 TKIND(FunctionProto);
481 TKIND(ConstantArray);
482 TKIND(IncompleteArray);
483 TKIND(VariableArray);
484 TKIND(DependentSizedArray);
486 TKIND(MemberPointer);
489 return cxstring::createRef(s);
492 unsigned clang_equalTypes(CXType A, CXType B) {
493 return A.data[0] == B.data[0] && A.data[1] == B.data[1];
496 unsigned clang_isFunctionTypeVariadic(CXType X) {
497 QualType T = GetQualType(X);
501 if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>())
502 return (unsigned)FD->isVariadic();
504 if (T->getAs<FunctionNoProtoType>())
510 CXCallingConv clang_getFunctionTypeCallingConv(CXType X) {
511 QualType T = GetQualType(X);
513 return CXCallingConv_Invalid;
515 if (const FunctionType *FD = T->getAs<FunctionType>()) {
516 #define TCALLINGCONV(X) case CC_##X: return CXCallingConv_##X
517 switch (FD->getCallConv()) {
519 TCALLINGCONV(X86StdCall);
520 TCALLINGCONV(X86FastCall);
521 TCALLINGCONV(X86ThisCall);
522 TCALLINGCONV(X86Pascal);
523 TCALLINGCONV(X86VectorCall);
524 TCALLINGCONV(X86_64Win64);
525 TCALLINGCONV(X86_64SysV);
527 TCALLINGCONV(AAPCS_VFP);
528 TCALLINGCONV(IntelOclBicc);
529 case CC_SpirFunction: return CXCallingConv_Unexposed;
530 case CC_SpirKernel: return CXCallingConv_Unexposed;
536 return CXCallingConv_Invalid;
539 int clang_getNumArgTypes(CXType X) {
540 QualType T = GetQualType(X);
544 if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>()) {
545 return FD->getNumParams();
548 if (T->getAs<FunctionNoProtoType>()) {
555 CXType clang_getArgType(CXType X, unsigned i) {
556 QualType T = GetQualType(X);
558 return MakeCXType(QualType(), GetTU(X));
560 if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>()) {
561 unsigned numParams = FD->getNumParams();
563 return MakeCXType(QualType(), GetTU(X));
565 return MakeCXType(FD->getParamType(i), GetTU(X));
568 return MakeCXType(QualType(), GetTU(X));
571 CXType clang_getResultType(CXType X) {
572 QualType T = GetQualType(X);
574 return MakeCXType(QualType(), GetTU(X));
576 if (const FunctionType *FD = T->getAs<FunctionType>())
577 return MakeCXType(FD->getReturnType(), GetTU(X));
579 return MakeCXType(QualType(), GetTU(X));
582 CXType clang_getCursorResultType(CXCursor C) {
583 if (clang_isDeclaration(C.kind)) {
584 const Decl *D = cxcursor::getCursorDecl(C);
585 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
586 return MakeCXType(MD->getReturnType(), cxcursor::getCursorTU(C));
588 return clang_getResultType(clang_getCursorType(C));
591 return MakeCXType(QualType(), cxcursor::getCursorTU(C));
594 unsigned clang_isPODType(CXType X) {
595 QualType T = GetQualType(X);
599 CXTranslationUnit TU = GetTU(X);
601 return T.isPODType(cxtu::getASTUnit(TU)->getASTContext()) ? 1 : 0;
604 CXType clang_getElementType(CXType CT) {
605 QualType ET = QualType();
606 QualType T = GetQualType(CT);
607 const Type *TP = T.getTypePtrOrNull();
610 switch (TP->getTypeClass()) {
611 case Type::ConstantArray:
612 ET = cast<ConstantArrayType> (TP)->getElementType();
614 case Type::IncompleteArray:
615 ET = cast<IncompleteArrayType> (TP)->getElementType();
617 case Type::VariableArray:
618 ET = cast<VariableArrayType> (TP)->getElementType();
620 case Type::DependentSizedArray:
621 ET = cast<DependentSizedArrayType> (TP)->getElementType();
624 ET = cast<VectorType> (TP)->getElementType();
627 ET = cast<ComplexType> (TP)->getElementType();
633 return MakeCXType(ET, GetTU(CT));
636 long long clang_getNumElements(CXType CT) {
637 long long result = -1;
638 QualType T = GetQualType(CT);
639 const Type *TP = T.getTypePtrOrNull();
642 switch (TP->getTypeClass()) {
643 case Type::ConstantArray:
644 result = cast<ConstantArrayType> (TP)->getSize().getSExtValue();
647 result = cast<VectorType> (TP)->getNumElements();
656 CXType clang_getArrayElementType(CXType CT) {
657 QualType ET = QualType();
658 QualType T = GetQualType(CT);
659 const Type *TP = T.getTypePtrOrNull();
662 switch (TP->getTypeClass()) {
663 case Type::ConstantArray:
664 ET = cast<ConstantArrayType> (TP)->getElementType();
666 case Type::IncompleteArray:
667 ET = cast<IncompleteArrayType> (TP)->getElementType();
669 case Type::VariableArray:
670 ET = cast<VariableArrayType> (TP)->getElementType();
672 case Type::DependentSizedArray:
673 ET = cast<DependentSizedArrayType> (TP)->getElementType();
679 return MakeCXType(ET, GetTU(CT));
682 long long clang_getArraySize(CXType CT) {
683 long long result = -1;
684 QualType T = GetQualType(CT);
685 const Type *TP = T.getTypePtrOrNull();
688 switch (TP->getTypeClass()) {
689 case Type::ConstantArray:
690 result = cast<ConstantArrayType> (TP)->getSize().getSExtValue();
699 long long clang_Type_getAlignOf(CXType T) {
700 if (T.kind == CXType_Invalid)
701 return CXTypeLayoutError_Invalid;
702 ASTContext &Ctx = cxtu::getASTUnit(GetTU(T))->getASTContext();
703 QualType QT = GetQualType(T);
704 // [expr.alignof] p1: return size_t value for complete object type, reference
706 // [expr.alignof] p3: if reference type, return size of referenced type
707 if (QT->isReferenceType())
708 QT = QT.getNonReferenceType();
709 if (QT->isIncompleteType())
710 return CXTypeLayoutError_Incomplete;
711 if (QT->isDependentType())
712 return CXTypeLayoutError_Dependent;
713 // Exceptions by GCC extension - see ASTContext.cpp:1313 getTypeInfoImpl
714 // if (QT->isFunctionType()) return 4; // Bug #15511 - should be 1
715 // if (QT->isVoidType()) return 1;
716 return Ctx.getTypeAlignInChars(QT).getQuantity();
719 CXType clang_Type_getClassType(CXType CT) {
720 QualType ET = QualType();
721 QualType T = GetQualType(CT);
722 const Type *TP = T.getTypePtrOrNull();
724 if (TP && TP->getTypeClass() == Type::MemberPointer) {
725 ET = QualType(cast<MemberPointerType> (TP)->getClass(), 0);
727 return MakeCXType(ET, GetTU(CT));
730 long long clang_Type_getSizeOf(CXType T) {
731 if (T.kind == CXType_Invalid)
732 return CXTypeLayoutError_Invalid;
733 ASTContext &Ctx = cxtu::getASTUnit(GetTU(T))->getASTContext();
734 QualType QT = GetQualType(T);
735 // [expr.sizeof] p2: if reference type, return size of referenced type
736 if (QT->isReferenceType())
737 QT = QT.getNonReferenceType();
738 // [expr.sizeof] p1: return -1 on: func, incomplete, bitfield, incomplete
740 // Note: We get the cxtype, not the cxcursor, so we can't call
741 // FieldDecl->isBitField()
742 // [expr.sizeof] p3: pointer ok, function not ok.
743 // [gcc extension] lib/AST/ExprConstant.cpp:1372 HandleSizeof : vla == error
744 if (QT->isIncompleteType())
745 return CXTypeLayoutError_Incomplete;
746 if (QT->isDependentType())
747 return CXTypeLayoutError_Dependent;
748 if (!QT->isConstantSizeType())
749 return CXTypeLayoutError_NotConstantSize;
750 // [gcc extension] lib/AST/ExprConstant.cpp:1372
751 // HandleSizeof : {voidtype,functype} == 1
752 // not handled by ASTContext.cpp:1313 getTypeInfoImpl
753 if (QT->isVoidType() || QT->isFunctionType())
755 return Ctx.getTypeSizeInChars(QT).getQuantity();
758 static long long visitRecordForValidation(const RecordDecl *RD) {
759 for (const auto *I : RD->fields()){
760 QualType FQT = I->getType();
761 if (FQT->isIncompleteType())
762 return CXTypeLayoutError_Incomplete;
763 if (FQT->isDependentType())
764 return CXTypeLayoutError_Dependent;
766 if (const RecordType *ChildType = I->getType()->getAs<RecordType>()) {
767 if (const RecordDecl *Child = ChildType->getDecl()) {
768 long long ret = visitRecordForValidation(Child);
773 // else try next field
778 static long long validateFieldParentType(CXCursor PC, CXType PT){
779 if (clang_isInvalid(PC.kind))
780 return CXTypeLayoutError_Invalid;
781 const RecordDecl *RD =
782 dyn_cast_or_null<RecordDecl>(cxcursor::getCursorDecl(PC));
783 // validate parent declaration
784 if (!RD || RD->isInvalidDecl())
785 return CXTypeLayoutError_Invalid;
786 RD = RD->getDefinition();
788 return CXTypeLayoutError_Incomplete;
789 if (RD->isInvalidDecl())
790 return CXTypeLayoutError_Invalid;
791 // validate parent type
792 QualType RT = GetQualType(PT);
793 if (RT->isIncompleteType())
794 return CXTypeLayoutError_Incomplete;
795 if (RT->isDependentType())
796 return CXTypeLayoutError_Dependent;
797 // We recurse into all record fields to detect incomplete and dependent types.
798 long long Error = visitRecordForValidation(RD);
804 long long clang_Type_getOffsetOf(CXType PT, const char *S) {
805 // check that PT is not incomplete/dependent
806 CXCursor PC = clang_getTypeDeclaration(PT);
807 long long Error = validateFieldParentType(PC,PT);
811 return CXTypeLayoutError_InvalidFieldName;
813 ASTContext &Ctx = cxtu::getASTUnit(GetTU(PT))->getASTContext();
814 IdentifierInfo *II = &Ctx.Idents.get(S);
815 DeclarationName FieldName(II);
816 const RecordDecl *RD =
817 dyn_cast_or_null<RecordDecl>(cxcursor::getCursorDecl(PC));
818 // verified in validateFieldParentType
819 RD = RD->getDefinition();
820 RecordDecl::lookup_result Res = RD->lookup(FieldName);
821 // If a field of the parent record is incomplete, lookup will fail.
822 // and we would return InvalidFieldName instead of Incomplete.
823 // But this erroneous results does protects again a hidden assertion failure
824 // in the RecordLayoutBuilder
826 return CXTypeLayoutError_InvalidFieldName;
827 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Res.front()))
828 return Ctx.getFieldOffset(FD);
829 if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(Res.front()))
830 return Ctx.getFieldOffset(IFD);
831 // we don't want any other Decl Type.
832 return CXTypeLayoutError_InvalidFieldName;
835 long long clang_Cursor_getOffsetOfField(CXCursor C) {
836 if (clang_isDeclaration(C.kind)) {
837 // we need to validate the parent type
838 CXCursor PC = clang_getCursorSemanticParent(C);
839 CXType PT = clang_getCursorType(PC);
840 long long Error = validateFieldParentType(PC,PT);
843 // proceed with the offset calculation
844 const Decl *D = cxcursor::getCursorDecl(C);
845 ASTContext &Ctx = cxcursor::getCursorContext(C);
846 if (const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D))
847 return Ctx.getFieldOffset(FD);
848 if (const IndirectFieldDecl *IFD = dyn_cast_or_null<IndirectFieldDecl>(D))
849 return Ctx.getFieldOffset(IFD);
854 enum CXRefQualifierKind clang_Type_getCXXRefQualifier(CXType T) {
855 QualType QT = GetQualType(T);
857 return CXRefQualifier_None;
858 const FunctionProtoType *FD = QT->getAs<FunctionProtoType>();
860 return CXRefQualifier_None;
861 switch (FD->getRefQualifier()) {
863 return CXRefQualifier_None;
865 return CXRefQualifier_LValue;
867 return CXRefQualifier_RValue;
869 return CXRefQualifier_None;
872 unsigned clang_Cursor_isBitField(CXCursor C) {
873 if (!clang_isDeclaration(C.kind))
875 const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(cxcursor::getCursorDecl(C));
878 return FD->isBitField();
881 CXString clang_getDeclObjCTypeEncoding(CXCursor C) {
882 if (!clang_isDeclaration(C.kind))
883 return cxstring::createEmpty();
885 const Decl *D = cxcursor::getCursorDecl(C);
886 ASTContext &Ctx = cxcursor::getCursorContext(C);
887 std::string encoding;
889 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
890 if (Ctx.getObjCEncodingForMethodDecl(OMD, encoding))
891 return cxstring::createRef("?");
892 } else if (const ObjCPropertyDecl *OPD = dyn_cast<ObjCPropertyDecl>(D))
893 Ctx.getObjCEncodingForPropertyDecl(OPD, nullptr, encoding);
894 else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
895 Ctx.getObjCEncodingForFunctionDecl(FD, encoding);
898 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
899 Ty = Ctx.getTypeDeclType(TD);
900 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
902 else return cxstring::createRef("?");
903 Ctx.getObjCEncodingForType(Ty, encoding);
906 return cxstring::createDup(encoding);
909 int clang_Type_getNumTemplateArguments(CXType CT) {
910 QualType T = GetQualType(CT);
913 const CXXRecordDecl *RecordDecl = T->getAsCXXRecordDecl();
916 const ClassTemplateSpecializationDecl *TemplateDecl =
917 dyn_cast<ClassTemplateSpecializationDecl>(RecordDecl);
920 return TemplateDecl->getTemplateArgs().size();
923 CXType clang_Type_getTemplateArgumentAsType(CXType CT, unsigned i) {
924 QualType T = GetQualType(CT);
926 return MakeCXType(QualType(), GetTU(CT));
927 const CXXRecordDecl *RecordDecl = T->getAsCXXRecordDecl();
929 return MakeCXType(QualType(), GetTU(CT));
930 const ClassTemplateSpecializationDecl *TemplateDecl =
931 dyn_cast<ClassTemplateSpecializationDecl>(RecordDecl);
933 return MakeCXType(QualType(), GetTU(CT));
934 const TemplateArgumentList &TA = TemplateDecl->getTemplateArgs();
936 return MakeCXType(QualType(), GetTU(CT));
937 const TemplateArgument &A = TA.get(i);
938 if (A.getKind() != TemplateArgument::Type)
939 return MakeCXType(QualType(), GetTU(CT));
940 return MakeCXType(A.getAsType(), GetTU(CT));
943 unsigned clang_Type_visitFields(CXType PT,
944 CXFieldVisitor visitor,
945 CXClientData client_data){
946 CXCursor PC = clang_getTypeDeclaration(PT);
947 if (clang_isInvalid(PC.kind))
949 const RecordDecl *RD =
950 dyn_cast_or_null<RecordDecl>(cxcursor::getCursorDecl(PC));
951 if (!RD || RD->isInvalidDecl())
953 RD = RD->getDefinition();
954 if (!RD || RD->isInvalidDecl())
957 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
959 const FieldDecl *FD = dyn_cast_or_null<FieldDecl>((*I));
960 // Callback to the client.
961 switch (visitor(cxcursor::MakeCXCursor(FD, GetTU(PT)), client_data)){
964 case CXVisit_Continue:
971 unsigned clang_Cursor_isAnonymous(CXCursor C){
972 if (!clang_isDeclaration(C.kind))
974 const Decl *D = cxcursor::getCursorDecl(C);
975 if (const RecordDecl *FD = dyn_cast_or_null<RecordDecl>(D))
976 return FD->isAnonymousStructOrUnion();