• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /// \brief 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 /// \brief 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 
getLocStart()58   SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
getLocEnd()59   SourceLocation getLocEnd() const LLVM_READONLY {
60     return SubExprs[BODY]->getLocEnd();
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 /// \brief 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 
getLocStart()107   SourceLocation getLocStart() const LLVM_READONLY { return AtCatchLoc; }
getLocEnd()108   SourceLocation getLocEnd() const LLVM_READONLY { return Body->getLocEnd(); }
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 /// \brief Represents Objective-C's \@finally statement
120 class ObjCAtFinallyStmt : public Stmt {
121   Stmt *AtFinallyStmt;
122   SourceLocation AtFinallyLoc;
123 public:
ObjCAtFinallyStmt(SourceLocation atFinallyLoc,Stmt * atFinallyStmt)124   ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt)
125   : Stmt(ObjCAtFinallyStmtClass),
126     AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {}
127 
ObjCAtFinallyStmt(EmptyShell Empty)128   explicit ObjCAtFinallyStmt(EmptyShell Empty) :
129     Stmt(ObjCAtFinallyStmtClass, Empty) { }
130 
getFinallyBody()131   const Stmt *getFinallyBody() const { return AtFinallyStmt; }
getFinallyBody()132   Stmt *getFinallyBody() { return AtFinallyStmt; }
setFinallyBody(Stmt * S)133   void setFinallyBody(Stmt *S) { AtFinallyStmt = S; }
134 
getLocStart()135   SourceLocation getLocStart() const LLVM_READONLY { return AtFinallyLoc; }
getLocEnd()136   SourceLocation getLocEnd() const LLVM_READONLY {
137     return AtFinallyStmt->getLocEnd();
138   }
139 
getAtFinallyLoc()140   SourceLocation getAtFinallyLoc() const { return AtFinallyLoc; }
setAtFinallyLoc(SourceLocation Loc)141   void setAtFinallyLoc(SourceLocation Loc) { AtFinallyLoc = Loc; }
142 
classof(const Stmt * T)143   static bool classof(const Stmt *T) {
144     return T->getStmtClass() == ObjCAtFinallyStmtClass;
145   }
146 
children()147   child_range children() {
148     return child_range(&AtFinallyStmt, &AtFinallyStmt+1);
149   }
150 };
151 
152 /// \brief Represents Objective-C's \@try ... \@catch ... \@finally statement.
153 class ObjCAtTryStmt : public Stmt {
154 private:
155   // The location of the @ in the \@try.
156   SourceLocation AtTryLoc;
157 
158   // The number of catch blocks in this statement.
159   unsigned NumCatchStmts : 16;
160 
161   // Whether this statement has a \@finally statement.
162   bool HasFinally : 1;
163 
164   /// \brief Retrieve the statements that are stored after this \@try statement.
165   ///
166   /// The order of the statements in memory follows the order in the source,
167   /// with the \@try body first, followed by the \@catch statements (if any)
168   /// and, finally, the \@finally (if it exists).
getStmts()169   Stmt **getStmts() { return reinterpret_cast<Stmt **> (this + 1); }
getStmts()170   const Stmt* const *getStmts() const {
171     return reinterpret_cast<const Stmt * const*> (this + 1);
172   }
173 
174   ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
175                 Stmt **CatchStmts, unsigned NumCatchStmts,
176                 Stmt *atFinallyStmt);
177 
ObjCAtTryStmt(EmptyShell Empty,unsigned NumCatchStmts,bool HasFinally)178   explicit ObjCAtTryStmt(EmptyShell Empty, unsigned NumCatchStmts,
179                          bool HasFinally)
180     : Stmt(ObjCAtTryStmtClass, Empty), NumCatchStmts(NumCatchStmts),
181       HasFinally(HasFinally) { }
182 
183 public:
184   static ObjCAtTryStmt *Create(const ASTContext &Context,
185                                SourceLocation atTryLoc, Stmt *atTryStmt,
186                                Stmt **CatchStmts, unsigned NumCatchStmts,
187                                Stmt *atFinallyStmt);
188   static ObjCAtTryStmt *CreateEmpty(const ASTContext &Context,
189                                     unsigned NumCatchStmts, bool HasFinally);
190 
191   /// \brief Retrieve the location of the @ in the \@try.
getAtTryLoc()192   SourceLocation getAtTryLoc() const { return AtTryLoc; }
setAtTryLoc(SourceLocation Loc)193   void setAtTryLoc(SourceLocation Loc) { AtTryLoc = Loc; }
194 
195   /// \brief Retrieve the \@try body.
getTryBody()196   const Stmt *getTryBody() const { return getStmts()[0]; }
getTryBody()197   Stmt *getTryBody() { return getStmts()[0]; }
setTryBody(Stmt * S)198   void setTryBody(Stmt *S) { getStmts()[0] = S; }
199 
200   /// \brief Retrieve the number of \@catch statements in this try-catch-finally
201   /// block.
getNumCatchStmts()202   unsigned getNumCatchStmts() const { return NumCatchStmts; }
203 
204   /// \brief Retrieve a \@catch statement.
getCatchStmt(unsigned I)205   const ObjCAtCatchStmt *getCatchStmt(unsigned I) const {
206     assert(I < NumCatchStmts && "Out-of-bounds @catch index");
207     return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]);
208   }
209 
210   /// \brief Retrieve a \@catch statement.
getCatchStmt(unsigned I)211   ObjCAtCatchStmt *getCatchStmt(unsigned I) {
212     assert(I < NumCatchStmts && "Out-of-bounds @catch index");
213     return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]);
214   }
215 
216   /// \brief Set a particular catch statement.
setCatchStmt(unsigned I,ObjCAtCatchStmt * S)217   void setCatchStmt(unsigned I, ObjCAtCatchStmt *S) {
218     assert(I < NumCatchStmts && "Out-of-bounds @catch index");
219     getStmts()[I + 1] = S;
220   }
221 
222   /// \brief Retrieve the \@finally statement, if any.
getFinallyStmt()223   const ObjCAtFinallyStmt *getFinallyStmt() const {
224     if (!HasFinally)
225       return nullptr;
226 
227     return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
228   }
getFinallyStmt()229   ObjCAtFinallyStmt *getFinallyStmt() {
230     if (!HasFinally)
231       return nullptr;
232 
233     return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
234   }
setFinallyStmt(Stmt * S)235   void setFinallyStmt(Stmt *S) {
236     assert(HasFinally && "@try does not have a @finally slot!");
237     getStmts()[1 + NumCatchStmts] = S;
238   }
239 
getLocStart()240   SourceLocation getLocStart() const LLVM_READONLY { return AtTryLoc; }
241   SourceLocation getLocEnd() const LLVM_READONLY;
242 
classof(const Stmt * T)243   static bool classof(const Stmt *T) {
244     return T->getStmtClass() == ObjCAtTryStmtClass;
245   }
246 
children()247   child_range children() {
248     return child_range(getStmts(),
249                        getStmts() + 1 + NumCatchStmts + HasFinally);
250   }
251 };
252 
253 /// \brief Represents Objective-C's \@synchronized statement.
254 ///
255 /// Example:
256 /// \code
257 ///   @synchronized (sem) {
258 ///     do-something;
259 ///   }
260 /// \endcode
261 class ObjCAtSynchronizedStmt : public Stmt {
262 private:
263   enum { SYNC_EXPR, SYNC_BODY, END_EXPR };
264   Stmt* SubStmts[END_EXPR];
265   SourceLocation AtSynchronizedLoc;
266 
267 public:
ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc,Stmt * synchExpr,Stmt * synchBody)268   ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr,
269                          Stmt *synchBody)
270   : Stmt(ObjCAtSynchronizedStmtClass) {
271     SubStmts[SYNC_EXPR] = synchExpr;
272     SubStmts[SYNC_BODY] = synchBody;
273     AtSynchronizedLoc = atSynchronizedLoc;
274   }
ObjCAtSynchronizedStmt(EmptyShell Empty)275   explicit ObjCAtSynchronizedStmt(EmptyShell Empty) :
276     Stmt(ObjCAtSynchronizedStmtClass, Empty) { }
277 
getAtSynchronizedLoc()278   SourceLocation getAtSynchronizedLoc() const { return AtSynchronizedLoc; }
setAtSynchronizedLoc(SourceLocation Loc)279   void setAtSynchronizedLoc(SourceLocation Loc) { AtSynchronizedLoc = Loc; }
280 
getSynchBody()281   const CompoundStmt *getSynchBody() const {
282     return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
283   }
getSynchBody()284   CompoundStmt *getSynchBody() {
285     return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
286   }
setSynchBody(Stmt * S)287   void setSynchBody(Stmt *S) { SubStmts[SYNC_BODY] = S; }
288 
getSynchExpr()289   const Expr *getSynchExpr() const {
290     return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
291   }
getSynchExpr()292   Expr *getSynchExpr() {
293     return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
294   }
setSynchExpr(Stmt * S)295   void setSynchExpr(Stmt *S) { SubStmts[SYNC_EXPR] = S; }
296 
getLocStart()297   SourceLocation getLocStart() const LLVM_READONLY { return AtSynchronizedLoc; }
getLocEnd()298   SourceLocation getLocEnd() const LLVM_READONLY {
299     return getSynchBody()->getLocEnd();
300   }
301 
classof(const Stmt * T)302   static bool classof(const Stmt *T) {
303     return T->getStmtClass() == ObjCAtSynchronizedStmtClass;
304   }
305 
children()306   child_range children() {
307     return child_range(&SubStmts[0], &SubStmts[0]+END_EXPR);
308   }
309 };
310 
311 /// \brief Represents Objective-C's \@throw statement.
312 class ObjCAtThrowStmt : public Stmt {
313   Stmt *Throw;
314   SourceLocation AtThrowLoc;
315 public:
ObjCAtThrowStmt(SourceLocation atThrowLoc,Stmt * throwExpr)316   ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr)
317   : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) {
318     AtThrowLoc = atThrowLoc;
319   }
ObjCAtThrowStmt(EmptyShell Empty)320   explicit ObjCAtThrowStmt(EmptyShell Empty) :
321     Stmt(ObjCAtThrowStmtClass, Empty) { }
322 
getThrowExpr()323   const Expr *getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); }
getThrowExpr()324   Expr *getThrowExpr() { return reinterpret_cast<Expr*>(Throw); }
setThrowExpr(Stmt * S)325   void setThrowExpr(Stmt *S) { Throw = S; }
326 
getThrowLoc()327   SourceLocation getThrowLoc() { return AtThrowLoc; }
setThrowLoc(SourceLocation Loc)328   void setThrowLoc(SourceLocation Loc) { AtThrowLoc = Loc; }
329 
getLocStart()330   SourceLocation getLocStart() const LLVM_READONLY { return AtThrowLoc; }
getLocEnd()331   SourceLocation getLocEnd() const LLVM_READONLY {
332     return Throw ? Throw->getLocEnd() : AtThrowLoc;
333   }
334 
classof(const Stmt * T)335   static bool classof(const Stmt *T) {
336     return T->getStmtClass() == ObjCAtThrowStmtClass;
337   }
338 
children()339   child_range children() { return child_range(&Throw, &Throw+1); }
340 };
341 
342 /// \brief Represents Objective-C's \@autoreleasepool Statement
343 class ObjCAutoreleasePoolStmt : public Stmt {
344   Stmt *SubStmt;
345   SourceLocation AtLoc;
346 public:
ObjCAutoreleasePoolStmt(SourceLocation atLoc,Stmt * subStmt)347   ObjCAutoreleasePoolStmt(SourceLocation atLoc,
348                             Stmt *subStmt)
349   : Stmt(ObjCAutoreleasePoolStmtClass),
350     SubStmt(subStmt), AtLoc(atLoc) {}
351 
ObjCAutoreleasePoolStmt(EmptyShell Empty)352   explicit ObjCAutoreleasePoolStmt(EmptyShell Empty) :
353     Stmt(ObjCAutoreleasePoolStmtClass, Empty) { }
354 
getSubStmt()355   const Stmt *getSubStmt() const { return SubStmt; }
getSubStmt()356   Stmt *getSubStmt() { return SubStmt; }
setSubStmt(Stmt * S)357   void setSubStmt(Stmt *S) { SubStmt = S; }
358 
getLocStart()359   SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
getLocEnd()360   SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
361 
getAtLoc()362   SourceLocation getAtLoc() const { return AtLoc; }
setAtLoc(SourceLocation Loc)363   void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
364 
classof(const Stmt * T)365   static bool classof(const Stmt *T) {
366     return T->getStmtClass() == ObjCAutoreleasePoolStmtClass;
367   }
368 
children()369   child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
370 };
371 
372 }  // end namespace clang
373 
374 #endif
375