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.  



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@122639 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/libclang/CXType.cpp b/tools/libclang/CXType.cpp
index e1297a4..a72b02f 100644
--- a/tools/libclang/CXType.cpp
+++ b/tools/libclang/CXType.cpp
@@ -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"