[cfe-commits] r122639 - in /cfe/trunk: include/clang-c/Index.h include/clang/AST/ASTContext.h lib/AST/ASTContext.cpp tools/libclang/CXType.cpp tools/libclang/libclang.darwin.exports tools/libclang/libclang.exports
David Chisnall
csdavec at swan.ac.uk
Thu Dec 30 06:05:53 PST 2010
Author: theraven
Date: Thu Dec 30 08:05:53 2010
New Revision: 122639
URL: http://llvm.org/viewvc/llvm-project?rev=122639&view=rev
Log:
Expose Objective-C type encodings of declarations to libclang users. This also adds a method in ASTContext which encodes FunctionDecls using the same encoding format that is used for Objective-C methods.
Modified:
cfe/trunk/include/clang-c/Index.h
cfe/trunk/include/clang/AST/ASTContext.h
cfe/trunk/lib/AST/ASTContext.cpp
cfe/trunk/tools/libclang/CXType.cpp
cfe/trunk/tools/libclang/libclang.darwin.exports
cfe/trunk/tools/libclang/libclang.exports
Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=122639&r1=122638&r2=122639&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Thu Dec 30 08:05:53 2010
@@ -1792,6 +1792,10 @@
*/
CINDEX_LINKAGE CXCursor clang_getTypeDeclaration(CXType T);
+/**
+ * Returns the Objective-C type encoding for the specified declaration.
+ */
+CINDEX_LINKAGE CXString clang_getDeclObjCTypeEncoding(CXCursor C);
/**
* \brief Retrieve the spelling of a given CXTypeKind.
Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=122639&r1=122638&r2=122639&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Thu Dec 30 08:05:53 2010
@@ -828,6 +828,10 @@
void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
std::string &S) const;
+ /// getObjCEncodingForFunctionDecl - Returns the encoded type for this
+ //function. This is in the same format as Objective-C method encodings.
+ void getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, std::string& S);
+
/// getObjCEncodingForMethodDecl - Return the encoded type for this method
/// declaration.
void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S);
Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=122639&r1=122638&r2=122639&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Thu Dec 30 08:05:53 2010
@@ -3526,6 +3526,42 @@
}
}
+void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
+ std::string& S) {
+ // Encode result type.
+ getObjCEncodingForType(Decl->getResultType(), S);
+ CharUnits ParmOffset;
+ // Compute size of all parameters.
+ for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
+ E = Decl->param_end(); PI != E; ++PI) {
+ QualType PType = (*PI)->getType();
+ CharUnits sz = getObjCEncodingTypeSize(PType);
+ assert (sz.isPositive() &&
+ "getObjCEncodingForMethodDecl - Incomplete param type");
+ ParmOffset += sz;
+ }
+ S += charUnitsToString(ParmOffset);
+ ParmOffset = CharUnits::Zero();
+
+ // Argument types.
+ for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
+ E = Decl->param_end(); PI != E; ++PI) {
+ ParmVarDecl *PVDecl = *PI;
+ QualType PType = PVDecl->getOriginalType();
+ if (const ArrayType *AT =
+ dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
+ // Use array's original type only if it has known number of
+ // elements.
+ if (!isa<ConstantArrayType>(AT))
+ PType = PVDecl->getType();
+ } else if (PType->isFunctionType())
+ PType = PVDecl->getType();
+ getObjCEncodingForType(PType, S);
+ S += charUnitsToString(ParmOffset);
+ ParmOffset += getObjCEncodingTypeSize(PType);
+ }
+}
+
/// getObjCEncodingForMethodDecl - Return the encoded type for this method
/// declaration.
void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Modified: cfe/trunk/tools/libclang/CXType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXType.cpp?rev=122639&r1=122638&r2=122639&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXType.cpp (original)
+++ cfe/trunk/tools/libclang/CXType.cpp Thu Dec 30 08:05:53 2010
@@ -353,4 +353,33 @@
return T->isPODType() ? 1 : 0;
}
+CXString clang_getDeclObjCTypeEncoding(CXCursor C) {
+ if ((C.kind < CXCursor_FirstDecl) || (C.kind > CXCursor_LastDecl))
+ return cxstring::createCXString("");
+
+ Decl *D = static_cast<Decl*>(C.data[0]);
+ CXTranslationUnit TU = static_cast<CXTranslationUnit>(C.data[2]);
+ ASTUnit *AU = static_cast<ASTUnit*>(TU->TUData);
+ ASTContext &Ctx = AU->getASTContext();
+ std::string encoding;
+
+ if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
+ Ctx.getObjCEncodingForMethodDecl(OMD, encoding);
+ else if (ObjCPropertyDecl *OPD = dyn_cast<ObjCPropertyDecl>(D))
+ Ctx.getObjCEncodingForPropertyDecl(OPD, NULL, encoding);
+ else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
+ Ctx.getObjCEncodingForFunctionDecl(FD, encoding);
+ else {
+ QualType Ty;
+ if (TypeDecl *TD = dyn_cast<TypeDecl>(D))
+ Ty = QualType(TD->getTypeForDecl(), 0);
+ if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
+ Ty = VD->getType();
+ else return cxstring::createCXString("?");
+ Ctx.getObjCEncodingForType(Ty, encoding);
+ }
+
+ return cxstring::createCXString(encoding);
+}
+
} // end: extern "C"
Modified: cfe/trunk/tools/libclang/libclang.darwin.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.darwin.exports?rev=122639&r1=122638&r2=122639&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.darwin.exports (original)
+++ cfe/trunk/tools/libclang/libclang.darwin.exports Thu Dec 30 08:05:53 2010
@@ -61,6 +61,7 @@
_clang_getCursorSpelling
_clang_getCursorType
_clang_getCursorUSR
+_clang_getDeclObjCTypeEncoding
_clang_getDefinitionSpellingAndExtent
_clang_getDiagnostic
_clang_getDiagnosticCategory
Modified: cfe/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=122639&r1=122638&r2=122639&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Thu Dec 30 08:05:53 2010
@@ -61,6 +61,7 @@
clang_getCursorSpelling
clang_getCursorType
clang_getCursorUSR
+clang_getDeclObjCTypeEncoding
clang_getDefinitionSpellingAndExtent
clang_getDiagnostic
clang_getDiagnosticCategory
More information about the cfe-commits
mailing list