Representation of template type parameters and non-type template
parameters, with some semantic analysis:
  - Template parameters are introduced into template parameter scope
  - Complain about template parameter shadowing (except in Microsoft mode)

Note that we leak template parameter declarations like crazy, a
problem we'll remedy once we actually create proper declarations for
templates. 

Next up: dependent types and value-dependent/type-dependent
expressions.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@60597 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index e1b56ad..7c8b4b1 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -943,6 +943,8 @@
   
   if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
     return getTypedefType(Typedef);
+  else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
+    return getTemplateTypeParmType(TP);
   else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
     return getObjCInterfaceType(ObjCInterface);
 
@@ -982,6 +984,16 @@
   return QualType(Decl->TypeForDecl, 0);
 }
 
+/// getTemplateTypeParmType - Return the unique reference to the type
+/// for the specified template type parameter declaration. 
+QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
+  if (!Decl->TypeForDecl) {
+    Decl->TypeForDecl = new TemplateTypeParmType(Decl);
+    Types.push_back(Decl->TypeForDecl);
+  }
+  return QualType(Decl->TypeForDecl, 0);
+}
+
 /// getObjCInterfaceType - Return the unique reference to the type for the
 /// specified ObjC interface decl.
 QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp
index 5cb2a3e..7b5df1c 100644
--- a/lib/AST/DeclBase.cpp
+++ b/lib/AST/DeclBase.cpp
@@ -239,6 +239,8 @@
   case CXXField:            nCXXFieldDecls++; break;
   case CXXRecord:           nCXXSUC++; break;
   // FIXME: Statistics for C++ decls.
+  case TemplateTypeParm:
+  case NonTypeTemplateParm:
   case CXXMethod:
   case CXXConstructor:
   case CXXDestructor:
diff --git a/lib/AST/DeclCXX.cpp b/lib/AST/DeclCXX.cpp
index 6be9eaf..a26803c 100644
--- a/lib/AST/DeclCXX.cpp
+++ b/lib/AST/DeclCXX.cpp
@@ -19,7 +19,23 @@
 //===----------------------------------------------------------------------===//
 // Decl Allocation/Deallocation Method Implementations
 //===----------------------------------------------------------------------===//
- 
+
+TemplateTypeParmDecl *
+TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC,
+			     SourceLocation L, IdentifierInfo *Id,
+			     bool Typename) {
+  void *Mem = C.getAllocator().Allocate<TemplateTypeParmDecl>();
+  return new (Mem) TemplateTypeParmDecl(DC, L, Id, Typename);
+}
+
+NonTypeTemplateParmDecl *
+NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC, 
+				SourceLocation L, IdentifierInfo *Id,
+				QualType T, SourceLocation TypeSpecStartLoc) {
+  void *Mem = C.getAllocator().Allocate<NonTypeTemplateParmDecl>();
+  return new (Mem) NonTypeTemplateParmDecl(DC, L, Id, T, TypeSpecStartLoc);
+}
+
 CXXFieldDecl *CXXFieldDecl::Create(ASTContext &C, CXXRecordDecl *RD,
                                    SourceLocation L, IdentifierInfo *Id,
                                    QualType T, bool Mut, Expr *BW) {
diff --git a/lib/AST/DeclSerialization.cpp b/lib/AST/DeclSerialization.cpp
index 8a4612b..29714c0 100644
--- a/lib/AST/DeclSerialization.cpp
+++ b/lib/AST/DeclSerialization.cpp
@@ -88,6 +88,10 @@
       Dcl = TypedefDecl::CreateImpl(D, C);
       break;
       
+    case TemplateTypeParm:
+      Dcl = TemplateTypeParmDecl::CreateImpl(D, C);
+      break;
+
     case FileScopeAsm:
       Dcl = FileScopeAsmDecl::CreateImpl(D, C);
       break;
@@ -630,6 +634,27 @@
 }
 
 //===----------------------------------------------------------------------===//
+//      TemplateTypeParmDecl Serialization.
+//===----------------------------------------------------------------------===//
+
+void TemplateTypeParmDecl::EmitImpl(Serializer& S) const {
+  S.EmitBool(Typename);
+  ScopedDecl::EmitInRec(S);
+  ScopedDecl::EmitOutRec(S);
+}
+
+TemplateTypeParmDecl *
+TemplateTypeParmDecl::CreateImpl(Deserializer& D, ASTContext& C) {
+  bool Typename = D.ReadBool();
+  void *Mem = C.getAllocator().Allocate<TemplateTypeParmDecl>();
+  TemplateTypeParmDecl *decl
+    = new (Mem) TemplateTypeParmDecl(0, SourceLocation(), NULL, Typename);
+  decl->ScopedDecl::ReadInRec(D, C);
+  decl->ScopedDecl::ReadOutRec(D, C);
+  return decl;
+}
+
+//===----------------------------------------------------------------------===//
 //      LinkageSpec Serialization.
 //===----------------------------------------------------------------------===//
 
diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp
index e4386ec..a3264b0 100644
--- a/lib/AST/Expr.cpp
+++ b/lib/AST/Expr.cpp
@@ -341,6 +341,12 @@
 /// DeclCanBeLvalue - Determine whether the given declaration can be
 /// an lvalue. This is a helper routine for isLvalue.
 static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
+  // C++ [temp.param]p6:
+  //   A non-type non-reference template-parameter is not an lvalue.
+  if (const NonTypeTemplateParmDecl *NTTParm 
+        = dyn_cast<NonTypeTemplateParmDecl>(Decl))
+    return NTTParm->getType()->isReferenceType();
+
   return isa<VarDecl>(Decl) || isa<CXXFieldDecl>(Decl) ||
     // C++ 3.10p2: An lvalue refers to an object or function.
     (Ctx.getLangOptions().CPlusPlus &&
diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp
index 6aa3c8e..303fc7e 100644
--- a/lib/AST/Type.cpp
+++ b/lib/AST/Type.cpp
@@ -443,6 +443,11 @@
   return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
 }
 
+const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
+  // There is no sugar for template type parameters, so just return
+  // the canonical type pointer if it is the right class.
+  return dyn_cast<TemplateTypeParmType>(CanonicalType);
+}
 
 bool Type::isIntegerType() const {
   if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
@@ -1002,6 +1007,12 @@
   InnerString = getDecl()->getIdentifier()->getName() + InnerString;
 }
 
+void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
+  if (!InnerString.empty())    // Prefix the basic type, e.g. 'parmname X'.
+    InnerString = ' ' + InnerString;
+  InnerString = getDecl()->getIdentifier()->getName() + InnerString;
+}
+
 void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
   if (!InnerString.empty())    // Prefix the basic type, e.g. 'typedefname X'.
     InnerString = ' ' + InnerString;
diff --git a/lib/AST/TypeSerialization.cpp b/lib/AST/TypeSerialization.cpp
index db93e4b..3bdc946 100644
--- a/lib/AST/TypeSerialization.cpp
+++ b/lib/AST/TypeSerialization.cpp
@@ -110,6 +110,10 @@
       D.RegisterPtr(PtrID,TypedefType::CreateImpl(Context,D));
       break;
       
+    case Type::TemplateTypeParm:
+      D.RegisterPtr(PtrID,TemplateTypeParmType::CreateImpl(Context, D));
+      break;
+
     case Type::VariableArray:
       D.RegisterPtr(PtrID,VariableArrayType::CreateImpl(Context,D));
       break;
@@ -272,6 +276,25 @@
 }
   
 //===----------------------------------------------------------------------===//
+// TemplateTypeParmType
+//===----------------------------------------------------------------------===//
+
+void TemplateTypeParmType::EmitImpl(Serializer& S) const {
+  S.EmitPtr(getDecl());
+}
+
+Type* TemplateTypeParmType::CreateImpl(ASTContext& Context, Deserializer& D) {
+  std::vector<Type*>& Types = 
+    const_cast<std::vector<Type*>&>(Context.getTypes());
+  
+  TemplateTypeParmType* T = new TemplateTypeParmType(NULL);
+  Types.push_back(T);
+  
+  D.ReadPtr(T->Decl); // May be backpatched.
+  return T;
+}
+  
+//===----------------------------------------------------------------------===//
 // VariableArrayType
 //===----------------------------------------------------------------------===//
 
diff --git a/lib/CodeGen/CodeGenTypes.cpp b/lib/CodeGen/CodeGenTypes.cpp
index 18cfdea..fbf1120 100644
--- a/lib/CodeGen/CodeGenTypes.cpp
+++ b/lib/CodeGen/CodeGenTypes.cpp
@@ -190,6 +190,7 @@
   
   switch (Ty.getTypeClass()) {
   case Type::TypeName:        // typedef isn't canonical.
+  case Type::TemplateTypeParm:// template type parameters never generated
   case Type::TypeOfExp:       // typeof isn't canonical.
   case Type::TypeOfTyp:       // typeof isn't canonical.
     assert(0 && "Non-canonical type, shouldn't happen");
diff --git a/lib/Parse/ParseTemplate.cpp b/lib/Parse/ParseTemplate.cpp
index 6bcf81d..5801f9e 100644
--- a/lib/Parse/ParseTemplate.cpp
+++ b/lib/Parse/ParseTemplate.cpp
@@ -41,7 +41,7 @@
   SourceLocation TemplateLoc = ConsumeToken();
   
   // Enter template-parameter scope.
-  EnterScope(Scope::TemplateParamScope|Scope::DeclScope);
+  EnterScope(Scope::TemplateParamScope);
 
   // Try to parse the template parameters, and the declaration if
   // successful.
diff --git a/lib/Parse/Parser.cpp b/lib/Parse/Parser.cpp
index f23fedf..bce73cc 100644
--- a/lib/Parse/Parser.cpp
+++ b/lib/Parse/Parser.cpp
@@ -441,7 +441,7 @@
 
       if (Tok.is(tok::l_brace)) {
         // This recovery skips the entire function body. It would be nice
-        // to simply call ParseFunctionDefintion() below, however Sema
+        // to simply call ParseFunctionDefinition() below, however Sema
         // assumes the declarator represents a function, not a typedef.
         ConsumeBrace();
         SkipUntil(tok::r_brace, true);
diff --git a/lib/Sema/CMakeLists.txt b/lib/Sema/CMakeLists.txt
index cb28fca..fd961fa 100644
--- a/lib/Sema/CMakeLists.txt
+++ b/lib/Sema/CMakeLists.txt
@@ -18,5 +18,6 @@
   SemaNamedCast.cpp
   SemaOverload.cpp
   SemaStmt.cpp
+  SemaTemplate.cpp
   SemaType.cpp
   )
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index ef15064..f1d0ee2 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -988,6 +988,17 @@
 
   bool CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl);
 
+  //===--------------------------------------------------------------------===//
+  // C++ Templates [C++ 14]
+  //
+  bool isTemplateParameterDecl(Decl *D);
+  bool DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl);
+  virtual DeclTy *ActOnTypeParameter(Scope *S, bool Typename, 
+				     SourceLocation KeyLoc,
+				     IdentifierInfo *ParamName,
+				     SourceLocation ParamNameLoc);
+  virtual DeclTy *ActOnNonTypeTemplateParameter(Scope *S, Declarator &D);
+
   // Objective-C declarations.
   virtual DeclTy *ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
                                            IdentifierInfo *ClassName,
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 4fd13c3..c893d27 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -39,7 +39,8 @@
 
   if (IIDecl && (isa<TypedefDecl>(IIDecl) || 
                  isa<ObjCInterfaceDecl>(IIDecl) ||
-                 isa<TagDecl>(IIDecl)))
+                 isa<TagDecl>(IIDecl) ||
+		 isa<TemplateTypeParmDecl>(IIDecl)))
     return IIDecl;
   return 0;
 }
@@ -148,7 +149,8 @@
 
 void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
   if (S->decl_empty()) return;
-  assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!");
+  assert((S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) &&
+	 "Scope shouldn't contain decls!");
 
   for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
        I != E; ++I) {
@@ -165,7 +167,10 @@
     
     // We only want to remove the decls from the identifier decl chains for
     // local scopes, when inside a function/method.
-    if (S->getFnParent() != 0)
+    // However, we *always* remove template parameters, since they are
+    // purely lexically scoped (and can never be found by qualified
+    // name lookup).
+    if (S->getFnParent() != 0 || isa<TemplateTypeParmDecl>(D))
       IdResolver.RemoveDecl(D);
 
     // Chain this decl to the containing DeclContext.
@@ -863,6 +868,15 @@
     }
   }
 
+  if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
+    // Maybe we will complain about the shadowed template parameter.
+    InvalidDecl 
+      = InvalidDecl || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), 
+						       PrevDecl);
+    // Just pretend that we didn't see the previous declaration.
+    PrevDecl = 0;
+  }
+
   // In C++, the previous declaration we find might be a tag type
   // (class or enum). In this case, the new declaration will hide the
   // tag type. 
@@ -1991,7 +2005,12 @@
   // among each other.  Here they can only shadow globals, which is ok.
   IdentifierInfo *II = D.getIdentifier();
   if (Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S)) {
-    if (S->isDeclScope(PrevDecl)) {
+    if (isTemplateParameterDecl(PrevDecl)) {
+      // Maybe we will complain about the shadowed template parameter.
+      DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
+      // Just pretend that we didn't see the previous declaration.
+      PrevDecl = 0;
+    } else if (S->isDeclScope(PrevDecl)) {
       Diag(D.getIdentifierLoc(), diag::err_param_redefinition) << II;
 
       // Recover by removing the name
@@ -2250,6 +2269,13 @@
     PrevDecl = dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag,S));
   }
 
+  if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
+    // Maybe we will complain about the shadowed template parameter.
+    DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
+    // Just pretend that we didn't see the previous declaration.
+    PrevDecl = 0;
+  }
+
   if (PrevDecl) {    
     assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) &&
             "unexpected Decl type");
@@ -2386,6 +2412,13 @@
     PrevDecl = dyn_cast_or_null<ScopedDecl>(LookupDecl(Name, Decl::IDNS_Tag,S));
   }
   
+  if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
+    // Maybe we will complain about the shadowed template parameter.
+    DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
+    // Just pretend that we didn't see the previous declaration.
+    PrevDecl = 0;
+  }
+
   if (PrevDecl) {    
     assert((isa<TagDecl>(PrevDecl) || isa<NamespaceDecl>(PrevDecl)) &&
            "unexpected Decl type");
@@ -2875,7 +2908,15 @@
   
   // Verify that there isn't already something declared with this name in this
   // scope.
-  if (Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S)) {
+  Decl *PrevDecl = LookupDecl(Id, Decl::IDNS_Ordinary, S);
+  if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
+    // Maybe we will complain about the shadowed template parameter.
+    DiagnoseTemplateParameterShadow(IdLoc, PrevDecl);
+    // Just pretend that we didn't see the previous declaration.
+    PrevDecl = 0;
+  }
+
+  if (PrevDecl) {
     // When in C++, we may get a TagDecl with the same name; in this case the
     // enum constant will 'hide' the tag.
     assert((getLangOptions().CPlusPlus || !isa<TagDecl>(PrevDecl)) &&
diff --git a/lib/Sema/SemaDeclObjC.cpp b/lib/Sema/SemaDeclObjC.cpp
index 9da662e..01a453d 100644
--- a/lib/Sema/SemaDeclObjC.cpp
+++ b/lib/Sema/SemaDeclObjC.cpp
@@ -66,6 +66,13 @@
   
   // Check for another declaration kind with the same name.
   Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
+  if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
+    // Maybe we will complain about the shadowed template parameter.
+    DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
+    // Just pretend that we didn't see the previous declaration.
+    PrevDecl = 0;
+  }
+
   if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
     Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
     Diag(PrevDecl->getLocation(), diag::note_previous_definition);
@@ -742,6 +749,13 @@
   for (unsigned i = 0; i != NumElts; ++i) {
     // Check for another declaration kind with the same name.
     Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
+    if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
+      // Maybe we will complain about the shadowed template parameter.
+      DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
+      // Just pretend that we didn't see the previous declaration.
+      PrevDecl = 0;
+    }
+
     if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
       // GCC apparently allows the following idiom:
       //
diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp
new file mode 100644
index 0000000..749d181
--- /dev/null
+++ b/lib/Sema/SemaTemplate.cpp
@@ -0,0 +1,116 @@
+//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
+
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//+//===----------------------------------------------------------------------===/
+
+//
+//  This file implements semantic analysis for C++ templates.
+//+//===----------------------------------------------------------------------===/
+
+#include "Sema.h"
+#include "clang/Parse/DeclSpec.h"
+#include "clang/Basic/LangOptions.h"
+
+using namespace clang;
+
+/// isTemplateParameterDecl - Determines whether the given declaration
+/// 'D' names a template parameter.
+bool Sema::isTemplateParameterDecl(Decl *D) {
+  return isa<TemplateTypeParmDecl>(D) || isa<NonTypeTemplateParmDecl>(D);
+}
+
+/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
+/// that the template parameter 'PrevDecl' is being shadowed by a new
+/// declaration at location Loc. Returns true to indicate that this is
+/// an error, and false otherwise.
+bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
+  assert(isTemplateParameterDecl(PrevDecl) && "Not a template parameter");
+
+  // Microsoft Visual C++ permits template parameters to be shadowed.
+  if (getLangOptions().Microsoft)
+    return false;
+
+  // C++ [temp.local]p4:
+  //   A template-parameter shall not be redeclared within its
+  //   scope (including nested scopes).
+  Diag(Loc, diag::err_template_param_shadow) 
+    << cast<NamedDecl>(PrevDecl)->getDeclName();
+  Diag(PrevDecl->getLocation(), diag::note_template_param_here);
+  return true;
+}
+
+/// ActOnTypeParameter - Called when a C++ template type parameter
+/// (e.g., "typename T") has been parsed. Typename specifies whether
+/// the keyword "typename" was used to declare the type parameter
+/// (otherwise, "class" was used), and KeyLoc is the location of the
+/// "class" or "typename" keyword. ParamName is the name of the
+/// parameter (NULL indicates an unnamed template parameter) and
+/// ParamName is the location of the parameter name (if any). 
+/// If the type parameter has a default argument, it will be added
+/// later via ActOnTypeParameterDefault.
+Sema::DeclTy *Sema::ActOnTypeParameter(Scope *S, bool Typename, 
+				       SourceLocation KeyLoc,
+				       IdentifierInfo *ParamName,
+				       SourceLocation ParamNameLoc) {
+  assert(S->isTemplateParamScope() && 
+	 "Template type parameter not in template parameter scope!");
+  bool Invalid = false;
+
+  if (ParamName) {
+    Decl *PrevDecl = LookupDecl(ParamName, Decl::IDNS_Tag, S);
+    if (PrevDecl && isTemplateParameterDecl(PrevDecl))
+      Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
+							   PrevDecl);
+  }
+
+  TemplateTypeParmDecl *Param
+    = TemplateTypeParmDecl::Create(Context, CurContext, 
+				   ParamNameLoc, ParamName, Typename);
+  if (Invalid)
+    Param->setInvalidDecl();
+
+  if (ParamName) {
+    // Add the template parameter into the current scope.
+    S->AddDecl(Param);
+    IdResolver.AddDecl(Param);
+  }
+
+  return Param;
+}
+
+/// ActOnNonTypeTemplateParameter - Called when a C++ non-type
+/// template parameter (e.g., "int Size" in "template<int Size>
+/// class Array") has been parsed. S is the current scope and D is
+/// the parsed declarator.
+Sema::DeclTy *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D) {
+  QualType T = GetTypeForDeclarator(D, S);
+
+  assert(S->isTemplateParamScope() && 
+	 "Template type parameter not in template parameter scope!");
+  bool Invalid = false;
+
+  IdentifierInfo *ParamName = D.getIdentifier();
+  if (ParamName) {
+    Decl *PrevDecl = LookupDecl(ParamName, Decl::IDNS_Tag, S);
+    if (PrevDecl && isTemplateParameterDecl(PrevDecl))
+      Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
+							   PrevDecl);
+  }
+
+  NonTypeTemplateParmDecl *Param
+    = NonTypeTemplateParmDecl::Create(Context, CurContext, D.getIdentifierLoc(),
+				      ParamName, T);
+  if (Invalid)
+    Param->setInvalidDecl();
+
+  if (D.getIdentifier()) {
+    // Add the template parameter into the current scope.
+    S->AddDecl(Param);
+    IdResolver.AddDecl(Param);
+  }
+  return Param;
+}