2007-10-18 08:24:38 +08:00
|
|
|
//===--- StmtIterator.cpp - Iterators for Statements ------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-10-18 08:24:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines internal methods for StmtIterator.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/StmtIterator.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-12-06 07:32:09 +08:00
|
|
|
// FIXME: Add support for dependent-sized array types in C++?
|
|
|
|
// Does it even make sense to build a CFG for an uninstantiated template?
|
2007-10-30 04:50:16 +08:00
|
|
|
static inline VariableArrayType* FindVA(Type* t) {
|
|
|
|
while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
|
|
|
|
if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
|
|
|
|
if (vat->getSizeExpr())
|
|
|
|
return vat;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-30 04:50:16 +08:00
|
|
|
t = vt->getElementType().getTypePtr();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-30 04:50:16 +08:00
|
|
|
return NULL;
|
2007-10-26 06:24:19 +08:00
|
|
|
}
|
|
|
|
|
2007-10-30 04:50:16 +08:00
|
|
|
void StmtIteratorBase::NextVA() {
|
|
|
|
assert (getVAPtr());
|
2007-10-19 00:25:40 +08:00
|
|
|
|
2007-10-30 04:50:16 +08:00
|
|
|
VariableArrayType* p = getVAPtr();
|
|
|
|
p = FindVA(p->getElementType().getTypePtr());
|
|
|
|
setVAPtr(p);
|
2007-10-19 00:25:40 +08:00
|
|
|
|
2008-10-08 07:35:42 +08:00
|
|
|
if (p)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-08 07:35:42 +08:00
|
|
|
if (inDecl()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(decl))
|
2008-10-08 07:35:42 +08:00
|
|
|
if (VD->Init)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-08 07:35:42 +08:00
|
|
|
NextDecl();
|
|
|
|
}
|
|
|
|
else if (inDeclGroup()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(*DGI))
|
2008-10-08 07:35:42 +08:00
|
|
|
if (VD->Init)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
NextDecl();
|
2008-10-08 07:35:42 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert (inSizeOfTypeVA());
|
2008-05-21 13:06:46 +08:00
|
|
|
assert(!decl);
|
2007-12-15 06:52:23 +08:00
|
|
|
RawVAPtr = 0;
|
2008-05-21 13:06:46 +08:00
|
|
|
}
|
2007-10-19 02:19:31 +08:00
|
|
|
}
|
|
|
|
|
2007-10-30 04:50:16 +08:00
|
|
|
void StmtIteratorBase::NextDecl(bool ImmediateAdvance) {
|
|
|
|
assert (getVAPtr() == NULL);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-08 07:04:14 +08:00
|
|
|
if (inDecl()) {
|
2009-12-24 07:38:34 +08:00
|
|
|
assert(decl);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
// FIXME: SIMPLIFY AWAY.
|
2008-10-08 07:04:14 +08:00
|
|
|
if (ImmediateAdvance)
|
2009-03-30 00:50:03 +08:00
|
|
|
decl = 0;
|
|
|
|
else if (HandleDecl(decl))
|
|
|
|
return;
|
2008-10-08 07:04:14 +08:00
|
|
|
}
|
|
|
|
else {
|
2009-12-24 07:38:34 +08:00
|
|
|
assert(inDeclGroup());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-08 07:04:14 +08:00
|
|
|
if (ImmediateAdvance)
|
|
|
|
++DGI;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-08 07:04:14 +08:00
|
|
|
for ( ; DGI != DGE; ++DGI)
|
|
|
|
if (HandleDecl(*DGI))
|
2007-10-30 05:38:03 +08:00
|
|
|
return;
|
2008-10-08 07:04:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RawVAPtr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StmtIteratorBase::HandleDecl(Decl* D) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
|
2008-10-08 07:04:14 +08:00
|
|
|
if (VariableArrayType* VAPtr = FindVA(VD->getType().getTypePtr())) {
|
|
|
|
setVAPtr(VAPtr);
|
|
|
|
return true;
|
2007-10-30 05:38:03 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-08 07:04:14 +08:00
|
|
|
if (VD->getInit())
|
|
|
|
return true;
|
2007-10-19 02:19:31 +08:00
|
|
|
}
|
2008-10-08 07:04:14 +08:00
|
|
|
else if (TypedefDecl* TD = dyn_cast<TypedefDecl>(D)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (VariableArrayType* VAPtr =
|
2008-10-08 07:04:14 +08:00
|
|
|
FindVA(TD->getUnderlyingType().getTypePtr())) {
|
|
|
|
setVAPtr(VAPtr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (EnumConstantDecl* ECD = dyn_cast<EnumConstantDecl>(D)) {
|
|
|
|
if (ECD->getInitExpr())
|
|
|
|
return true;
|
2007-10-30 05:23:58 +08:00
|
|
|
}
|
2008-10-08 07:04:14 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
return false;
|
2007-10-30 04:50:16 +08:00
|
|
|
}
|
|
|
|
|
2009-12-24 07:38:34 +08:00
|
|
|
StmtIteratorBase::StmtIteratorBase(Decl *d, Stmt **s)
|
|
|
|
: stmt(s), decl(d), RawVAPtr(d ? DeclMode : 0) {
|
|
|
|
if (decl)
|
|
|
|
NextDecl(false);
|
2007-10-19 00:25:40 +08:00
|
|
|
}
|
2007-10-18 08:24:38 +08:00
|
|
|
|
2008-10-08 07:04:14 +08:00
|
|
|
StmtIteratorBase::StmtIteratorBase(Decl** dgi, Decl** dge)
|
2009-12-24 06:31:49 +08:00
|
|
|
: stmt(0), DGI(dgi), RawVAPtr(DeclGroupMode), DGE(dge) {
|
2008-10-08 07:04:14 +08:00
|
|
|
NextDecl(false);
|
|
|
|
}
|
|
|
|
|
2007-12-15 06:52:23 +08:00
|
|
|
StmtIteratorBase::StmtIteratorBase(VariableArrayType* t)
|
2009-12-24 06:31:49 +08:00
|
|
|
: stmt(0), decl(0), RawVAPtr(SizeOfTypeVAMode) {
|
2007-12-15 06:52:23 +08:00
|
|
|
RawVAPtr |= reinterpret_cast<uintptr_t>(t);
|
|
|
|
}
|
|
|
|
|
2007-10-26 06:24:19 +08:00
|
|
|
Stmt*& StmtIteratorBase::GetDeclExpr() const {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-30 04:50:16 +08:00
|
|
|
if (VariableArrayType* VAPtr = getVAPtr()) {
|
|
|
|
assert (VAPtr->SizeExpr);
|
2008-05-31 00:14:41 +08:00
|
|
|
return VAPtr->SizeExpr;
|
2007-10-26 06:24:19 +08:00
|
|
|
}
|
2009-03-12 02:17:16 +08:00
|
|
|
|
|
|
|
assert (inDecl() || inDeclGroup());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-12 02:17:16 +08:00
|
|
|
if (inDeclGroup()) {
|
|
|
|
VarDecl* VD = cast<VarDecl>(*DGI);
|
2009-05-27 02:54:04 +08:00
|
|
|
return *VD->getInitAddress();
|
2009-03-12 02:17:16 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-08 07:04:14 +08:00
|
|
|
assert (inDecl());
|
2007-10-30 04:50:16 +08:00
|
|
|
|
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(decl)) {
|
|
|
|
assert (VD->Init);
|
2009-05-27 02:54:04 +08:00
|
|
|
return *VD->getInitAddress();
|
2007-10-30 04:50:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EnumConstantDecl* ECD = cast<EnumConstantDecl>(decl);
|
2008-05-31 00:14:41 +08:00
|
|
|
return ECD->Init;
|
2007-10-18 08:24:38 +08:00
|
|
|
}
|