• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- StmtObjC.cpp - Classes for representing ObjC 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 StmtObjC.h
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/AST/StmtObjC.h"
15 
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/ASTContext.h"
18 
19 using namespace clang;
20 
ObjCForCollectionStmt(Stmt * Elem,Expr * Collect,Stmt * Body,SourceLocation FCL,SourceLocation RPL)21 ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
22                                              Stmt *Body, SourceLocation FCL,
23                                              SourceLocation RPL)
24     : Stmt(ObjCForCollectionStmtClass) {
25   SubExprs[ELEM] = Elem;
26   SubExprs[COLLECTION] = Collect;
27   SubExprs[BODY] = Body;
28   ForLoc = FCL;
29   RParenLoc = RPL;
30 }
31 
ObjCAtTryStmt(SourceLocation atTryLoc,Stmt * atTryStmt,Stmt ** CatchStmts,unsigned NumCatchStmts,Stmt * atFinallyStmt)32 ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
33                              Stmt **CatchStmts, unsigned NumCatchStmts,
34                              Stmt *atFinallyStmt)
35     : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
36       NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != nullptr) {
37   Stmt **Stmts = getStmts();
38   Stmts[0] = atTryStmt;
39   for (unsigned I = 0; I != NumCatchStmts; ++I)
40     Stmts[I + 1] = CatchStmts[I];
41 
42   if (HasFinally)
43     Stmts[NumCatchStmts + 1] = atFinallyStmt;
44 }
45 
Create(const ASTContext & Context,SourceLocation atTryLoc,Stmt * atTryStmt,Stmt ** CatchStmts,unsigned NumCatchStmts,Stmt * atFinallyStmt)46 ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
47                                      SourceLocation atTryLoc, Stmt *atTryStmt,
48                                      Stmt **CatchStmts, unsigned NumCatchStmts,
49                                      Stmt *atFinallyStmt) {
50   unsigned Size =
51       sizeof(ObjCAtTryStmt) +
52       (1 + NumCatchStmts + (atFinallyStmt != nullptr)) * sizeof(Stmt *);
53   void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
54   return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
55                                  atFinallyStmt);
56 }
57 
CreateEmpty(const ASTContext & Context,unsigned NumCatchStmts,bool HasFinally)58 ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
59                                           unsigned NumCatchStmts,
60                                           bool HasFinally) {
61   unsigned Size =
62       sizeof(ObjCAtTryStmt) + (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
63   void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
64   return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
65 }
66 
getLocEnd() const67 SourceLocation ObjCAtTryStmt::getLocEnd() const {
68   if (HasFinally)
69     return getFinallyStmt()->getLocEnd();
70   if (NumCatchStmts)
71     return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
72   return getTryBody()->getLocEnd();
73 }
74