1 //===--- StmtObjC.h - Classes for representing ObjC statements --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Defines the Objective-C statement AST node classes.
13 #ifndef LLVM_CLANG_AST_STMTOBJC_H
14 #define LLVM_CLANG_AST_STMTOBJC_H
16 #include "clang/AST/Stmt.h"
17 #include "llvm/Support/Compiler.h"
21 /// \brief Represents Objective-C's collection statement.
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;
30 ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, Stmt *Body,
31 SourceLocation FCL, SourceLocation RPL);
32 explicit ObjCForCollectionStmt(EmptyShell Empty) :
33 Stmt(ObjCForCollectionStmtClass, Empty) { }
35 Stmt *getElement() { return SubExprs[ELEM]; }
36 Expr *getCollection() {
37 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
39 Stmt *getBody() { return SubExprs[BODY]; }
41 const Stmt *getElement() const { return SubExprs[ELEM]; }
42 const Expr *getCollection() const {
43 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
45 const Stmt *getBody() const { return SubExprs[BODY]; }
47 void setElement(Stmt *S) { SubExprs[ELEM] = S; }
48 void setCollection(Expr *E) {
49 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(E);
51 void setBody(Stmt *S) { SubExprs[BODY] = S; }
53 SourceLocation getForLoc() const { return ForLoc; }
54 void setForLoc(SourceLocation Loc) { ForLoc = Loc; }
55 SourceLocation getRParenLoc() const { return RParenLoc; }
56 void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; }
58 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
59 SourceLocation getLocEnd() const LLVM_READONLY {
60 return SubExprs[BODY]->getLocEnd();
63 static bool classof(const Stmt *T) {
64 return T->getStmtClass() == ObjCForCollectionStmtClass;
68 child_range children() {
69 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
73 /// \brief Represents Objective-C's \@catch statement.
74 class ObjCAtCatchStmt : public Stmt {
76 VarDecl *ExceptionDecl;
78 SourceLocation AtCatchLoc, RParenLoc;
81 ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc,
82 VarDecl *catchVarDecl,
84 : Stmt(ObjCAtCatchStmtClass), ExceptionDecl(catchVarDecl),
85 Body(atCatchStmt), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) { }
87 explicit ObjCAtCatchStmt(EmptyShell Empty) :
88 Stmt(ObjCAtCatchStmtClass, Empty) { }
90 const Stmt *getCatchBody() const { return Body; }
91 Stmt *getCatchBody() { return Body; }
92 void setCatchBody(Stmt *S) { Body = S; }
94 const VarDecl *getCatchParamDecl() const {
97 VarDecl *getCatchParamDecl() {
100 void setCatchParamDecl(VarDecl *D) { ExceptionDecl = D; }
102 SourceLocation getAtCatchLoc() const { return AtCatchLoc; }
103 void setAtCatchLoc(SourceLocation Loc) { AtCatchLoc = Loc; }
104 SourceLocation getRParenLoc() const { return RParenLoc; }
105 void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; }
107 SourceLocation getLocStart() const LLVM_READONLY { return AtCatchLoc; }
108 SourceLocation getLocEnd() const LLVM_READONLY { return Body->getLocEnd(); }
110 bool hasEllipsis() const { return getCatchParamDecl() == nullptr; }
112 static bool classof(const Stmt *T) {
113 return T->getStmtClass() == ObjCAtCatchStmtClass;
116 child_range children() { return child_range(&Body, &Body + 1); }
119 /// \brief Represents Objective-C's \@finally statement
120 class ObjCAtFinallyStmt : public Stmt {
121 SourceLocation AtFinallyLoc;
125 ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt)
126 : Stmt(ObjCAtFinallyStmtClass), AtFinallyLoc(atFinallyLoc),
127 AtFinallyStmt(atFinallyStmt) {}
129 explicit ObjCAtFinallyStmt(EmptyShell Empty) :
130 Stmt(ObjCAtFinallyStmtClass, Empty) { }
132 const Stmt *getFinallyBody() const { return AtFinallyStmt; }
133 Stmt *getFinallyBody() { return AtFinallyStmt; }
134 void setFinallyBody(Stmt *S) { AtFinallyStmt = S; }
136 SourceLocation getLocStart() const LLVM_READONLY { return AtFinallyLoc; }
137 SourceLocation getLocEnd() const LLVM_READONLY {
138 return AtFinallyStmt->getLocEnd();
141 SourceLocation getAtFinallyLoc() const { return AtFinallyLoc; }
142 void setAtFinallyLoc(SourceLocation Loc) { AtFinallyLoc = Loc; }
144 static bool classof(const Stmt *T) {
145 return T->getStmtClass() == ObjCAtFinallyStmtClass;
148 child_range children() {
149 return child_range(&AtFinallyStmt, &AtFinallyStmt+1);
153 /// \brief Represents Objective-C's \@try ... \@catch ... \@finally statement.
154 class ObjCAtTryStmt : public Stmt {
156 // The location of the @ in the \@try.
157 SourceLocation AtTryLoc;
159 // The number of catch blocks in this statement.
160 unsigned NumCatchStmts : 16;
162 // Whether this statement has a \@finally statement.
165 /// \brief Retrieve the statements that are stored after this \@try statement.
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).
170 Stmt **getStmts() { return reinterpret_cast<Stmt **> (this + 1); }
171 const Stmt* const *getStmts() const {
172 return reinterpret_cast<const Stmt * const*> (this + 1);
175 ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
176 Stmt **CatchStmts, unsigned NumCatchStmts,
177 Stmt *atFinallyStmt);
179 explicit ObjCAtTryStmt(EmptyShell Empty, unsigned NumCatchStmts,
181 : Stmt(ObjCAtTryStmtClass, Empty), NumCatchStmts(NumCatchStmts),
182 HasFinally(HasFinally) { }
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);
192 /// \brief Retrieve the location of the @ in the \@try.
193 SourceLocation getAtTryLoc() const { return AtTryLoc; }
194 void setAtTryLoc(SourceLocation Loc) { AtTryLoc = Loc; }
196 /// \brief Retrieve the \@try body.
197 const Stmt *getTryBody() const { return getStmts()[0]; }
198 Stmt *getTryBody() { return getStmts()[0]; }
199 void setTryBody(Stmt *S) { getStmts()[0] = S; }
201 /// \brief Retrieve the number of \@catch statements in this try-catch-finally
203 unsigned getNumCatchStmts() const { return NumCatchStmts; }
205 /// \brief Retrieve a \@catch statement.
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]);
211 /// \brief Retrieve a \@catch statement.
212 ObjCAtCatchStmt *getCatchStmt(unsigned I) {
213 assert(I < NumCatchStmts && "Out-of-bounds @catch index");
214 return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]);
217 /// \brief Set a particular catch statement.
218 void setCatchStmt(unsigned I, ObjCAtCatchStmt *S) {
219 assert(I < NumCatchStmts && "Out-of-bounds @catch index");
220 getStmts()[I + 1] = S;
223 /// \brief Retrieve the \@finally statement, if any.
224 const ObjCAtFinallyStmt *getFinallyStmt() const {
228 return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
230 ObjCAtFinallyStmt *getFinallyStmt() {
234 return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
236 void setFinallyStmt(Stmt *S) {
237 assert(HasFinally && "@try does not have a @finally slot!");
238 getStmts()[1 + NumCatchStmts] = S;
241 SourceLocation getLocStart() const LLVM_READONLY { return AtTryLoc; }
242 SourceLocation getLocEnd() const LLVM_READONLY;
244 static bool classof(const Stmt *T) {
245 return T->getStmtClass() == ObjCAtTryStmtClass;
248 child_range children() {
249 return child_range(getStmts(),
250 getStmts() + 1 + NumCatchStmts + HasFinally);
254 /// \brief Represents Objective-C's \@synchronized statement.
258 /// @synchronized (sem) {
262 class ObjCAtSynchronizedStmt : public Stmt {
264 SourceLocation AtSynchronizedLoc;
265 enum { SYNC_EXPR, SYNC_BODY, END_EXPR };
266 Stmt* SubStmts[END_EXPR];
269 ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr,
271 : Stmt(ObjCAtSynchronizedStmtClass) {
272 SubStmts[SYNC_EXPR] = synchExpr;
273 SubStmts[SYNC_BODY] = synchBody;
274 AtSynchronizedLoc = atSynchronizedLoc;
276 explicit ObjCAtSynchronizedStmt(EmptyShell Empty) :
277 Stmt(ObjCAtSynchronizedStmtClass, Empty) { }
279 SourceLocation getAtSynchronizedLoc() const { return AtSynchronizedLoc; }
280 void setAtSynchronizedLoc(SourceLocation Loc) { AtSynchronizedLoc = Loc; }
282 const CompoundStmt *getSynchBody() const {
283 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
285 CompoundStmt *getSynchBody() {
286 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
288 void setSynchBody(Stmt *S) { SubStmts[SYNC_BODY] = S; }
290 const Expr *getSynchExpr() const {
291 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
293 Expr *getSynchExpr() {
294 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
296 void setSynchExpr(Stmt *S) { SubStmts[SYNC_EXPR] = S; }
298 SourceLocation getLocStart() const LLVM_READONLY { return AtSynchronizedLoc; }
299 SourceLocation getLocEnd() const LLVM_READONLY {
300 return getSynchBody()->getLocEnd();
303 static bool classof(const Stmt *T) {
304 return T->getStmtClass() == ObjCAtSynchronizedStmtClass;
307 child_range children() {
308 return child_range(&SubStmts[0], &SubStmts[0]+END_EXPR);
312 /// \brief Represents Objective-C's \@throw statement.
313 class ObjCAtThrowStmt : public Stmt {
314 SourceLocation AtThrowLoc;
318 ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr)
319 : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) {
320 AtThrowLoc = atThrowLoc;
322 explicit ObjCAtThrowStmt(EmptyShell Empty) :
323 Stmt(ObjCAtThrowStmtClass, Empty) { }
325 const Expr *getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); }
326 Expr *getThrowExpr() { return reinterpret_cast<Expr*>(Throw); }
327 void setThrowExpr(Stmt *S) { Throw = S; }
329 SourceLocation getThrowLoc() const LLVM_READONLY { return AtThrowLoc; }
330 void setThrowLoc(SourceLocation Loc) { AtThrowLoc = Loc; }
332 SourceLocation getLocStart() const LLVM_READONLY { return AtThrowLoc; }
333 SourceLocation getLocEnd() const LLVM_READONLY {
334 return Throw ? Throw->getLocEnd() : AtThrowLoc;
337 static bool classof(const Stmt *T) {
338 return T->getStmtClass() == ObjCAtThrowStmtClass;
341 child_range children() { return child_range(&Throw, &Throw+1); }
344 /// \brief Represents Objective-C's \@autoreleasepool Statement
345 class ObjCAutoreleasePoolStmt : public Stmt {
346 SourceLocation AtLoc;
350 ObjCAutoreleasePoolStmt(SourceLocation atLoc, Stmt *subStmt)
351 : Stmt(ObjCAutoreleasePoolStmtClass), AtLoc(atLoc), SubStmt(subStmt) {}
353 explicit ObjCAutoreleasePoolStmt(EmptyShell Empty) :
354 Stmt(ObjCAutoreleasePoolStmtClass, Empty) { }
356 const Stmt *getSubStmt() const { return SubStmt; }
357 Stmt *getSubStmt() { return SubStmt; }
358 void setSubStmt(Stmt *S) { SubStmt = S; }
360 SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
361 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
363 SourceLocation getAtLoc() const { return AtLoc; }
364 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
366 static bool classof(const Stmt *T) {
367 return T->getStmtClass() == ObjCAutoreleasePoolStmtClass;
370 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
373 } // end namespace clang