1 //===-- Redeclarable.h - Base for Decls that can be redeclared -*- C++ -*-====//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Redeclarable interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_REDECLARABLE_H
15 #define LLVM_CLANG_AST_REDECLARABLE_H
17 #include "llvm/ADT/PointerIntPair.h"
18 #include "llvm/Support/Casting.h"
23 /// \brief Provides common interface for the Decls that can be redeclared.
24 template<typename decl_type>
29 llvm::PointerIntPair<decl_type *, 1, bool> NextAndIsPrevious;
31 DeclLink(decl_type *D, bool isLatest)
32 : NextAndIsPrevious(D, isLatest) { }
34 bool NextIsPrevious() const { return !NextAndIsPrevious.getInt(); }
35 bool NextIsLatest() const { return NextAndIsPrevious.getInt(); }
36 decl_type *getNext() const { return NextAndIsPrevious.getPointer(); }
37 void setNext(decl_type *D) { NextAndIsPrevious.setPointer(D); }
40 static DeclLink PreviousDeclLink(decl_type *D) {
41 return DeclLink(D, false);
44 static DeclLink LatestDeclLink(decl_type *D) {
45 return DeclLink(D, true);
48 /// \brief Points to the next redeclaration in the chain.
50 /// If NextIsPrevious() is true, this is a link to the previous declaration
51 /// of this same Decl. If NextIsLatest() is true, this is the first
52 /// declaration and Link points to the latest declaration. For example:
54 /// #1 int f(int x, int y = 1); // <pointer to #3, true>
55 /// #2 int f(int x = 0, int y); // <pointer to #1, false>
56 /// #3 int f(int x, int y) { return x + y; } // <pointer to #2, false>
58 /// If there is only one declaration, it is <pointer to self, true>
62 Redeclarable() : RedeclLink(LatestDeclLink(static_cast<decl_type*>(this))) { }
64 /// \brief Return the previous declaration of this declaration or NULL if this
65 /// is the first declaration.
66 decl_type *getPreviousDecl() {
67 if (RedeclLink.NextIsPrevious())
68 return RedeclLink.getNext();
71 const decl_type *getPreviousDecl() const {
72 return const_cast<decl_type *>(
73 static_cast<const decl_type*>(this))->getPreviousDecl();
76 /// \brief Return the first declaration of this declaration or itself if this
77 /// is the only declaration.
78 decl_type *getFirstDeclaration() {
79 decl_type *D = static_cast<decl_type*>(this);
80 while (D->getPreviousDecl())
81 D = D->getPreviousDecl();
85 /// \brief Return the first declaration of this declaration or itself if this
86 /// is the only declaration.
87 const decl_type *getFirstDeclaration() const {
88 const decl_type *D = static_cast<const decl_type*>(this);
89 while (D->getPreviousDecl())
90 D = D->getPreviousDecl();
94 /// \brief Returns true if this is the first declaration.
95 bool isFirstDeclaration() const {
96 return RedeclLink.NextIsLatest();
99 /// \brief Returns the most recent (re)declaration of this declaration.
100 decl_type *getMostRecentDecl() {
101 return getFirstDeclaration()->RedeclLink.getNext();
104 /// \brief Returns the most recent (re)declaration of this declaration.
105 const decl_type *getMostRecentDecl() const {
106 return getFirstDeclaration()->RedeclLink.getNext();
109 /// \brief Set the previous declaration. If PrevDecl is NULL, set this as the
110 /// first and only declaration.
111 void setPreviousDeclaration(decl_type *PrevDecl);
113 /// \brief Iterates through all the redeclarations of the same decl.
114 class redecl_iterator {
115 /// Current - The current declaration.
121 typedef decl_type* value_type;
122 typedef decl_type* reference;
123 typedef decl_type* pointer;
124 typedef std::forward_iterator_tag iterator_category;
125 typedef std::ptrdiff_t difference_type;
127 redecl_iterator() : Current(0) { }
128 explicit redecl_iterator(decl_type *C)
129 : Current(C), Starter(C), PassedFirst(false) { }
131 reference operator*() const { return Current; }
132 pointer operator->() const { return Current; }
134 redecl_iterator& operator++() {
135 assert(Current && "Advancing while iterator has reached end");
136 // Sanity check to avoid infinite loop on invalid redecl chain.
137 if (Current->isFirstDeclaration()) {
139 assert(0 && "Passed first decl twice, invalid redecl chain!");
146 // Get either previous decl or latest decl.
147 decl_type *Next = Current->RedeclLink.getNext();
148 Current = (Next != Starter ? Next : 0);
152 redecl_iterator operator++(int) {
153 redecl_iterator tmp(*this);
158 friend bool operator==(redecl_iterator x, redecl_iterator y) {
159 return x.Current == y.Current;
161 friend bool operator!=(redecl_iterator x, redecl_iterator y) {
162 return x.Current != y.Current;
166 /// \brief Returns iterator for all the redeclarations of the same decl.
167 /// It will iterate at least once (when this decl is the only one).
168 redecl_iterator redecls_begin() const {
169 return redecl_iterator(const_cast<decl_type*>(
170 static_cast<const decl_type*>(this)));
172 redecl_iterator redecls_end() const { return redecl_iterator(); }
174 friend class ASTDeclReader;
175 friend class ASTDeclWriter;