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 // This file defines the Objective-C statement AST node classes. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_AST_STMTOBJC_H 15 #define LLVM_CLANG_AST_STMTOBJC_H 16 17 #include "clang/AST/Stmt.h" 18 19 namespace clang { 20 21 /// ObjCForCollectionStmt - This represents Objective-c's collection statement; 22 /// represented as 'for (element 'in' collection-expression)' stmt. 23 /// 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 getSourceRange()58 SourceRange getSourceRange() const { 59 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); 60 } classof(const Stmt * T)61 static bool classof(const Stmt *T) { 62 return T->getStmtClass() == ObjCForCollectionStmtClass; 63 } classof(const ObjCForCollectionStmt *)64 static bool classof(const ObjCForCollectionStmt *) { return true; } 65 66 // Iterators children()67 child_range children() { 68 return child_range(&SubExprs[0], &SubExprs[END_EXPR]); 69 } 70 }; 71 72 /// ObjCAtCatchStmt - This represents objective-c's @catch statement. 73 class ObjCAtCatchStmt : public Stmt { 74 private: 75 VarDecl *ExceptionDecl; 76 Stmt *Body; 77 SourceLocation AtCatchLoc, RParenLoc; 78 79 public: ObjCAtCatchStmt(SourceLocation atCatchLoc,SourceLocation rparenloc,VarDecl * catchVarDecl,Stmt * atCatchStmt)80 ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc, 81 VarDecl *catchVarDecl, 82 Stmt *atCatchStmt) 83 : Stmt(ObjCAtCatchStmtClass), ExceptionDecl(catchVarDecl), 84 Body(atCatchStmt), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) { } 85 ObjCAtCatchStmt(EmptyShell Empty)86 explicit ObjCAtCatchStmt(EmptyShell Empty) : 87 Stmt(ObjCAtCatchStmtClass, Empty) { } 88 getCatchBody()89 const Stmt *getCatchBody() const { return Body; } getCatchBody()90 Stmt *getCatchBody() { return Body; } setCatchBody(Stmt * S)91 void setCatchBody(Stmt *S) { Body = S; } 92 getCatchParamDecl()93 const VarDecl *getCatchParamDecl() const { 94 return ExceptionDecl; 95 } getCatchParamDecl()96 VarDecl *getCatchParamDecl() { 97 return ExceptionDecl; 98 } setCatchParamDecl(VarDecl * D)99 void setCatchParamDecl(VarDecl *D) { ExceptionDecl = D; } 100 getAtCatchLoc()101 SourceLocation getAtCatchLoc() const { return AtCatchLoc; } setAtCatchLoc(SourceLocation Loc)102 void setAtCatchLoc(SourceLocation Loc) { AtCatchLoc = Loc; } getRParenLoc()103 SourceLocation getRParenLoc() const { return RParenLoc; } setRParenLoc(SourceLocation Loc)104 void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; } 105 getSourceRange()106 SourceRange getSourceRange() const { 107 return SourceRange(AtCatchLoc, Body->getLocEnd()); 108 } 109 hasEllipsis()110 bool hasEllipsis() const { return getCatchParamDecl() == 0; } 111 classof(const Stmt * T)112 static bool classof(const Stmt *T) { 113 return T->getStmtClass() == ObjCAtCatchStmtClass; 114 } classof(const ObjCAtCatchStmt *)115 static bool classof(const ObjCAtCatchStmt *) { return true; } 116 children()117 child_range children() { return child_range(&Body, &Body + 1); } 118 }; 119 120 /// ObjCAtFinallyStmt - This represent objective-c's @finally Statement 121 class ObjCAtFinallyStmt : public Stmt { 122 Stmt *AtFinallyStmt; 123 SourceLocation AtFinallyLoc; 124 public: ObjCAtFinallyStmt(SourceLocation atFinallyLoc,Stmt * atFinallyStmt)125 ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt) 126 : Stmt(ObjCAtFinallyStmtClass), 127 AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {} 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 getSourceRange()136 SourceRange getSourceRange() const { 137 return SourceRange(AtFinallyLoc, 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 } classof(const ObjCAtFinallyStmt *)146 static bool classof(const ObjCAtFinallyStmt *) { return true; } 147 children()148 child_range children() { 149 return child_range(&AtFinallyStmt, &AtFinallyStmt+1); 150 } 151 }; 152 153 /// ObjCAtTryStmt - This represent objective-c's over-all 154 /// @try ... @catch ... @finally statement. 155 class ObjCAtTryStmt : public Stmt { 156 private: 157 // The location of the 158 SourceLocation AtTryLoc; 159 160 // The number of catch blocks in this statement. 161 unsigned NumCatchStmts : 16; 162 163 // Whether this statement has a @finally statement. 164 bool HasFinally : 1; 165 166 /// \brief Retrieve the statements that are stored after this @try statement. 167 /// 168 /// The order of the statements in memory follows the order in the source, 169 /// with the @try body first, followed by the @catch statements (if any) and, 170 /// finally, the @finally (if it exists). getStmts()171 Stmt **getStmts() { return reinterpret_cast<Stmt **> (this + 1); } getStmts()172 const Stmt* const *getStmts() const { 173 return reinterpret_cast<const Stmt * const*> (this + 1); 174 } 175 176 ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt, 177 Stmt **CatchStmts, unsigned NumCatchStmts, 178 Stmt *atFinallyStmt); 179 ObjCAtTryStmt(EmptyShell Empty,unsigned NumCatchStmts,bool HasFinally)180 explicit ObjCAtTryStmt(EmptyShell Empty, unsigned NumCatchStmts, 181 bool HasFinally) 182 : Stmt(ObjCAtTryStmtClass, Empty), NumCatchStmts(NumCatchStmts), 183 HasFinally(HasFinally) { } 184 185 public: 186 static ObjCAtTryStmt *Create(ASTContext &Context, SourceLocation atTryLoc, 187 Stmt *atTryStmt, 188 Stmt **CatchStmts, unsigned NumCatchStmts, 189 Stmt *atFinallyStmt); 190 static ObjCAtTryStmt *CreateEmpty(ASTContext &Context, 191 unsigned NumCatchStmts, 192 bool HasFinally); 193 194 /// \brief Retrieve the location of the @ in the @try. getAtTryLoc()195 SourceLocation getAtTryLoc() const { return AtTryLoc; } setAtTryLoc(SourceLocation Loc)196 void setAtTryLoc(SourceLocation Loc) { AtTryLoc = Loc; } 197 198 /// \brief Retrieve the @try body. getTryBody()199 const Stmt *getTryBody() const { return getStmts()[0]; } getTryBody()200 Stmt *getTryBody() { return getStmts()[0]; } setTryBody(Stmt * S)201 void setTryBody(Stmt *S) { getStmts()[0] = S; } 202 203 /// \brief Retrieve the number of @catch statements in this try-catch-finally 204 /// block. getNumCatchStmts()205 unsigned getNumCatchStmts() const { return NumCatchStmts; } 206 207 /// \brief Retrieve a @catch statement. getCatchStmt(unsigned I)208 const ObjCAtCatchStmt *getCatchStmt(unsigned I) const { 209 assert(I < NumCatchStmts && "Out-of-bounds @catch index"); 210 return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]); 211 } 212 213 /// \brief Retrieve a @catch statement. getCatchStmt(unsigned I)214 ObjCAtCatchStmt *getCatchStmt(unsigned I) { 215 assert(I < NumCatchStmts && "Out-of-bounds @catch index"); 216 return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]); 217 } 218 219 /// \brief Set a particular catch statement. setCatchStmt(unsigned I,ObjCAtCatchStmt * S)220 void setCatchStmt(unsigned I, ObjCAtCatchStmt *S) { 221 assert(I < NumCatchStmts && "Out-of-bounds @catch index"); 222 getStmts()[I + 1] = S; 223 } 224 225 /// Retrieve the @finally statement, if any. getFinallyStmt()226 const ObjCAtFinallyStmt *getFinallyStmt() const { 227 if (!HasFinally) 228 return 0; 229 230 return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]); 231 } getFinallyStmt()232 ObjCAtFinallyStmt *getFinallyStmt() { 233 if (!HasFinally) 234 return 0; 235 236 return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]); 237 } setFinallyStmt(Stmt * S)238 void setFinallyStmt(Stmt *S) { 239 assert(HasFinally && "@try does not have a @finally slot!"); 240 getStmts()[1 + NumCatchStmts] = S; 241 } 242 243 SourceRange getSourceRange() const; 244 classof(const Stmt * T)245 static bool classof(const Stmt *T) { 246 return T->getStmtClass() == ObjCAtTryStmtClass; 247 } classof(const ObjCAtTryStmt *)248 static bool classof(const ObjCAtTryStmt *) { return true; } 249 children()250 child_range children() { 251 return child_range(getStmts(), 252 getStmts() + 1 + NumCatchStmts + HasFinally); 253 } 254 }; 255 256 /// ObjCAtSynchronizedStmt - This is for objective-c's @synchronized statement. 257 /// Example: @synchronized (sem) { 258 /// do-something; 259 /// } 260 /// 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 getSourceRange()297 SourceRange getSourceRange() const { 298 return SourceRange(AtSynchronizedLoc, getSynchBody()->getLocEnd()); 299 } 300 classof(const Stmt * T)301 static bool classof(const Stmt *T) { 302 return T->getStmtClass() == ObjCAtSynchronizedStmtClass; 303 } classof(const ObjCAtSynchronizedStmt *)304 static bool classof(const ObjCAtSynchronizedStmt *) { return true; } 305 children()306 child_range children() { 307 return child_range(&SubStmts[0], &SubStmts[0]+END_EXPR); 308 } 309 }; 310 311 /// ObjCAtThrowStmt - This 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 getSourceRange()330 SourceRange getSourceRange() const { 331 if (Throw) 332 return SourceRange(AtThrowLoc, Throw->getLocEnd()); 333 else 334 return SourceRange(AtThrowLoc); 335 } 336 classof(const Stmt * T)337 static bool classof(const Stmt *T) { 338 return T->getStmtClass() == ObjCAtThrowStmtClass; 339 } classof(const ObjCAtThrowStmt *)340 static bool classof(const ObjCAtThrowStmt *) { return true; } 341 children()342 child_range children() { return child_range(&Throw, &Throw+1); } 343 }; 344 345 /// ObjCAutoreleasePoolStmt - This represent objective-c's 346 /// @autoreleasepool Statement 347 class ObjCAutoreleasePoolStmt : public Stmt { 348 Stmt *SubStmt; 349 SourceLocation AtLoc; 350 public: ObjCAutoreleasePoolStmt(SourceLocation atLoc,Stmt * subStmt)351 ObjCAutoreleasePoolStmt(SourceLocation atLoc, 352 Stmt *subStmt) 353 : Stmt(ObjCAutoreleasePoolStmtClass), 354 SubStmt(subStmt), AtLoc(atLoc) {} 355 ObjCAutoreleasePoolStmt(EmptyShell Empty)356 explicit ObjCAutoreleasePoolStmt(EmptyShell Empty) : 357 Stmt(ObjCAutoreleasePoolStmtClass, Empty) { } 358 getSubStmt()359 const Stmt *getSubStmt() const { return SubStmt; } getSubStmt()360 Stmt *getSubStmt() { return SubStmt; } setSubStmt(Stmt * S)361 void setSubStmt(Stmt *S) { SubStmt = S; } 362 getSourceRange()363 SourceRange getSourceRange() const { 364 return SourceRange(AtLoc, SubStmt->getLocEnd()); 365 } 366 getAtLoc()367 SourceLocation getAtLoc() const { return AtLoc; } setAtLoc(SourceLocation Loc)368 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; } 369 classof(const Stmt * T)370 static bool classof(const Stmt *T) { 371 return T->getStmtClass() == ObjCAutoreleasePoolStmtClass; 372 } classof(const ObjCAutoreleasePoolStmt *)373 static bool classof(const ObjCAutoreleasePoolStmt *) { return true; } 374 children()375 child_range children() { return child_range(&SubStmt, &SubStmt + 1); } 376 }; 377 378 } // end namespace clang 379 380 #endif 381