2006-10-25 12:09:21 +08:00
|
|
|
//===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
2006-10-25 12:09:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Stmt class and statement subclasses.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/Stmt.h"
|
2006-12-05 02:06:35 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2006-11-04 14:21:51 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2007-10-07 16:58:51 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2006-10-25 12:09:21 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2007-05-24 05:48:04 +08:00
|
|
|
static struct StmtClassNameTable {
|
2007-08-25 09:42:24 +08:00
|
|
|
const char *Name;
|
|
|
|
unsigned Counter;
|
|
|
|
unsigned Size;
|
2007-08-25 09:55:00 +08:00
|
|
|
} StmtClassInfo[Stmt::lastExprConstant+1];
|
2007-08-25 09:42:24 +08:00
|
|
|
|
|
|
|
static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
|
|
|
|
static bool Initialized = false;
|
|
|
|
if (Initialized)
|
|
|
|
return StmtClassInfo[E];
|
|
|
|
|
|
|
|
// Intialize the table on the first use.
|
|
|
|
Initialized = true;
|
|
|
|
#define STMT(N, CLASS, PARENT) \
|
|
|
|
StmtClassInfo[N].Name = #CLASS; \
|
|
|
|
StmtClassInfo[N].Size = sizeof(CLASS);
|
2007-03-24 06:27:02 +08:00
|
|
|
#include "clang/AST/StmtNodes.def"
|
|
|
|
|
2007-08-25 09:42:24 +08:00
|
|
|
return StmtClassInfo[E];
|
|
|
|
}
|
|
|
|
|
2007-03-24 06:27:02 +08:00
|
|
|
const char *Stmt::getStmtClassName() const {
|
2007-08-25 09:42:24 +08:00
|
|
|
return getStmtInfoTableEntry(sClass).Name;
|
2007-03-24 06:27:02 +08:00
|
|
|
}
|
2007-05-24 05:48:04 +08:00
|
|
|
|
2008-05-20 08:43:19 +08:00
|
|
|
void Stmt::DestroyChildren(ASTContext& C) {
|
2008-05-20 06:02:12 +08:00
|
|
|
for (child_iterator I = child_begin(), E = child_end(); I !=E; ++I)
|
2008-05-20 08:43:19 +08:00
|
|
|
if (Stmt* Child = *I) Child->Destroy(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stmt::Destroy(ASTContext& C) {
|
|
|
|
DestroyChildren(C);
|
2008-05-20 12:10:52 +08:00
|
|
|
// FIXME: Eventually all Stmts should be allocated with the allocator
|
|
|
|
// in ASTContext, just like with Decls.
|
|
|
|
// this->~Stmt();
|
|
|
|
delete this;
|
2008-05-20 06:02:12 +08:00
|
|
|
}
|
|
|
|
|
2008-05-21 23:53:55 +08:00
|
|
|
void DeclStmt::Destroy(ASTContext& C) {
|
|
|
|
TheDecl->Destroy(C);
|
|
|
|
Stmt::Destroy(C);
|
|
|
|
}
|
|
|
|
|
2007-05-24 05:48:04 +08:00
|
|
|
void Stmt::PrintStats() {
|
2007-08-25 09:42:24 +08:00
|
|
|
// Ensure the table is primed.
|
|
|
|
getStmtInfoTableEntry(Stmt::NullStmtClass);
|
|
|
|
|
2007-05-24 05:48:04 +08:00
|
|
|
unsigned sum = 0;
|
|
|
|
fprintf(stderr, "*** Stmt/Expr Stats:\n");
|
2007-08-25 09:55:00 +08:00
|
|
|
for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
|
2007-08-25 09:42:24 +08:00
|
|
|
if (StmtClassInfo[i].Name == 0) continue;
|
|
|
|
sum += StmtClassInfo[i].Counter;
|
2007-05-24 05:48:04 +08:00
|
|
|
}
|
|
|
|
fprintf(stderr, " %d stmts/exprs total.\n", sum);
|
|
|
|
sum = 0;
|
2007-08-25 09:55:00 +08:00
|
|
|
for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
|
2007-08-25 09:42:24 +08:00
|
|
|
if (StmtClassInfo[i].Name == 0) continue;
|
2007-05-24 05:48:04 +08:00
|
|
|
fprintf(stderr, " %d %s, %d each (%d bytes)\n",
|
2007-08-25 09:42:24 +08:00
|
|
|
StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
|
|
|
|
StmtClassInfo[i].Size,
|
|
|
|
StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
|
|
|
|
sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
|
2007-05-24 05:48:04 +08:00
|
|
|
}
|
|
|
|
fprintf(stderr, "Total bytes = %d\n", sum);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stmt::addStmtClass(StmtClass s) {
|
2007-08-25 09:42:24 +08:00
|
|
|
++getStmtInfoTableEntry(s).Counter;
|
2007-05-24 05:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool StatSwitch = false;
|
|
|
|
|
|
|
|
bool Stmt::CollectingStats(bool enable) {
|
|
|
|
if (enable) StatSwitch = true;
|
2007-08-25 09:42:24 +08:00
|
|
|
return StatSwitch;
|
2007-05-24 05:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-28 14:56:27 +08:00
|
|
|
const char *LabelStmt::getName() const {
|
|
|
|
return getID()->getName();
|
|
|
|
}
|
|
|
|
|
2007-09-01 07:49:30 +08:00
|
|
|
// This is defined here to avoid polluting Stmt.h with importing Expr.h
|
|
|
|
SourceRange ReturnStmt::getSourceRange() const {
|
|
|
|
if (RetExpr)
|
|
|
|
return SourceRange(RetLoc, RetExpr->getLocEnd());
|
|
|
|
else
|
|
|
|
return SourceRange(RetLoc);
|
|
|
|
}
|
|
|
|
|
2007-10-02 00:34:52 +08:00
|
|
|
bool Stmt::hasImplicitControlFlow() const {
|
|
|
|
switch (sClass) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case CallExprClass:
|
|
|
|
case ConditionalOperatorClass:
|
|
|
|
case ChooseExprClass:
|
|
|
|
case StmtExprClass:
|
|
|
|
case DeclStmtClass:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Stmt::BinaryOperatorClass: {
|
|
|
|
const BinaryOperator* B = cast<BinaryOperator>(this);
|
|
|
|
if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-30 13:01:46 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Constructors
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-06 07:03:50 +08:00
|
|
|
AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
|
2008-01-30 13:01:46 +08:00
|
|
|
unsigned numoutputs, unsigned numinputs,
|
|
|
|
std::string *names, StringLiteral **constraints,
|
|
|
|
Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
|
|
|
|
StringLiteral **clobbers, SourceLocation rparenloc)
|
2007-11-22 09:36:19 +08:00
|
|
|
: Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
|
2008-02-06 07:03:50 +08:00
|
|
|
, IsSimple(issimple), IsVolatile(isvolatile)
|
|
|
|
, NumOutputs(numoutputs), NumInputs(numinputs) {
|
2007-11-22 09:36:19 +08:00
|
|
|
for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
|
|
|
|
Names.push_back(names[i]);
|
|
|
|
Exprs.push_back(exprs[i]);
|
|
|
|
Constraints.push_back(constraints[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != numclobbers; i++)
|
|
|
|
Clobbers.push_back(clobbers[i]);
|
|
|
|
}
|
|
|
|
|
2008-01-30 13:01:46 +08:00
|
|
|
ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
|
|
|
|
Stmt *Body, SourceLocation FCL,
|
|
|
|
SourceLocation RPL)
|
|
|
|
: Stmt(ObjCForCollectionStmtClass) {
|
|
|
|
SubExprs[ELEM] = Elem;
|
|
|
|
SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
|
|
|
|
SubExprs[BODY] = Body;
|
|
|
|
ForLoc = FCL;
|
|
|
|
RParenLoc = RPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
|
|
|
|
SourceLocation rparenloc,
|
|
|
|
Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
|
|
|
|
Stmt *atCatchList)
|
|
|
|
: Stmt(ObjCAtCatchStmtClass) {
|
|
|
|
SubExprs[SELECTOR] = catchVarStmtDecl;
|
|
|
|
SubExprs[BODY] = atCatchStmt;
|
|
|
|
if (!atCatchList)
|
2008-02-02 05:28:59 +08:00
|
|
|
SubExprs[NEXT_CATCH] = NULL;
|
2008-01-30 13:01:46 +08:00
|
|
|
else {
|
2008-02-02 05:28:59 +08:00
|
|
|
ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
|
|
|
|
|
|
|
|
while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
|
|
|
|
AtCatchList = NextCatch;
|
|
|
|
|
|
|
|
AtCatchList->SubExprs[NEXT_CATCH] = this;
|
2008-01-30 13:01:46 +08:00
|
|
|
}
|
|
|
|
AtCatchLoc = atCatchLoc;
|
|
|
|
RParenLoc = rparenloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-25 05:09:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Child Iterators for iterating over subexpressions/substatements
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// DeclStmt
|
2007-10-19 07:28:49 +08:00
|
|
|
Stmt::child_iterator DeclStmt::child_begin() { return getDecl(); }
|
|
|
|
Stmt::child_iterator DeclStmt::child_end() { return child_iterator(); }
|
2007-08-25 05:09:09 +08:00
|
|
|
|
|
|
|
// NullStmt
|
2007-10-19 07:28:49 +08:00
|
|
|
Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
|
|
|
|
Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
|
2007-08-25 05:09:09 +08:00
|
|
|
|
|
|
|
// CompoundStmt
|
|
|
|
Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
|
|
|
|
Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
|
|
|
|
|
2007-08-31 00:50:46 +08:00
|
|
|
// CaseStmt
|
|
|
|
Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
|
|
|
|
Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
|
|
|
|
|
|
|
|
// DefaultStmt
|
|
|
|
Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
|
|
|
|
Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
|
2007-08-25 05:09:09 +08:00
|
|
|
|
|
|
|
// LabelStmt
|
|
|
|
Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
|
2007-08-30 08:53:54 +08:00
|
|
|
Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
|
2007-08-25 05:09:09 +08:00
|
|
|
|
|
|
|
// IfStmt
|
|
|
|
Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
|
|
|
|
Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
|
|
|
|
|
|
|
|
// SwitchStmt
|
|
|
|
Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
|
|
|
|
Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
|
|
|
|
|
|
|
|
// WhileStmt
|
|
|
|
Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
|
|
|
|
Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
|
|
|
|
|
|
|
|
// DoStmt
|
|
|
|
Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
|
|
|
|
Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
|
|
|
|
|
|
|
|
// ForStmt
|
|
|
|
Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
|
|
|
|
Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
// ObjCForCollectionStmt
|
|
|
|
Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
|
2008-01-03 06:54:34 +08:00
|
|
|
return &SubExprs[0];
|
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
Stmt::child_iterator ObjCForCollectionStmt::child_end() {
|
2008-01-03 06:54:34 +08:00
|
|
|
return &SubExprs[0]+END_EXPR;
|
|
|
|
}
|
|
|
|
|
2007-08-25 05:09:09 +08:00
|
|
|
// GotoStmt
|
2007-10-19 07:28:49 +08:00
|
|
|
Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
|
|
|
|
Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
|
2007-08-25 05:09:09 +08:00
|
|
|
|
|
|
|
// IndirectGotoStmt
|
|
|
|
Stmt::child_iterator IndirectGotoStmt::child_begin() {
|
|
|
|
return reinterpret_cast<Stmt**>(&Target);
|
|
|
|
}
|
|
|
|
|
2007-10-18 08:24:38 +08:00
|
|
|
Stmt::child_iterator IndirectGotoStmt::child_end() { return ++child_begin(); }
|
2007-08-25 05:09:09 +08:00
|
|
|
|
|
|
|
// ContinueStmt
|
2007-10-19 07:28:49 +08:00
|
|
|
Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
|
|
|
|
Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
|
2007-08-25 05:09:09 +08:00
|
|
|
|
|
|
|
// BreakStmt
|
2007-10-19 07:28:49 +08:00
|
|
|
Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
|
|
|
|
Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
|
2007-08-25 05:09:09 +08:00
|
|
|
|
|
|
|
// ReturnStmt
|
2007-08-28 04:58:16 +08:00
|
|
|
Stmt::child_iterator ReturnStmt::child_begin() {
|
|
|
|
if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr);
|
2007-10-19 07:28:49 +08:00
|
|
|
else return child_iterator();
|
2007-08-25 05:09:09 +08:00
|
|
|
}
|
|
|
|
|
2007-08-28 04:58:16 +08:00
|
|
|
Stmt::child_iterator ReturnStmt::child_end() {
|
|
|
|
if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr)+1;
|
2007-10-19 07:28:49 +08:00
|
|
|
else return child_iterator();
|
2007-08-28 04:58:16 +08:00
|
|
|
}
|
2007-08-25 05:09:09 +08:00
|
|
|
|
2007-10-29 12:04:16 +08:00
|
|
|
// AsmStmt
|
|
|
|
Stmt::child_iterator AsmStmt::child_begin() { return child_iterator(); }
|
|
|
|
Stmt::child_iterator AsmStmt::child_end() { return child_iterator(); }
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
// ObjCAtCatchStmt
|
|
|
|
Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
|
|
|
|
Stmt::child_iterator ObjCAtCatchStmt::child_end() {
|
2007-11-02 07:59:59 +08:00
|
|
|
return &SubExprs[0]+END_EXPR;
|
|
|
|
}
|
2007-11-02 05:12:44 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
// ObjCAtFinallyStmt
|
|
|
|
Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
|
|
|
|
Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
|
2007-11-02 05:12:44 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
// ObjCAtTryStmt
|
|
|
|
Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
|
|
|
|
Stmt::child_iterator ObjCAtTryStmt::child_end() {
|
2007-11-08 01:43:16 +08:00
|
|
|
return &SubStmts[0]+END_EXPR;
|
2007-11-02 07:59:59 +08:00
|
|
|
}
|
2007-11-02 05:12:44 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
// ObjCAtThrowStmt
|
|
|
|
Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
|
2007-11-07 10:00:49 +08:00
|
|
|
return &Throw;
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
Stmt::child_iterator ObjCAtThrowStmt::child_end() {
|
2007-11-07 10:00:49 +08:00
|
|
|
return &Throw+1;
|
|
|
|
}
|
2008-01-30 03:14:59 +08:00
|
|
|
|
|
|
|
// ObjCAtSynchronizedStmt
|
|
|
|
Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
|
2008-01-30 06:59:37 +08:00
|
|
|
return &SubStmts[0];
|
2008-01-30 03:14:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
|
2008-01-30 06:59:37 +08:00
|
|
|
return &SubStmts[0]+END_EXPR;
|
2008-01-30 03:14:59 +08:00
|
|
|
}
|
|
|
|
|