[cfe-commits] r80989 - in /cfe/trunk: include/clang/Parse/Action.h include/clang/Parse/Parser.h lib/Parse/ParseDeclCXX.cpp lib/Parse/ParseTemplate.cpp lib/Parse/Parser.cpp lib/Sema/Sema.h lib/Sema/SemaTemplate.cpp test/SemaTemplate/extern-templates.cpp

Douglas Gregor dgregor at apple.com
Thu Sep 3 23:33:52 PDT 2009


Author: dgregor
Date: Fri Sep  4 01:33:52 2009
New Revision: 80989

URL: http://llvm.org/viewvc/llvm-project?rev=80989&view=rev
Log:
Parse extern templates, pass that information all the way to Sema,
then drop it on the floor.

Added:
    cfe/trunk/test/SemaTemplate/extern-templates.cpp   (with props)
Modified:
    cfe/trunk/include/clang/Parse/Action.h
    cfe/trunk/include/clang/Parse/Parser.h
    cfe/trunk/lib/Parse/ParseDeclCXX.cpp
    cfe/trunk/lib/Parse/ParseTemplate.cpp
    cfe/trunk/lib/Parse/Parser.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaTemplate.cpp

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

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Fri Sep  4 01:33:52 2009
@@ -1749,6 +1749,9 @@
   ///
   /// \param S the current scope
   ///
+  /// \param ExternLoc the location of the 'extern' keyword that specifies that
+  /// this is an extern template (if any).
+  ///
   /// \param TemplateLoc the location of the 'template' keyword that
   /// specifies that this is an explicit instantiation.
   ///
@@ -1774,7 +1777,9 @@
   ///
   /// \param Attr attributes that apply to this instantiation.
   virtual DeclResult
-  ActOnExplicitInstantiation(Scope *S, SourceLocation TemplateLoc,
+  ActOnExplicitInstantiation(Scope *S, 
+                             SourceLocation ExternLoc,
+                             SourceLocation TemplateLoc,
                              unsigned TagSpec, 
                              SourceLocation KWLoc,
                              const CXXScopeSpec &SS,
@@ -1804,6 +1809,9 @@
   ///
   /// \param S the current scope
   ///
+  /// \param ExternLoc the location of the 'extern' keyword that specifies that
+  /// this is an extern template (if any).
+  ///
   /// \param TemplateLoc the location of the 'template' keyword that
   /// specifies that this is an explicit instantiation.
   ///
@@ -1829,7 +1837,9 @@
   ///
   /// \param Attr attributes that apply to this instantiation.
   virtual DeclResult
-  ActOnExplicitInstantiation(Scope *S, SourceLocation TemplateLoc,
+  ActOnExplicitInstantiation(Scope *S, 
+                             SourceLocation ExternLoc,
+                             SourceLocation TemplateLoc,
                              unsigned TagSpec, 
                              SourceLocation KWLoc,
                              const CXXScopeSpec &SS,

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

==============================================================================
--- cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/trunk/include/clang/Parse/Parser.h Fri Sep  4 01:33:52 2009
@@ -607,9 +607,10 @@
       : Kind(isSpecialization? ExplicitSpecialization : Template),
         TemplateParams(TemplateParams) { }
 
-    explicit ParsedTemplateInfo(SourceLocation TemplateLoc)
+    explicit ParsedTemplateInfo(SourceLocation ExternLoc,
+                                SourceLocation TemplateLoc)
       : Kind(ExplicitInstantiation), TemplateParams(0), 
-        TemplateLoc(TemplateLoc) { }
+        ExternLoc(ExternLoc), TemplateLoc(TemplateLoc) { }
 
     /// \brief The kind of template we are parsing.
     enum {
@@ -627,6 +628,10 @@
     /// and explicit specializations.
     TemplateParameterLists *TemplateParams;
 
+    /// \brief The location of the 'extern' keyword, if any, for an explicit
+    /// instantiation
+    SourceLocation ExternLoc;
+    
     /// \brief The location of the 'template' keyword, for an explicit
     /// instantiation.
     SourceLocation TemplateLoc;
@@ -1241,7 +1246,8 @@
                                  TemplateArgIsTypeList &TemplateArgIsType,
                                  TemplateArgLocationList &TemplateArgLocations);
   void *ParseTemplateArgument(bool &ArgIsType);
-  DeclPtrTy ParseExplicitInstantiation(SourceLocation TemplateLoc, 
+  DeclPtrTy ParseExplicitInstantiation(SourceLocation ExternLoc,
+                                       SourceLocation TemplateLoc, 
                                        SourceLocation &DeclEnd);
 
   //===--------------------------------------------------------------------===//

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

==============================================================================
--- cfe/trunk/lib/Parse/ParseDeclCXX.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDeclCXX.cpp Fri Sep  4 01:33:52 2009
@@ -638,6 +638,7 @@
       // This is an explicit instantiation of a class template.
       TagOrTempResult
         = Actions.ActOnExplicitInstantiation(CurScope, 
+                                             TemplateInfo.ExternLoc,
                                              TemplateInfo.TemplateLoc, 
                                              TagType,
                                              StartLoc, 
@@ -734,6 +735,7 @@
     //
     TagOrTempResult
       = Actions.ActOnExplicitInstantiation(CurScope, 
+                                           TemplateInfo.ExternLoc,
                                            TemplateInfo.TemplateLoc, 
                                            TagType, StartLoc, SS, Name, 
                                            NameLoc, Attr);

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

==============================================================================
--- cfe/trunk/lib/Parse/ParseTemplate.cpp (original)
+++ cfe/trunk/lib/Parse/ParseTemplate.cpp Fri Sep  4 01:33:52 2009
@@ -25,7 +25,8 @@
                                              SourceLocation &DeclEnd,
                                              AccessSpecifier AS) {
   if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less))
-    return ParseExplicitInstantiation(ConsumeToken(), DeclEnd);
+    return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(), 
+                                      DeclEnd);
 
   return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS);
 }
@@ -186,7 +187,6 @@
   
   // Parse the declaration specifiers.
   DeclSpec DS;
-  // FIXME: Pass TemplateLoc through for explicit template instantiations
   ParseDeclarationSpecifiers(DS, TemplateInfo, AS);
 
   if (Tok.is(tok::semi)) {
@@ -871,11 +871,15 @@
 /// (C++ [temp.explicit]).
 ///
 ///       explicit-instantiation:
-///         'template' declaration
+///         'extern' [opt] 'template' declaration
+///
+/// Note that the 'extern' is a GNU extension and C++0x feature.
 Parser::DeclPtrTy 
-Parser::ParseExplicitInstantiation(SourceLocation TemplateLoc,
+Parser::ParseExplicitInstantiation(SourceLocation ExternLoc,
+                                   SourceLocation TemplateLoc,
                                    SourceLocation &DeclEnd) {
   return ParseSingleDeclarationAfterTemplate(Declarator::FileContext, 
-                                             ParsedTemplateInfo(TemplateLoc),
+                                             ParsedTemplateInfo(ExternLoc,
+                                                                TemplateLoc),
                                              DeclEnd, AS_none);
 }

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

==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Fri Sep  4 01:33:52 2009
@@ -391,6 +391,7 @@
 /// [C++0x] empty-declaration:
 ///           ';'
 ///
+/// [C++0x/GNU] 'extern' 'template' declaration
 Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration() {
   DeclPtrTy SingleDecl;
   switch (Tok.getKind()) {
@@ -452,6 +453,20 @@
       SourceLocation DeclEnd;
       return ParseDeclaration(Declarator::FileContext, DeclEnd);
     }
+  case tok::kw_extern:
+    if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
+      // Extern templates
+      SourceLocation ExternLoc = ConsumeToken();
+      SourceLocation TemplateLoc = ConsumeToken();
+      SourceLocation DeclEnd;
+      return Actions.ConvertDeclToDeclGroup(
+                  ParseExplicitInstantiation(ExternLoc, TemplateLoc, DeclEnd));
+    }
+    
+    // FIXME: Detect C++ linkage specifications here?
+      
+    // Fall through to handle other declarations or function definitions.
+      
   default:
     // We can't tell whether this is a function-definition or declaration yet.
     return ParseDeclarationOrFunctionDefinition();

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=80989&r1=80988&r2=80989&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Fri Sep  4 01:33:52 2009
@@ -2454,7 +2454,9 @@
                                                     Declarator &D);
   
   virtual DeclResult
-  ActOnExplicitInstantiation(Scope *S, SourceLocation TemplateLoc,
+  ActOnExplicitInstantiation(Scope *S, 
+                             SourceLocation ExternLoc,
+                             SourceLocation TemplateLoc,
                              unsigned TagSpec, 
                              SourceLocation KWLoc,
                              const CXXScopeSpec &SS,
@@ -2467,7 +2469,9 @@
                              AttributeList *Attr);
 
   virtual DeclResult
-  ActOnExplicitInstantiation(Scope *S, SourceLocation TemplateLoc,
+  ActOnExplicitInstantiation(Scope *S, 
+                             SourceLocation ExternLoc,
+                             SourceLocation TemplateLoc,
                              unsigned TagSpec, 
                              SourceLocation KWLoc,
                              const CXXScopeSpec &SS,

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Fri Sep  4 01:33:52 2009
@@ -2849,8 +2849,11 @@
 }
 
 // Explicit instantiation of a class template specialization
+// FIXME: Implement extern template semantics
 Sema::DeclResult
-Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation TemplateLoc,
+Sema::ActOnExplicitInstantiation(Scope *S, 
+                                 SourceLocation ExternLoc,
+                                 SourceLocation TemplateLoc,
                                  unsigned TagSpec, 
                                  SourceLocation KWLoc,
                                  const CXXScopeSpec &SS,
@@ -3034,8 +3037,11 @@
 }
 
 // Explicit instantiation of a member class of a class template.
+// FIXME: Implement extern template semantics.
 Sema::DeclResult
-Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation TemplateLoc,
+Sema::ActOnExplicitInstantiation(Scope *S, 
+                                 SourceLocation ExternLoc,
+                                 SourceLocation TemplateLoc,
                                  unsigned TagSpec, 
                                  SourceLocation KWLoc,
                                  const CXXScopeSpec &SS,

Added: cfe/trunk/test/SemaTemplate/extern-templates.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/extern-templates.cpp?rev=80989&view=auto

==============================================================================
--- cfe/trunk/test/SemaTemplate/extern-templates.cpp (added)
+++ cfe/trunk/test/SemaTemplate/extern-templates.cpp Fri Sep  4 01:33:52 2009
@@ -0,0 +1,20 @@
+// RUN: clang-cc -fsyntax-only %s
+
+template<typename T>
+class X0 {
+public:
+  void f(T t);
+};
+
+template<typename T>
+void X0<T>::f(T t) {
+  t = 17;
+}
+
+// FIXME: Later, we'll want to write an explicit template
+// declaration (extern template) for X0<int*>, then try to
+// call X0<int*>::f. The translation unit should succeed, 
+// because we're not allowed to instantiate the out-of-line
+// definition of X0<T>::f. For now, this is just a parsing
+// test.
+extern template class X0<int>;

Propchange: cfe/trunk/test/SemaTemplate/extern-templates.cpp

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/SemaTemplate/extern-templates.cpp

------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/SemaTemplate/extern-templates.cpp

------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list