freebsd-nq/include/clang/AST/StmtIterator.h

161 lines
4.3 KiB
C
Raw Normal View History

//===--- StmtIterator.h - Iterators for Statements --------------*- C++ -*-===//
2009-06-02 17:58:47 +00:00
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the StmtIterator and ConstStmtIterator classes.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_AST_STMTITERATOR_H
#define LLVM_CLANG_AST_STMTITERATOR_H
2009-06-02 17:58:47 +00:00
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"
2009-06-02 17:58:47 +00:00
#include <cassert>
2010-07-13 17:21:42 +00:00
#include <cstddef>
2009-10-14 18:03:49 +00:00
#include <iterator>
#include <utility>
2009-06-02 17:58:47 +00:00
namespace clang {
class Stmt;
class Decl;
class VariableArrayType;
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
class StmtIteratorBase {
protected:
enum { StmtMode = 0x0, SizeOfTypeVAMode = 0x1, DeclGroupMode = 0x2,
2009-06-02 17:58:47 +00:00
Flags = 0x3 };
2010-01-01 10:34:51 +00:00
union {
Stmt **stmt;
Decl **DGI;
};
2009-10-14 18:03:49 +00:00
uintptr_t RawVAPtr;
2010-01-01 10:34:51 +00:00
Decl **DGE;
2009-06-02 17:58:47 +00:00
bool inDeclGroup() const {
return (RawVAPtr & Flags) == DeclGroupMode;
}
2009-10-14 18:03:49 +00:00
bool inSizeOfTypeVA() const {
2009-06-02 17:58:47 +00:00
return (RawVAPtr & Flags) == SizeOfTypeVAMode;
}
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
bool inStmt() const {
return (RawVAPtr & Flags) == StmtMode;
2009-06-02 17:58:47 +00:00
}
2009-10-14 18:03:49 +00:00
const VariableArrayType *getVAPtr() const {
return reinterpret_cast<const VariableArrayType*>(RawVAPtr & ~Flags);
2009-06-02 17:58:47 +00:00
}
2009-10-14 18:03:49 +00:00
void setVAPtr(const VariableArrayType *P) {
assert (inDeclGroup() || inSizeOfTypeVA());
2009-06-02 17:58:47 +00:00
RawVAPtr = reinterpret_cast<uintptr_t>(P) | (RawVAPtr & Flags);
}
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
void NextDecl(bool ImmediateAdvance = true);
bool HandleDecl(Decl* D);
void NextVA();
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
Stmt*& GetDeclExpr() const;
StmtIteratorBase(Stmt **s) : stmt(s), RawVAPtr(0) {}
StmtIteratorBase(const VariableArrayType *t);
2010-01-01 10:34:51 +00:00
StmtIteratorBase(Decl **dgi, Decl **dge);
StmtIteratorBase() : stmt(nullptr), RawVAPtr(0) {}
2009-06-02 17:58:47 +00:00
};
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
template <typename DERIVED, typename REFERENCE>
2009-10-14 18:03:49 +00:00
class StmtIteratorImpl : public StmtIteratorBase,
2009-06-02 17:58:47 +00:00
public std::iterator<std::forward_iterator_tag,
2009-10-14 18:03:49 +00:00
REFERENCE, ptrdiff_t,
REFERENCE, REFERENCE> {
2009-06-02 17:58:47 +00:00
protected:
StmtIteratorImpl(const StmtIteratorBase& RHS) : StmtIteratorBase(RHS) {}
public:
2009-10-14 18:03:49 +00:00
StmtIteratorImpl() {}
2010-01-01 10:34:51 +00:00
StmtIteratorImpl(Stmt **s) : StmtIteratorBase(s) {}
StmtIteratorImpl(Decl **dgi, Decl **dge) : StmtIteratorBase(dgi, dge) {}
StmtIteratorImpl(const VariableArrayType *t) : StmtIteratorBase(t) {}
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
DERIVED& operator++() {
if (inStmt())
++stmt;
else if (getVAPtr())
2009-06-02 17:58:47 +00:00
NextVA();
else
NextDecl();
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
return static_cast<DERIVED&>(*this);
}
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
DERIVED operator++(int) {
DERIVED tmp = static_cast<DERIVED&>(*this);
operator++();
return tmp;
}
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
bool operator==(const DERIVED& RHS) const {
return stmt == RHS.stmt && DGI == RHS.DGI && RawVAPtr == RHS.RawVAPtr;
2009-06-02 17:58:47 +00:00
}
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
bool operator!=(const DERIVED& RHS) const {
return stmt != RHS.stmt || DGI != RHS.DGI || RawVAPtr != RHS.RawVAPtr;
2009-06-02 17:58:47 +00:00
}
2009-10-14 18:03:49 +00:00
REFERENCE operator*() const {
return inStmt() ? *stmt : GetDeclExpr();
2009-06-02 17:58:47 +00:00
}
2009-10-14 18:03:49 +00:00
REFERENCE operator->() const { return operator*(); }
2009-06-02 17:58:47 +00:00
};
struct ConstStmtIterator;
2009-06-02 17:58:47 +00:00
struct StmtIterator : public StmtIteratorImpl<StmtIterator,Stmt*&> {
explicit StmtIterator() : StmtIteratorImpl<StmtIterator,Stmt*&>() {}
StmtIterator(Stmt** S) : StmtIteratorImpl<StmtIterator,Stmt*&>(S) {}
2010-01-01 10:34:51 +00:00
2009-10-14 18:03:49 +00:00
StmtIterator(Decl** dgi, Decl** dge)
2009-06-02 17:58:47 +00:00
: StmtIteratorImpl<StmtIterator,Stmt*&>(dgi, dge) {}
StmtIterator(const VariableArrayType *t)
2010-01-01 10:34:51 +00:00
: StmtIteratorImpl<StmtIterator,Stmt*&>(t) {}
private:
StmtIterator(const StmtIteratorBase &RHS)
: StmtIteratorImpl<StmtIterator, Stmt *&>(RHS) {}
inline friend StmtIterator
cast_away_const(const ConstStmtIterator &RHS);
2009-06-02 17:58:47 +00:00
};
struct ConstStmtIterator : public StmtIteratorImpl<ConstStmtIterator,
const Stmt*> {
2009-10-14 18:03:49 +00:00
explicit ConstStmtIterator() :
2009-06-02 17:58:47 +00:00
StmtIteratorImpl<ConstStmtIterator,const Stmt*>() {}
2009-10-14 18:03:49 +00:00
ConstStmtIterator(const StmtIterator& RHS) :
2009-06-02 17:58:47 +00:00
StmtIteratorImpl<ConstStmtIterator,const Stmt*>(RHS) {}
ConstStmtIterator(Stmt * const *S)
: StmtIteratorImpl<ConstStmtIterator, const Stmt *>(
const_cast<Stmt **>(S)) {}
2009-06-02 17:58:47 +00:00
};
inline StmtIterator cast_away_const(const ConstStmtIterator &RHS) {
return RHS;
}
2009-06-02 17:58:47 +00:00
} // end namespace clang
#endif