1*7330f729Sjoerg //===--- StmtObjC.cpp - Classes for representing ObjC statements ---------===//
2*7330f729Sjoerg //
3*7330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*7330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7330f729Sjoerg //
7*7330f729Sjoerg //===----------------------------------------------------------------------===//
8*7330f729Sjoerg //
9*7330f729Sjoerg // This file implements the subclesses of Stmt class declared in StmtObjC.h
10*7330f729Sjoerg //
11*7330f729Sjoerg //===----------------------------------------------------------------------===//
12*7330f729Sjoerg
13*7330f729Sjoerg #include "clang/AST/StmtObjC.h"
14*7330f729Sjoerg
15*7330f729Sjoerg #include "clang/AST/Expr.h"
16*7330f729Sjoerg #include "clang/AST/ASTContext.h"
17*7330f729Sjoerg
18*7330f729Sjoerg using namespace clang;
19*7330f729Sjoerg
ObjCForCollectionStmt(Stmt * Elem,Expr * Collect,Stmt * Body,SourceLocation FCL,SourceLocation RPL)20*7330f729Sjoerg ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
21*7330f729Sjoerg Stmt *Body, SourceLocation FCL,
22*7330f729Sjoerg SourceLocation RPL)
23*7330f729Sjoerg : Stmt(ObjCForCollectionStmtClass) {
24*7330f729Sjoerg SubExprs[ELEM] = Elem;
25*7330f729Sjoerg SubExprs[COLLECTION] = Collect;
26*7330f729Sjoerg SubExprs[BODY] = Body;
27*7330f729Sjoerg ForLoc = FCL;
28*7330f729Sjoerg RParenLoc = RPL;
29*7330f729Sjoerg }
30*7330f729Sjoerg
ObjCAtTryStmt(SourceLocation atTryLoc,Stmt * atTryStmt,Stmt ** CatchStmts,unsigned NumCatchStmts,Stmt * atFinallyStmt)31*7330f729Sjoerg ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
32*7330f729Sjoerg Stmt **CatchStmts, unsigned NumCatchStmts,
33*7330f729Sjoerg Stmt *atFinallyStmt)
34*7330f729Sjoerg : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
35*7330f729Sjoerg NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != nullptr) {
36*7330f729Sjoerg Stmt **Stmts = getStmts();
37*7330f729Sjoerg Stmts[0] = atTryStmt;
38*7330f729Sjoerg for (unsigned I = 0; I != NumCatchStmts; ++I)
39*7330f729Sjoerg Stmts[I + 1] = CatchStmts[I];
40*7330f729Sjoerg
41*7330f729Sjoerg if (HasFinally)
42*7330f729Sjoerg Stmts[NumCatchStmts + 1] = atFinallyStmt;
43*7330f729Sjoerg }
44*7330f729Sjoerg
Create(const ASTContext & Context,SourceLocation atTryLoc,Stmt * atTryStmt,Stmt ** CatchStmts,unsigned NumCatchStmts,Stmt * atFinallyStmt)45*7330f729Sjoerg ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
46*7330f729Sjoerg SourceLocation atTryLoc, Stmt *atTryStmt,
47*7330f729Sjoerg Stmt **CatchStmts, unsigned NumCatchStmts,
48*7330f729Sjoerg Stmt *atFinallyStmt) {
49*7330f729Sjoerg unsigned Size =
50*7330f729Sjoerg sizeof(ObjCAtTryStmt) +
51*7330f729Sjoerg (1 + NumCatchStmts + (atFinallyStmt != nullptr)) * sizeof(Stmt *);
52*7330f729Sjoerg void *Mem = Context.Allocate(Size, alignof(ObjCAtTryStmt));
53*7330f729Sjoerg return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
54*7330f729Sjoerg atFinallyStmt);
55*7330f729Sjoerg }
56*7330f729Sjoerg
CreateEmpty(const ASTContext & Context,unsigned NumCatchStmts,bool HasFinally)57*7330f729Sjoerg ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
58*7330f729Sjoerg unsigned NumCatchStmts,
59*7330f729Sjoerg bool HasFinally) {
60*7330f729Sjoerg unsigned Size =
61*7330f729Sjoerg sizeof(ObjCAtTryStmt) + (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
62*7330f729Sjoerg void *Mem = Context.Allocate(Size, alignof(ObjCAtTryStmt));
63*7330f729Sjoerg return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
64*7330f729Sjoerg }
65*7330f729Sjoerg
getEndLoc() const66*7330f729Sjoerg SourceLocation ObjCAtTryStmt::getEndLoc() const {
67*7330f729Sjoerg if (HasFinally)
68*7330f729Sjoerg return getFinallyStmt()->getEndLoc();
69*7330f729Sjoerg if (NumCatchStmts)
70*7330f729Sjoerg return getCatchStmt(NumCatchStmts - 1)->getEndLoc();
71*7330f729Sjoerg return getTryBody()->getEndLoc();
72*7330f729Sjoerg }
73