Introduce the virtual method Decl::getPrimaryDecl().

When a Decl subclass can have multiple re-declarations in the same declaration context (like FunctionDecl),
getPrimaryDecl() will return a particular Decl that all of them will point to as the "primary" declaration.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@74800 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index 3d02150..bcbf091 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -358,6 +358,14 @@
   return Def? Def->getInit() : 0;
 }
 
+Decl *VarDecl::getPrimaryDecl() const {
+  const VarDecl *Prim = this;
+  while (Prim->getPreviousDeclaration())
+    Prim = Prim->getPreviousDeclaration();
+
+  return const_cast<VarDecl *>(Prim);
+}
+
 //===----------------------------------------------------------------------===//
 // FunctionDecl Implementation
 //===----------------------------------------------------------------------===//
@@ -569,6 +577,14 @@
   }
 }
 
+Decl *FunctionDecl::getPrimaryDecl() const {
+  const FunctionDecl *Prim = this;
+  while (Prim->getPreviousDeclaration())
+    Prim = Prim->getPreviousDeclaration();
+
+  return const_cast<FunctionDecl *>(Prim);
+}
+
 /// getOverloadedOperator - Which C++ overloaded operator this
 /// function represents, if any.
 OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {