]> granicus.if.org Git - clang/commitdiff
Introduce the virtual method Decl::getPrimaryDecl().
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sun, 5 Jul 2009 22:21:56 +0000 (22:21 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sun, 5 Jul 2009 22:21:56 +0000 (22:21 +0000)
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

include/clang/AST/Decl.h
include/clang/AST/DeclBase.h
lib/AST/Decl.cpp

index 69a52869d8188710572d66cb80f7aa58e6dfb5b4..e7c7496d8b4ef0c21f8cfed70d8edd1188b1c26f 100644 (file)
@@ -413,6 +413,8 @@ public:
     PreviousDeclaration = PrevDecl;
   }
 
+  virtual Decl *getPrimaryDecl() const;
+
   /// hasLocalStorage - Returns true if a variable with function scope
   ///  is a non-static local variable.
   bool hasLocalStorage() const {
@@ -811,6 +813,8 @@ public:
 
   void setPreviousDeclaration(FunctionDecl * PrevDecl);
 
+  virtual Decl *getPrimaryDecl() const;
+
   unsigned getBuiltinID(ASTContext &Context) const;
 
   unsigned getNumParmVarDeclsFromType() const;
index 0bce2f84c7ba7688d51465bff0bb632bd897f85b..019a0fe965b90c560d0c04d7d994bc56df584dbf 100644 (file)
@@ -311,6 +311,13 @@ public:
   // be defined inside or outside a function etc).
   bool isDefinedOutsideFunctionOrMethod() const;
 
+  /// \brief When there are multiple re-declarations (e.g. for functions),
+  /// this will return the primary one which all of them point to.
+  virtual Decl *getPrimaryDecl() const { return const_cast<Decl*>(this); }
+
+  /// \brief Whether this particular Decl is a primary one.
+  bool isPrimaryDecl() const { return getPrimaryDecl() == this; }
+
   /// getBody - If this Decl represents a declaration for a body of code,
   ///  such as a function or method definition, this method returns the
   ///  top-level Stmt* of that body.  Otherwise this method returns null.
index 3d02150b65bff96863f9c444e0e74148362915ca..bcbf091a195addd8000387165cba70b016d54e94 100644 (file)
@@ -358,6 +358,14 @@ const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
   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 @@ FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
   }
 }
 
+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 {