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 /// 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 /// 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 getBeginLoc(); }
59 SourceLocation getBeginLoc() const LLVM_READONLY { return ForLoc; }
60 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
61 SourceLocation getEndLoc() const LLVM_READONLY {
62 return SubExprs[BODY]->getLocEnd();
65 static bool classof(const Stmt *T) {
66 return T->getStmtClass() == ObjCForCollectionStmtClass;
70 child_range children() {
71 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
75 /// Represents Objective-C's \@catch statement.
76 class ObjCAtCatchStmt : public Stmt {
78 VarDecl *ExceptionDecl;
80 SourceLocation AtCatchLoc, RParenLoc;
83 ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc,
84 VarDecl *catchVarDecl,
86 : Stmt(ObjCAtCatchStmtClass), ExceptionDecl(catchVarDecl),
87 Body(atCatchStmt), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) { }
89 explicit ObjCAtCatchStmt(EmptyShell Empty) :
90 Stmt(ObjCAtCatchStmtClass, Empty) { }
92 const Stmt *getCatchBody() const { return Body; }
93 Stmt *getCatchBody() { return Body; }
94 void setCatchBody(Stmt *S) { Body = S; }
96 const VarDecl *getCatchParamDecl() const {
99 VarDecl *getCatchParamDecl() {
100 return ExceptionDecl;
102 void setCatchParamDecl(VarDecl *D) { ExceptionDecl = D; }
104 SourceLocation getAtCatchLoc() const { return AtCatchLoc; }
105 void setAtCatchLoc(SourceLocation Loc) { AtCatchLoc = Loc; }
106 SourceLocation getRParenLoc() const { return RParenLoc; }
107 void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; }
109 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
110 SourceLocation getBeginLoc() const LLVM_READONLY { return AtCatchLoc; }
111 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
112 SourceLocation getEndLoc() const LLVM_READONLY { return Body->getLocEnd(); }
114 bool hasEllipsis() const { return getCatchParamDecl() == nullptr; }
116 static bool classof(const Stmt *T) {
117 return T->getStmtClass() == ObjCAtCatchStmtClass;
120 child_range children() { return child_range(&Body, &Body + 1); }
123 /// Represents Objective-C's \@finally statement
124 class ObjCAtFinallyStmt : public Stmt {
125 SourceLocation AtFinallyLoc;
129 ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt)
130 : Stmt(ObjCAtFinallyStmtClass), AtFinallyLoc(atFinallyLoc),
131 AtFinallyStmt(atFinallyStmt) {}
133 explicit ObjCAtFinallyStmt(EmptyShell Empty) :
134 Stmt(ObjCAtFinallyStmtClass, Empty) { }
136 const Stmt *getFinallyBody() const { return AtFinallyStmt; }
137 Stmt *getFinallyBody() { return AtFinallyStmt; }
138 void setFinallyBody(Stmt *S) { AtFinallyStmt = S; }
140 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
141 SourceLocation getBeginLoc() const LLVM_READONLY { return AtFinallyLoc; }
142 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
143 SourceLocation getEndLoc() const LLVM_READONLY {
144 return AtFinallyStmt->getLocEnd();
147 SourceLocation getAtFinallyLoc() const { return AtFinallyLoc; }
148 void setAtFinallyLoc(SourceLocation Loc) { AtFinallyLoc = Loc; }
150 static bool classof(const Stmt *T) {
151 return T->getStmtClass() == ObjCAtFinallyStmtClass;
154 child_range children() {
155 return child_range(&AtFinallyStmt, &AtFinallyStmt+1);
159 /// Represents Objective-C's \@try ... \@catch ... \@finally statement.
160 class ObjCAtTryStmt : public Stmt {
162 // The location of the @ in the \@try.
163 SourceLocation AtTryLoc;
165 // The number of catch blocks in this statement.
166 unsigned NumCatchStmts : 16;
168 // Whether this statement has a \@finally statement.
171 /// Retrieve the statements that are stored after this \@try statement.
173 /// The order of the statements in memory follows the order in the source,
174 /// with the \@try body first, followed by the \@catch statements (if any)
175 /// and, finally, the \@finally (if it exists).
176 Stmt **getStmts() { return reinterpret_cast<Stmt **> (this + 1); }
177 const Stmt* const *getStmts() const {
178 return reinterpret_cast<const Stmt * const*> (this + 1);
181 ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
182 Stmt **CatchStmts, unsigned NumCatchStmts,
183 Stmt *atFinallyStmt);
185 explicit ObjCAtTryStmt(EmptyShell Empty, unsigned NumCatchStmts,
187 : Stmt(ObjCAtTryStmtClass, Empty), NumCatchStmts(NumCatchStmts),
188 HasFinally(HasFinally) { }
191 static ObjCAtTryStmt *Create(const ASTContext &Context,
192 SourceLocation atTryLoc, Stmt *atTryStmt,
193 Stmt **CatchStmts, unsigned NumCatchStmts,
194 Stmt *atFinallyStmt);
195 static ObjCAtTryStmt *CreateEmpty(const ASTContext &Context,
196 unsigned NumCatchStmts, bool HasFinally);
198 /// Retrieve the location of the @ in the \@try.
199 SourceLocation getAtTryLoc() const { return AtTryLoc; }
200 void setAtTryLoc(SourceLocation Loc) { AtTryLoc = Loc; }
202 /// Retrieve the \@try body.
203 const Stmt *getTryBody() const { return getStmts()[0]; }
204 Stmt *getTryBody() { return getStmts()[0]; }
205 void setTryBody(Stmt *S) { getStmts()[0] = S; }
207 /// Retrieve the number of \@catch statements in this try-catch-finally
209 unsigned getNumCatchStmts() const { return NumCatchStmts; }
211 /// Retrieve a \@catch statement.
212 const ObjCAtCatchStmt *getCatchStmt(unsigned I) const {
213 assert(I < NumCatchStmts && "Out-of-bounds @catch index");
214 return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]);
217 /// Retrieve a \@catch statement.
218 ObjCAtCatchStmt *getCatchStmt(unsigned I) {
219 assert(I < NumCatchStmts && "Out-of-bounds @catch index");
220 return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]);
223 /// Set a particular catch statement.
224 void setCatchStmt(unsigned I, ObjCAtCatchStmt *S) {
225 assert(I < NumCatchStmts && "Out-of-bounds @catch index");
226 getStmts()[I + 1] = S;
229 /// Retrieve the \@finally statement, if any.
230 const ObjCAtFinallyStmt *getFinallyStmt() const {
234 return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
236 ObjCAtFinallyStmt *getFinallyStmt() {
240 return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
242 void setFinallyStmt(Stmt *S) {
243 assert(HasFinally && "@try does not have a @finally slot!");
244 getStmts()[1 + NumCatchStmts] = S;
247 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
248 SourceLocation getBeginLoc() const LLVM_READONLY { return AtTryLoc; }
249 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
250 SourceLocation getEndLoc() const LLVM_READONLY;
252 static bool classof(const Stmt *T) {
253 return T->getStmtClass() == ObjCAtTryStmtClass;
256 child_range children() {
257 return child_range(getStmts(),
258 getStmts() + 1 + NumCatchStmts + HasFinally);
262 /// Represents Objective-C's \@synchronized statement.
266 /// @synchronized (sem) {
270 class ObjCAtSynchronizedStmt : public Stmt {
272 SourceLocation AtSynchronizedLoc;
273 enum { SYNC_EXPR, SYNC_BODY, END_EXPR };
274 Stmt* SubStmts[END_EXPR];
277 ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr,
279 : Stmt(ObjCAtSynchronizedStmtClass) {
280 SubStmts[SYNC_EXPR] = synchExpr;
281 SubStmts[SYNC_BODY] = synchBody;
282 AtSynchronizedLoc = atSynchronizedLoc;
284 explicit ObjCAtSynchronizedStmt(EmptyShell Empty) :
285 Stmt(ObjCAtSynchronizedStmtClass, Empty) { }
287 SourceLocation getAtSynchronizedLoc() const { return AtSynchronizedLoc; }
288 void setAtSynchronizedLoc(SourceLocation Loc) { AtSynchronizedLoc = Loc; }
290 const CompoundStmt *getSynchBody() const {
291 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
293 CompoundStmt *getSynchBody() {
294 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
296 void setSynchBody(Stmt *S) { SubStmts[SYNC_BODY] = S; }
298 const Expr *getSynchExpr() const {
299 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
301 Expr *getSynchExpr() {
302 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
304 void setSynchExpr(Stmt *S) { SubStmts[SYNC_EXPR] = S; }
306 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
307 SourceLocation getBeginLoc() const LLVM_READONLY { return AtSynchronizedLoc; }
308 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
309 SourceLocation getEndLoc() const LLVM_READONLY {
310 return getSynchBody()->getLocEnd();
313 static bool classof(const Stmt *T) {
314 return T->getStmtClass() == ObjCAtSynchronizedStmtClass;
317 child_range children() {
318 return child_range(&SubStmts[0], &SubStmts[0]+END_EXPR);
322 /// Represents Objective-C's \@throw statement.
323 class ObjCAtThrowStmt : public Stmt {
324 SourceLocation AtThrowLoc;
328 ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr)
329 : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) {
330 AtThrowLoc = atThrowLoc;
332 explicit ObjCAtThrowStmt(EmptyShell Empty) :
333 Stmt(ObjCAtThrowStmtClass, Empty) { }
335 const Expr *getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); }
336 Expr *getThrowExpr() { return reinterpret_cast<Expr*>(Throw); }
337 void setThrowExpr(Stmt *S) { Throw = S; }
339 SourceLocation getThrowLoc() const LLVM_READONLY { return AtThrowLoc; }
340 void setThrowLoc(SourceLocation Loc) { AtThrowLoc = Loc; }
342 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
343 SourceLocation getBeginLoc() const LLVM_READONLY { return AtThrowLoc; }
344 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
345 SourceLocation getEndLoc() const LLVM_READONLY {
346 return Throw ? Throw->getLocEnd() : AtThrowLoc;
349 static bool classof(const Stmt *T) {
350 return T->getStmtClass() == ObjCAtThrowStmtClass;
353 child_range children() { return child_range(&Throw, &Throw+1); }
356 /// Represents Objective-C's \@autoreleasepool Statement
357 class ObjCAutoreleasePoolStmt : public Stmt {
358 SourceLocation AtLoc;
362 ObjCAutoreleasePoolStmt(SourceLocation atLoc, Stmt *subStmt)
363 : Stmt(ObjCAutoreleasePoolStmtClass), AtLoc(atLoc), SubStmt(subStmt) {}
365 explicit ObjCAutoreleasePoolStmt(EmptyShell Empty) :
366 Stmt(ObjCAutoreleasePoolStmtClass, Empty) { }
368 const Stmt *getSubStmt() const { return SubStmt; }
369 Stmt *getSubStmt() { return SubStmt; }
370 void setSubStmt(Stmt *S) { SubStmt = S; }
372 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
373 SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; }
374 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
375 SourceLocation getEndLoc() const LLVM_READONLY {
376 return SubStmt->getLocEnd();
379 SourceLocation getAtLoc() const { return AtLoc; }
380 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
382 static bool classof(const Stmt *T) {
383 return T->getStmtClass() == ObjCAutoreleasePoolStmtClass;
386 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
389 } // end namespace clang