1 //===--- StmtObjC.h - Classes for representing ObjC statements --*- C++ -*-===//
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 /// \file
11 /// Defines the Objective-C statement AST node classes.
12 
13 #ifndef LLVM_CLANG_AST_STMTOBJC_H
14 #define LLVM_CLANG_AST_STMTOBJC_H
15 
16 #include "clang/AST/Stmt.h"
17 #include "llvm/Support/Compiler.h"
18 
19 namespace clang {
20 
21 /// Represents Objective-C's collection statement.
22 ///
23 /// This is represented as 'for (element 'in' collection-expression)' stmt.
24 class ObjCForCollectionStmt : public Stmt {
25   enum { ELEM, COLLECTION, BODY, END_EXPR };
26   Stmt* SubExprs[END_EXPR]; // SubExprs[ELEM] is an expression or declstmt.
27   SourceLocation ForLoc;
28   SourceLocation RParenLoc;
29 public:
30   ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, Stmt *Body,
31                         SourceLocation FCL, SourceLocation RPL);
ObjCForCollectionStmt(EmptyShell Empty)32   explicit ObjCForCollectionStmt(EmptyShell Empty) :
33     Stmt(ObjCForCollectionStmtClass, Empty) { }
34 
getElement()35   Stmt *getElement() { return SubExprs[ELEM]; }
getCollection()36   Expr *getCollection() {
37     return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
38   }
getBody()39   Stmt *getBody() { return SubExprs[BODY]; }
40 
getElement()41   const Stmt *getElement() const { return SubExprs[ELEM]; }
getCollection()42   const Expr *getCollection() const {
43     return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
44   }
getBody()45   const Stmt *getBody() const { return SubExprs[BODY]; }
46 
setElement(Stmt * S)47   void setElement(Stmt *S) { SubExprs[ELEM] = S; }
setCollection(Expr * E)48   void setCollection(Expr *E) {
49     SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(E);
50   }
setBody(Stmt * S)51   void setBody(Stmt *S) { SubExprs[BODY] = S; }
52 
getForLoc()53   SourceLocation getForLoc() const { return ForLoc; }
setForLoc(SourceLocation Loc)54   void setForLoc(SourceLocation Loc) { ForLoc = Loc; }
getRParenLoc()55   SourceLocation getRParenLoc() const { return RParenLoc; }
setRParenLoc(SourceLocation Loc)56   void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; }
57 
getBeginLoc()58   SourceLocation getBeginLoc() const LLVM_READONLY { return ForLoc; }
getEndLoc()59   SourceLocation getEndLoc() const LLVM_READONLY {
60     return SubExprs[BODY]->getEndLoc();
61   }
62 
classof(const Stmt * T)63   static bool classof(const Stmt *T) {
64     return T->getStmtClass() == ObjCForCollectionStmtClass;
65   }
66 
67   // Iterators
children()68   child_range children() {
69     return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
70   }
71 };
72 
73 /// Represents Objective-C's \@catch statement.
74 class ObjCAtCatchStmt : public Stmt {
75 private:
76   VarDecl *ExceptionDecl;
77   Stmt *Body;
78   SourceLocation AtCatchLoc, RParenLoc;
79 
80 public:
ObjCAtCatchStmt(SourceLocation atCatchLoc,SourceLocation rparenloc,VarDecl * catchVarDecl,Stmt * atCatchStmt)81   ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc,
82                   VarDecl *catchVarDecl,
83                   Stmt *atCatchStmt)
84     : Stmt(ObjCAtCatchStmtClass), ExceptionDecl(catchVarDecl),
85     Body(atCatchStmt), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) { }
86 
ObjCAtCatchStmt(EmptyShell Empty)87   explicit ObjCAtCatchStmt(EmptyShell Empty) :
88     Stmt(ObjCAtCatchStmtClass, Empty) { }
89 
getCatchBody()90   const Stmt *getCatchBody() const { return Body; }
getCatchBody()91   Stmt *getCatchBody() { return Body; }
setCatchBody(Stmt * S)92   void setCatchBody(Stmt *S) { Body = S; }
93 
getCatchParamDecl()94   const VarDecl *getCatchParamDecl() const {
95     return ExceptionDecl;
96   }
getCatchParamDecl()97   VarDecl *getCatchParamDecl() {
98     return ExceptionDecl;
99   }
setCatchParamDecl(VarDecl * D)100   void setCatchParamDecl(VarDecl *D) { ExceptionDecl = D; }
101 
getAtCatchLoc()102   SourceLocation getAtCatchLoc() const { return AtCatchLoc; }
setAtCatchLoc(SourceLocation Loc)103   void setAtCatchLoc(SourceLocation Loc) { AtCatchLoc = Loc; }
getRParenLoc()104   SourceLocation getRParenLoc() const { return RParenLoc; }
setRParenLoc(SourceLocation Loc)105   void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; }
106 
getBeginLoc()107   SourceLocation getBeginLoc() const LLVM_READONLY { return AtCatchLoc; }
getEndLoc()108   SourceLocation getEndLoc() const LLVM_READONLY { return Body->getEndLoc(); }
109 
hasEllipsis()110   bool hasEllipsis() const { return getCatchParamDecl() == nullptr; }
111 
classof(const Stmt * T)112   static bool classof(const Stmt *T) {
113     return T->getStmtClass() == ObjCAtCatchStmtClass;
114   }
115 
children()116   child_range children() { return child_range(&Body, &Body + 1); }
117 };
118 
119 /// Represents Objective-C's \@finally statement
120 class ObjCAtFinallyStmt : public Stmt {
121   SourceLocation AtFinallyLoc;
122   Stmt *AtFinallyStmt;
123 
124 public:
ObjCAtFinallyStmt(SourceLocation atFinallyLoc,Stmt * atFinallyStmt)125   ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt)
126       : Stmt(ObjCAtFinallyStmtClass), AtFinallyLoc(atFinallyLoc),
127         AtFinallyStmt(atFinallyStmt) {}
128 
ObjCAtFinallyStmt(EmptyShell Empty)129   explicit ObjCAtFinallyStmt(EmptyShell Empty) :
130     Stmt(ObjCAtFinallyStmtClass, Empty) { }
131 
getFinallyBody()132   const Stmt *getFinallyBody() const { return AtFinallyStmt; }
getFinallyBody()133   Stmt *getFinallyBody() { return AtFinallyStmt; }
setFinallyBody(Stmt * S)134   void setFinallyBody(Stmt *S) { AtFinallyStmt = S; }
135 
getBeginLoc()136   SourceLocation getBeginLoc() const LLVM_READONLY { return AtFinallyLoc; }
getEndLoc()137   SourceLocation getEndLoc() const LLVM_READONLY {
138     return AtFinallyStmt->getEndLoc();
139   }
140 
getAtFinallyLoc()141   SourceLocation getAtFinallyLoc() const { return AtFinallyLoc; }
setAtFinallyLoc(SourceLocation Loc)142   void setAtFinallyLoc(SourceLocation Loc) { AtFinallyLoc = Loc; }
143 
classof(const Stmt * T)144   static bool classof(const Stmt *T) {
145     return T->getStmtClass() == ObjCAtFinallyStmtClass;
146   }
147 
children()148   child_range children() {
149     return child_range(&AtFinallyStmt, &AtFinallyStmt+1);
150   }
151 };
152 
153 /// Represents Objective-C's \@try ... \@catch ... \@finally statement.
154 class ObjCAtTryStmt : public Stmt {
155 private:
156   // The location of the @ in the \@try.
157   SourceLocation AtTryLoc;
158 
159   // The number of catch blocks in this statement.
160   unsigned NumCatchStmts : 16;
161 
162   // Whether this statement has a \@finally statement.
163   bool HasFinally : 1;
164 
165   /// Retrieve the statements that are stored after this \@try statement.
166   ///
167   /// The order of the statements in memory follows the order in the source,
168   /// with the \@try body first, followed by the \@catch statements (if any)
169   /// and, finally, the \@finally (if it exists).
getStmts()170   Stmt **getStmts() { return reinterpret_cast<Stmt **> (this + 1); }
getStmts()171   const Stmt* const *getStmts() const {
172     return reinterpret_cast<const Stmt * const*> (this + 1);
173   }
174 
175   ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
176                 Stmt **CatchStmts, unsigned NumCatchStmts,
177                 Stmt *atFinallyStmt);
178 
ObjCAtTryStmt(EmptyShell Empty,unsigned NumCatchStmts,bool HasFinally)179   explicit ObjCAtTryStmt(EmptyShell Empty, unsigned NumCatchStmts,
180                          bool HasFinally)
181     : Stmt(ObjCAtTryStmtClass, Empty), NumCatchStmts(NumCatchStmts),
182       HasFinally(HasFinally) { }
183 
184 public:
185   static ObjCAtTryStmt *Create(const ASTContext &Context,
186                                SourceLocation atTryLoc, Stmt *atTryStmt,
187                                Stmt **CatchStmts, unsigned NumCatchStmts,
188                                Stmt *atFinallyStmt);
189   static ObjCAtTryStmt *CreateEmpty(const ASTContext &Context,
190                                     unsigned NumCatchStmts, bool HasFinally);
191 
192   /// Retrieve the location of the @ in the \@try.
getAtTryLoc()193   SourceLocation getAtTryLoc() const { return AtTryLoc; }
setAtTryLoc(SourceLocation Loc)194   void setAtTryLoc(SourceLocation Loc) { AtTryLoc = Loc; }
195 
196   /// Retrieve the \@try body.
getTryBody()197   const Stmt *getTryBody() const { return getStmts()[0]; }
getTryBody()198   Stmt *getTryBody() { return getStmts()[0]; }
setTryBody(Stmt * S)199   void setTryBody(Stmt *S) { getStmts()[0] = S; }
200 
201   /// Retrieve the number of \@catch statements in this try-catch-finally
202   /// block.
getNumCatchStmts()203   unsigned getNumCatchStmts() const { return NumCatchStmts; }
204 
205   /// Retrieve a \@catch statement.
getCatchStmt(unsigned I)206   const ObjCAtCatchStmt *getCatchStmt(unsigned I) const {
207     assert(I < NumCatchStmts && "Out-of-bounds @catch index");
208     return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]);
209   }
210 
211   /// Retrieve a \@catch statement.
getCatchStmt(unsigned I)212   ObjCAtCatchStmt *getCatchStmt(unsigned I) {
213     assert(I < NumCatchStmts && "Out-of-bounds @catch index");
214     return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]);
215   }
216 
217   /// Set a particular catch statement.
setCatchStmt(unsigned I,ObjCAtCatchStmt * S)218   void setCatchStmt(unsigned I, ObjCAtCatchStmt *S) {
219     assert(I < NumCatchStmts && "Out-of-bounds @catch index");
220     getStmts()[I + 1] = S;
221   }
222 
223   /// Retrieve the \@finally statement, if any.
getFinallyStmt()224   const ObjCAtFinallyStmt *getFinallyStmt() const {
225     if (!HasFinally)
226       return nullptr;
227 
228     return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
229   }
getFinallyStmt()230   ObjCAtFinallyStmt *getFinallyStmt() {
231     if (!HasFinally)
232       return nullptr;
233 
234     return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
235   }
setFinallyStmt(Stmt * S)236   void setFinallyStmt(Stmt *S) {
237     assert(HasFinally && "@try does not have a @finally slot!");
238     getStmts()[1 + NumCatchStmts] = S;
239   }
240 
getBeginLoc()241   SourceLocation getBeginLoc() const LLVM_READONLY { return AtTryLoc; }
242   SourceLocation getEndLoc() const LLVM_READONLY;
243 
classof(const Stmt * T)244   static bool classof(const Stmt *T) {
245     return T->getStmtClass() == ObjCAtTryStmtClass;
246   }
247 
children()248   child_range children() {
249     return child_range(getStmts(),
250                        getStmts() + 1 + NumCatchStmts + HasFinally);
251   }
252 };
253 
254 /// Represents Objective-C's \@synchronized statement.
255 ///
256 /// Example:
257 /// \code
258 ///   @synchronized (sem) {
259 ///     do-something;
260 ///   }
261 /// \endcode
262 class ObjCAtSynchronizedStmt : public Stmt {
263 private:
264   SourceLocation AtSynchronizedLoc;
265   enum { SYNC_EXPR, SYNC_BODY, END_EXPR };
266   Stmt* SubStmts[END_EXPR];
267 
268 public:
ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc,Stmt * synchExpr,Stmt * synchBody)269   ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr,
270                          Stmt *synchBody)
271   : Stmt(ObjCAtSynchronizedStmtClass) {
272     SubStmts[SYNC_EXPR] = synchExpr;
273     SubStmts[SYNC_BODY] = synchBody;
274     AtSynchronizedLoc = atSynchronizedLoc;
275   }
ObjCAtSynchronizedStmt(EmptyShell Empty)276   explicit ObjCAtSynchronizedStmt(EmptyShell Empty) :
277     Stmt(ObjCAtSynchronizedStmtClass, Empty) { }
278 
getAtSynchronizedLoc()279   SourceLocation getAtSynchronizedLoc() const { return AtSynchronizedLoc; }
setAtSynchronizedLoc(SourceLocation Loc)280   void setAtSynchronizedLoc(SourceLocation Loc) { AtSynchronizedLoc = Loc; }
281 
getSynchBody()282   const CompoundStmt *getSynchBody() const {
283     return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
284   }
getSynchBody()285   CompoundStmt *getSynchBody() {
286     return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
287   }
setSynchBody(Stmt * S)288   void setSynchBody(Stmt *S) { SubStmts[SYNC_BODY] = S; }
289 
getSynchExpr()290   const Expr *getSynchExpr() const {
291     return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
292   }
getSynchExpr()293   Expr *getSynchExpr() {
294     return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
295   }
setSynchExpr(Stmt * S)296   void setSynchExpr(Stmt *S) { SubStmts[SYNC_EXPR] = S; }
297 
getBeginLoc()298   SourceLocation getBeginLoc() const LLVM_READONLY { return AtSynchronizedLoc; }
getEndLoc()299   SourceLocation getEndLoc() const LLVM_READONLY {
300     return getSynchBody()->getEndLoc();
301   }
302 
classof(const Stmt * T)303   static bool classof(const Stmt *T) {
304     return T->getStmtClass() == ObjCAtSynchronizedStmtClass;
305   }
306 
children()307   child_range children() {
308     return child_range(&SubStmts[0], &SubStmts[0]+END_EXPR);
309   }
310 };
311 
312 /// Represents Objective-C's \@throw statement.
313 class ObjCAtThrowStmt : public Stmt {
314   SourceLocation AtThrowLoc;
315   Stmt *Throw;
316 
317 public:
ObjCAtThrowStmt(SourceLocation atThrowLoc,Stmt * throwExpr)318   ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr)
319   : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) {
320     AtThrowLoc = atThrowLoc;
321   }
ObjCAtThrowStmt(EmptyShell Empty)322   explicit ObjCAtThrowStmt(EmptyShell Empty) :
323     Stmt(ObjCAtThrowStmtClass, Empty) { }
324 
getThrowExpr()325   const Expr *getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); }
getThrowExpr()326   Expr *getThrowExpr() { return reinterpret_cast<Expr*>(Throw); }
setThrowExpr(Stmt * S)327   void setThrowExpr(Stmt *S) { Throw = S; }
328 
getThrowLoc()329   SourceLocation getThrowLoc() const LLVM_READONLY { return AtThrowLoc; }
setThrowLoc(SourceLocation Loc)330   void setThrowLoc(SourceLocation Loc) { AtThrowLoc = Loc; }
331 
getBeginLoc()332   SourceLocation getBeginLoc() const LLVM_READONLY { return AtThrowLoc; }
getEndLoc()333   SourceLocation getEndLoc() const LLVM_READONLY {
334     return Throw ? Throw->getEndLoc() : AtThrowLoc;
335   }
336 
classof(const Stmt * T)337   static bool classof(const Stmt *T) {
338     return T->getStmtClass() == ObjCAtThrowStmtClass;
339   }
340 
children()341   child_range children() { return child_range(&Throw, &Throw+1); }
342 };
343 
344 /// Represents Objective-C's \@autoreleasepool Statement
345 class ObjCAutoreleasePoolStmt : public Stmt {
346   SourceLocation AtLoc;
347   Stmt *SubStmt;
348 
349 public:
ObjCAutoreleasePoolStmt(SourceLocation atLoc,Stmt * subStmt)350   ObjCAutoreleasePoolStmt(SourceLocation atLoc, Stmt *subStmt)
351       : Stmt(ObjCAutoreleasePoolStmtClass), AtLoc(atLoc), SubStmt(subStmt) {}
352 
ObjCAutoreleasePoolStmt(EmptyShell Empty)353   explicit ObjCAutoreleasePoolStmt(EmptyShell Empty) :
354     Stmt(ObjCAutoreleasePoolStmtClass, Empty) { }
355 
getSubStmt()356   const Stmt *getSubStmt() const { return SubStmt; }
getSubStmt()357   Stmt *getSubStmt() { return SubStmt; }
setSubStmt(Stmt * S)358   void setSubStmt(Stmt *S) { SubStmt = S; }
359 
getBeginLoc()360   SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; }
getEndLoc()361   SourceLocation getEndLoc() const LLVM_READONLY {
362     return SubStmt->getEndLoc();
363   }
364 
getAtLoc()365   SourceLocation getAtLoc() const { return AtLoc; }
setAtLoc(SourceLocation Loc)366   void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
367 
classof(const Stmt * T)368   static bool classof(const Stmt *T) {
369     return T->getStmtClass() == ObjCAutoreleasePoolStmtClass;
370   }
371 
children()372   child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
373 };
374 
375 }  // end namespace clang
376 
377 #endif
378