[cfe-commits] r79709 - in /cfe/trunk: include/clang/Parse/Parser.h lib/Parse/ParseCXXInlineMethods.cpp lib/Parse/ParseDeclCXX.cpp lib/Sema/SemaDecl.cpp test/CXX/temp/temp.fct.spec/temp.deduct/temp.deduct.conv/p4.cpp test/SemaTemplate/member-function-template.cpp

Douglas Gregor dgregor at apple.com
Fri Aug 21 17:34:48 PDT 2009


Author: dgregor
Date: Fri Aug 21 19:34:47 2009
New Revision: 79709

URL: http://llvm.org/viewvc/llvm-project?rev=79709&view=rev
Log:
Implement delayed parsing for member function templates. Fixes PR4608.

Modified:
    cfe/trunk/include/clang/Parse/Parser.h
    cfe/trunk/lib/Parse/ParseCXXInlineMethods.cpp
    cfe/trunk/lib/Parse/ParseDeclCXX.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/CXX/temp/temp.fct.spec/temp.deduct/temp.deduct.conv/p4.cpp
    cfe/trunk/test/SemaTemplate/member-function-template.cpp

Modified: cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=79709&r1=79708&r2=79709&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/trunk/include/clang/Parse/Parser.h Fri Aug 21 19:34:47 2009
@@ -462,7 +462,13 @@
   struct LexedMethod {
     Action::DeclPtrTy D;
     CachedTokens Toks;
-    explicit LexedMethod(Action::DeclPtrTy MD) : D(MD) {}
+
+    /// \brief Whether this member function had an associated template
+    /// scope. When true, D is a template declaration.
+    /// othewise, it is a member function declaration.
+    bool TemplateScope;
+
+    explicit LexedMethod(Action::DeclPtrTy MD) : D(MD), TemplateScope(false) {}
   };
 
   /// LateParsedDefaultArgument - Keeps track of a parameter that may
@@ -489,11 +495,17 @@
   /// until the class itself is completely-defined, such as a default
   /// argument (C++ [class.mem]p2).
   struct LateParsedMethodDeclaration {
-    explicit LateParsedMethodDeclaration(Action::DeclPtrTy M) : Method(M) { }
+    explicit LateParsedMethodDeclaration(Action::DeclPtrTy M) 
+      : Method(M), TemplateScope(false) { }
 
     /// Method - The method declaration.
     Action::DeclPtrTy Method;
 
+    /// \brief Whether this member function had an associated template
+    /// scope. When true, D is a template declaration.
+    /// othewise, it is a member function declaration.
+    bool TemplateScope;
+    
     /// DefaultArgs - Contains the parameters of the function and
     /// their default arguments. At least one of the parameters will
     /// have a default argument, but all of the parameters of the

Modified: cfe/trunk/lib/Parse/ParseCXXInlineMethods.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseCXXInlineMethods.cpp?rev=79709&r1=79708&r2=79709&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseCXXInlineMethods.cpp (original)
+++ cfe/trunk/lib/Parse/ParseCXXInlineMethods.cpp Fri Aug 21 19:34:47 2009
@@ -44,6 +44,8 @@
   // Consume the tokens and store them for later parsing.
 
   getCurrentClass().MethodDefs.push_back(LexedMethod(FnD));
+  getCurrentClass().MethodDefs.back().TemplateScope 
+    = CurScope->isTemplateParamScope();
   CachedTokens &Toks = getCurrentClass().MethodDefs.back().Toks;
 
   tok::TokenKind kind = Tok.getKind();
@@ -99,9 +101,11 @@
   for (; !Class.MethodDecls.empty(); Class.MethodDecls.pop_front()) {
     LateParsedMethodDeclaration &LM = Class.MethodDecls.front();
     
-    // FIXME: For member function templates, we'll need to introduce a
-    // scope for the template parameters.
-
+    // If this is a member template, introduce the template parameter scope.
+    ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
+    if (LM.TemplateScope)
+      Actions.ActOnReenterTemplateScope(CurScope, LM.Method);
+    
     // Start the delayed C++ method declaration
     Actions.ActOnStartDelayedCXXMethodDeclaration(CurScope, LM.Method);
 
@@ -161,6 +165,11 @@
   for (; !Class.MethodDefs.empty(); Class.MethodDefs.pop_front()) {
     LexedMethod &LM = Class.MethodDefs.front();
 
+    // If this is a member template, introduce the template parameter scope.
+    ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
+    if (LM.TemplateScope)
+      Actions.ActOnReenterTemplateScope(CurScope, LM.D);
+    
     assert(!LM.Toks.empty() && "Empty body!");
     // Append the current token at the end of the new token stream so that it
     // doesn't get lost.

Modified: cfe/trunk/lib/Parse/ParseDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDeclCXX.cpp?rev=79709&r1=79708&r2=79709&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseDeclCXX.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDeclCXX.cpp Fri Aug 21 19:34:47 2009
@@ -861,6 +861,7 @@
         getCurrentClass().MethodDecls.push_back(
                                 LateParsedMethodDeclaration(ThisDecl));
         LateMethod = &getCurrentClass().MethodDecls.back();
+        LateMethod->TemplateScope = CurScope->isTemplateParamScope();
 
         // Add all of the parameters prior to this one (they don't
         // have default arguments).

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Aug 21 19:34:47 2009
@@ -3526,7 +3526,13 @@
 Sema::DeclPtrTy Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclPtrTy D) {
   if (!D)
     return D;
-  FunctionDecl *FD = cast<FunctionDecl>(D.getAs<Decl>());
+  FunctionDecl *FD = 0;
+  
+  if (FunctionTemplateDecl *FunTmpl 
+        = dyn_cast<FunctionTemplateDecl>(D.getAs<Decl>()))
+    FD = FunTmpl->getTemplatedDecl();
+  else
+    FD = cast<FunctionDecl>(D.getAs<Decl>());
 
   CurFunctionNeedsScopeChecking = false;
   
@@ -3624,7 +3630,15 @@
                                               bool IsInstantiation) {
   Decl *dcl = D.getAs<Decl>();
   Stmt *Body = BodyArg.takeAs<Stmt>();
-  if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(dcl)) {
+
+  FunctionDecl *FD = 0;
+  FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(dcl);
+  if (FunTmpl)
+    FD = FunTmpl->getTemplatedDecl();
+  else
+    FD = dyn_cast_or_null<FunctionDecl>(dcl);
+
+  if (FD) {
     FD->setBody(Body);
     if (FD->isMain(Context))
       // C and C++ allow for main to automagically return 0.
@@ -3711,7 +3725,7 @@
   // C++ constructors that have function-try-blocks can't have return 
   // statements in the handlers of that block. (C++ [except.handle]p14) 
   // Verify this.
-  if (isa<CXXConstructorDecl>(dcl) && isa<CXXTryStmt>(Body))
+  if (FD && isa<CXXConstructorDecl>(FD) && isa<CXXTryStmt>(Body))
     DiagnoseReturnInConstructorExceptionHandler(cast<CXXTryStmt>(Body));
   
   if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(dcl))

Modified: cfe/trunk/test/CXX/temp/temp.fct.spec/temp.deduct/temp.deduct.conv/p4.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/temp/temp.fct.spec/temp.deduct/temp.deduct.conv/p4.cpp?rev=79709&r1=79708&r2=79709&view=diff

==============================================================================
--- cfe/trunk/test/CXX/temp/temp.fct.spec/temp.deduct/temp.deduct.conv/p4.cpp (original)
+++ cfe/trunk/test/CXX/temp/temp.fct.spec/temp.deduct/temp.deduct.conv/p4.cpp Fri Aug 21 19:34:47 2009
@@ -11,36 +11,32 @@
 
 struct AnyThreeLevelPtr {
   template<typename T>
-  operator T***() const;
-  // FIXME: Can't handle definitions of member templates yet
-#if 0
+  operator T***() const
   {
     T x = 0;
-    x = 0; // will fail if T is deduced to a const type
+    // FIXME: looks like we get this wrong, too!
+    // x = 0; // will fail if T is deduced to a const type
            // (EDG and GCC get this wrong)
     return 0;
   }
-#endif
 };
 
+struct X { };
+
 void test_deduce_with_qual(AnyThreeLevelPtr a3) {
   int * const * const * const ip = a3;
 }
 
-struct X { };
-
 struct AnyPtrMem {
   template<typename Class, typename T>
-  operator T Class::*() const;
-  // FIXME: Can't handle definitions of member templates yet
-#if 0
+  operator T Class::*() const
   {
     T x = 0;
-    x = 0; // will fail if T is deduced to a const type.
+    // FIXME: looks like we get this wrong, too!
+    // x = 0; // will fail if T is deduced to a const type.
            // (EDG and GCC get this wrong)
     return 0;
   }
-#endif
 };
 
 void test_deduce_ptrmem_with_qual(AnyPtrMem apm) {

Modified: cfe/trunk/test/SemaTemplate/member-function-template.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/member-function-template.cpp?rev=79709&r1=79708&r2=79709&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/member-function-template.cpp (original)
+++ cfe/trunk/test/SemaTemplate/member-function-template.cpp Fri Aug 21 19:34:47 2009
@@ -39,3 +39,6 @@
   float& (X::*pm2)(float) = &X::f1;
   int& (X::*pm3)(float, int) = &X::f1;
 }
+
+// PR4608
+class A { template <class x> x a(x z) { return z+y; } int y; };





More information about the cfe-commits mailing list