//===--- Stmt.h - Classes for representing statements -----------*- C++ -*-===//
//
// 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 Stmt interface and subclasses.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_AST_STMT_H
#define LLVM_CLANG_AST_STMT_H
#include "llvm/Support/Casting.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/AST/StmtIterator.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/iterator"
#include "llvm/Bitcode/SerializationFwd.h"
#include <iosfwd>
#include <string>
using llvm::dyn_cast_or_null;
namespace clang {
class ASTContext;
class Expr;
class Decl;
class ScopedDecl;
class IdentifierInfo;
class SourceManager;
class StringLiteral;
class SwitchStmt;
class PrinterHelper;
/// Stmt - This represents one statement.
///
class Stmt {
public:
enum StmtClass {
#define STMT(N, CLASS, PARENT) CLASS##Class = N,
#define FIRST_STMT(N) firstStmtConstant = N,
#define LAST_STMT(N) lastStmtConstant = N,
#define FIRST_EXPR(N) firstExprConstant = N,
#define LAST_EXPR(N) lastExprConstant = N
#include "clang/AST/StmtNodes.def"
};
private:
const StmtClass sClass;
public:
Stmt(StmtClass SC) : sClass(SC) {
if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
}
virtual ~Stmt() {}
StmtClass getStmtClass() const { return sClass; }
const char *getStmtClassName() const;
/// SourceLocation tokens are not useful in isolation - they are low level
/// value objects created/interpreted by SourceManager. We assume AST
/// clients will have a pointer to the respective SourceManager.
virtual SourceRange getSourceRange() const = 0;
SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
// global temp stats (until we have a per-module visitor)
static void addStmtClass(const StmtClass s);
static bool CollectingStats(bool enable=false);
static void PrintStats();
/// dump - This does a local dump of the specified AST fragment. It dumps the
/// specified node and a few nodes underneath it, but not the whole subtree.
/// This is useful in a debugger.
void dump() const;
void dump(SourceManager &SM) const;
/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
void dumpAll() const;
void dumpAll(SourceManager &SM) const;
/// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
/// back to its original source language syntax.
void dumpPretty() const;
void printPretty(std::ostream &OS, PrinterHelper* = NULL) const;
/// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
/// works on systems with GraphViz (Mac OS X) or dot+gv installed.
void viewAST() const;
// Implement isa<T> support.
static bool classof(const Stmt *) { return true; }
/// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
/// contain implicit control-flow in the order their subexpressions
/// are evaluated. This predicate returns true if this statement has
/// such implicit control-flow. Such statements are also specially handled
/// within CFGs.
bool hasImplicitControlFlow() const;
/// Child Iterators: All subclasses must implement child_begin and child_end
/// to permit easy iteration over the substatements/subexpessions of an
/// AST node. This permits easy iteration over all nodes in the AST.
typedef StmtIterator child_iterator;
typedef ConstStmtIterator const_child_iterator;
virtual child_iterator child_begin() = 0;
virtual child_iterator child_end() = 0;
const_child_iterator child_begin() const {
return const_child_iterator(const_cast<Stmt*>(this)->child_begin());
}
const_child_iterator child_end() const {
return const_child_iterator(const_cast<Stmt*>(this)->child_end());
}
void Emit(llvm::Serializer& S) const;
static Stmt* Create(llvm::Deserializer& D, ASTContext& C);
virtual void EmitImpl(llvm::Serializer& S) const {
// This method will eventually be a pure-virtual function.
assert (false && "Not implemented.");
}
};
/// DeclStmt - Adaptor class for mixing declarations with statements and
/// expressions. For example, CompoundStmt mixes statements, expressions
/// and declarations (variables, types). Another example is ForStmt, where
/// the first statement can be an expression or a declaration.
///
class DeclStmt : public Stmt {
ScopedDecl *TheDecl;
SourceLocation StartLoc, EndLoc;
public:
DeclStmt(ScopedDecl *D, SourceLocation startLoc, SourceLocation endLoc)
: Stmt(DeclStmtClass), TheDecl(D