[cfe-commits] r103943 - in /cfe/trunk: lib/Sema/SemaTemplateInstantiateDecl.cpp test/CXX/class.access/class.friend/p1.cpp test/CXX/temp/temp.decls/temp.friend/p4.cpp

Douglas Gregor dgregor at apple.com
Mon May 17 09:38:00 PDT 2010


Author: dgregor
Date: Mon May 17 11:38:00 2010
New Revision: 103943

URL: http://llvm.org/viewvc/llvm-project?rev=103943&view=rev
Log:
C++98/03 [temp.friend]p4 requires that inline function definitions
within class templates be instantiated along with each class template
specialization, even if the functions are not used. Do so, as a baby
step toward PR6952.

Added:
    cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p4.cpp   (with props)
Modified:
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
    cfe/trunk/test/CXX/class.access/class.friend/p1.cpp

Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=103943&r1=103942&r2=103943&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Mon May 17 11:38:00 2010
@@ -1009,6 +1009,7 @@
     Params[P]->setOwningFunction(Function);
   Function->setParams(Params.data(), Params.size());
 
+  SourceLocation InstantiateAtPOI;
   if (TemplateParams) {
     // Our resulting instantiation is actually a function template, since we
     // are substituting only the outer template parameters. For example, given
@@ -1047,6 +1048,23 @@
     // TODO: should we remember this connection regardless of whether
     // the friend declaration provided a body?
     Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
+    if (!SemaRef.getLangOptions().CPlusPlus0x) {
+      // C++03 [temp.friend]p4:
+      //   When a function is defined in a friend function declaration in a 
+      //   class template, the function is defined at each instantiation of the
+      //   class template. The function is defined even if it is never used.
+      if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Owner)) {
+        if (ClassTemplateSpecializationDecl *Spec 
+                        = dyn_cast<ClassTemplateSpecializationDecl>(Record))
+          InstantiateAtPOI = Spec->getPointOfInstantiation();
+        else if (MemberSpecializationInfo *MSInfo 
+                                      = Record->getMemberSpecializationInfo())
+          InstantiateAtPOI = MSInfo->getPointOfInstantiation();
+      }
+      
+      if (InstantiateAtPOI.isInvalid())
+        InstantiateAtPOI = Function->getLocation();        
+    }
   }
     
   if (InitFunctionInstantiation(Function, D))
@@ -1133,6 +1151,11 @@
       PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
     PrincipalDecl->setNonMemberOperator();
 
+  // If we need to instantiate this function now (because it is a C++98/03 
+  // friend function defined inside a class template), do so.
+  if (InstantiateAtPOI.isValid())
+    SemaRef.MarkDeclarationReferenced(InstantiateAtPOI, Function);
+
   return Function;
 }
 

Modified: cfe/trunk/test/CXX/class.access/class.friend/p1.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/class.access/class.friend/p1.cpp?rev=103943&r1=103942&r2=103943&view=diff
==============================================================================
--- cfe/trunk/test/CXX/class.access/class.friend/p1.cpp (original)
+++ cfe/trunk/test/CXX/class.access/class.friend/p1.cpp Mon May 17 11:38:00 2010
@@ -188,8 +188,8 @@
 
   struct Inequal {};
   bool test() {
-    Holder<Inequal> a, b;
-    return a == b; // expected-note {{requested here}}
+    Holder<Inequal> a, b; // expected-note {{requested here}}
+    return a == b; 
   }
 }
 

Added: cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p4.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p4.cpp?rev=103943&view=auto
==============================================================================
--- cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p4.cpp (added)
+++ cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p4.cpp Mon May 17 11:38:00 2010
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+template<typename T>
+struct X {
+  friend void f(int x) { T* y = x; } // expected-error{{cannot initialize a variable of type 'int *' with an lvalue of type 'int'}}
+};
+
+X<int> xi; // expected-note{{in instantiation of member function 'f' requested here}}
+

Propchange: cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p4.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p4.cpp
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p4.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list