]> granicus.if.org Git - clang/commitdiff
Rewrote type serialization to used the same methodology as we do for Decls.
authorTed Kremenek <kremenek@apple.com>
Tue, 13 Nov 2007 22:02:55 +0000 (22:02 +0000)
committerTed Kremenek <kremenek@apple.com>
Tue, 13 Nov 2007 22:02:55 +0000 (22:02 +0000)
Removed tons of dead code in ASTContext concerning how types use to be
serialized.
Removed serialization methods from QualType that are no longer used.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44070 91177308-0d34-0410-b5e6-96231b3b80d8

AST/ASTContext.cpp
AST/TypeSerialization.cpp
include/clang/AST/ASTContext.h
include/clang/AST/Type.h

index 1438992bd0593680380935df13bf1a1fca6d33fd..132224cbd70642b6d570a4838351e782f2934459 100644 (file)
@@ -1319,59 +1319,6 @@ bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
   return true; // should never get here...
 }
 
-
-template <typename T> static inline
-void EmitSet(const llvm::FoldingSet<T>& set, llvm::Serializer& S) {
-  S.EmitInt(set.size());
-  
-  for (typename llvm::FoldingSet<T>::const_iterator I=set.begin(), E=set.end();
-       I!=E; ++I)
-    S.EmitOwnedPtr(&*I);
-}
-
-template <typename T> static inline
-void ReadSet(llvm::FoldingSet<T>& set, std::vector<Type*>& V, 
-             llvm::Deserializer& D) {
-  
-  unsigned size = D.ReadInt();
-
-  for (unsigned i = 0 ; i < size; ++i) {
-    T* t = D.ReadOwnedPtr<T>();
-    set.GetOrInsertNode(t);
-    V.push_back(t);
-  }
-}
-
-template <typename T> static inline
-void EmitVector(const std::vector<T*>& V, llvm::Serializer& S) {
-  S.EmitInt(V.size());
-
-  for (typename std::vector<T*>::const_iterator I=V.begin(),E=V.end(); I!=E;++I)
-    S.EmitOwnedPtr(*I);
-}
-template <typename T> static inline
-void ReadVector(std::vector<T*>& V, std::vector<Type*>& Types, 
-                llvm::Deserializer& D) {
-  
-  unsigned size = D.ReadInt();
-  V.reserve(size);
-  
-  for (unsigned i = 0 ; i < size ; ++i) {
-    T* t = D.Create<T>();
-    V.push_back(t);
-    Types.push_back(t);
-  }
-}
-
-static inline void EmitBuiltin(llvm::Serializer& S, QualType Q) {
-  S.EmitPtr(Q.getTypePtr());  
-}
-
-static inline void RegisterBuiltin(llvm::Deserializer& D, QualType Q) {
-  D.RegisterPtr(Q.getTypePtr());
-}
-
 /// Emit - Serialize an ASTContext object to Bitcode.
 void ASTContext::Emit(llvm::Serializer& S) const {
   S.EmitRef(SourceMgr);
@@ -1383,131 +1330,10 @@ void ASTContext::Emit(llvm::Serializer& S) const {
   // when we reconstitute the ASTContext object.
   S.EmitInt(Types.size());
   
-  for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
-         I!=E; ++I) {
-    
-    Type* t = *I;    
-    Type::TypeClass k = t->getTypeClass();
-    
-    S.EmitInt(k);
-    
-    switch (k) {
-      default:
-        assert (false && "Serialization for type not supported.");
-        break;
-      
-      case Type::Builtin:
-        break;
-        
-      case Type::Complex:
-        S.Emit(cast<ComplexType>(t)->getElementType());
-        break;
-        
-      case Type::Pointer:
-        S.Emit(cast<PointerType>(t)->getPointeeType());
-        break;
-        
-      case Type::FunctionProto: {
-        FunctionTypeProto& FT = *cast<FunctionTypeProto>(t);
-        
-        S.Emit(FT.getResultType());
-        S.Emit(FT.isVariadic());
-        S.Emit(FT.getNumArgs());
-
-        for (FunctionTypeProto::arg_type_iterator
-              I=FT.arg_type_begin(), E=FT.arg_type_end(); I!=E; ++I)
-          S.Emit(*I);
+  for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end(); 
+                                          I!=E;++I)    
+    (*I)->Emit(S);
 
-        break;
-      }
-    }
-    
-    S.EmitPtr(t);
-  }
-  
-  
-  // Emit pointers to builtin types.  Although these objects will be
-  // reconsituted automatically when ASTContext is created, any pointers to them
-  // will not be (and will need to be patched).  Thus we must register them 
-  // with the Serializer anyway as pointed-to-objects, even if we won't 
-  // serialize them out using EmitOwnedPtr.  This "registration" will then
-  // be used by the Deserializer to backpatch references to the builtins.
-#if 0
-  {
-  EmitBuiltin(S,VoidTy);
-  EmitBuiltin(S,BoolTy);
-  EmitBuiltin(S,CharTy);
-  EmitBuiltin(S,SignedCharTy);
-  EmitBuiltin(S,ShortTy);
-  EmitBuiltin(S,IntTy);
-  EmitBuiltin(S,LongTy);
-  EmitBuiltin(S,LongLongTy);
-  EmitBuiltin(S,UnsignedCharTy);
-  EmitBuiltin(S,UnsignedShortTy);
-  EmitBuiltin(S,UnsignedIntTy);
-  EmitBuiltin(S,UnsignedLongTy);
-  EmitBuiltin(S,UnsignedLongLongTy);
-  EmitBuiltin(S,FloatTy);
-  EmitBuiltin(S,DoubleTy);
-  EmitBuiltin(S,LongDoubleTy);
-  EmitBuiltin(S,FloatComplexTy);
-  EmitBuiltin(S,DoubleComplexTy);
-  EmitBuiltin(S,LongDoubleComplexTy);
-  EmitBuiltin(S,VoidPtrTy);
-
-  // Emit the remaining types.
-
-  assert (ComplexTypes.size() >= 3);
-  S.EmitInt(ComplexTypes.size() - 3);
-  
-  if (ComplexTypes.size() > 3) {
-    
-    for (llvm::FoldingSet<ComplexType>::const_iterator
-           I=ComplexTypes.begin(), E=ComplexTypes.end(); I!=E; ++I) {
-      
-      const ComplexType* T = &*I;
-    
-      if (T != FloatComplexTy.getTypePtr() &&
-          T != DoubleComplexTy.getTypePtr() &&
-          T != LongDoubleComplexTy.getTypePtr())
-        S.EmitOwnedPtr(&*I);
-    }
-  }
-  
-  assert (PointerTypes.size() >= 1);  
-  S.EmitInt(PointerTypes.size() - 1);
-  
-  if (PointerTypes.size() > 1) {
-    
-    for (llvm::FoldingSet<PointerType>::const_iterator
-         I=PointerTypes.begin(), E=PointerTypes.end(); I!=E; ++I) {
-      
-      const PointerType* T = &*I;
-      
-      if (T != VoidPtrTy.getTypePtr())
-        S.EmitOwnedPtr(&*I);
-    }
-  }
-  
-  EmitSet(ReferenceTypes, S);
-  EmitSet(ConstantArrayTypes, S);
-  EmitSet(IncompleteVariableArrayTypes, S);
-  EmitVector(CompleteVariableArrayTypes, S);
-  EmitSet(VectorTypes,S);
-  EmitSet(FunctionTypeNoProtos,S);
-  EmitSet(FunctionTypeProtos,S);
-  // FIXME: EmitSet(ObjcQualifiedInterfaceTypes,S);
-  
-  S.Emit(BuiltinVaListType);
-  }
-#endif
-// FIXME:  S.Emit(ObjcIdType);
-// FIXME:  S.EmitPtr(IdStructType);
-// FIXME:  S.Emit(ObjcProtoType);
- // FIXME:  S.EmitPtr(ProtoStructType);
- // FIXME: S.Emit(ObjcClassType);
-// FIXME:  S.EmitPtr(ClassStructType);
-// FIXME:  S.Emit(ObjcConstantStringType);
   // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
 }
 
@@ -1521,106 +1347,9 @@ ASTContext* ASTContext::Create(llvm::Deserializer& D) {
   
   ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
   
-  for (unsigned i = 0; i < size_reserve; ++i) {
-    Type::TypeClass K = static_cast<Type::TypeClass>(D.ReadInt());
-    
-    switch (K) {
-      default:
-        assert (false && "Deserializaton for type not supported.");
-        break;
-        
-      case Type::Builtin:
-        assert (i < A->Types.size());
-        assert (isa<BuiltinType>(A->Types[i]));
-        D.RegisterPtr(A->Types[i]);
-        break;
-        
-      case Type::Complex: {
-        QualType ElementType;
-        D.Read(ElementType);
-        D.RegisterPtr(A->getComplexType(ElementType).getTypePtr());
-        break;
-      }
-        
-      case Type::Pointer: {
-        QualType PointeeType;
-        D.Read(PointeeType);
-        D.RegisterPtr(A->getPointerType(PointeeType).getTypePtr());
-        break;
-      }
-        
-      case Type::FunctionProto: {
-        QualType ResultType;
-        D.Read(ResultType);
-        
-        bool isVariadic = D.ReadBool();
-        
-        unsigned NumArgs = D.ReadInt();        
-        llvm::SmallVector<QualType,15> Args;
-        for (unsigned j = 0; j < NumArgs; ++j) { 
-          QualType Q;
-          D.Read(Q);
-          Args.push_back(Q);
-        }
-
-        D.RegisterPtr(A->getFunctionType(ResultType,&*Args.begin(),
-                                         NumArgs,isVariadic).getTypePtr());
-                       
-        break;
-      }
-
-    }
-  }
-  
-  // Register the addresses of the BuiltinTypes with the Deserializer.
-#if 0
-  {
-  RegisterBuiltin(D,A->VoidTy);
-  RegisterBuiltin(D,A->BoolTy);
-  RegisterBuiltin(D,A->CharTy);
-  RegisterBuiltin(D,A->SignedCharTy);
-  RegisterBuiltin(D,A->ShortTy);
-  RegisterBuiltin(D,A->IntTy);
-  RegisterBuiltin(D,A->LongTy);
-  RegisterBuiltin(D,A->LongLongTy);
-  RegisterBuiltin(D,A->UnsignedCharTy);
-  RegisterBuiltin(D,A->UnsignedShortTy);
-  RegisterBuiltin(D,A->UnsignedIntTy);
-  RegisterBuiltin(D,A->UnsignedLongTy);
-  RegisterBuiltin(D,A->UnsignedLongLongTy);
-  RegisterBuiltin(D,A->FloatTy);
-  RegisterBuiltin(D,A->DoubleTy);
-  RegisterBuiltin(D,A->LongDoubleTy);
-  RegisterBuiltin(D,A->FloatComplexTy);
-  RegisterBuiltin(D,A->DoubleComplexTy);
-  RegisterBuiltin(D,A->LongDoubleComplexTy);
-  RegisterBuiltin(D,A->VoidPtrTy);
-
-  // Deserialize all other types.  
-  ReadSet<ComplexType>(A->ComplexTypes, A->Types, D);
-  ReadSet(A->PointerTypes, A->Types, D);
-  ReadSet(A->ReferenceTypes, A->Types, D);
-  ReadSet(A->ConstantArrayTypes, A->Types, D);
-  ReadSet(A->IncompleteVariableArrayTypes, A->Types, D);
-  ReadVector(A->CompleteVariableArrayTypes, A->Types, D);
-  ReadSet(A->VectorTypes, A->Types, D);
-  ReadSet(A->FunctionTypeNoProtos, A->Types, D);
-  ReadSet(A->FunctionTypeProtos, A->Types, D);
-  // ReadSet(A->ObjcQualifiedInterfaceTypes,D);
+  for (unsigned i = 0; i < size_reserve; ++i)
+    Type::Create(*A,i,D);
 
-
-  
-  D.Read(A->BuiltinVaListType);
-  }
-#endif
-  
-// FIXME:  D.Read(A->ObjcIdType);
-// FIXME:  D.ReadPtr(A->IdStructType);
-// FIXME:  D.Read(A->ObjcProtoType);
-// FIXME:  D.ReadPtr(A->ProtoStructType);
-// FIXME:  D.Read(A->ObjcClassType);
-// FIXME:  D.ReadPtr(A->ClassStructType);
-// FIXME:  D.Read(A->ObjcConstantStringType);
   // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
   
   return A;
index 5bb31e7362fa1ed634457d37ab2411d8eca1d5d9..bef662e254b4f914c5911e8744017d0205a032a6 100644 (file)
 
 #include "clang/AST/Type.h"
 #include "clang/AST/Expr.h"
+#include "clang/AST/ASTContext.h"
 #include "llvm/Bitcode/Serialize.h"
 #include "llvm/Bitcode/Deserialize.h"
 
 using namespace clang;
+using llvm::Serializer;
+using llvm::Deserializer;
+using llvm::SerializedPtrID;
 
-void QualType::Emit(llvm::Serializer& S) const {
+
+void QualType::Emit(Serializer& S) const {
   S.EmitPtr(getAsOpaquePtr());
   S.EmitInt(getQualifiers());
 }
 
-void QualType::Read(llvm::Deserializer& D) {
-  D.ReadUIntPtr(ThePtr,false);
-  ThePtr |= D.ReadInt();
+QualType QualType::ReadVal(Deserializer& D) {
+  QualType Q;
+  D.ReadUIntPtr(Q.ThePtr,false);
+  Q.ThePtr |= D.ReadInt();
+  return Q;
 }
 
-void QualType::EmitOwned(llvm::Serializer& S) const {
-  S.EmitInt(getQualifiers());
-  S.EmitOwnedPtr(cast<BuiltinType>(getTypePtr()));
-}
+//===----------------------------------------------------------------------===//
+// Type Serialization: Dispatch code to handle specific types.
+//===----------------------------------------------------------------------===//
 
-void QualType::ReadOwned(llvm::Deserializer& D) {
-  ThePtr = D.ReadInt();
-  ThePtr |= reinterpret_cast<uintptr_t>(D.ReadOwnedPtr<BuiltinType>());
+void Type::Emit(Serializer& S) const {
+  S.EmitInt(getTypeClass());
+  S.EmitPtr(this);
+  
+  if (!isa<BuiltinType>(this))
+    EmitImpl(S);
 }
 
-/*  FIXME: Either remove this method or complete it.
+void Type::EmitImpl(Serializer& S) const {
+  assert (false && "Serializization for type not supported.");
+}
 
-void Type::Emit(llvm::Serializer& S) {
-  switch (getTypeClass()) {
+void Type::Create(ASTContext& Context, unsigned i, Deserializer& D) {
+  Type::TypeClass K = static_cast<Type::TypeClass>(D.ReadInt());
+  SerializedPtrID PtrID = D.ReadPtrID();  
+  
+  switch (K) {
     default:
-      assert (false && "Serialization for type class not implemented.");
+      assert (false && "Deserialization for type not supported.");
       break;
-      
+            
     case Type::Builtin:
-      cast<BuiltinType>(this)->Emit(S);
+      assert (i < Context.getTypes().size());
+      assert (isa<BuiltinType>(Context.getTypes()[i]));
+      D.RegisterPtr(PtrID,Context.getTypes()[i]); 
       break;
+      
+    case Type::Complex:
+      D.RegisterPtr(PtrID,ComplexType::CreateImpl(Context,D));
+      break;
+      
+    case Type::FunctionProto:
+      D.RegisterPtr(PtrID,FunctionTypeProto::CreateImpl(Context,D));
+      break;
+      
+    case Type::Pointer:
+      D.RegisterPtr(PtrID,PointerType::CreateImpl(Context,D));
+      break;      
   }
 }
- */
-
-void Type::EmitTypeInternal(llvm::Serializer& S) const {
-  S.Emit(CanonicalType);
-}
-
-void Type::ReadTypeInternal(llvm::Deserializer& D) {
-  D.Read(CanonicalType);
-}
-
-void BuiltinType::Emit(llvm::Serializer& S) const {
-  S.EmitInt(TypeKind);
-}
-
-BuiltinType* BuiltinType::Create(llvm::Deserializer& D) {
-  Kind k = static_cast<Kind>(D.ReadInt());
-  BuiltinType* T = new BuiltinType(k);
-  return T;
-}
-
-
-
-void ComplexType::Emit(llvm::Serializer& S) const {
-  EmitTypeInternal(S);
-  S.Emit(ElementType);
-}
-
-ComplexType* ComplexType::Create(llvm::Deserializer& D) {
-  ComplexType* T = new ComplexType(QualType(),QualType());
-  T->ReadTypeInternal(D);
-  D.Read(T->ElementType);
-  return T;
-}
-
-void PointerType::Emit(llvm::Serializer& S) const {
-  EmitTypeInternal(S);
-  S.Emit(PointeeType);
-}
-
-PointerType* PointerType::Create(llvm::Deserializer& D) {
-  PointerType* T = new PointerType(QualType(),QualType());
-  T->ReadTypeInternal(D);
-  D.Read(T->PointeeType);
-  return T;
-}
-
-void ReferenceType::Emit(llvm::Serializer& S) const {
-  EmitTypeInternal(S);
-  S.Emit(ReferenceeType);
-}
 
-ReferenceType* ReferenceType::Create(llvm::Deserializer& D) {
-  ReferenceType* T = new ReferenceType(QualType(),QualType());
-  T->ReadTypeInternal(D);
-  D.Read(T->ReferenceeType);
-  return T;
-}
+//===----------------------------------------------------------------------===//
+// ComplexType
+//===----------------------------------------------------------------------===//
 
-void ArrayType::EmitArrayTypeInternal(llvm::Serializer& S) const {
-  EmitTypeInternal(S);
-  S.Emit(ElementType);
-  S.EmitInt(SizeModifier);
-  S.EmitInt(IndexTypeQuals);
+void ComplexType::EmitImpl(Serializer& S) const {
+  S.Emit(getElementType());
 }
 
-void ArrayType::ReadArrayTypeInternal(llvm::Deserializer& D) {
-  ReadTypeInternal(D);
-  D.Read(ElementType);
-  SizeModifier = static_cast<ArraySizeModifier>(D.ReadInt());
-  IndexTypeQuals = D.ReadInt();
+Type* ComplexType::CreateImpl(ASTContext& Context, Deserializer& D) {
+  return Context.getComplexType(QualType::ReadVal(D)).getTypePtr();
 }
 
-void ConstantArrayType::Emit(llvm::Serializer& S) const {
-  EmitArrayTypeInternal(S);
-  S.Emit(Size);
-}
+//===----------------------------------------------------------------------===//
+// FunctionTypeProto
+//===----------------------------------------------------------------------===//
 
-ConstantArrayType* ConstantArrayType::Create(llvm::Deserializer& D) {
-  // "Default" construct the array type.
-  ConstantArrayType* T =
-    new ConstantArrayType(QualType(), QualType(), llvm::APInt(), 
-                          ArrayType::Normal, 0);
+void FunctionTypeProto::EmitImpl(Serializer& S) const {
+  S.Emit(getResultType());
+  S.EmitBool(isVariadic());
+  S.EmitInt(getNumArgs());
   
-  // Deserialize the internal values.
-  T->ReadArrayTypeInternal(D);  
-  D.Read(T->Size);
-
-  return T;
-}
-
-void VariableArrayType::Emit(llvm::Serializer& S) const {
-  EmitArrayTypeInternal(S);
-  S.EmitOwnedPtr(SizeExpr);
+  for (arg_type_iterator I=arg_type_begin(), E=arg_type_end(); I!=E; ++I)
+    S.Emit(*I);
 }
 
-VariableArrayType* VariableArrayType::Create(llvm::Deserializer& D) {
-  // "Default" construct the array type.
-  VariableArrayType* T =
-    new VariableArrayType(QualType(), QualType(), NULL, ArrayType::Normal, 0);
-  
-  // Deserialize the internal values.
-  T->ReadArrayTypeInternal(D);
-  T->SizeExpr = D.ReadOwnedPtr<Expr>();
+Type* FunctionTypeProto::CreateImpl(ASTContext& Context, Deserializer& D) {
+  QualType ResultType = QualType::ReadVal(D);
+  bool isVariadic = D.ReadBool();
+  unsigned NumArgs = D.ReadInt();
   
-  return T;
-}
-
-void VectorType::Emit(llvm::Serializer& S) const {
-  EmitTypeInternal(S);
-  S.Emit(ElementType);
-  S.EmitInt(NumElements);
-}
-
-VectorType* VectorType::Create(llvm::Deserializer& D) {
-  VectorType* T = new VectorType(QualType(),0,QualType());
-  T->ReadTypeInternal(D);
-  D.Read(T->ElementType);
-  T->NumElements = D.ReadInt();
-  return T;
-}
+  llvm::SmallVector<QualType,15> Args;
 
-void FunctionType::EmitFunctionTypeInternal(llvm::Serializer &S) const {
-  EmitTypeInternal(S);
-  S.EmitBool(SubClassData);
-  S.Emit(ResultType);
-}
+  for (unsigned j = 0; j < NumArgs; ++j)
+    Args.push_back(QualType::ReadVal(D));
 
-void FunctionType::ReadFunctionTypeInternal(llvm::Deserializer& D) {
-  ReadTypeInternal(D);
-  SubClassData = D.ReadBool();
-  D.Read(ResultType);
+  return Context.getFunctionType(ResultType,&*Args.begin(), 
+                           NumArgs,isVariadic).getTypePtr();
 }
 
+//===----------------------------------------------------------------------===//
+// PointerType
+//===----------------------------------------------------------------------===//
 
-FunctionTypeNoProto* FunctionTypeNoProto::Create(llvm::Deserializer& D) {
-  FunctionTypeNoProto* T = new FunctionTypeNoProto(QualType(),QualType());
-  T->ReadFunctionTypeInternal(D);
-  return T;
-}
-
-void FunctionTypeProto::Emit(llvm::Serializer& S) const {
-  S.EmitInt(NumArgs);
-  EmitFunctionTypeInternal(S);
-  
-  for (arg_type_iterator i = arg_type_begin(), e = arg_type_end(); i!=e; ++i)
-    S.Emit(*i);    
-}
-
-FunctionTypeProto* FunctionTypeProto::Create(llvm::Deserializer& D) {
-  unsigned NumArgs = D.ReadInt();
-  
-  FunctionTypeProto *FTP = 
-  (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + 
-                             NumArgs*sizeof(QualType));
-  
-  // Default construct.  Internal fields will be populated using
-  // deserialization.
-  new (FTP) FunctionTypeProto();
-  
-  FTP->NumArgs = NumArgs;
-  FTP->ReadFunctionTypeInternal(D);
-  
-  // Fill in the trailing argument array.
-  QualType *ArgInfo = reinterpret_cast<QualType *>(FTP+1);;
-
-  for (unsigned i = 0; i != NumArgs; ++i)
-    D.Read(ArgInfo[i]);
-  
-  return FTP;
-}
-
-void TypedefType::Emit(llvm::Serializer& S) const {
-  EmitTypeInternal(S);
-  S.EmitPtr(Decl);
+void PointerType::EmitImpl(Serializer& S) const {
+  S.Emit(getPointeeType());
 }
 
-TypedefType* TypedefType::Create(llvm::Deserializer& D) {
-  TypedefType* T = new TypedefType(NULL,QualType());
-  T->ReadTypeInternal(D);
-  D.ReadPtr(T->Decl);
-  return T;
+Type* PointerType::CreateImpl(ASTContext& Context, Deserializer& D) {
+  return Context.getPointerType(QualType::ReadVal(D)).getTypePtr();
 }
index ae3ff0c2216ae60e67d94d287b4b978c700b4800..c4a7c85c6b32435af0cfa15f6b952db6f354f692 100644 (file)
@@ -105,7 +105,8 @@ public:
   ~ASTContext();
   
   void PrintStats() const;
+  const std::vector<Type*>& getTypes() const { return Types; }
+  
   //===--------------------------------------------------------------------===//
   //                           Type Constructors
   //===--------------------------------------------------------------------===//
index a264c24c89d236e89e3232cb20378996a1f6c0d4..900f261fe2804811b607e0b726fe262f9630bdc6 100644 (file)
@@ -156,28 +156,11 @@ public:
   /// appropriate type qualifiers on it.
   inline QualType getCanonicalType() const;
   
-  /// Emit - Serialize a QualType using a Bitcode Serializer.
+  /// Emit - Serialize a QualType to Bitcode.
   void Emit(llvm::Serializer& S) const;
   
-  /// Read - Deserialize a QualType using a Bitcode Deserializer.  This
-  ///  deserialization requires that a QualType be default constructed
-  ///  first.  This is because internally the deserialization relies on
-  ///  pointer backpatching performed by the Deserializer.  Deserialization
-  ///  of a QualType should only be done on an instance of QualType that
-  ///  exists, in place, within its containing object.
-  void Read(llvm::Deserializer& D);
-  
-  static inline QualType ReadVal(llvm::Deserializer& D) {
-    QualType Q;
-    Q.Read(D);
-    return Q;
-  }
-  
-  /// EmitOwned - Serialize a QualType that owns the underlying Type*.
-  void EmitOwned(llvm::Serializer& S) const;
-  
-  /// ReadOwned - Deserialize a QualType that owns the underlying Thpe*.
-  void ReadOwned(llvm::Deserializer& S);
+  /// Read - Deserialize a QualType from Bitcode.
+  static QualType ReadVal(llvm::Deserializer& D);
 };
 
 } // end clang.
@@ -359,6 +342,19 @@ private:
 public:
   virtual void getAsStringInternal(std::string &InnerString) const = 0;
   static bool classof(const Type *) { return true; }
+  
+protected:  
+  /// Emit - Emit a Type to bitcode.  Used by ASTContext.
+  void Emit(llvm::Serializer& S) const;
+  
+  /// Create - Construct a Type from bitcode.  Used by ASTContext.
+  static void Create(ASTContext& Context, unsigned i, llvm::Deserializer& S);
+  
+  /// EmitImpl - Subclasses must implement this method in order to
+  ///  be serialized.
+  virtual void EmitImpl(llvm::Serializer& S) const;  
+  
+  friend class ASTCotnext;
 };
 
 /// BuiltinType - This class is used for builtin types like 'int'.  Builtin
@@ -397,9 +393,6 @@ public:
   
   static bool classof(const Type *T) { return T->getTypeClass() == Builtin; }
   static bool classof(const BuiltinType *) { return true; }
-  
-  void Emit(llvm::Serializer& S) const;
-  static BuiltinType* Create(llvm::Deserializer& D);
 };
 
 /// ComplexType - C99 6.2.5p11 - Complex values.  This supports the C99 complex
@@ -415,8 +408,7 @@ public:
   QualType getElementType() const { return ElementType; }
   
   virtual void getAsStringInternal(std::string &InnerString) const;
-  
-  
+    
   void Profile(llvm::FoldingSetNodeID &ID) {
     Profile(ID, getElementType());
   }
@@ -427,8 +419,10 @@ public:
   static bool classof(const Type *T) { return T->getTypeClass() == Complex; }
   static bool classof(const ComplexType *) { return true; }
   
-  void Emit(llvm::Serializer& S) const;
-  static ComplexType* Create(llvm::Deserializer& D);
+protected:  
+  virtual void EmitImpl(llvm::Serializer& S) const;
+  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
+  friend class Type;
 };
 
 
@@ -456,9 +450,11 @@ public:
   
   static bool classof(const Type *T) { return T->getTypeClass() == Pointer; }
   static bool classof(const PointerType *) { return true; }
-  
-  void Emit(llvm::Serializer& S) const;
-  static PointerType* Create(llvm::Deserializer& D);
+
+protected:  
+  virtual void EmitImpl(llvm::Serializer& S) const;
+  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
+  friend class Type;
 };
 
 /// ReferenceType - C++ 8.3.2 - Reference Declarators.
@@ -483,9 +479,6 @@ public:
 
   static bool classof(const Type *T) { return T->getTypeClass() == Reference; }
   static bool classof(const ReferenceType *) { return true; }
-  
-  void Emit(llvm::Serializer& S) const;
-  static ReferenceType* Create(llvm::Deserializer& D);
 };
 
 /// ArrayType - C99 6.7.5.2 - Array Declarators.
@@ -532,10 +525,6 @@ public:
            T->getTypeClass() == VariableArray;
   }
   static bool classof(const ArrayType *) { return true; }
-  
-protected:
-  void EmitArrayTypeInternal(llvm::Serializer& S) const;
-  void ReadArrayTypeInternal(llvm::Deserializer& S);
 };
 
 class ConstantArrayType : public ArrayType {
@@ -573,9 +562,6 @@ public:
     return T->getTypeClass() == ConstantArray; 
   }
   static bool classof(const ConstantArrayType *) { return true; }
-  
-  void Emit(llvm::Serializer& S) const;
-  static ConstantArrayType* Create(llvm::Deserializer& D);
 };
 
 // FIXME: VariableArrayType's aren't uniqued (since expressions aren't).
@@ -610,9 +596,6 @@ public:
   static void Profile(llvm::FoldingSetNodeID &ID, QualType ET) {
     ID.AddPointer(ET.getAsOpaquePtr());
   }
-  
-  void Emit(llvm::Serializer& S) const;
-  static VariableArrayType* Create(llvm::Deserializer& D);
 };
 
 /// VectorType - GCC generic vector type. This type is created using
@@ -653,9 +636,6 @@ public:
     return T->getTypeClass() == Vector || T->getTypeClass() == OCUVector; 
   }
   static bool classof(const VectorType *) { return true; }
-  
-  void Emit(llvm::Serializer& S) const;
-  static VectorType* Create(llvm::Deserializer& D);
 };
 
 /// OCUVectorType - Extended vector type. This type is created using
@@ -739,10 +719,6 @@ public:
            T->getTypeClass() == FunctionProto;
   }
   static bool classof(const FunctionType *) { return true; }
-  
-protected:
-  void EmitFunctionTypeInternal(llvm::Serializer& S) const;
-  void ReadFunctionTypeInternal(llvm::Deserializer& D);
 };
 
 /// FunctionTypeNoProto - Represents a K&R-style 'int foo()' function, which has
@@ -767,9 +743,6 @@ public:
     return T->getTypeClass() == FunctionNoProto;
   }
   static bool classof(const FunctionTypeNoProto *) { return true; }
-  
-  void Emit(llvm::Serializer& S) const { EmitFunctionTypeInternal(S); }
-  static FunctionTypeNoProto* Create(llvm::Deserializer& D);
 };
 
 /// FunctionTypeProto - Represents a prototype with argument type info, e.g.
@@ -818,14 +791,11 @@ public:
   static void Profile(llvm::FoldingSetNodeID &ID, QualType Result,
                       arg_type_iterator ArgTys, unsigned NumArgs,
                       bool isVariadic);
-  
-  void Emit(llvm::Serializer& S) const;
-  static FunctionTypeProto* Create(llvm::Deserializer& D);
-  
-protected:
-  // Used by deserialization.
-  FunctionTypeProto() 
-  : FunctionType(FunctionProto, QualType(), false, QualType()) {}
+
+protected:  
+  virtual void EmitImpl(llvm::Serializer& S) const;
+  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
+  friend class Type;
 };
 
 
@@ -851,9 +821,6 @@ public:
 
   static bool classof(const Type *T) { return T->getTypeClass() == TypeName; }
   static bool classof(const TypedefType *) { return true; }
-  
-  void Emit(llvm::Serializer& S) const;
-  static TypedefType* Create(llvm::Deserializer& D);
 };
 
 /// TypeOfExpr (GCC extension).