1*67e74705SXin Li //===--- StmtCXX.cpp - Classes for representing C++ statements ------------===//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This file implements the subclesses of Stmt class declared in StmtCXX.h
11*67e74705SXin Li //
12*67e74705SXin Li //===----------------------------------------------------------------------===//
13*67e74705SXin Li
14*67e74705SXin Li #include "clang/AST/StmtCXX.h"
15*67e74705SXin Li
16*67e74705SXin Li #include "clang/AST/ASTContext.h"
17*67e74705SXin Li
18*67e74705SXin Li using namespace clang;
19*67e74705SXin Li
getCaughtType() const20*67e74705SXin Li QualType CXXCatchStmt::getCaughtType() const {
21*67e74705SXin Li if (ExceptionDecl)
22*67e74705SXin Li return ExceptionDecl->getType();
23*67e74705SXin Li return QualType();
24*67e74705SXin Li }
25*67e74705SXin Li
Create(const ASTContext & C,SourceLocation tryLoc,Stmt * tryBlock,ArrayRef<Stmt * > handlers)26*67e74705SXin Li CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
27*67e74705SXin Li Stmt *tryBlock, ArrayRef<Stmt *> handlers) {
28*67e74705SXin Li std::size_t Size = sizeof(CXXTryStmt);
29*67e74705SXin Li Size += ((handlers.size() + 1) * sizeof(Stmt *));
30*67e74705SXin Li
31*67e74705SXin Li void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
32*67e74705SXin Li return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
33*67e74705SXin Li }
34*67e74705SXin Li
Create(const ASTContext & C,EmptyShell Empty,unsigned numHandlers)35*67e74705SXin Li CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
36*67e74705SXin Li unsigned numHandlers) {
37*67e74705SXin Li std::size_t Size = sizeof(CXXTryStmt);
38*67e74705SXin Li Size += ((numHandlers + 1) * sizeof(Stmt *));
39*67e74705SXin Li
40*67e74705SXin Li void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
41*67e74705SXin Li return new (Mem) CXXTryStmt(Empty, numHandlers);
42*67e74705SXin Li }
43*67e74705SXin Li
CXXTryStmt(SourceLocation tryLoc,Stmt * tryBlock,ArrayRef<Stmt * > handlers)44*67e74705SXin Li CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
45*67e74705SXin Li ArrayRef<Stmt *> handlers)
46*67e74705SXin Li : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
47*67e74705SXin Li Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
48*67e74705SXin Li Stmts[0] = tryBlock;
49*67e74705SXin Li std::copy(handlers.begin(), handlers.end(), Stmts + 1);
50*67e74705SXin Li }
51*67e74705SXin Li
CXXForRangeStmt(DeclStmt * Range,DeclStmt * BeginStmt,DeclStmt * EndStmt,Expr * Cond,Expr * Inc,DeclStmt * LoopVar,Stmt * Body,SourceLocation FL,SourceLocation CAL,SourceLocation CL,SourceLocation RPL)52*67e74705SXin Li CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range,
53*67e74705SXin Li DeclStmt *BeginStmt, DeclStmt *EndStmt,
54*67e74705SXin Li Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
55*67e74705SXin Li Stmt *Body, SourceLocation FL,
56*67e74705SXin Li SourceLocation CAL, SourceLocation CL,
57*67e74705SXin Li SourceLocation RPL)
58*67e74705SXin Li : Stmt(CXXForRangeStmtClass), ForLoc(FL), CoawaitLoc(CAL), ColonLoc(CL),
59*67e74705SXin Li RParenLoc(RPL) {
60*67e74705SXin Li SubExprs[RANGE] = Range;
61*67e74705SXin Li SubExprs[BEGINSTMT] = BeginStmt;
62*67e74705SXin Li SubExprs[ENDSTMT] = EndStmt;
63*67e74705SXin Li SubExprs[COND] = Cond;
64*67e74705SXin Li SubExprs[INC] = Inc;
65*67e74705SXin Li SubExprs[LOOPVAR] = LoopVar;
66*67e74705SXin Li SubExprs[BODY] = Body;
67*67e74705SXin Li }
68*67e74705SXin Li
getRangeInit()69*67e74705SXin Li Expr *CXXForRangeStmt::getRangeInit() {
70*67e74705SXin Li DeclStmt *RangeStmt = getRangeStmt();
71*67e74705SXin Li VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
72*67e74705SXin Li assert(RangeDecl && "for-range should have a single var decl");
73*67e74705SXin Li return RangeDecl->getInit();
74*67e74705SXin Li }
75*67e74705SXin Li
getRangeInit() const76*67e74705SXin Li const Expr *CXXForRangeStmt::getRangeInit() const {
77*67e74705SXin Li return const_cast<CXXForRangeStmt *>(this)->getRangeInit();
78*67e74705SXin Li }
79*67e74705SXin Li
getLoopVariable()80*67e74705SXin Li VarDecl *CXXForRangeStmt::getLoopVariable() {
81*67e74705SXin Li Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
82*67e74705SXin Li assert(LV && "No loop variable in CXXForRangeStmt");
83*67e74705SXin Li return cast<VarDecl>(LV);
84*67e74705SXin Li }
85*67e74705SXin Li
getLoopVariable() const86*67e74705SXin Li const VarDecl *CXXForRangeStmt::getLoopVariable() const {
87*67e74705SXin Li return const_cast<CXXForRangeStmt *>(this)->getLoopVariable();
88*67e74705SXin Li }
89