[cfe-commits] r39835 - in /cfe/trunk: AST/ASTContext.cpp AST/Type.cpp CodeGen/CGExpr.cpp CodeGen/CodeGenTypes.h Sema/SemaDecl.cpp include/clang/AST/ASTContext.h include/clang/AST/Type.h

Chris Lattner sabre at nondot.org
Fri Jul 13 15:13:22 PDT 2007


Author: lattner
Date: Fri Jul 13 17:13:22 2007
New Revision: 39835

URL: http://llvm.org/viewvc/llvm-project?rev=39835&view=rev
Log:
Move getSize() out of type, into ASTContext, where it has target info, and
where ASTContext can manage caches for struct layout, etc.

Modified:
    cfe/trunk/AST/ASTContext.cpp
    cfe/trunk/AST/Type.cpp
    cfe/trunk/CodeGen/CGExpr.cpp
    cfe/trunk/CodeGen/CodeGenTypes.h
    cfe/trunk/Sema/SemaDecl.cpp
    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=39835&r1=39834&r2=39835&view=diff

==============================================================================
--- cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/trunk/AST/ASTContext.cpp Fri Jul 13 17:13:22 2007
@@ -137,6 +137,59 @@
   LongDoubleComplexTy = getComplexType(LongDoubleTy);
 }
 
+
+/// getTypeSize - Return the size of the specified type, in bits.  This method
+/// does not work on incomplete types.
+unsigned ASTContext::getTypeSize(QualType T) {
+  T = T.getCanonicalType();
+  switch (T->getTypeClass()) {
+  case Type::Builtin: {
+    // FIXME: need to use TargetInfo to derive the target specific sizes. This
+    // implementation will suffice for play with vector support.
+    switch (cast<BuiltinType>(T)->getKind()) {
+    case BuiltinType::Void:       return 0;
+    case BuiltinType::Bool:       
+    case BuiltinType::Char_S:     
+    case BuiltinType::Char_U:     return sizeof(char) * 8;
+    case BuiltinType::SChar:      return sizeof(signed char) * 8;
+    case BuiltinType::Short:      return sizeof(short) * 8;
+    case BuiltinType::Int:        return sizeof(int) * 8;
+    case BuiltinType::Long:       return sizeof(long) * 8;
+    case BuiltinType::LongLong:   return sizeof(long long) * 8;
+    case BuiltinType::UChar:      return sizeof(unsigned char) * 8;
+    case BuiltinType::UShort:     return sizeof(unsigned short) * 8;
+    case BuiltinType::UInt:       return sizeof(unsigned int) * 8;
+    case BuiltinType::ULong:      return sizeof(unsigned long) * 8;
+    case BuiltinType::ULongLong:  return sizeof(unsigned long long) * 8;
+    case BuiltinType::Float:      return sizeof(float) * 8;
+    case BuiltinType::Double:     return sizeof(double) * 8;
+    case BuiltinType::LongDouble: return sizeof(long double) * 8;
+    }
+    assert(0 && "Can't get here");
+  }
+  case Type::Pointer:
+    // FIXME: need to use TargetInfo again
+    return sizeof(void *) * 8;
+  case Type::Reference:
+    // seems that sizeof(T&) == sizeof(T) -- spec reference?
+    return getTypeSize(cast<ReferenceType>(T)->getReferenceeType());
+  case Type::Complex:
+  case Type::Array:
+  case Type::Vector:
+  case Type::FunctionNoProto:
+  case Type::FunctionProto:
+  case Type::TypeName:
+  case Type::Tagged:
+    assert(0 && "Type sizes are not yet known, in general");
+  }
+  assert(0 && "Can't get here");
+}
+
+//===----------------------------------------------------------------------===//
+//                   Type creation/memoization methods
+//===----------------------------------------------------------------------===//
+
+
 /// getComplexType - Return the uniqued reference to the type for a complex
 /// number with the specified element type.
 QualType ASTContext::getComplexType(QualType T) {

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

==============================================================================
--- cfe/trunk/AST/Type.cpp (original)
+++ cfe/trunk/AST/Type.cpp Fri Jul 13 17:13:22 2007
@@ -22,52 +22,6 @@
 
 Type::~Type() {}
 
-/// getSize - the number of bits to represent the type.
-unsigned Type::getSize() const
-{
-  switch (CanonicalType->getTypeClass()) {
-  case Builtin: {
-    // FIXME: need to use TargetInfo to derive the target specific sizes. This
-    // implementation will suffice for play with vector support.
-    switch (cast<BuiltinType>(this)->getKind()) {
-    case BuiltinType::Void:       return 0;
-    case BuiltinType::Bool:       
-    case BuiltinType::Char_S:     
-    case BuiltinType::Char_U:     return sizeof(char) * 8;
-    case BuiltinType::SChar:      return sizeof(signed char) * 8;
-    case BuiltinType::Short:      return sizeof(short) * 8;
-    case BuiltinType::Int:        return sizeof(int) * 8;
-    case BuiltinType::Long:       return sizeof(long) * 8;
-    case BuiltinType::LongLong:   return sizeof(long long) * 8;
-    case BuiltinType::UChar:      return sizeof(unsigned char) * 8;
-    case BuiltinType::UShort:     return sizeof(unsigned short) * 8;
-    case BuiltinType::UInt:       return sizeof(unsigned int) * 8;
-    case BuiltinType::ULong:      return sizeof(unsigned long) * 8;
-    case BuiltinType::ULongLong:  return sizeof(unsigned long long) * 8;
-    case BuiltinType::Float:      return sizeof(float) * 8;
-    case BuiltinType::Double:     return sizeof(double) * 8;
-    case BuiltinType::LongDouble: return sizeof(long double) * 8;
-    }
-    assert(0 && "Can't get here");
-  }
-  case Pointer:
-    // FIXME: need to use TargetInfo again
-    return sizeof(void *) * 8;
-  case Reference:
-    // seems that sizeof(T&) == sizeof(T) -- spec reference?
-    return (cast<ReferenceType>(this)->getReferenceeType()->getSize());
-  case Complex:
-  case Array:
-  case Vector:
-  case FunctionNoProto:
-  case FunctionProto:
-  case TypeName:
-  case Tagged:
-    assert(0 && "Type sizes are not yet known, in general");
-  }
-  assert(0 && "Can't get here");
-}
-
 /// isVoidType - Helper method to determine if this is the 'void' type.
 bool Type::isVoidType() const {
   if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))

Modified: cfe/trunk/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExpr.cpp?rev=39835&r1=39834&r2=39835&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/CodeGen/CGExpr.cpp Fri Jul 13 17:13:22 2007
@@ -1152,7 +1152,7 @@
     QualType LHSElementType = LHSPtrType->getPointeeType();
     assert(LHSElementType == RHSPtrType->getPointeeType() &&
       "can't subtract pointers with differing element types");
-    unsigned ElementSize = LHSElementType->getSize() / 8;
+    unsigned ElementSize = getContext().getTypeSize(LHSElementType) / 8;
     const llvm::Type *ResultType = ConvertType(ResTy);
     llvm::Value *CastLHS = Builder.CreatePtrToInt(LHSValue, ResultType,
                                                   "sub.ptr.lhs.cast");

Modified: cfe/trunk/CodeGen/CodeGenTypes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenTypes.h?rev=39835&r1=39834&r2=39835&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenTypes.h (original)
+++ cfe/trunk/CodeGen/CodeGenTypes.h Fri Jul 13 17:13:22 2007
@@ -41,6 +41,7 @@
   void DecodeArgumentTypes(const FunctionTypeProto &FTP, 
                            std::vector<const llvm::Type*> &ArgTys);
 };
+
 }  // end namespace CodeGen
 }  // end namespace clang
 

Modified: cfe/trunk/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaDecl.cpp?rev=39835&r1=39834&r2=39835&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Fri Jul 13 17:13:22 2007
@@ -972,7 +972,7 @@
 }
 
 QualType Sema::HandleVectorTypeAttribute(QualType curType, 
-                                      AttributeList *rawAttr) {
+                                         AttributeList *rawAttr) {
   // check the attribute arugments.
   if (rawAttr->getNumArgs() != 1) {
     Diag(rawAttr->getAttributeLoc(), diag::err_attribute_wrong_number_arguments,
@@ -1005,8 +1005,7 @@
          curType.getCanonicalType().getAsString());
     return QualType();
   }
-  BuiltinType *baseType = cast<BuiltinType>(canonType);
-  unsigned typeSize = baseType->getSize();
+  unsigned typeSize = Context.getTypeSize(curType);
   // vecSize is specified in bytes - convert to bits.
   unsigned vectorSize = vecSize.getZExtValue() * 8; 
   

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

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Fri Jul 13 17:13:22 2007
@@ -54,6 +54,11 @@
   ~ASTContext();
   
   void PrintStats() const;
+  
+  /// getTypeSize - Return the size of the specified type, in bits.  This method
+  /// does not work on incomplete types.
+  unsigned getTypeSize(QualType T);
+  //TODO: unsigned getTypeAlign(QualType T);
 
   /// getComplexType - Return the uniqued reference to the type for a complex
   /// number with the specified element type.

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

==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Fri Jul 13 17:13:22 2007
@@ -196,9 +196,6 @@
   virtual ~Type();
   friend class ASTContext;
 public:
-  /// getSize - the number of bits to represent the type.
-  unsigned getSize() const;
-  
   TypeClass getTypeClass() const { return TC; }
   
   bool isCanonical() const { return CanonicalType.getTypePtr() == this; }





More information about the cfe-commits mailing list