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

John McCall rjmccall at apple.com
Thu Aug 13 19:03:10 PDT 2009


Author: rjmccall
Date: Thu Aug 13 21:03:10 2009
New Revision: 78979

URL: http://llvm.org/viewvc/llvm-project?rev=78979&view=rev
Log:
Support friend declarations in templates and test that argdep lookup
still works.


Added:
    cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p1.cpp
Modified:
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Thu Aug 13 21:03:10 2009
@@ -44,6 +44,7 @@
     Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
     Decl *VisitEnumDecl(EnumDecl *D);
     Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
+    Decl *VisitFriendClassDecl(FriendClassDecl *D);
     Decl *VisitFunctionDecl(FunctionDecl *D);
     Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
     Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
@@ -59,6 +60,10 @@
       return 0;
     }
 
+    const LangOptions &getLangOptions() {
+      return SemaRef.getLangOptions();
+    }
+
     // Helper functions for instantiating methods.
     QualType InstantiateFunctionType(FunctionDecl *D,
                              llvm::SmallVectorImpl<ParmVarDecl *> &Params);
@@ -211,6 +216,25 @@
   return Field;
 }
 
+Decl *TemplateDeclInstantiator::VisitFriendClassDecl(FriendClassDecl *D) {
+  QualType T = D->getFriendType();
+  if (T->isDependentType())  {
+    T = SemaRef.InstantiateType(T, TemplateArgs, D->getLocation(),
+                                DeclarationName());
+    assert(T.isNull() || getLangOptions().CPlusPlus0x || T->isRecordType());
+  }
+
+  // FIXME: the target context might be dependent.
+  DeclContext *DC = D->getDeclContext();
+  assert(DC->isFileContext());
+
+  FriendClassDecl *NewD =
+    FriendClassDecl::Create(SemaRef.Context, DC, D->getLocation(), T,
+                            D->getFriendLoc());
+  Owner->addDecl(NewD);
+  return NewD;
+}
+
 Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
   Expr *AssertExpr = D->getAssertExpr();
       
@@ -342,15 +366,30 @@
   QualType T = InstantiateFunctionType(D, Params);
   if (T.isNull())
     return 0;
-  
+
   // Build the instantiated method declaration.
-  FunctionDecl *Function
-    = FunctionDecl::Create(SemaRef.Context, Owner, D->getLocation(), 
+  FunctionDecl *Function;
+  if (FriendFunctionDecl* FFD = dyn_cast<FriendFunctionDecl>(D)) {
+    // The new decl's semantic context.  FIXME:  this might need
+    // to be instantiated.
+    DeclContext *DC = D->getDeclContext();
+
+    // This assert is bogus and exists only to catch cases we don't
+    // handle yet.
+    assert(!DC->isDependentContext());
+
+    Function =
+      FriendFunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
+                                 D->getDeclName(), T, D->isInline(),
+                                 FFD->getFriendLoc());
+    Function->setLexicalDeclContext(Owner);
+  } else {
+    Function =
+      FunctionDecl::Create(SemaRef.Context, Owner, D->getLocation(), 
                            D->getDeclName(), T, D->getStorageClass(),
                            D->isInline(), D->hasWrittenPrototype(),
                            D->getTypeSpecStartLoc());
-  
-  // FIXME: friend functions
+  }
   
   // Attach the parameters
   for (unsigned P = 0; P < Params.size(); ++P)
@@ -372,7 +411,18 @@
                                                 FunctionTemplate,
                                                 &TemplateArgs,
                                                 InsertPos);
-   }
+  }
+
+  // If this was a friend function decl, it's a member which
+  // needs to be added.
+  if (isa<FriendFunctionDecl>(Function)) {
+    // If the new context is still dependent, this declaration
+    // needs to remain hidden.
+    if (Owner->isDependentContext())
+      Owner->addHiddenDecl(Function);
+    else
+      Owner->addDecl(Function);
+  }
 
   return Function;
 }

Added: cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p1.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p1.cpp?rev=78979&view=auto

==============================================================================
--- cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p1.cpp (added)
+++ cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p1.cpp Thu Aug 13 21:03:10 2009
@@ -0,0 +1,20 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+
+template <typename T> class Num {
+  T value_;
+
+public:
+  Num(T value) : value_(value) {}
+  T get() const { return value_; }
+  
+  friend Num operator+(const Num &a, const Num &b) {
+    return a.value_ + b.value_;
+  }
+};
+
+int main() {
+  Num<int> left = -1;
+  Num<int> right = 1;
+  Num<int> result = left + right;
+  return result.get();
+}





More information about the cfe-commits mailing list