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