[llvm-branch-commits] [cfe-branch] r122476 - in /cfe/branches/Apple/whitney: include/clang/AST/Type.h lib/AST/Type.cpp

Daniel Dunbar daniel at zuster.org
Wed Dec 22 21:40:10 PST 2010


Author: ddunbar
Date: Wed Dec 22 23:40:10 2010
New Revision: 122476

URL: http://llvm.org/viewvc/llvm-project?rev=122476&view=rev
Log:
Merge r120562:
--
Author: John McCall <rjmccall at apple.com>
Date:   Wed Dec 1 08:12:46 2010 +0000

    Eliminate vtables from the Type hierarchy.

Modified:
    cfe/branches/Apple/whitney/include/clang/AST/Type.h
    cfe/branches/Apple/whitney/lib/AST/Type.cpp

Modified: cfe/branches/Apple/whitney/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/include/clang/AST/Type.h?rev=122476&r1=122475&r2=122476&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/include/clang/AST/Type.h (original)
+++ cfe/branches/Apple/whitney/include/clang/AST/Type.h Wed Dec 22 23:40:10 2010
@@ -789,6 +789,7 @@
   /// Bitfields required by the Type class.
   class TypeBitfields {
     friend class Type;
+    template <class T> friend class TypePropertyCache;
 
     /// TypeClass bitfield - Enum that specifies what subclass this belongs to.
     unsigned TC : 8;
@@ -954,36 +955,9 @@
     TypeBits.FromAST = V;
   }
 
-  void ensureCachedProperties() const;
+  template <class T> friend class TypePropertyCache;
 
 protected:
-  /// \brief Compute the cached properties of this type.
-  class CachedProperties {
-    char linkage;
-    char visibility;
-    bool local;
-
-  public:
-    CachedProperties(Linkage linkage, Visibility visibility, bool local)
-      : linkage(linkage), visibility(visibility), local(local) {}
-
-    Linkage getLinkage() const { return (Linkage) linkage; }
-    Visibility getVisibility() const { return (Visibility) visibility; }
-    bool hasLocalOrUnnamedType() const { return local; }
-
-    friend CachedProperties merge(CachedProperties L, CachedProperties R) {
-      return CachedProperties(minLinkage(L.getLinkage(), R.getLinkage()),
-                         minVisibility(L.getVisibility(), R.getVisibility()),
-                      L.hasLocalOrUnnamedType() | R.hasLocalOrUnnamedType());
-    }
-  };
-
-  virtual CachedProperties getCachedProperties() const;
-  static CachedProperties getCachedProperties(QualType T) {
-    return getCachedProperties(T.getTypePtr());
-  }
-  static CachedProperties getCachedProperties(const Type *T);
-
   // silence VC++ warning C4355: 'this' : used in base member initializer list
   Type *this_() { return this; }
   Type(TypeClass tc, QualType Canonical, bool Dependent, bool VariablyModified)
@@ -996,7 +970,6 @@
     TypeBits.CachedLinkage = NoLinkage;
     TypeBits.FromAST = false;
   }
-  virtual ~Type();
   friend class ASTContext;
 
   void setDependent(bool D = true) { TypeBits.Dependent = D; }
@@ -1324,9 +1297,6 @@
     ObjCSel    // This represents the ObjC 'SEL' type.
   };
 
-protected:
-  virtual CachedProperties getCachedProperties() const;
-  
 public:
   BuiltinType(Kind K)
     : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent),
@@ -1380,9 +1350,6 @@
   }
   friend class ASTContext;  // ASTContext creates these.
 
-protected:
-  virtual CachedProperties getCachedProperties() const;
-  
 public:
   QualType getElementType() const { return ElementType; }
 
@@ -1412,9 +1379,6 @@
   }
   friend class ASTContext;  // ASTContext creates these.
 
-protected:
-  virtual CachedProperties getCachedProperties() const;
-  
 public:
 
   QualType getPointeeType() const { return PointeeType; }
@@ -1446,9 +1410,6 @@
   }
   friend class ASTContext;  // ASTContext creates these.
   
-protected:
-  virtual CachedProperties getCachedProperties() const;
-  
 public:
 
   // Get the pointee type. Pointee is required to always be a function type.
@@ -1484,8 +1445,6 @@
     ReferenceTypeBits.InnerRef = Referencee->isReferenceType();
   }
   
-  virtual CachedProperties getCachedProperties() const;
-  
 public:
   bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; }
   bool isInnerRef() const { return ReferenceTypeBits.InnerRef; }
@@ -1567,9 +1526,6 @@
   }
   friend class ASTContext; // ASTContext creates these.
   
-protected:
-  virtual CachedProperties getCachedProperties() const;
-  
 public:
   QualType getPointeeType() const { return PointeeType; }
 
@@ -1637,8 +1593,6 @@
 
   friend class ASTContext;  // ASTContext creates these.
 
-  virtual CachedProperties getCachedProperties() const;
-  
 public:
   QualType getElementType() const { return ElementType; }
   ArraySizeModifier getSizeModifier() const {
@@ -1940,8 +1894,6 @@
   }
   friend class ASTContext;  // ASTContext creates these.
   
-  virtual CachedProperties getCachedProperties() const;
-  
 public:
 
   QualType getElementType() const { return ElementType; }
@@ -2043,8 +1995,6 @@
 /// class of FunctionNoProtoType and FunctionProtoType.
 ///
 class FunctionType : public Type {
-  virtual void ANCHOR(); // Key function for FunctionType.
-
   // The type returned by the function.
   QualType ResultType;
 
@@ -2169,9 +2119,6 @@
                    Info) {}
   friend class ASTContext;  // ASTContext creates these.
   
-protected:
-  virtual CachedProperties getCachedProperties() const;
-  
 public:
   // No additional state past what FunctionType provides.
 
@@ -2225,9 +2172,6 @@
 
   friend class ASTContext;  // ASTContext creates these.
 
-protected:
-  virtual CachedProperties getCachedProperties() const;
-  
 public:
   unsigned getNumArgs() const { return NumArgs; }
   QualType getArgType(unsigned i) const {
@@ -2468,8 +2412,6 @@
 protected:
   TagType(TypeClass TC, const TagDecl *D, QualType can);
 
-  virtual CachedProperties getCachedProperties() const;
-  
 public:
   TagDecl *getDecl() const;
 
@@ -2843,8 +2785,6 @@
   }
 
 public:
-  virtual ~TypeWithKeyword(); // pin vtable to Type.cpp
-
   ElaboratedTypeKeyword getKeyword() const {
     return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword);
   }
@@ -2969,8 +2909,6 @@
   friend class ASTContext;  // ASTContext creates these
 
 public:
-  virtual ~DependentNameType();
-
   /// \brief Retrieve the qualification on this type.
   NestedNameSpecifier *getQualifier() const { return NNS; }
 
@@ -3037,8 +2975,6 @@
   friend class ASTContext;  // ASTContext creates these
 
 public:
-  virtual ~DependentTemplateSpecializationType();
-
   NestedNameSpecifier *getQualifier() const { return NNS; }
   const IdentifierInfo *getIdentifier() const { return Name; }
 
@@ -3130,9 +3066,6 @@
     ObjCObjectTypeBits.NumProtocols = 0;
   }
 
-protected:
-  CachedProperties getCachedProperties() const; // key function
-  
 public:
   /// getBaseType - Gets the base type of this object type.  This is
   /// always (possibly sugar for) one of:
@@ -3240,9 +3173,6 @@
       Decl(const_cast<ObjCInterfaceDecl*>(D)) {}
   friend class ASTContext;  // ASTContext creates these.
 
-protected:
-  virtual CachedProperties getCachedProperties() const;
-  
 public:
   /// getDecl - Get the declaration of this interface.
   ObjCInterfaceDecl *getDecl() const { return Decl; }
@@ -3292,9 +3222,6 @@
       PointeeType(Pointee) {}
   friend class ASTContext;  // ASTContext creates these.
 
-protected:
-  virtual CachedProperties getCachedProperties() const;
-  
 public:
   /// getPointeeType - Gets the type pointed to by this ObjC pointer.
   /// The result will always be an ObjCObjectType or sugar thereof.

Modified: cfe/branches/Apple/whitney/lib/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/AST/Type.cpp?rev=122476&r1=122475&r2=122476&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/AST/Type.cpp (original)
+++ cfe/branches/Apple/whitney/lib/AST/Type.cpp Wed Dec 22 23:40:10 2010
@@ -19,6 +19,7 @@
 #include "clang/AST/DeclTemplate.h"
 #include "clang/AST/Expr.h"
 #include "clang/AST/PrettyPrinter.h"
+#include "clang/AST/TypeVisitor.h"
 #include "clang/Basic/Specifiers.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/raw_ostream.h"
@@ -35,8 +36,6 @@
   return false;
 }
 
-Type::~Type() { }
-
 unsigned ConstantArrayType::getNumAddressingBits(ASTContext &Context,
                                                  QualType ElementType,
                                                const llvm::APInt &NumElements) {
@@ -797,9 +796,6 @@
   }
 }
 
-TypeWithKeyword::~TypeWithKeyword() {
-}
-
 ElaboratedTypeKeyword
 TypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) {
   switch (TypeSpec) {
@@ -876,10 +872,6 @@
   }
 }
 
-ElaboratedType::~ElaboratedType() {}
-DependentNameType::~DependentNameType() {}
-DependentTemplateSpecializationType::~DependentTemplateSpecializationType() {}
-
 DependentTemplateSpecializationType::DependentTemplateSpecializationType(
                          ElaboratedTypeKeyword Keyword,
                          NestedNameSpecifier *NNS, const IdentifierInfo *Name,
@@ -968,8 +960,6 @@
   }
 }
 
-void FunctionType::ANCHOR() {} // Key function for FunctionType.
-
 QualType QualType::getNonLValueExprType(ASTContext &Context) const {
   if (const ReferenceType *RefType = getTypePtr()->getAs<ReferenceType>())
     return RefType->getPointeeType();
@@ -1276,154 +1266,194 @@
   Profile(ID, getBaseType(), qual_begin(), getNumProtocols());
 }
 
-void Type::ensureCachedProperties() const {
-  if (!TypeBits.isCacheValid()) {
-    CachedProperties Result = getCachedProperties();
-    TypeBits.CacheValidAndVisibility = Result.getVisibility() + 1U;
-    assert(TypeBits.isCacheValid() &&
-           TypeBits.getVisibility() == Result.getVisibility());
-    TypeBits.CachedLinkage = Result.getLinkage();
-    TypeBits.CachedLocalOrUnnamed = Result.hasLocalOrUnnamedType();
-  }  
+namespace {
+
+/// \brief The cached properties of a type.
+class CachedProperties {
+  char linkage;
+  char visibility;
+  bool local;
+  
+public:
+  CachedProperties(Linkage linkage, Visibility visibility, bool local)
+    : linkage(linkage), visibility(visibility), local(local) {}
+  
+  Linkage getLinkage() const { return (Linkage) linkage; }
+  Visibility getVisibility() const { return (Visibility) visibility; }
+  bool hasLocalOrUnnamedType() const { return local; }
+  
+  friend CachedProperties merge(CachedProperties L, CachedProperties R) {
+    return CachedProperties(minLinkage(L.getLinkage(), R.getLinkage()),
+                            minVisibility(L.getVisibility(), R.getVisibility()),
+                         L.hasLocalOrUnnamedType() | R.hasLocalOrUnnamedType());
+  }
+};
+}
+
+static CachedProperties computeCachedProperties(const Type *T);
+
+namespace clang {
+/// The type-property cache.  This is templated so as to be
+/// instantiated at an internal type to prevent unnecessary symbol
+/// leakage.
+template <class Private> class TypePropertyCache {
+public:
+  static CachedProperties get(QualType T) {
+    return get(T.getTypePtr());
+  }
+
+  static CachedProperties get(const Type *T) {
+    ensure(T);
+    return CachedProperties(T->TypeBits.getLinkage(),
+                            T->TypeBits.getVisibility(),
+                            T->TypeBits.hasLocalOrUnnamedType());
+  }
+
+  static void ensure(const Type *T) {
+    // If the cache is valid, we're okay.
+    if (T->TypeBits.isCacheValid()) return;
+
+    // If this type is non-canonical, ask its canonical type for the
+    // relevant information.
+    if (QualType(T, 0) != T->CanonicalType) {
+      const Type *CT = T->CanonicalType.getTypePtr();
+      ensure(CT);
+      T->TypeBits.CacheValidAndVisibility =
+        CT->TypeBits.CacheValidAndVisibility;
+      T->TypeBits.CachedLinkage = CT->TypeBits.CachedLinkage;
+      T->TypeBits.CachedLocalOrUnnamed = CT->TypeBits.CachedLocalOrUnnamed;
+      return;
+    }
+
+    // Compute the cached properties and then set the cache.
+    CachedProperties Result = computeCachedProperties(T);
+    T->TypeBits.CacheValidAndVisibility = Result.getVisibility() + 1U;
+    assert(T->TypeBits.isCacheValid() &&
+           T->TypeBits.getVisibility() == Result.getVisibility());
+    T->TypeBits.CachedLinkage = Result.getLinkage();
+    T->TypeBits.CachedLocalOrUnnamed = Result.hasLocalOrUnnamedType();
+  }
+};
+}
+
+// Instantiate the friend template at a private class.  In a
+// reasonable implementation, these symbols will be internal.
+// It is terrible that this is the best way to accomplish this.
+namespace { class Private {}; }
+typedef TypePropertyCache<Private> Cache;
+
+static CachedProperties computeCachedProperties(const Type *T) {
+  switch (T->getTypeClass()) {
+#define TYPE(Class,Base)
+#define NON_CANONICAL_TYPE(Class,Base) case Type::Class:
+#include "clang/AST/TypeNodes.def"
+    llvm_unreachable("didn't expect a non-canonical type here");
+
+#define TYPE(Class,Base)
+#define DEPENDENT_TYPE(Class,Base) case Type::Class:
+#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class:
+#include "clang/AST/TypeNodes.def"
+    // Treat dependent types as external.
+    assert(T->isDependentType());
+    return CachedProperties(ExternalLinkage, DefaultVisibility, false);
+
+  case Type::Builtin:
+    // C++ [basic.link]p8:
+    //   A type is said to have linkage if and only if:
+    //     - it is a fundamental type (3.9.1); or
+    return CachedProperties(ExternalLinkage, DefaultVisibility, false);
+
+  case Type::Record:
+  case Type::Enum: {
+    const TagDecl *Tag = cast<TagType>(T)->getDecl();
+
+    // C++ [basic.link]p8:
+    //     - it is a class or enumeration type that is named (or has a name
+    //       for linkage purposes (7.1.3)) and the name has linkage; or
+    //     -  it is a specialization of a class template (14); or
+    NamedDecl::LinkageInfo LV = Tag->getLinkageAndVisibility();
+    bool IsLocalOrUnnamed =
+      Tag->getDeclContext()->isFunctionOrMethod() ||
+      (!Tag->getIdentifier() && !Tag->getTypedefForAnonDecl());
+    return CachedProperties(LV.linkage(), LV.visibility(), IsLocalOrUnnamed);
+  }
+
+    // C++ [basic.link]p8:
+    //   - it is a compound type (3.9.2) other than a class or enumeration, 
+    //     compounded exclusively from types that have linkage; or
+  case Type::Complex:
+    return Cache::get(cast<ComplexType>(T)->getElementType());
+  case Type::Pointer:
+    return Cache::get(cast<PointerType>(T)->getPointeeType());
+  case Type::BlockPointer:
+    return Cache::get(cast<BlockPointerType>(T)->getPointeeType());
+  case Type::LValueReference:
+  case Type::RValueReference:
+    return Cache::get(cast<ReferenceType>(T)->getPointeeType());
+  case Type::MemberPointer: {
+    const MemberPointerType *MPT = cast<MemberPointerType>(T);
+    return merge(Cache::get(MPT->getClass()),
+                 Cache::get(MPT->getPointeeType()));
+  }
+  case Type::ConstantArray:
+  case Type::IncompleteArray:
+  case Type::VariableArray:
+    return Cache::get(cast<ArrayType>(T)->getElementType());
+  case Type::Vector:
+  case Type::ExtVector:
+    return Cache::get(cast<VectorType>(T)->getElementType());
+  case Type::FunctionNoProto:
+    return Cache::get(cast<FunctionType>(T)->getResultType());
+  case Type::FunctionProto: {
+    const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
+    CachedProperties result = Cache::get(FPT->getResultType());
+    for (FunctionProtoType::arg_type_iterator ai = FPT->arg_type_begin(),
+           ae = FPT->arg_type_end(); ai != ae; ++ai)
+      result = merge(result, Cache::get(*ai));
+    return result;
+  }
+  case Type::ObjCInterface: {
+    NamedDecl::LinkageInfo LV =
+      cast<ObjCInterfaceType>(T)->getDecl()->getLinkageAndVisibility();
+    return CachedProperties(LV.linkage(), LV.visibility(), false);
+  }
+  case Type::ObjCObject:
+    return Cache::get(cast<ObjCObjectType>(T)->getBaseType());
+  case Type::ObjCObjectPointer:
+    return Cache::get(cast<ObjCObjectPointerType>(T)->getPointeeType());
+  }
+
+  llvm_unreachable("unhandled type class");
+
+  // C++ [basic.link]p8:
+  //   Names not covered by these rules have no linkage.
+  return CachedProperties(NoLinkage, DefaultVisibility, false);
 }
 
 /// \brief Determine the linkage of this type.
 Linkage Type::getLinkage() const {
-  if (this != CanonicalType.getTypePtr())
-    return CanonicalType->getLinkage();
-
-  ensureCachedProperties();
+  Cache::ensure(this);
   return TypeBits.getLinkage();
 }
 
 /// \brief Determine the linkage of this type.
 Visibility Type::getVisibility() const {
-  if (this != CanonicalType.getTypePtr())
-    return CanonicalType->getVisibility();
-
-  ensureCachedProperties();
+  Cache::ensure(this);
   return TypeBits.getVisibility();
 }
 
 bool Type::hasUnnamedOrLocalType() const {
-  if (this != CanonicalType.getTypePtr())
-    return CanonicalType->hasUnnamedOrLocalType();
-
-  ensureCachedProperties();
+  Cache::ensure(this);
   return TypeBits.hasLocalOrUnnamedType();
 }
 
 std::pair<Linkage,Visibility> Type::getLinkageAndVisibility() const {
-  if (this != CanonicalType.getTypePtr())
-    return CanonicalType->getLinkageAndVisibility();
-
-  ensureCachedProperties();
+  Cache::ensure(this);
   return std::make_pair(TypeBits.getLinkage(), TypeBits.getVisibility());
 }
 
-
-Type::CachedProperties Type::getCachedProperties(const Type *T) {
-  T = T->CanonicalType.getTypePtr();
-  T->ensureCachedProperties();
-  return CachedProperties(T->TypeBits.getLinkage(),
-                          T->TypeBits.getVisibility(),
-                          T->TypeBits.hasLocalOrUnnamedType());
-}
-
 void Type::ClearLinkageCache() {
-  if (this != CanonicalType.getTypePtr())
-    CanonicalType->ClearLinkageCache();
-  else
-    TypeBits.CacheValidAndVisibility = 0;
-}
-
-Type::CachedProperties Type::getCachedProperties() const { 
-  // Treat dependent types as external.
-  if (isDependentType())
-    return CachedProperties(ExternalLinkage, DefaultVisibility, false);
-
-  // C++ [basic.link]p8:
-  //   Names not covered by these rules have no linkage.
-  return CachedProperties(NoLinkage, DefaultVisibility, false);
-}
-
-Type::CachedProperties BuiltinType::getCachedProperties() const {
-  // C++ [basic.link]p8:
-  //   A type is said to have linkage if and only if:
-  //     - it is a fundamental type (3.9.1); or
-  return CachedProperties(ExternalLinkage, DefaultVisibility, false);
-}
-
-Type::CachedProperties TagType::getCachedProperties() const {
-  // C++ [basic.link]p8:
-  //     - it is a class or enumeration type that is named (or has a name for
-  //       linkage purposes (7.1.3)) and the name has linkage; or
-  //     -  it is a specialization of a class template (14); or
-
-  NamedDecl::LinkageInfo LV = getDecl()->getLinkageAndVisibility();
-  bool IsLocalOrUnnamed =
-    getDecl()->getDeclContext()->isFunctionOrMethod() ||
-                        (!getDecl()->getIdentifier() &&
-                         !getDecl()->getTypedefForAnonDecl());
-  return CachedProperties(LV.linkage(), LV.visibility(), IsLocalOrUnnamed);
-}
-
-// C++ [basic.link]p8:
-//   - it is a compound type (3.9.2) other than a class or enumeration, 
-//     compounded exclusively from types that have linkage; or
-Type::CachedProperties ComplexType::getCachedProperties() const {
-  return Type::getCachedProperties(ElementType);
-}
-
-Type::CachedProperties PointerType::getCachedProperties() const {
-  return Type::getCachedProperties(PointeeType);
-}
-
-Type::CachedProperties BlockPointerType::getCachedProperties() const {
-  return Type::getCachedProperties(PointeeType);
-}
-
-Type::CachedProperties ReferenceType::getCachedProperties() const {
-  return Type::getCachedProperties(PointeeType);
-}
-
-Type::CachedProperties MemberPointerType::getCachedProperties() const {
-  return merge(Type::getCachedProperties(Class),
-               Type::getCachedProperties(PointeeType));
-}
-
-Type::CachedProperties ArrayType::getCachedProperties() const {
-  return Type::getCachedProperties(ElementType);
-}
-
-Type::CachedProperties VectorType::getCachedProperties() const {
-  return Type::getCachedProperties(ElementType);
-}
-
-Type::CachedProperties FunctionNoProtoType::getCachedProperties() const {
-  return Type::getCachedProperties(getResultType());
-}
-
-Type::CachedProperties FunctionProtoType::getCachedProperties() const {
-  CachedProperties Cached = Type::getCachedProperties(getResultType());
-  for (arg_type_iterator A = arg_type_begin(), AEnd = arg_type_end();
-       A != AEnd; ++A) {
-    Cached = merge(Cached, Type::getCachedProperties(*A));
-  }
-  return Cached;
-}
-
-Type::CachedProperties ObjCInterfaceType::getCachedProperties() const {
-  NamedDecl::LinkageInfo LV = getDecl()->getLinkageAndVisibility();
-  return CachedProperties(LV.linkage(), LV.visibility(), false);
-}
-
-Type::CachedProperties ObjCObjectType::getCachedProperties() const {
-  if (const ObjCInterfaceType *T = getBaseType()->getAs<ObjCInterfaceType>())
-    return Type::getCachedProperties(T);
-  return CachedProperties(ExternalLinkage, DefaultVisibility, false);
-}
-
-Type::CachedProperties ObjCObjectPointerType::getCachedProperties() const {
-  return Type::getCachedProperties(PointeeType);
+  TypeBits.CacheValidAndVisibility = 0;
+  if (QualType(this, 0) != CanonicalType)
+    CanonicalType->TypeBits.CacheValidAndVisibility = 0;
 }





More information about the llvm-branch-commits mailing list