blob: 14083e30a992c72a9cb090766dc67fd0077aba18 [file] [log] [blame]
Ted Kremenek9caf8b12007-10-18 00:24:38 +00001//===--- StmtIterator.cpp - Iterators for Statements ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek9caf8b12007-10-18 00:24:38 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines internal methods for StmtIterator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/StmtIterator.h"
Ted Kremenekc325e7f2007-10-18 18:19:31 +000015#include "clang/AST/Expr.h"
Ted Kremenek9caf8b12007-10-18 00:24:38 +000016#include "clang/AST/Decl.h"
17
18using namespace clang;
19
Ted Kremenek92866e22007-10-29 20:50:16 +000020static inline VariableArrayType* FindVA(Type* t) {
21 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
22 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
23 if (vat->getSizeExpr())
24 return vat;
25
26 t = vt->getElementType().getTypePtr();
Ted Kremenekc325e7f2007-10-18 18:19:31 +000027 }
28
Ted Kremenek92866e22007-10-29 20:50:16 +000029 return NULL;
30}
31
32void StmtIteratorBase::NextVA() {
33 assert (getVAPtr());
34
35 VariableArrayType* p = getVAPtr();
36 p = FindVA(p->getElementType().getTypePtr());
37 setVAPtr(p);
38
Ted Kremenek699e9fb2007-12-14 22:52:23 +000039 if (!p && decl) {
Ted Kremenek771fe162007-10-29 21:38:03 +000040 if (VarDecl* VD = dyn_cast<VarDecl>(decl))
41 if (VD->Init)
42 return;
43
44 NextDecl();
Ted Kremenek92866e22007-10-29 20:50:16 +000045 }
Ted Kremenek699e9fb2007-12-14 22:52:23 +000046 else {
47 RawVAPtr = 0;
48 }
Ted Kremenek92866e22007-10-29 20:50:16 +000049}
50
51void StmtIteratorBase::NextDecl(bool ImmediateAdvance) {
Ted Kremenekb51d2b22007-12-14 23:40:56 +000052 assert (inDecl());
Ted Kremenek92866e22007-10-29 20:50:16 +000053 assert (getVAPtr() == NULL);
54 assert (decl);
55
56 if (ImmediateAdvance) {
57 decl = decl->getNextDeclarator();
58
59 if (!decl) {
60 RawVAPtr = 0;
61 return;
62 }
63 }
64
65 for ( ; decl ; decl = decl->getNextDeclarator()) {
Ted Kremenek92866e22007-10-29 20:50:16 +000066 if (VarDecl* VD = dyn_cast<VarDecl>(decl)) {
67 if (VariableArrayType* VAPtr = FindVA(VD->getType().getTypePtr())) {
68 setVAPtr(VAPtr);
69 return;
70 }
71
72 if (VD->getInit())
73 return;
74 }
Ted Kremenek771fe162007-10-29 21:38:03 +000075 else if (TypedefDecl* TD = dyn_cast<TypedefDecl>(decl)) {
76 if (VariableArrayType* VAPtr =
77 FindVA(TD->getUnderlyingType().getTypePtr())) {
78 setVAPtr(VAPtr);
79 return;
80 }
81 }
Ted Kremenek92866e22007-10-29 20:50:16 +000082 else if (EnumConstantDecl* ECD = dyn_cast<EnumConstantDecl>(decl))
83 if (ECD->getInitExpr())
84 return;
85 }
Ted Kremenek103a1b42007-10-29 21:23:58 +000086
87 if (!decl) {
88 RawVAPtr = 0;
89 return;
90 }
Ted Kremenek92866e22007-10-29 20:50:16 +000091}
92
93StmtIteratorBase::StmtIteratorBase(ScopedDecl* d)
94 : decl(d), RawVAPtr(DeclMode) {
95 assert (decl);
96 NextDecl(false);
Ted Kremenekc7c326a2007-10-18 16:25:40 +000097}
Ted Kremenek9caf8b12007-10-18 00:24:38 +000098
Ted Kremenek699e9fb2007-12-14 22:52:23 +000099StmtIteratorBase::StmtIteratorBase(VariableArrayType* t)
Ted Kremenekb51d2b22007-12-14 23:40:56 +0000100: decl(NULL), RawVAPtr(SizeOfTypeVAMode) {
Ted Kremenek699e9fb2007-12-14 22:52:23 +0000101 RawVAPtr |= reinterpret_cast<uintptr_t>(t);
102}
103
104
Ted Kremenekb990f182007-10-25 22:24:19 +0000105Stmt*& StmtIteratorBase::GetDeclExpr() const {
Ted Kremenek92866e22007-10-29 20:50:16 +0000106 if (VariableArrayType* VAPtr = getVAPtr()) {
107 assert (VAPtr->SizeExpr);
108 return reinterpret_cast<Stmt*&>(VAPtr->SizeExpr);
Ted Kremenekb990f182007-10-25 22:24:19 +0000109 }
Ted Kremenek92866e22007-10-29 20:50:16 +0000110
111 if (VarDecl* VD = dyn_cast<VarDecl>(decl)) {
112 assert (VD->Init);
113 return reinterpret_cast<Stmt*&>(VD->Init);
114 }
115
116 EnumConstantDecl* ECD = cast<EnumConstantDecl>(decl);
117 return reinterpret_cast<Stmt*&>(ECD->Init);
Ted Kremenek9caf8b12007-10-18 00:24:38 +0000118}