From 9d702ae1cd5cfa19d884cbef77e1df99395138bb Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Thu, 30 Jul 2009 23:36:40 +0000 Subject: [PATCH] Canonicalization of dependent C++0x decltype types. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@77643 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/ASTContext.h | 2 ++ include/clang/AST/Type.h | 17 +++++++++++++++ lib/AST/ASTContext.cpp | 23 +++++++++++++++++--- lib/AST/Type.cpp | 8 +++++++ test/SemaTemplate/canonical-expr-type-0x.cpp | 16 ++++++++++++++ 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 test/SemaTemplate/canonical-expr-type-0x.cpp diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h index a8ed08e387..a1f190a74b 100644 --- a/include/clang/AST/ASTContext.h +++ b/include/clang/AST/ASTContext.h @@ -76,12 +76,14 @@ class ASTContext { llvm::FoldingSet FunctionNoProtoTypes; llvm::FoldingSet FunctionProtoTypes; llvm::FoldingSet DependentTypeOfExprTypes; + llvm::FoldingSet DependentDecltypeTypes; llvm::FoldingSet TemplateTypeParmTypes; llvm::FoldingSet TemplateSpecializationTypes; llvm::FoldingSet QualifiedNameTypes; llvm::FoldingSet TypenameTypes; llvm::FoldingSet ObjCInterfaceTypes; llvm::FoldingSet ObjCObjectPointerTypes; + llvm::FoldingSet QualifiedTemplateNames; llvm::FoldingSet DependentTemplateNames; diff --git a/include/clang/AST/Type.h b/include/clang/AST/Type.h index 3072c67556..6eaa4e67a7 100644 --- a/include/clang/AST/Type.h +++ b/include/clang/AST/Type.h @@ -1636,6 +1636,7 @@ class DecltypeType : public Type { // from it. QualType UnderlyingType; +protected: DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); friend class ASTContext; // ASTContext creates these. public: @@ -1649,6 +1650,22 @@ public: static bool classof(const DecltypeType *) { return true; } }; +/// Subclass of DecltypeType that is used for canonical, dependent +/// C++0x decltype types. +class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { + ASTContext &Context; + +public: + DependentDecltypeType(ASTContext &Context, Expr *E); + + void Profile(llvm::FoldingSetNodeID &ID) { + Profile(ID, Context, getUnderlyingExpr()); + } + + static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, + Expr *E); +}; + class TagType : public Type { /// Stores the TagDecl associated with this type. The decl will /// point to the TagDecl that actually defines the entity (or is a diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 81cab925eb..f47e28b8f2 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -1954,9 +1954,26 @@ static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { /// on canonical type's (which are always unique). QualType ASTContext::getDecltypeType(Expr *e) { DecltypeType *dt; - if (e->isTypeDependent()) // FIXME: canonicalize the expression - dt = new (*this, 8) DecltypeType(e, DependentTy); - else { + if (e->isTypeDependent()) { + llvm::FoldingSetNodeID ID; + DependentDecltypeType::Profile(ID, *this, e); + + void *InsertPos = 0; + DependentDecltypeType *Canon + = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); + if (Canon) { + // We already have a "canonical" version of an equivalent, dependent + // decltype type. Use that as our canonical type. + dt = new (*this, 8) DecltypeType(e, DependentTy, + QualType((DecltypeType*)Canon, 0)); + } + else { + // Build a new, canonical typeof(expr) type. + Canon = new (*this, 8) DependentDecltypeType(*this, e); + DependentDecltypeTypes.InsertNode(Canon, InsertPos); + dt = Canon; + } + } else { QualType T = getDecltypeForExpr(e, *this); dt = new (*this, 8) DecltypeType(e, T, getCanonicalType(T)); } diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp index 76d35465fe..c8e317c7d3 100644 --- a/lib/AST/Type.cpp +++ b/lib/AST/Type.cpp @@ -968,6 +968,14 @@ DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can) UnderlyingType(underlyingType) { } +DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E) + : DecltypeType(E, Context.DependentTy), Context(Context) { } + +void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID, + ASTContext &Context, Expr *E) { + E->Profile(ID, Context, true); +} + TagType::TagType(TypeClass TC, TagDecl *D, QualType can) : Type(TC, can, D->isDependentType()), decl(D, 0) {} diff --git a/test/SemaTemplate/canonical-expr-type-0x.cpp b/test/SemaTemplate/canonical-expr-type-0x.cpp new file mode 100644 index 0000000000..a3c177e3d2 --- /dev/null +++ b/test/SemaTemplate/canonical-expr-type-0x.cpp @@ -0,0 +1,16 @@ +// RUN: clang-cc -fsyntax-only -verify -std=c++0x %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 +void f0(T x, decltype(f(N)) y) { } // expected-note{{previous}} + +template +void f0(T x, decltype((f)(N)) y) { } + +template +void f0(U u, decltype(f(M))) { } // expected-error{{redefinition}} \ No newline at end of file -- 2.40.0