};
/// CompoundStmt - This represents a group of statements like { stmt stmt }.
-class CompoundStmt : public Stmt {
+class CompoundStmt final : public Stmt,
+ private llvm::TrailingObjects<CompoundStmt, Stmt *> {
friend class ASTStmtReader;
+ friend TrailingObjects;
- Stmt** Body = nullptr;
SourceLocation LBraceLoc, RBraceLoc;
+ CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB, SourceLocation RB);
+ explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {}
+
+ void setStmts(ArrayRef<Stmt *> Stmts);
+
public:
- CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
- SourceLocation LB, SourceLocation RB);
+ static CompoundStmt *Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
+ SourceLocation LB, SourceLocation RB);
// \brief Build an empty compound statement with a location.
explicit CompoundStmt(SourceLocation Loc)
}
// \brief Build an empty compound statement.
- explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {
- CompoundStmtBits.NumStmts = 0;
- }
-
- void setStmts(const ASTContext &C, ArrayRef<Stmt *> Stmts);
+ static CompoundStmt *CreateEmpty(const ASTContext &C, unsigned NumStmts);
bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
unsigned size() const { return CompoundStmtBits.NumStmts; }
using body_range = llvm::iterator_range<body_iterator>;
body_range body() { return body_range(body_begin(), body_end()); }
- body_iterator body_begin() { return Body; }
- body_iterator body_end() { return Body + size(); }
- Stmt *body_front() { return !body_empty() ? Body[0] : nullptr; }
- Stmt *body_back() { return !body_empty() ? Body[size()-1] : nullptr; }
+ body_iterator body_begin() { return getTrailingObjects<Stmt *>(); }
+ body_iterator body_end() { return body_begin() + size(); }
+ Stmt *body_front() { return !body_empty() ? body_begin()[0] : nullptr; }
+ Stmt *body_back() {
+ return !body_empty() ? body_begin()[size() - 1] : nullptr;
+ }
void setLastStmt(Stmt *S) {
assert(!body_empty() && "setLastStmt");
- Body[size()-1] = S;
+ body_begin()[size() - 1] = S;
}
using const_body_iterator = Stmt* const *;
return body_const_range(body_begin(), body_end());
}
- const_body_iterator body_begin() const { return Body; }
- const_body_iterator body_end() const { return Body + size(); }
+ const_body_iterator body_begin() const {
+ return getTrailingObjects<Stmt *>();
+ }
+ const_body_iterator body_end() const { return body_begin() + size(); }
const Stmt *body_front() const {
- return !body_empty() ? Body[0] : nullptr;
+ return !body_empty() ? body_begin()[0] : nullptr;
}
const Stmt *body_back() const {
- return !body_empty() ? Body[size() - 1] : nullptr;
+ return !body_empty() ? body_begin()[size() - 1] : nullptr;
}
using reverse_body_iterator = std::reverse_iterator<body_iterator>;
}
// Iterators
- child_range children() {
- return child_range(Body, Body + CompoundStmtBits.NumStmts);
- }
+ child_range children() { return child_range(body_begin(), body_end()); }
const_child_range children() const {
- return const_child_range(child_iterator(Body),
- child_iterator(Body + CompoundStmtBits.NumStmts));
+ return const_child_range(body_begin(), body_end());
}
};
SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
- return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
- ToStmts,
- ToLBraceLoc, ToRBraceLoc);
+ return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
+ ToRBraceLoc);
}
Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
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 {
}
CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) {
- return new (C) CompoundStmt(C, Stmts, SourceLocation(), SourceLocation());
+ return CompoundStmt::Create(C, Stmts, SourceLocation(), SourceLocation());
}
DeclRefExpr *ASTMaker::makeDeclRefExpr(
// Construct the body of the conversion function { return __invoke; }.
Expr *FunctionRef = BuildDeclRefExpr(Invoker, Invoker->getType(),
VK_LValue, Conv->getLocation()).get();
- assert(FunctionRef && "Can't refer to __invoke function?");
- Stmt *Return = BuildReturnStmt(Conv->getLocation(), FunctionRef).get();
- Conv->setBody(new (Context) CompoundStmt(Context, Return,
- Conv->getLocation(),
- Conv->getLocation()));
+ assert(FunctionRef && "Can't refer to __invoke function?");
+ Stmt *Return = BuildReturnStmt(Conv->getLocation(), FunctionRef).get();
+ Conv->setBody(CompoundStmt::Create(Context, Return, Conv->getLocation(),
+ Conv->getLocation()));
Conv->markUsed(Context);
Conv->setReferenced();
// Set the body of the conversion function.
Stmt *ReturnS = Return.get();
- Conv->setBody(new (Context) CompoundStmt(Context, ReturnS,
- Conv->getLocation(),
- Conv->getLocation()));
+ Conv->setBody(CompoundStmt::Create(Context, ReturnS, Conv->getLocation(),
+ Conv->getLocation()));
Conv->markUsed(Context);
// We're done; notify the mutation listener, if any.
// a StmtExpr; currently this is only used for asm statements.
// This is hacky, either create a new CXXStmtWithTemporaries statement or
// a new AsmStmtWithTemporaries.
- CompoundStmt *CompStmt = new (Context) CompoundStmt(Context, SubStmt,
- SourceLocation(),
- SourceLocation());
+ CompoundStmt *CompStmt = CompoundStmt::Create(
+ Context, SubStmt, SourceLocation(), SourceLocation());
Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(),
SourceLocation());
return MaybeCreateExprWithCleanups(E);
DiagnoseEmptyLoopBody(Elts[i], Elts[i + 1]);
}
- return new (Context) CompoundStmt(Context, Elts, L, R);
+ return CompoundStmt::Create(Context, Elts, L, R);
}
StmtResult
unsigned NumStmts = Record.readInt();
while (NumStmts--)
Stmts.push_back(Record.readSubStmt());
- S->setStmts(Record.getContext(), Stmts);
+ S->setStmts(Stmts);
S->LBraceLoc = ReadSourceLocation();
S->RBraceLoc = ReadSourceLocation();
}
break;
case STMT_COMPOUND:
- S = new (Context) CompoundStmt(Empty);
+ S = CompoundStmt::CreateEmpty(
+ Context, /*NumStmts=*/Record[ASTStmtReader::NumStmtFields]);
break;
case STMT_CASE: