StmtObjC.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //===--- StmtObjC.cpp - Classes for representing ObjC statements ---------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the subclesses of Stmt class declared in StmtObjC.h
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/AST/StmtObjC.h"
  13. #include "clang/AST/Expr.h"
  14. #include "clang/AST/ASTContext.h"
  15. using namespace clang;
  16. ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
  17. Stmt *Body, SourceLocation FCL,
  18. SourceLocation RPL)
  19. : Stmt(ObjCForCollectionStmtClass) {
  20. SubExprs[ELEM] = Elem;
  21. SubExprs[COLLECTION] = Collect;
  22. SubExprs[BODY] = Body;
  23. ForLoc = FCL;
  24. RParenLoc = RPL;
  25. }
  26. ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
  27. Stmt **CatchStmts, unsigned NumCatchStmts,
  28. Stmt *atFinallyStmt)
  29. : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
  30. NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != nullptr) {
  31. Stmt **Stmts = getStmts();
  32. Stmts[0] = atTryStmt;
  33. for (unsigned I = 0; I != NumCatchStmts; ++I)
  34. Stmts[I + 1] = CatchStmts[I];
  35. if (HasFinally)
  36. Stmts[NumCatchStmts + 1] = atFinallyStmt;
  37. }
  38. ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
  39. SourceLocation atTryLoc, Stmt *atTryStmt,
  40. Stmt **CatchStmts, unsigned NumCatchStmts,
  41. Stmt *atFinallyStmt) {
  42. unsigned Size =
  43. sizeof(ObjCAtTryStmt) +
  44. (1 + NumCatchStmts + (atFinallyStmt != nullptr)) * sizeof(Stmt *);
  45. void *Mem = Context.Allocate(Size, alignof(ObjCAtTryStmt));
  46. return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
  47. atFinallyStmt);
  48. }
  49. ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
  50. unsigned NumCatchStmts,
  51. bool HasFinally) {
  52. unsigned Size =
  53. sizeof(ObjCAtTryStmt) + (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
  54. void *Mem = Context.Allocate(Size, alignof(ObjCAtTryStmt));
  55. return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
  56. }
  57. SourceLocation ObjCAtTryStmt::getEndLoc() const {
  58. if (HasFinally)
  59. return getFinallyStmt()->getEndLoc();
  60. if (NumCatchStmts)
  61. return getCatchStmt(NumCatchStmts - 1)->getEndLoc();
  62. return getTryBody()->getEndLoc();
  63. }