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() == 0; }
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 {
122 SourceLocation AtFinallyLoc;
124 ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt)
125 : Stmt(ObjCAtFinallyStmtClass),
126 AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {}
128 explicit ObjCAtFinallyStmt(EmptyShell Empty) :
129 Stmt(ObjCAtFinallyStmtClass, Empty) { }
131 const Stmt *getFinallyBody() const { return AtFinallyStmt; }
132 Stmt *getFinallyBody() { return AtFinallyStmt; }
133 void setFinallyBody(Stmt *S) { AtFinallyStmt = S; }
135 SourceLocation getLocStart() const LLVM_READONLY { return AtFinallyLoc; }
136 SourceLocation getLocEnd() const LLVM_READONLY {
137 return AtFinallyStmt->getLocEnd();
140 SourceLocation getAtFinallyLoc() const { return AtFinallyLoc; }
141 void setAtFinallyLoc(SourceLocation Loc) { AtFinallyLoc = Loc; }
143 static bool classof(const Stmt *T) {
144 return T->getStmtClass() == ObjCAtFinallyStmtClass;
147 child_range children() {
148 return child_range(&AtFinallyStmt, &AtFinallyStmt+1);
152 /// \brief Represents Objective-C's \@try ... \@catch ... \@finally statement.
153 class ObjCAtTryStmt : public Stmt {
155 // The location of the @ in the \@try.
156 SourceLocation AtTryLoc;
158 // The number of catch blocks in this statement.
159 unsigned NumCatchStmts : 16;
161 // Whether this statement has a \@finally statement.
164 /// \brief Retrieve the statements that are stored after this \@try statement.
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).
169 Stmt **getStmts() { return reinterpret_cast<Stmt **> (this + 1); }
170 const Stmt* const *getStmts() const {
171 return reinterpret_cast<const Stmt * const*> (this + 1);
174 ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
175 Stmt **CatchStmts, unsigned NumCatchStmts,
176 Stmt *atFinallyStmt);
178 explicit ObjCAtTryStmt(EmptyShell Empty, unsigned NumCatchStmts,
180 : Stmt(ObjCAtTryStmtClass, Empty), NumCatchStmts(NumCatchStmts),
181 HasFinally(HasFinally) { }
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);
191 /// \brief Retrieve the location of the @ in the \@try.
192 SourceLocation getAtTryLoc() const { return AtTryLoc; }
193 void setAtTryLoc(SourceLocation Loc) { AtTryLoc = Loc; }
195 /// \brief Retrieve the \@try body.
196 const Stmt *getTryBody() const { return getStmts()[0]; }
197 Stmt *getTryBody() { return getStmts()[0]; }
198 void setTryBody(Stmt *S) { getStmts()[0] = S; }
200 /// \brief Retrieve the number of \@catch statements in this try-catch-finally
202 unsigned getNumCatchStmts() const { return NumCatchStmts; }
204 /// \brief Retrieve a \@catch statement.
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]);
210 /// \brief Retrieve a \@catch statement.
211 ObjCAtCatchStmt *getCatchStmt(unsigned I) {
212 assert(I < NumCatchStmts && "Out-of-bounds @catch index");
213 return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]);
216 /// \brief Set a particular catch statement.
217 void setCatchStmt(unsigned I, ObjCAtCatchStmt *S) {
218 assert(I < NumCatchStmts && "Out-of-bounds @catch index");
219 getStmts()[I + 1] = S;
222 /// \brief Retrieve the \@finally statement, if any.
223 const ObjCAtFinallyStmt *getFinallyStmt() const {
227 return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
229 ObjCAtFinallyStmt *getFinallyStmt() {
233 return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
235 void setFinallyStmt(Stmt *S) {
236 assert(HasFinally && "@try does not have a @finally slot!");
237 getStmts()[1 + NumCatchStmts] = S;
240 SourceLocation getLocStart() const LLVM_READONLY { return AtTryLoc; }
241 SourceLocation getLocEnd() const LLVM_READONLY;
243 static bool classof(const Stmt *T) {
244 return T->getStmtClass() == ObjCAtTryStmtClass;
247 child_range children() {
248 return child_range(getStmts(),
249 getStmts() + 1 + NumCatchStmts + HasFinally);
253 /// \brief Represents Objective-C's \@synchronized statement.
257 /// @synchronized (sem) {
261 class ObjCAtSynchronizedStmt : public Stmt {
263 enum { SYNC_EXPR, SYNC_BODY, END_EXPR };
264 Stmt* SubStmts[END_EXPR];
265 SourceLocation AtSynchronizedLoc;
268 ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr,
270 : Stmt(ObjCAtSynchronizedStmtClass) {
271 SubStmts[SYNC_EXPR] = synchExpr;
272 SubStmts[SYNC_BODY] = synchBody;
273 AtSynchronizedLoc = atSynchronizedLoc;
275 explicit ObjCAtSynchronizedStmt(EmptyShell Empty) :
276 Stmt(ObjCAtSynchronizedStmtClass, Empty) { }
278 SourceLocation getAtSynchronizedLoc() const { return AtSynchronizedLoc; }
279 void setAtSynchronizedLoc(SourceLocation Loc) { AtSynchronizedLoc = Loc; }
281 const CompoundStmt *getSynchBody() const {
282 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
284 CompoundStmt *getSynchBody() {
285 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
287 void setSynchBody(Stmt *S) { SubStmts[SYNC_BODY] = S; }
289 const Expr *getSynchExpr() const {
290 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
292 Expr *getSynchExpr() {
293 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
295 void setSynchExpr(Stmt *S) { SubStmts[SYNC_EXPR] = S; }
297 SourceLocation getLocStart() const LLVM_READONLY { return AtSynchronizedLoc; }
298 SourceLocation getLocEnd() const LLVM_READONLY {
299 return getSynchBody()->getLocEnd();
302 static bool classof(const Stmt *T) {
303 return T->getStmtClass() == ObjCAtSynchronizedStmtClass;
306 child_range children() {
307 return child_range(&SubStmts[0], &SubStmts[0]+END_EXPR);
311 /// \brief Represents Objective-C's \@throw statement.
312 class ObjCAtThrowStmt : public Stmt {
314 SourceLocation AtThrowLoc;
316 ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr)
317 : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) {
318 AtThrowLoc = atThrowLoc;
320 explicit ObjCAtThrowStmt(EmptyShell Empty) :
321 Stmt(ObjCAtThrowStmtClass, Empty) { }
323 const Expr *getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); }
324 Expr *getThrowExpr() { return reinterpret_cast<Expr*>(Throw); }
325 void setThrowExpr(Stmt *S) { Throw = S; }
327 SourceLocation getThrowLoc() { return AtThrowLoc; }
328 void setThrowLoc(SourceLocation Loc) { AtThrowLoc = Loc; }
330 SourceLocation getLocStart() const LLVM_READONLY { return AtThrowLoc; }
331 SourceLocation getLocEnd() const LLVM_READONLY {
332 return Throw ? Throw->getLocEnd() : AtThrowLoc;
335 static bool classof(const Stmt *T) {
336 return T->getStmtClass() == ObjCAtThrowStmtClass;
339 child_range children() { return child_range(&Throw, &Throw+1); }
342 /// \brief Represents Objective-C's \@autoreleasepool Statement
343 class ObjCAutoreleasePoolStmt : public Stmt {
345 SourceLocation AtLoc;
347 ObjCAutoreleasePoolStmt(SourceLocation atLoc,
349 : Stmt(ObjCAutoreleasePoolStmtClass),
350 SubStmt(subStmt), AtLoc(atLoc) {}
352 explicit ObjCAutoreleasePoolStmt(EmptyShell Empty) :
353 Stmt(ObjCAutoreleasePoolStmtClass, Empty) { }
355 const Stmt *getSubStmt() const { return SubStmt; }
356 Stmt *getSubStmt() { return SubStmt; }
357 void setSubStmt(Stmt *S) { SubStmt = S; }
359 SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
360 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
362 SourceLocation getAtLoc() const { return AtLoc; }
363 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
365 static bool classof(const Stmt *T) {
366 return T->getStmtClass() == ObjCAutoreleasePoolStmtClass;
369 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
372 } // end namespace clang