[cfe-commits] r43481 - in /cfe/trunk: AST/ASTContext.cpp AST/Type.cpp Driver/RewriteTest.cpp clang.xcodeproj/project.pbxproj include/clang/AST/ASTContext.h include/clang/AST/Type.h

Fariborz Jahanian fjahanian at apple.com
Mon Oct 29 15:57:28 PDT 2007


Author: fjahanian
Date: Mon Oct 29 17:57:28 2007
New Revision: 43481

URL: http://llvm.org/viewvc/llvm-project?rev=43481&view=rev
Log:
Encoding for objectiive-c methods.

Modified:
    cfe/trunk/AST/ASTContext.cpp
    cfe/trunk/AST/Type.cpp
    cfe/trunk/Driver/RewriteTest.cpp
    cfe/trunk/clang.xcodeproj/project.pbxproj
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/AST/Type.h

Modified: cfe/trunk/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/ASTContext.cpp?rev=43481&r1=43480&r2=43481&view=diff

==============================================================================
--- cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/trunk/AST/ASTContext.cpp Mon Oct 29 17:57:28 2007
@@ -153,6 +153,9 @@
   ObjcIdType = QualType();
   IdStructType = 0;
   ObjcConstantStringType = QualType();
+  
+  // void * type
+  VoidPtrTy = getPointerType(VoidTy);
 }
 
 //===----------------------------------------------------------------------===//
@@ -848,6 +851,60 @@
   return false;
 }
 
+/// getObjcEncodingTypeSize returns size of type for objective-c encoding
+/// purpose.
+int ASTContext::getObjcEncodingTypeSize(QualType type) {
+  SourceLocation Loc;
+  uint64_t sz = getTypeSize(type, Loc);
+  
+  // Make all integer and enum types at least as large as an int
+  if (sz > 0 && type->isIntegralType())
+    sz = std::max(sz, getTypeSize(IntTy, Loc));
+  // Treat arrays as pointers, since that's how they're passed in.
+  else if (type->isArrayType())
+    sz = getTypeSize(VoidPtrTy, Loc);
+  return sz / getTypeSize(CharTy, Loc);
+}
+
+/// getObjcEncodingForMethodDecl - Return the encoded type for this method
+/// declaration.
+void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl, 
+                                              std::string& S)
+{
+  // TODO: First encode type qualifer, 'in', 'inout', etc. for the return type.
+  // Encode result type.
+  getObjcEncodingForType(Decl->getResultType(), S);
+  // Compute size of all parameters.
+  // Start with computing size of a pointer in number of bytes.
+  // FIXME: There might(should) be a better way of doing this computation!
+  SourceLocation Loc;
+  int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
+  // The first two arguments (self and _cmd) are pointers; account for
+  // their size.
+  int ParmOffset = 2 * PtrSize;
+  int NumOfParams = Decl->getNumParams();
+  for (int i = 0; i < NumOfParams; i++) {
+    QualType PType = Decl->getParamDecl(i)->getType();
+    int sz = getObjcEncodingTypeSize (PType);
+    assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type");
+    ParmOffset += sz;
+  }
+  S += llvm::utostr(ParmOffset);
+  S += "@0:";
+  S += llvm::utostr(PtrSize);
+  
+  // Argument types.
+  ParmOffset = 2 * PtrSize;
+  for (int i = 0; i < NumOfParams; i++) {
+    QualType PType = Decl->getParamDecl(i)->getType();
+    // TODO: Process argument qualifiers for user supplied arguments; such as,
+    // 'in', 'inout', etc.
+    getObjcEncodingForType(PType, S);
+    S += llvm::utostr(ParmOffset);
+    ParmOffset += getObjcEncodingTypeSize(PType);
+  }
+}
+
 void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const
 {
   // FIXME: This currently doesn't encode:

Modified: cfe/trunk/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/Type.cpp?rev=43481&r1=43480&r2=43481&view=diff

==============================================================================
--- cfe/trunk/AST/Type.cpp (original)
+++ cfe/trunk/AST/Type.cpp Mon Oct 29 17:57:28 2007
@@ -315,6 +315,16 @@
   return false;
 }
 
+bool Type::isIntegralType() const {
+  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
+    return BT->getKind() >= BuiltinType::Bool &&
+    BT->getKind() <= BuiltinType::LongLong;
+  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
+    if (TT->getDecl()->getKind() == Decl::Enum)
+      return true;
+  return false;
+}
+
 bool Type::isEnumeralType() const {
   if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
     return TT->getDecl()->getKind() == Decl::Enum;

Modified: cfe/trunk/Driver/RewriteTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteTest.cpp?rev=43481&r1=43480&r2=43481&view=diff

==============================================================================
--- cfe/trunk/Driver/RewriteTest.cpp (original)
+++ cfe/trunk/Driver/RewriteTest.cpp Mon Oct 29 17:57:28 2007
@@ -495,15 +495,20 @@
 
     Result += "\t,{{(SEL)\"";
     Result += Methods[0]->getSelector().getName().c_str();
-    Result += "\", \"\", 0}\n";
-  
+    std::string MethodTypeString;
+    Context->getObjcEncodingForMethodDecl(Methods[0], MethodTypeString);
+    Result += "\", \"";
+    Result += MethodTypeString;
+    Result += "\", 0}\n";
     for (int i = 1; i < NumMethods; i++) {
-      // TODO: 1) method selector name may hav to go into their own section
-      // 2) encode method types for use here (which may have to go into 
-      // __meth_var_types section, 3) Need method address as 3rd initializer.
+      // TODO: Need method address as 3rd initializer.
       Result += "\t  ,{(SEL)\"";
       Result += Methods[i]->getSelector().getName().c_str();
-      Result += "\", \"\", 0}\n";
+      std::string MethodTypeString;
+      Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
+      Result += "\", \"";
+      Result += MethodTypeString;
+      Result += "\", 0}\n";
     }
     Result += "\t }\n};\n";
   }
@@ -559,12 +564,13 @@
         Result += "\", \"\"}\n";
                        
         for (int i = 1; i < NumMethods; i++) {
-          // TODO: 1) method selector name may hav to go into their own section
-          // 2) encode method types for use here (which may have to go into 
-          // __meth_var_types section.
           Result += "\t  ,{(SEL)\"";
           Result += Methods[i]->getSelector().getName().c_str();
-          Result += "\", \"\"}\n";
+          std::string MethodTypeString;
+          Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
+          Result += "\", \"";
+          Result += MethodTypeString;
+          Result += "\"}\n";
         }
         Result += "\t }\n};\n";
       }
@@ -586,12 +592,13 @@
         Result += "\", \"\"}\n";
             
         for (int i = 1; i < NumMethods; i++) {
-          // TODO: 1) method selector name may hav to go into their own section
-          // 2) encode method types for use here (which may have to go into 
-          // __meth_var_types section.
           Result += "\t  ,{(SEL)\"";
           Result += Methods[i]->getSelector().getName().c_str();
-          Result += "\", \"\"}\n";
+          std::string MethodTypeString;
+          Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
+          Result += "\", \"";
+          Result += MethodTypeString;
+          Result += "\"}\n";
         }
         Result += "\t }\n};\n";
       }

Modified: cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=43481&r1=43480&r2=43481&view=diff

==============================================================================
--- cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/trunk/clang.xcodeproj/project.pbxproj Mon Oct 29 17:57:28 2007
@@ -756,6 +756,7 @@
 		08FB7793FE84155DC02AAC07 /* Project object */ = {
 			isa = PBXProject;
 			buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
+			compatibilityVersion = "Xcode 2.4";
 			hasScannedForEncodings = 1;
 			mainGroup = 08FB7794FE84155DC02AAC07 /* clang */;
 			projectDirPath = "";

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=43481&r1=43480&r2=43481&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Mon Oct 29 17:57:28 2007
@@ -77,6 +77,7 @@
   QualType UnsignedLongLongTy;
   QualType FloatTy, DoubleTy, LongDoubleTy;
   QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
+  QualType VoidPtrTy;
   
   ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents,
              SelectorTable &sels) : 
@@ -173,6 +174,14 @@
 
   // Return the ObjC type encoding for a given type.
   void getObjcEncodingForType(QualType t, std::string &S) const;
+  
+  /// getObjcEncodingForMethodDecl - Return the encoded type for this method
+  /// declaration.
+  void getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl, std::string &S);
+  
+  /// getObjcEncodingTypeSize returns size of type for objective-c encoding
+  /// purpose.
+  int getObjcEncodingTypeSize(QualType t);
     
   // This setter/getter repreents the ObjC 'id' type. It is setup lazily, by
   // Sema.

Modified: cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Type.h?rev=43481&r1=43480&r2=43481&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Mon Oct 29 17:57:28 2007
@@ -34,6 +34,7 @@
   class EnumDecl;
   class ObjcInterfaceDecl;
   class ObjcProtocolDecl;
+  class ObjcMethodDecl;
   class Expr;
   class SourceLocation;
   class PointerType;
@@ -273,6 +274,7 @@
   bool isEnumeralType() const;
   bool isBooleanType() const;
   bool isCharType() const;
+  bool isIntegralType() const;
   
   /// Floating point categories.
   bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double)





More information about the cfe-commits mailing list