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