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