[cfe-commits] r106532 - in /cfe/trunk: include/clang/AST/Decl.h include/clang/AST/DeclTemplate.h lib/AST/Decl.cpp lib/AST/DeclTemplate.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Tue Jun 22 02:54:51 PDT 2010


Author: akirtzidis
Date: Tue Jun 22 04:54:51 2010
New Revision: 106532

URL: http://llvm.org/viewvc/llvm-project?rev=106532&view=rev
Log:
Make it easier to read/write the template part of FunctionDecl.

Introduce:
-FunctionDecl::getTemplatedKind() which returns an enum signifying what kind of templated
  FunctionDecl it is.
-An overload of FunctionDecl::setFunctionTemplateSpecialization() which accepts arrays of
  TemplateArguments and TemplateArgumentLocs
-A constructor to TemplateArgumentList which accepts an array of TemplateArguments.

Modified:
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/include/clang/AST/DeclTemplate.h
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/AST/DeclTemplate.cpp

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=106532&r1=106531&r2=106532&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Tue Jun 22 04:54:51 2010
@@ -1106,6 +1106,15 @@
     None, Extern, Static, PrivateExtern
   };
 
+  /// \brief The kind of templated function a FunctionDecl can be.
+  enum TemplatedKind {
+    TK_NonTemplate,
+    TK_FunctionTemplate,
+    TK_MemberSpecialization,
+    TK_FunctionTemplateSpecialization,
+    TK_DependentFunctionTemplateSpecialization
+  };
+
 private:
   /// ParamInfo - new[]'d array of pointers to VarDecls for the formal
   /// parameters of this function.  This is null if a prototype or if there are
@@ -1398,6 +1407,9 @@
   /// X<int>::A is required, it will be instantiated from the
   /// declaration returned by getInstantiatedFromMemberFunction().
   FunctionDecl *getInstantiatedFromMemberFunction() const;
+  
+  /// \brief What kind of templated function this is.
+  TemplatedKind getTemplatedKind() const;
 
   /// \brief If this function is an instantiation of a member function of a
   /// class template specialization, retrieves the member specialization
@@ -1480,8 +1492,6 @@
   /// \brief Specify that this function declaration is actually a function
   /// template specialization.
   ///
-  /// \param Context the AST context in which this function resides.
-  ///
   /// \param Template the function template that this function template
   /// specialization specializes.
   ///
@@ -1493,12 +1503,46 @@
   /// be inserted.
   ///
   /// \param TSK the kind of template specialization this is.
+  ///
+  /// \param TemplateArgsAsWritten location info of template arguments.
   void setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
                                       const TemplateArgumentList *TemplateArgs,
                                          void *InsertPos,
                     TemplateSpecializationKind TSK = TSK_ImplicitInstantiation,
                     const TemplateArgumentListInfo *TemplateArgsAsWritten = 0);
 
+  /// \brief Specify that this function declaration is actually a function
+  /// template specialization.
+  ///
+  /// \param Template the function template that this function template
+  /// specialization specializes.
+  ///
+  /// \param NumTemplateArgs number of template arguments that produced this
+  /// function template specialization from the template.
+  ///
+  /// \param TemplateArgs array of template arguments that produced this
+  /// function template specialization from the template.
+  ///
+  /// \param TSK the kind of template specialization this is.
+  ///
+  /// \param NumTemplateArgsAsWritten number of template arguments that produced
+  /// this function template specialization from the template.
+  ///
+  /// \param TemplateArgsAsWritten array of location info for the template
+  /// arguments.
+  ///
+  /// \param LAngleLoc location of left angle token.
+  ///
+  /// \param RAngleLoc location of right angle token.
+  void setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
+                                         unsigned NumTemplateArgs,
+                                         const TemplateArgument *TemplateArgs,
+                                         TemplateSpecializationKind TSK,
+                                         unsigned NumTemplateArgsAsWritten,
+                                     TemplateArgumentLoc *TemplateArgsAsWritten,
+                                          SourceLocation LAngleLoc,
+                                          SourceLocation RAngleLoc);
+
   /// \brief Specifies that this function declaration is actually a
   /// dependent function template specialization.
   void setDependentTemplateSpecialization(ASTContext &Context,

Modified: cfe/trunk/include/clang/AST/DeclTemplate.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclTemplate.h?rev=106532&r1=106531&r2=106532&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclTemplate.h (original)
+++ cfe/trunk/include/clang/AST/DeclTemplate.h Tue Jun 22 04:54:51 2010
@@ -180,6 +180,11 @@
                        TemplateArgumentListBuilder &Builder,
                        bool TakeArgs);
 
+  /// TemplateArgumentList - It copies the template arguments into a locally
+  /// new[]'d array.
+  TemplateArgumentList(ASTContext &Context,
+                       unsigned NumArgs, const TemplateArgument *Args);
+
   /// Produces a shallow copy of the given template argument list.  This
   /// assumes that the input argument list outlives it.  This takes the list as
   /// a pointer to avoid looking like a copy constructor, since this really

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=106532&r1=106531&r2=106532&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Tue Jun 22 04:54:51 2010
@@ -1228,6 +1228,23 @@
     return 0;
 }
 
+FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
+  if (TemplateOrSpecialization.isNull())
+    return TK_NonTemplate;
+  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
+    return TK_FunctionTemplate;
+  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
+    return TK_MemberSpecialization;
+  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
+    return TK_FunctionTemplateSpecialization;
+  if (TemplateOrSpecialization.is
+                               <DependentFunctionTemplateSpecializationInfo*>())
+    return TK_DependentFunctionTemplateSpecialization;
+
+  assert(false && "Did we miss a TemplateOrSpecialization type?");
+  return TK_NonTemplate;
+}
+
 FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
   if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
     return cast<FunctionDecl>(Info->getInstantiatedFrom());
@@ -1367,6 +1384,27 @@
 }
 
 void
+FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
+                                                unsigned NumTemplateArgs,
+                                           const TemplateArgument *TemplateArgs,
+                                                 TemplateSpecializationKind TSK,
+                                              unsigned NumTemplateArgsAsWritten,
+                                   TemplateArgumentLoc *TemplateArgsAsWritten,
+                                                SourceLocation LAngleLoc,
+                                                SourceLocation RAngleLoc) {
+  ASTContext &Ctx = getASTContext();
+  TemplateArgumentList *TemplArgs
+    = new (Ctx) TemplateArgumentList(Ctx, NumTemplateArgs, TemplateArgs);
+  TemplateArgumentListInfo *TemplArgsInfo
+    = new (Ctx) TemplateArgumentListInfo(LAngleLoc, RAngleLoc);
+  for (unsigned i=0; i != NumTemplateArgsAsWritten; ++i)
+    TemplArgsInfo->addArgument(TemplateArgsAsWritten[i]);
+
+  setFunctionTemplateSpecialization(Template, TemplArgs, /*InsertPos=*/0, TSK,
+                                    TemplArgsInfo);
+}
+
+void
 FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
                                     const UnresolvedSetImpl &Templates,
                              const TemplateArgumentListInfo &TemplateArgs) {

Modified: cfe/trunk/lib/AST/DeclTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclTemplate.cpp?rev=106532&r1=106531&r2=106532&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclTemplate.cpp (original)
+++ cfe/trunk/lib/AST/DeclTemplate.cpp Tue Jun 22 04:54:51 2010
@@ -391,6 +391,22 @@
   }
 }
 
+TemplateArgumentList::TemplateArgumentList(ASTContext &Context,
+                                           unsigned NumArgs,
+                                           const TemplateArgument *Args)
+  : NumFlatArguments(NumArgs),
+    NumStructuredArguments(NumArgs) {
+
+  TemplateArgument *NewArgs = new (Context) TemplateArgument[NumArgs];
+  std::copy(Args, Args+NumArgs, NewArgs);
+  FlatArguments.setPointer(NewArgs);
+  FlatArguments.setInt(1); // Owns the pointer.
+      
+  // Just reuse the flat arguments array.
+  StructuredArguments.setPointer(NewArgs);    
+  StructuredArguments.setInt(0); // Doesn't own the pointer.
+}
+
 /// Produces a shallow copy of the given template argument list.  This
 /// assumes that the input argument list outlives it.  This takes the list as
 /// a pointer to avoid looking like a copy constructor, since this really





More information about the cfe-commits mailing list