Clean up our handling of local instantiation scopes, which keep track
of the mapping from local declarations to their instantiated
counterparts during template instantiation. Previously, we tried to do
some unholy merging of local instantiation scopes that involved
storing a single hash table along with an "undo" list on the
side... which was ugly, and never handled function parameters
properly.

Now, we just keep separate hash tables for each local instantiation
scope, and "combining" two scopes means that we'll look in each of the
combined hash tables. The combined scope stack is rarely deep, and
this makes it easy to avoid the "undo" issues we were hitting. Also,
I've simplified the logic for function parameters: if we're declaring
a function and we need the function parameters to live longer, we just
push them back into the local instantiation scope where we need them. 

Fixes PR6990.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@102732 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 25ba282..48b517e 100644
--- a/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -822,7 +822,7 @@
   // merged with the local instantiation scope for the function template 
   // itself.
   Sema::LocalInstantiationScope Scope(SemaRef);
-  
+
   TemplateParameterList *TempParams = D->getTemplateParameters();
   TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
   if (!InstParams)
@@ -1775,11 +1775,18 @@
 
   if (NewTInfo != OldTInfo) {
     // Get parameters from the new type info.
+    TypeLoc OldTL = OldTInfo->getTypeLoc();
+    FunctionProtoTypeLoc *OldProtoLoc = cast<FunctionProtoTypeLoc>(&OldTL);
     TypeLoc NewTL = NewTInfo->getTypeLoc();
     FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
     assert(NewProtoLoc && "Missing prototype?");
-    for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i)
+    for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
+      // FIXME: Variadic templates will break this.
       Params.push_back(NewProtoLoc->getArg(i));
+      SemaRef.CurrentInstantiationScope->InstantiatedLocal(
+                                                        OldProtoLoc->getArg(i),
+                                                        NewProtoLoc->getArg(i));
+    }
   } else {
     // The function type itself was not dependent and therefore no
     // substitution occurred. However, we still need to instantiate