diff options
Diffstat (limited to 'lib/AST/Stmt.cpp')
-rw-r--r-- | lib/AST/Stmt.cpp | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/lib/AST/Stmt.cpp b/lib/AST/Stmt.cpp index 8d240c1336abd..982fd458493fc 100644 --- a/lib/AST/Stmt.cpp +++ b/lib/AST/Stmt.cpp @@ -299,31 +299,34 @@ SourceLocation Stmt::getLocEnd() const { llvm_unreachable("unknown statement kind"); } -CompoundStmt::CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts, - SourceLocation LB, SourceLocation RB) - : Stmt(CompoundStmtClass), LBraceLoc(LB), RBraceLoc(RB) { +CompoundStmt::CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB, + SourceLocation RB) + : Stmt(CompoundStmtClass), LBraceLoc(LB), RBraceLoc(RB) { CompoundStmtBits.NumStmts = Stmts.size(); + setStmts(Stmts); +} + +void CompoundStmt::setStmts(ArrayRef<Stmt *> Stmts) { assert(CompoundStmtBits.NumStmts == Stmts.size() && "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!"); - if (Stmts.empty()) { - Body = nullptr; - return; - } - - Body = new (C) Stmt*[Stmts.size()]; - std::copy(Stmts.begin(), Stmts.end(), Body); + std::copy(Stmts.begin(), Stmts.end(), body_begin()); } -void CompoundStmt::setStmts(const ASTContext &C, ArrayRef<Stmt *> Stmts) { - if (Body) - C.Deallocate(Body); - CompoundStmtBits.NumStmts = Stmts.size(); - assert(CompoundStmtBits.NumStmts == Stmts.size() && - "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!"); +CompoundStmt *CompoundStmt::Create(const ASTContext &C, ArrayRef<Stmt *> Stmts, + SourceLocation LB, SourceLocation RB) { + void *Mem = + C.Allocate(totalSizeToAlloc<Stmt *>(Stmts.size()), alignof(CompoundStmt)); + return new (Mem) CompoundStmt(Stmts, LB, RB); +} - Body = new (C) Stmt*[Stmts.size()]; - std::copy(Stmts.begin(), Stmts.end(), Body); +CompoundStmt *CompoundStmt::CreateEmpty(const ASTContext &C, + unsigned NumStmts) { + void *Mem = + C.Allocate(totalSizeToAlloc<Stmt *>(NumStmts), alignof(CompoundStmt)); + CompoundStmt *New = new (Mem) CompoundStmt(EmptyShell()); + New->CompoundStmtBits.NumStmts = NumStmts; + return New; } const char *LabelStmt::getName() const { @@ -334,7 +337,7 @@ AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt) { assert(!Attrs.empty() && "Attrs should not be empty"); - void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * Attrs.size(), + void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(Attrs.size()), alignof(AttributedStmt)); return new (Mem) AttributedStmt(Loc, Attrs, SubStmt); } @@ -342,7 +345,7 @@ AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc, AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C, unsigned NumAttrs) { assert(NumAttrs > 0 && "NumAttrs should be greater than zero"); - void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * NumAttrs, + void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(NumAttrs), alignof(AttributedStmt)); return new (Mem) AttributedStmt(EmptyShell(), NumAttrs); } |