Update UsingDecl, UnresolvedUsingTypenameDecl, and
UnresolvedUsingValueDecl to use NestedNameSpecifierLoc rather than the
extremely-lossy NestedNameSpecifier/SourceRange pair it used to use,
improving source-location information.

Various infrastructure updates to support NestedNameSpecifierLoc:
  - AST/PCH (de-)serialization
  - Recursive AST visitor
  - libclang traversal (including the first tests of this
    functionality)



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@126459 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index 8e1792b..89b2263 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -3919,16 +3919,16 @@
       if (OrigDC == CurContext) {
         Diag(Using->getLocation(),
              diag::err_using_decl_nested_name_specifier_is_current_class)
-          << Using->getNestedNameRange();
+          << Using->getQualifierLoc().getSourceRange();
         Diag(Orig->getLocation(), diag::note_using_decl_target);
         return true;
       }
 
-      Diag(Using->getNestedNameRange().getBegin(),
+      Diag(Using->getQualifierLoc().getBeginLoc(),
            diag::err_using_decl_nested_name_specifier_is_not_base_class)
-        << Using->getTargetNestedNameDecl()
+        << Using->getQualifier()
         << cast<CXXRecordDecl>(CurContext)
-        << Using->getNestedNameRange();
+        << Using->getQualifierLoc().getSourceRange();
       Diag(Orig->getLocation(), diag::note_using_decl_target);
       return true;
     }
@@ -4134,8 +4134,6 @@
     LookupQualifiedName(Previous, CurContext);
   }
 
-  NestedNameSpecifier *NNS = SS.getScopeRep();
-
   // Check for invalid redeclarations.
   if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous))
     return 0;
@@ -4146,22 +4144,21 @@
 
   DeclContext *LookupContext = computeDeclContext(SS);
   NamedDecl *D;
+  NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
   if (!LookupContext) {
     if (IsTypeName) {
       // FIXME: not all declaration name kinds are legal here
       D = UnresolvedUsingTypenameDecl::Create(Context, CurContext,
                                               UsingLoc, TypenameLoc,
-                                              SS.getRange(), NNS,
+                                              QualifierLoc,
                                               IdentLoc, NameInfo.getName());
     } else {
-      D = UnresolvedUsingValueDecl::Create(Context, CurContext,
-                                           UsingLoc, SS.getRange(),
-                                           NNS, NameInfo);
+      D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, 
+                                           QualifierLoc, NameInfo);
     }
   } else {
-    D = UsingDecl::Create(Context, CurContext,
-                          SS.getRange(), UsingLoc, NNS, NameInfo,
-                          IsTypeName);
+    D = UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc,
+                          NameInfo, IsTypeName);
   }
   D->setAccess(AS);
   CurContext->addDecl(D);
@@ -4252,7 +4249,7 @@
     return true;
   }
 
-  const Type *SourceType = UD->getTargetNestedNameDecl()->getAsType();
+  const Type *SourceType = UD->getQualifier()->getAsType();
   assert(SourceType &&
          "Using decl naming constructor doesn't have type in scope spec.");
   CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext);
@@ -4309,15 +4306,15 @@
     NestedNameSpecifier *DQual;
     if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
       DTypename = UD->isTypeName();
-      DQual = UD->getTargetNestedNameDecl();
+      DQual = UD->getQualifier();
     } else if (UnresolvedUsingValueDecl *UD
                  = dyn_cast<UnresolvedUsingValueDecl>(D)) {
       DTypename = false;
-      DQual = UD->getTargetNestedNameSpecifier();
+      DQual = UD->getQualifier();
     } else if (UnresolvedUsingTypenameDecl *UD
                  = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
       DTypename = true;
-      DQual = UD->getTargetNestedNameSpecifier();
+      DQual = UD->getQualifier();
     } else continue;
 
     // using decls differ if one says 'typename' and the other doesn't.
diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp
index 474a18d..8f7b7da 100644
--- a/lib/Sema/SemaTemplate.cpp
+++ b/lib/Sema/SemaTemplate.cpp
@@ -6036,7 +6036,7 @@
       << Name << Ctx << FullRange;
     if (UnresolvedUsingValueDecl *Using
           = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
-      SourceLocation Loc = Using->getTargetNestedNameRange().getBegin();
+      SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
       Diag(Loc, diag::note_using_value_decl_missing_typename)
         << FixItHint::CreateInsertion(Loc, "typename ");
     }
diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp
index de4cd46..e1a7065 100644
--- a/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -1661,12 +1661,12 @@
   //     template struct t<int>;
   // Here, in using s1::f1, s1 refers to t<T>::s1;
   // we need to substitute for t<int>::s1.
-  NestedNameSpecifier *NNS =
-      SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameDecl(),
-      D->getNestedNameRange(),
-      TemplateArgs);
+  NestedNameSpecifier *NNS 
+    = SemaRef.SubstNestedNameSpecifier(D->getQualifier(), 
+                                       D->getQualifierRange(),
+                                       TemplateArgs);
   if (!NNS)
-      return 0;
+    return 0;
 
   // The name info is non-dependent, so no transformation
   // is required.
@@ -1680,16 +1680,18 @@
   LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
                     Sema::ForRedeclaration);
 
+  CXXScopeSpec SS;
+  if (NNS == D->getQualifier())
+    SS.Adopt(D->getQualifierLoc());
+  else
+    SS.MakeTrivial(SemaRef.Context, NNS, D->getQualifierRange());
+
   UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
-                                       D->getNestedNameRange(),
                                        D->getUsingLocation(),
-                                       NNS,
+                                       SS.getWithLocInContext(SemaRef.Context),
                                        NameInfo,
                                        D->isTypeName());
 
-  CXXScopeSpec SS;
-  SS.MakeTrivial(SemaRef.Context, NNS, D->getNestedNameRange());
-
   if (CheckRedeclaration) {
     Prev.setHideTags(false);
     SemaRef.LookupQualifiedName(Prev, Owner);
@@ -1749,14 +1751,13 @@
 Decl * TemplateDeclInstantiator
     ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
   NestedNameSpecifier *NNS =
-    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
-                                     D->getTargetNestedNameRange(),
+    SemaRef.SubstNestedNameSpecifier(D->getQualifier(), D->getQualifierRange(),
                                      TemplateArgs);
   if (!NNS)
     return 0;
 
   CXXScopeSpec SS;
-  SS.MakeTrivial(SemaRef.Context, NNS, D->getTargetNestedNameRange());
+  SS.MakeTrivial(SemaRef.Context, NNS, D->getQualifierRange());
 
   // Since NameInfo refers to a typename, it cannot be a C++ special name.
   // Hence, no tranformation is required for it.
@@ -1775,14 +1776,13 @@
 Decl * TemplateDeclInstantiator
     ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
   NestedNameSpecifier *NNS =
-    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
-                                     D->getTargetNestedNameRange(),
+    SemaRef.SubstNestedNameSpecifier(D->getQualifier(), D->getQualifierRange(),
                                      TemplateArgs);
   if (!NNS)
     return 0;
 
   CXXScopeSpec SS;
-  SS.MakeTrivial(SemaRef.Context, NNS, D->getTargetNestedNameRange());
+  SS.MakeTrivial(SemaRef.Context, NNS, D->getQualifierRange());
 
   DeclarationNameInfo NameInfo
     = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);