[cfe-commits] r77639 - in /cfe/trunk: include/clang/AST/ASTContext.h include/clang/AST/Type.h lib/AST/ASTContext.cpp lib/AST/StmtProfile.cpp lib/AST/Type.cpp test/SemaTemplate/canonical-expr-type.cpp

Douglas Gregor dgregor at apple.com
Thu Jul 30 16:18:24 PDT 2009


Author: dgregor
Date: Thu Jul 30 18:18:24 2009
New Revision: 77639

URL: http://llvm.org/viewvc/llvm-project?rev=77639&view=rev
Log:
Canonicalization for dependent typeof(expr) types.

Added:
    cfe/trunk/test/SemaTemplate/canonical-expr-type.cpp
Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/AST/Type.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/AST/StmtProfile.cpp
    cfe/trunk/lib/AST/Type.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Thu Jul 30 18:18:24 2009
@@ -75,13 +75,13 @@
   llvm::FoldingSet<VectorType> VectorTypes;
   llvm::FoldingSet<FunctionNoProtoType> FunctionNoProtoTypes;
   llvm::FoldingSet<FunctionProtoType> FunctionProtoTypes;
+  llvm::FoldingSet<DependentTypeOfExprType> DependentTypeOfExprTypes;
   llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes;
   llvm::FoldingSet<TemplateSpecializationType> TemplateSpecializationTypes;
   llvm::FoldingSet<QualifiedNameType> QualifiedNameTypes;
   llvm::FoldingSet<TypenameType> TypenameTypes;
   llvm::FoldingSet<ObjCInterfaceType> ObjCInterfaceTypes;
   llvm::FoldingSet<ObjCObjectPointerType> ObjCObjectPointerTypes;
-
   llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames;
   llvm::FoldingSet<DependentTemplateName> DependentTemplateNames;
 

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

==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Thu Jul 30 18:18:24 2009
@@ -1577,6 +1577,8 @@
 /// TypeOfExprType (GCC extension).
 class TypeOfExprType : public Type {
   Expr *TOExpr;
+  
+protected:
   TypeOfExprType(Expr *E, QualType can = QualType());
   friend class ASTContext;  // ASTContext creates these.
 public:
@@ -1589,6 +1591,24 @@
   static bool classof(const TypeOfExprType *) { return true; }
 };
 
+/// Subclass of TypeOfExprType that is used for canonical, dependent 
+/// typeof(expr) types. 
+class DependentTypeOfExprType 
+  : public TypeOfExprType, public llvm::FoldingSetNode {
+  ASTContext &Context;
+  
+public:
+  DependentTypeOfExprType(ASTContext &Context, Expr *E) 
+    : TypeOfExprType(E), Context(Context) { }
+  
+  void Profile(llvm::FoldingSetNodeID &ID) {
+    Profile(ID, Context, getUnderlyingExpr());
+  }
+  
+  static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context,
+                      Expr *E);
+};
+  
 /// TypeOfType (GCC extension).
 class TypeOfType : public Type {
   QualType TOType;

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

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Thu Jul 30 18:18:24 2009
@@ -1876,9 +1876,26 @@
 /// on canonical type's (which are always unique).
 QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
   TypeOfExprType *toe;
-  if (tofExpr->isTypeDependent())
-    toe = new (*this, 8) TypeOfExprType(tofExpr);
-  else {
+  if (tofExpr->isTypeDependent()) {
+    llvm::FoldingSetNodeID ID;
+    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
+    
+    void *InsertPos = 0;
+    DependentTypeOfExprType *Canon
+      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
+    if (Canon) {
+      // We already have a "canonical" version of an identical, dependent
+      // typeof(expr) type. Use that as our canonical type.
+      toe = new (*this, 8) TypeOfExprType(tofExpr, 
+                                          QualType((TypeOfExprType*)Canon, 0));
+    }
+    else {
+      // Build a new, canonical typeof(expr) type.
+      Canon = new (*this, 8) DependentTypeOfExprType(*this, tofExpr);
+      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
+      toe = Canon;
+    }
+  } else {
     QualType Canonical = getCanonicalType(tofExpr->getType());
     toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
   }

Modified: cfe/trunk/lib/AST/StmtProfile.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtProfile.cpp?rev=77639&r1=77638&r2=77639&view=diff

==============================================================================
--- cfe/trunk/lib/AST/StmtProfile.cpp (original)
+++ cfe/trunk/lib/AST/StmtProfile.cpp Thu Jul 30 18:18:24 2009
@@ -602,9 +602,9 @@
 }
 
 void StmtProfiler::VisitDecl(Decl *D) {
-  if (Canonical) {
+  if (Canonical && D) {
     if (NonTypeTemplateParmDecl *NTTP 
-        = dyn_cast_or_null<NonTypeTemplateParmDecl>(D)) {
+        = dyn_cast<NonTypeTemplateParmDecl>(D)) {
       ID.AddInteger(NTTP->getDepth());
       ID.AddInteger(NTTP->getIndex());
       VisitType(NTTP->getType());

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

==============================================================================
--- cfe/trunk/lib/AST/Type.cpp (original)
+++ cfe/trunk/lib/AST/Type.cpp Thu Jul 30 18:18:24 2009
@@ -958,6 +958,11 @@
   : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
 }
 
+void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID, 
+                                      ASTContext &Context, Expr *E) {
+  E->Profile(ID, Context, true);
+}
+
 DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
   : Type(Decltype, can, E->isTypeDependent()), E(E), 
   UnderlyingType(underlyingType) {

Added: cfe/trunk/test/SemaTemplate/canonical-expr-type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/canonical-expr-type.cpp?rev=77639&view=auto

==============================================================================
--- cfe/trunk/test/SemaTemplate/canonical-expr-type.cpp (added)
+++ cfe/trunk/test/SemaTemplate/canonical-expr-type.cpp Thu Jul 30 18:18:24 2009
@@ -0,0 +1,16 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+
+void f();
+
+// FIXME: would like to refer to the first function parameter in these test,
+// but that won't work (yet).
+
+// Test typeof(expr) canonicalization
+template<typename T, T N>
+void f0(T x, __typeof__(f(N)) y) { } // expected-note{{previous}}
+
+template<typename T, T N>
+void f0(T x, __typeof__((f)(N)) y) { }
+
+template<typename U, U M>
+void f0(U u, __typeof__(f(M))) { } // expected-error{{redefinition}}
\ No newline at end of file





More information about the cfe-commits mailing list