Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 1 | //===--- StmtIterator.cpp - Iterators for Statements ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Ted Kremenek and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines internal methods for StmtIterator. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/StmtIterator.h" |
Ted Kremenek | c325e7f | 2007-10-18 18:19:31 +0000 | [diff] [blame] | 15 | #include "clang/AST/Expr.h" |
Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
| 17 | |
| 18 | using namespace clang; |
| 19 | |
Ted Kremenek | b990f18 | 2007-10-25 22:24:19 +0000 | [diff] [blame^] | 20 | static inline bool declHasExpr(ScopedDecl *decl) { |
| 21 | if (VarDecl* D = dyn_cast<VarDecl>(decl)) |
| 22 | if (D->getInit()) |
| 23 | return true; |
| 24 | |
| 25 | if (EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(decl)) |
| 26 | if (D->getInitExpr()) |
| 27 | return true; |
| 28 | |
| 29 | return false; |
| 30 | } |
| 31 | |
Ted Kremenek | c325e7f | 2007-10-18 18:19:31 +0000 | [diff] [blame] | 32 | void StmtIteratorBase::NextDecl() { |
| 33 | assert (FirstDecl && Ptr.D); |
| 34 | |
| 35 | do Ptr.D = Ptr.D->getNextDeclarator(); |
Ted Kremenek | b990f18 | 2007-10-25 22:24:19 +0000 | [diff] [blame^] | 36 | while (Ptr.D != NULL && !declHasExpr(Ptr.D)); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 37 | |
| 38 | if (Ptr.D == NULL) FirstDecl = NULL; |
Ted Kremenek | c7c326a | 2007-10-18 16:25:40 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Ted Kremenek | c325e7f | 2007-10-18 18:19:31 +0000 | [diff] [blame] | 41 | StmtIteratorBase::StmtIteratorBase(ScopedDecl* d) { |
Ted Kremenek | c7c326a | 2007-10-18 16:25:40 +0000 | [diff] [blame] | 42 | assert (d); |
| 43 | |
Ted Kremenek | b990f18 | 2007-10-25 22:24:19 +0000 | [diff] [blame^] | 44 | while (d != NULL && !declHasExpr(d)) |
Ted Kremenek | c325e7f | 2007-10-18 18:19:31 +0000 | [diff] [blame] | 45 | d = d->getNextDeclarator(); |
Ted Kremenek | c325e7f | 2007-10-18 18:19:31 +0000 | [diff] [blame] | 46 | |
| 47 | FirstDecl = d; |
| 48 | Ptr.D = d; |
| 49 | } |
| 50 | |
| 51 | void StmtIteratorBase::PrevDecl() { |
| 52 | assert (FirstDecl); |
| 53 | assert (Ptr.D != FirstDecl); |
Ted Kremenek | c7c326a | 2007-10-18 16:25:40 +0000 | [diff] [blame] | 54 | |
| 55 | // March through the list of decls until we find the decl just before |
| 56 | // the one we currently point |
| 57 | |
Ted Kremenek | c325e7f | 2007-10-18 18:19:31 +0000 | [diff] [blame] | 58 | ScopedDecl* d = FirstDecl; |
| 59 | ScopedDecl* lastVD = d; |
Ted Kremenek | c7c326a | 2007-10-18 16:25:40 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | c325e7f | 2007-10-18 18:19:31 +0000 | [diff] [blame] | 61 | while (d->getNextDeclarator() != Ptr.D) { |
| 62 | if (VarDecl* V = dyn_cast<VarDecl>(d)) |
| 63 | if (V->getInit()) |
| 64 | lastVD = d; |
| 65 | |
| 66 | d = d->getNextDeclarator(); |
| 67 | } |
| 68 | |
| 69 | Ptr.D = lastVD; |
Ted Kremenek | c7c326a | 2007-10-18 16:25:40 +0000 | [diff] [blame] | 70 | } |
Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | b990f18 | 2007-10-25 22:24:19 +0000 | [diff] [blame^] | 72 | Stmt*& StmtIteratorBase::GetDeclExpr() const { |
| 73 | if (VarDecl* D = dyn_cast<VarDecl>(Ptr.D)) |
| 74 | return reinterpret_cast<Stmt*&>(D->Init); |
| 75 | else { |
| 76 | EnumConstantDecl* D = cast<EnumConstantDecl>(Ptr.D); |
| 77 | return reinterpret_cast<Stmt*&>(D->Init); |
| 78 | } |
Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 79 | } |