1 //===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
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 //===----------------------------------------------------------------------===//
10 // This file implements the Stmt class and statement subclasses.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/ASTDiagnostic.h"
16 #include "clang/AST/ExprCXX.h"
17 #include "clang/AST/ExprObjC.h"
18 #include "clang/AST/Stmt.h"
19 #include "clang/AST/StmtCXX.h"
20 #include "clang/AST/StmtObjC.h"
21 #include "clang/AST/StmtOpenMP.h"
22 #include "clang/AST/Type.h"
23 #include "clang/Basic/CharInfo.h"
24 #include "clang/Basic/TargetInfo.h"
25 #include "clang/Lex/Token.h"
26 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/Support/raw_ostream.h"
28 using namespace clang;
30 static struct StmtClassNameTable {
34 } StmtClassInfo[Stmt::lastStmtConstant+1];
36 static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
37 static bool Initialized = false;
39 return StmtClassInfo[E];
41 // Intialize the table on the first use.
43 #define ABSTRACT_STMT(STMT)
44 #define STMT(CLASS, PARENT) \
45 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
46 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
47 #include "clang/AST/StmtNodes.inc"
49 return StmtClassInfo[E];
52 void *Stmt::operator new(size_t bytes, const ASTContext& C,
54 return ::operator new(bytes, C, alignment);
57 const char *Stmt::getStmtClassName() const {
58 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
61 void Stmt::PrintStats() {
62 // Ensure the table is primed.
63 getStmtInfoTableEntry(Stmt::NullStmtClass);
66 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
67 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
68 if (StmtClassInfo[i].Name == nullptr) continue;
69 sum += StmtClassInfo[i].Counter;
71 llvm::errs() << " " << sum << " stmts/exprs total.\n";
73 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
74 if (StmtClassInfo[i].Name == nullptr) continue;
75 if (StmtClassInfo[i].Counter == 0) continue;
76 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
77 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
78 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
80 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
83 llvm::errs() << "Total bytes = " << sum << "\n";
86 void Stmt::addStmtClass(StmtClass s) {
87 ++getStmtInfoTableEntry(s).Counter;
90 bool Stmt::StatisticsEnabled = false;
91 void Stmt::EnableStatistics() {
92 StatisticsEnabled = true;
95 Stmt *Stmt::IgnoreImplicit() {
98 if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s))
99 s = ewc->getSubExpr();
101 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s))
102 s = ice->getSubExpr();
107 /// \brief Strip off all label-like statements.
109 /// This will strip off label statements, case statements, attributed
110 /// statements and default statements recursively.
111 const Stmt *Stmt::stripLabelLikeStatements() const {
112 const Stmt *S = this;
114 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
115 S = LS->getSubStmt();
116 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
117 S = SC->getSubStmt();
118 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
119 S = AS->getSubStmt();
129 // These silly little functions have to be static inline to suppress
130 // unused warnings, and they have to be defined to suppress other
132 static inline good is_good(good) { return good(); }
134 typedef Stmt::child_range children_t();
135 template <class T> good implements_children(children_t T::*) {
138 LLVM_ATTRIBUTE_UNUSED
139 static inline bad implements_children(children_t Stmt::*) {
143 typedef SourceLocation getLocStart_t() const;
144 template <class T> good implements_getLocStart(getLocStart_t T::*) {
147 LLVM_ATTRIBUTE_UNUSED
148 static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
152 typedef SourceLocation getLocEnd_t() const;
153 template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
156 LLVM_ATTRIBUTE_UNUSED
157 static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
161 #define ASSERT_IMPLEMENTS_children(type) \
162 (void) is_good(implements_children(&type::children))
163 #define ASSERT_IMPLEMENTS_getLocStart(type) \
164 (void) is_good(implements_getLocStart(&type::getLocStart))
165 #define ASSERT_IMPLEMENTS_getLocEnd(type) \
166 (void) is_good(implements_getLocEnd(&type::getLocEnd))
169 /// Check whether the various Stmt classes implement their member
171 LLVM_ATTRIBUTE_UNUSED
172 static inline void check_implementations() {
173 #define ABSTRACT_STMT(type)
174 #define STMT(type, base) \
175 ASSERT_IMPLEMENTS_children(type); \
176 ASSERT_IMPLEMENTS_getLocStart(type); \
177 ASSERT_IMPLEMENTS_getLocEnd(type);
178 #include "clang/AST/StmtNodes.inc"
181 Stmt::child_range Stmt::children() {
182 switch (getStmtClass()) {
183 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
184 #define ABSTRACT_STMT(type)
185 #define STMT(type, base) \
186 case Stmt::type##Class: \
187 return static_cast<type*>(this)->children();
188 #include "clang/AST/StmtNodes.inc"
190 llvm_unreachable("unknown statement kind!");
193 // Amusing macro metaprogramming hack: check whether a class provides
194 // a more specific implementation of getSourceRange.
196 // See also Expr.cpp:getExprLoc().
198 /// This implementation is used when a class provides a custom
199 /// implementation of getSourceRange.
200 template <class S, class T>
201 SourceRange getSourceRangeImpl(const Stmt *stmt,
202 SourceRange (T::*v)() const) {
203 return static_cast<const S*>(stmt)->getSourceRange();
206 /// This implementation is used when a class doesn't provide a custom
207 /// implementation of getSourceRange. Overload resolution should pick it over
208 /// the implementation above because it's more specialized according to
209 /// function template partial ordering.
211 SourceRange getSourceRangeImpl(const Stmt *stmt,
212 SourceRange (Stmt::*v)() const) {
213 return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
214 static_cast<const S*>(stmt)->getLocEnd());
218 SourceRange Stmt::getSourceRange() const {
219 switch (getStmtClass()) {
220 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
221 #define ABSTRACT_STMT(type)
222 #define STMT(type, base) \
223 case Stmt::type##Class: \
224 return getSourceRangeImpl<type>(this, &type::getSourceRange);
225 #include "clang/AST/StmtNodes.inc"
227 llvm_unreachable("unknown statement kind!");
230 SourceLocation Stmt::getLocStart() const {
231 // llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
232 switch (getStmtClass()) {
233 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
234 #define ABSTRACT_STMT(type)
235 #define STMT(type, base) \
236 case Stmt::type##Class: \
237 return static_cast<const type*>(this)->getLocStart();
238 #include "clang/AST/StmtNodes.inc"
240 llvm_unreachable("unknown statement kind");
243 SourceLocation Stmt::getLocEnd() const {
244 switch (getStmtClass()) {
245 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
246 #define ABSTRACT_STMT(type)
247 #define STMT(type, base) \
248 case Stmt::type##Class: \
249 return static_cast<const type*>(this)->getLocEnd();
250 #include "clang/AST/StmtNodes.inc"
252 llvm_unreachable("unknown statement kind");
255 CompoundStmt::CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
256 SourceLocation LB, SourceLocation RB)
257 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
258 CompoundStmtBits.NumStmts = Stmts.size();
259 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
260 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
262 if (Stmts.size() == 0) {
267 Body = new (C) Stmt*[Stmts.size()];
268 std::copy(Stmts.begin(), Stmts.end(), Body);
271 void CompoundStmt::setStmts(const ASTContext &C, Stmt **Stmts,
275 this->CompoundStmtBits.NumStmts = NumStmts;
277 Body = new (C) Stmt*[NumStmts];
278 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
281 const char *LabelStmt::getName() const {
282 return getDecl()->getIdentifier()->getNameStart();
285 AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
286 ArrayRef<const Attr*> Attrs,
288 assert(!Attrs.empty() && "Attrs should not be empty");
289 void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * Attrs.size(),
290 llvm::alignOf<AttributedStmt>());
291 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
294 AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
296 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
297 void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * NumAttrs,
298 llvm::alignOf<AttributedStmt>());
299 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
302 std::string AsmStmt::generateAsmString(const ASTContext &C) const {
303 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
304 return gccAsmStmt->generateAsmString(C);
305 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
306 return msAsmStmt->generateAsmString(C);
307 llvm_unreachable("unknown asm statement kind!");
310 StringRef AsmStmt::getOutputConstraint(unsigned i) const {
311 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
312 return gccAsmStmt->getOutputConstraint(i);
313 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
314 return msAsmStmt->getOutputConstraint(i);
315 llvm_unreachable("unknown asm statement kind!");
318 const Expr *AsmStmt::getOutputExpr(unsigned i) const {
319 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
320 return gccAsmStmt->getOutputExpr(i);
321 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
322 return msAsmStmt->getOutputExpr(i);
323 llvm_unreachable("unknown asm statement kind!");
326 StringRef AsmStmt::getInputConstraint(unsigned i) const {
327 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
328 return gccAsmStmt->getInputConstraint(i);
329 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
330 return msAsmStmt->getInputConstraint(i);
331 llvm_unreachable("unknown asm statement kind!");
334 const Expr *AsmStmt::getInputExpr(unsigned i) const {
335 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
336 return gccAsmStmt->getInputExpr(i);
337 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
338 return msAsmStmt->getInputExpr(i);
339 llvm_unreachable("unknown asm statement kind!");
342 StringRef AsmStmt::getClobber(unsigned i) const {
343 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
344 return gccAsmStmt->getClobber(i);
345 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
346 return msAsmStmt->getClobber(i);
347 llvm_unreachable("unknown asm statement kind!");
350 /// getNumPlusOperands - Return the number of output operands that have a "+"
352 unsigned AsmStmt::getNumPlusOperands() const {
354 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
355 if (isOutputPlusConstraint(i))
360 StringRef GCCAsmStmt::getClobber(unsigned i) const {
361 return getClobberStringLiteral(i)->getString();
364 Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
365 return cast<Expr>(Exprs[i]);
368 /// getOutputConstraint - Return the constraint string for the specified
369 /// output operand. All output constraints are known to be non-empty (either
371 StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
372 return getOutputConstraintLiteral(i)->getString();
375 Expr *GCCAsmStmt::getInputExpr(unsigned i) {
376 return cast<Expr>(Exprs[i + NumOutputs]);
378 void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
379 Exprs[i + NumOutputs] = E;
382 /// getInputConstraint - Return the specified input constraint. Unlike output
383 /// constraints, these can be empty.
384 StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
385 return getInputConstraintLiteral(i)->getString();
388 void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
389 IdentifierInfo **Names,
390 StringLiteral **Constraints,
394 StringLiteral **Clobbers,
395 unsigned NumClobbers) {
396 this->NumOutputs = NumOutputs;
397 this->NumInputs = NumInputs;
398 this->NumClobbers = NumClobbers;
400 unsigned NumExprs = NumOutputs + NumInputs;
402 C.Deallocate(this->Names);
403 this->Names = new (C) IdentifierInfo*[NumExprs];
404 std::copy(Names, Names + NumExprs, this->Names);
406 C.Deallocate(this->Exprs);
407 this->Exprs = new (C) Stmt*[NumExprs];
408 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
410 C.Deallocate(this->Constraints);
411 this->Constraints = new (C) StringLiteral*[NumExprs];
412 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
414 C.Deallocate(this->Clobbers);
415 this->Clobbers = new (C) StringLiteral*[NumClobbers];
416 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
419 /// getNamedOperand - Given a symbolic operand reference like %[foo],
420 /// translate this into a numeric value needed to reference the same operand.
421 /// This returns -1 if the operand name is invalid.
422 int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
423 unsigned NumPlusOperands = 0;
425 // Check if this is an output operand.
426 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
427 if (getOutputName(i) == SymbolicName)
431 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
432 if (getInputName(i) == SymbolicName)
433 return getNumOutputs() + NumPlusOperands + i;
439 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
440 /// it into pieces. If the asm string is erroneous, emit errors and return
441 /// true, otherwise return false.
442 unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
443 const ASTContext &C, unsigned &DiagOffs) const {
444 StringRef Str = getAsmString()->getString();
445 const char *StrStart = Str.begin();
446 const char *StrEnd = Str.end();
447 const char *CurPtr = StrStart;
449 // "Simple" inline asms have no constraints or operands, just convert the asm
450 // string to escape $'s.
453 for (; CurPtr != StrEnd; ++CurPtr) {
463 Pieces.push_back(AsmStringPiece(Result));
467 // CurStringPiece - The current string that we are building up as we scan the
469 std::string CurStringPiece;
471 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
474 // Done with the string?
475 if (CurPtr == StrEnd) {
476 if (!CurStringPiece.empty())
477 Pieces.push_back(AsmStringPiece(CurStringPiece));
481 char CurChar = *CurPtr++;
483 case '$': CurStringPiece += "$$"; continue;
484 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
485 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
486 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
490 CurStringPiece += CurChar;
494 // Escaped "%" character in asm string.
495 if (CurPtr == StrEnd) {
496 // % at end of string is invalid (no escape).
497 DiagOffs = CurPtr-StrStart-1;
498 return diag::err_asm_invalid_escape;
501 char EscapedChar = *CurPtr++;
502 if (EscapedChar == '%') { // %% -> %
503 // Escaped percentage sign.
504 CurStringPiece += '%';
508 if (EscapedChar == '=') { // %= -> Generate an unique ID.
509 CurStringPiece += "${:uid}";
513 // Otherwise, we have an operand. If we have accumulated a string so far,
514 // add it to the Pieces list.
515 if (!CurStringPiece.empty()) {
516 Pieces.push_back(AsmStringPiece(CurStringPiece));
517 CurStringPiece.clear();
520 // Handle %x4 and %x[foo] by capturing x as the modifier character.
521 char Modifier = '\0';
522 if (isLetter(EscapedChar)) {
523 if (CurPtr == StrEnd) { // Premature end.
524 DiagOffs = CurPtr-StrStart-1;
525 return diag::err_asm_invalid_escape;
527 Modifier = EscapedChar;
528 EscapedChar = *CurPtr++;
531 if (isDigit(EscapedChar)) {
532 // %n - Assembler operand n
536 while (CurPtr != StrEnd && isDigit(*CurPtr))
537 N = N*10 + ((*CurPtr++)-'0');
539 unsigned NumOperands =
540 getNumOutputs() + getNumPlusOperands() + getNumInputs();
541 if (N >= NumOperands) {
542 DiagOffs = CurPtr-StrStart-1;
543 return diag::err_asm_invalid_operand_number;
546 Pieces.push_back(AsmStringPiece(N, Modifier));
550 // Handle %[foo], a symbolic operand reference.
551 if (EscapedChar == '[') {
552 DiagOffs = CurPtr-StrStart-1;
555 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
556 if (NameEnd == nullptr)
557 return diag::err_asm_unterminated_symbolic_operand_name;
558 if (NameEnd == CurPtr)
559 return diag::err_asm_empty_symbolic_operand_name;
561 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
563 int N = getNamedOperand(SymbolicName);
565 // Verify that an operand with that name exists.
566 DiagOffs = CurPtr-StrStart;
567 return diag::err_asm_unknown_symbolic_operand_name;
569 Pieces.push_back(AsmStringPiece(N, Modifier));
575 DiagOffs = CurPtr-StrStart-1;
576 return diag::err_asm_invalid_escape;
580 /// Assemble final IR asm string (GCC-style).
581 std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
582 // Analyze the asm string to decompose it into its pieces. We know that Sema
583 // has already done this, so it is guaranteed to be successful.
584 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
586 AnalyzeAsmString(Pieces, C, DiagOffs);
588 std::string AsmString;
589 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
590 if (Pieces[i].isString())
591 AsmString += Pieces[i].getString();
592 else if (Pieces[i].getModifier() == '\0')
593 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
595 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
596 Pieces[i].getModifier() + '}';
601 /// Assemble final IR asm string (MS-style).
602 std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
603 // FIXME: This needs to be translated into the IR string representation.
607 Expr *MSAsmStmt::getOutputExpr(unsigned i) {
608 return cast<Expr>(Exprs[i]);
611 Expr *MSAsmStmt::getInputExpr(unsigned i) {
612 return cast<Expr>(Exprs[i + NumOutputs]);
614 void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
615 Exprs[i + NumOutputs] = E;
618 QualType CXXCatchStmt::getCaughtType() const {
620 return ExceptionDecl->getType();
624 //===----------------------------------------------------------------------===//
626 //===----------------------------------------------------------------------===//
628 GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
629 bool issimple, bool isvolatile, unsigned numoutputs,
630 unsigned numinputs, IdentifierInfo **names,
631 StringLiteral **constraints, Expr **exprs,
632 StringLiteral *asmstr, unsigned numclobbers,
633 StringLiteral **clobbers, SourceLocation rparenloc)
634 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
635 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
637 unsigned NumExprs = NumOutputs + NumInputs;
639 Names = new (C) IdentifierInfo*[NumExprs];
640 std::copy(names, names + NumExprs, Names);
642 Exprs = new (C) Stmt*[NumExprs];
643 std::copy(exprs, exprs + NumExprs, Exprs);
645 Constraints = new (C) StringLiteral*[NumExprs];
646 std::copy(constraints, constraints + NumExprs, Constraints);
648 Clobbers = new (C) StringLiteral*[NumClobbers];
649 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
652 MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
653 SourceLocation lbraceloc, bool issimple, bool isvolatile,
654 ArrayRef<Token> asmtoks, unsigned numoutputs,
656 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
657 StringRef asmstr, ArrayRef<StringRef> clobbers,
658 SourceLocation endloc)
659 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
660 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
661 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
663 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
666 static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
667 size_t size = str.size();
668 char *buffer = new (C) char[size];
669 memcpy(buffer, str.data(), size);
670 return StringRef(buffer, size);
673 void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
674 ArrayRef<Token> asmtoks,
675 ArrayRef<StringRef> constraints,
676 ArrayRef<Expr*> exprs,
677 ArrayRef<StringRef> clobbers) {
678 assert(NumAsmToks == asmtoks.size());
679 assert(NumClobbers == clobbers.size());
681 unsigned NumExprs = exprs.size();
682 assert(NumExprs == NumOutputs + NumInputs);
683 assert(NumExprs == constraints.size());
685 AsmStr = copyIntoContext(C, asmstr);
687 Exprs = new (C) Stmt*[NumExprs];
688 for (unsigned i = 0, e = NumExprs; i != e; ++i)
691 AsmToks = new (C) Token[NumAsmToks];
692 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
693 AsmToks[i] = asmtoks[i];
695 Constraints = new (C) StringRef[NumExprs];
696 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
697 Constraints[i] = copyIntoContext(C, constraints[i]);
700 Clobbers = new (C) StringRef[NumClobbers];
701 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
702 // FIXME: Avoid the allocation/copy if at all possible.
703 Clobbers[i] = copyIntoContext(C, clobbers[i]);
707 ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
708 Stmt *Body, SourceLocation FCL,
710 : Stmt(ObjCForCollectionStmtClass) {
711 SubExprs[ELEM] = Elem;
712 SubExprs[COLLECTION] = Collect;
713 SubExprs[BODY] = Body;
718 ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
719 Stmt **CatchStmts, unsigned NumCatchStmts,
721 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
722 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != nullptr) {
723 Stmt **Stmts = getStmts();
724 Stmts[0] = atTryStmt;
725 for (unsigned I = 0; I != NumCatchStmts; ++I)
726 Stmts[I + 1] = CatchStmts[I];
729 Stmts[NumCatchStmts + 1] = atFinallyStmt;
732 ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
733 SourceLocation atTryLoc,
736 unsigned NumCatchStmts,
737 Stmt *atFinallyStmt) {
738 unsigned Size = sizeof(ObjCAtTryStmt) +
739 (1 + NumCatchStmts + (atFinallyStmt != nullptr)) * sizeof(Stmt *);
740 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
741 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
745 ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
746 unsigned NumCatchStmts,
748 unsigned Size = sizeof(ObjCAtTryStmt) +
749 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
750 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
751 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
754 SourceLocation ObjCAtTryStmt::getLocEnd() const {
756 return getFinallyStmt()->getLocEnd();
758 return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
759 return getTryBody()->getLocEnd();
762 CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
763 Stmt *tryBlock, ArrayRef<Stmt*> handlers) {
764 std::size_t Size = sizeof(CXXTryStmt);
765 Size += ((handlers.size() + 1) * sizeof(Stmt));
767 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
768 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
771 CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
772 unsigned numHandlers) {
773 std::size_t Size = sizeof(CXXTryStmt);
774 Size += ((numHandlers + 1) * sizeof(Stmt));
776 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
777 return new (Mem) CXXTryStmt(Empty, numHandlers);
780 CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
781 ArrayRef<Stmt*> handlers)
782 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
783 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
785 std::copy(handlers.begin(), handlers.end(), Stmts + 1);
788 CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
789 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
790 Stmt *Body, SourceLocation FL,
791 SourceLocation CL, SourceLocation RPL)
792 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
793 SubExprs[RANGE] = Range;
794 SubExprs[BEGINEND] = BeginEndStmt;
795 SubExprs[COND] = Cond;
797 SubExprs[LOOPVAR] = LoopVar;
798 SubExprs[BODY] = Body;
801 Expr *CXXForRangeStmt::getRangeInit() {
802 DeclStmt *RangeStmt = getRangeStmt();
803 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
804 assert(RangeDecl && "for-range should have a single var decl");
805 return RangeDecl->getInit();
808 const Expr *CXXForRangeStmt::getRangeInit() const {
809 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
812 VarDecl *CXXForRangeStmt::getLoopVariable() {
813 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
814 assert(LV && "No loop variable in CXXForRangeStmt");
815 return cast<VarDecl>(LV);
818 const VarDecl *CXXForRangeStmt::getLoopVariable() const {
819 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
822 IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
823 Stmt *then, SourceLocation EL, Stmt *elsev)
824 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
826 setConditionVariable(C, var);
827 SubExprs[COND] = cond;
828 SubExprs[THEN] = then;
829 SubExprs[ELSE] = elsev;
832 VarDecl *IfStmt::getConditionVariable() const {
836 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
837 return cast<VarDecl>(DS->getSingleDecl());
840 void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
842 SubExprs[VAR] = nullptr;
846 SourceRange VarRange = V->getSourceRange();
847 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
851 ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
852 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
854 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
856 SubExprs[INIT] = Init;
857 setConditionVariable(C, condVar);
858 SubExprs[COND] = Cond;
860 SubExprs[BODY] = Body;
863 VarDecl *ForStmt::getConditionVariable() const {
864 if (!SubExprs[CONDVAR])
867 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
868 return cast<VarDecl>(DS->getSingleDecl());
871 void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
873 SubExprs[CONDVAR] = nullptr;
877 SourceRange VarRange = V->getSourceRange();
878 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
882 SwitchStmt::SwitchStmt(const ASTContext &C, VarDecl *Var, Expr *cond)
883 : Stmt(SwitchStmtClass), FirstCase(nullptr), AllEnumCasesCovered(0)
885 setConditionVariable(C, Var);
886 SubExprs[COND] = cond;
887 SubExprs[BODY] = nullptr;
890 VarDecl *SwitchStmt::getConditionVariable() const {
894 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
895 return cast<VarDecl>(DS->getSingleDecl());
898 void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
900 SubExprs[VAR] = nullptr;
904 SourceRange VarRange = V->getSourceRange();
905 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
909 Stmt *SwitchCase::getSubStmt() {
910 if (isa<CaseStmt>(this))
911 return cast<CaseStmt>(this)->getSubStmt();
912 return cast<DefaultStmt>(this)->getSubStmt();
915 WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
917 : Stmt(WhileStmtClass) {
918 setConditionVariable(C, Var);
919 SubExprs[COND] = cond;
920 SubExprs[BODY] = body;
924 VarDecl *WhileStmt::getConditionVariable() const {
928 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
929 return cast<VarDecl>(DS->getSingleDecl());
932 void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
934 SubExprs[VAR] = nullptr;
938 SourceRange VarRange = V->getSourceRange();
939 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
944 LabelDecl *IndirectGotoStmt::getConstantTarget() {
945 if (AddrLabelExpr *E =
946 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
947 return E->getLabel();
952 const Expr* ReturnStmt::getRetValue() const {
953 return cast_or_null<Expr>(RetExpr);
955 Expr* ReturnStmt::getRetValue() {
956 return cast_or_null<Expr>(RetExpr);
959 SEHTryStmt::SEHTryStmt(bool IsCXXTry, SourceLocation TryLoc, Stmt *TryBlock,
960 Stmt *Handler, int HandlerIndex, int HandlerParentIndex)
961 : Stmt(SEHTryStmtClass), IsCXXTry(IsCXXTry), TryLoc(TryLoc),
962 HandlerIndex(HandlerIndex), HandlerParentIndex(HandlerParentIndex) {
963 Children[TRY] = TryBlock;
964 Children[HANDLER] = Handler;
967 SEHTryStmt *SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
968 SourceLocation TryLoc, Stmt *TryBlock,
969 Stmt *Handler, int HandlerIndex,
970 int HandlerParentIndex) {
971 return new (C) SEHTryStmt(IsCXXTry, TryLoc, TryBlock, Handler, HandlerIndex,
975 SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
976 return dyn_cast<SEHExceptStmt>(getHandler());
979 SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
980 return dyn_cast<SEHFinallyStmt>(getHandler());
983 SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
986 : Stmt(SEHExceptStmtClass),
989 Children[FILTER_EXPR] = FilterExpr;
990 Children[BLOCK] = Block;
993 SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
994 Expr *FilterExpr, Stmt *Block) {
995 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
998 SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1000 : Stmt(SEHFinallyStmtClass),
1005 SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
1007 return new(C)SEHFinallyStmt(Loc,Block);
1010 CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1011 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1013 // Offset of the first Capture object.
1014 unsigned FirstCaptureOffset =
1015 llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1017 return reinterpret_cast<Capture *>(
1018 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1019 + FirstCaptureOffset);
1022 CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1023 ArrayRef<Capture> Captures,
1024 ArrayRef<Expr *> CaptureInits,
1027 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
1028 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
1029 assert( S && "null captured statement");
1030 assert(CD && "null captured declaration for captured statement");
1031 assert(RD && "null record declaration for captured statement");
1033 // Copy initialization expressions.
1034 Stmt **Stored = getStoredStmts();
1035 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1036 *Stored++ = CaptureInits[I];
1038 // Copy the statement being captured.
1041 // Copy all Capture objects.
1042 Capture *Buffer = getStoredCaptures();
1043 std::copy(Captures.begin(), Captures.end(), Buffer);
1046 CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1047 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
1048 CapDeclAndKind(nullptr, CR_Default), TheRecordDecl(nullptr) {
1049 getStoredStmts()[NumCaptures] = nullptr;
1052 CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
1053 CapturedRegionKind Kind,
1054 ArrayRef<Capture> Captures,
1055 ArrayRef<Expr *> CaptureInits,
1060 // -----------------------------------------------------------
1061 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1062 // ----------------^-------------------^----------------------
1063 // getStoredStmts() getStoredCaptures()
1065 // where S is the statement being captured.
1067 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1069 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1070 if (!Captures.empty()) {
1071 // Realign for the following Capture array.
1072 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1073 Size += sizeof(Capture) * Captures.size();
1076 void *Mem = Context.Allocate(Size);
1077 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
1080 CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
1081 unsigned NumCaptures) {
1082 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1083 if (NumCaptures > 0) {
1084 // Realign for the following Capture array.
1085 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1086 Size += sizeof(Capture) * NumCaptures;
1089 void *Mem = Context.Allocate(Size);
1090 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1093 Stmt::child_range CapturedStmt::children() {
1094 // Children are captured field initilizers.
1095 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
1098 bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
1099 for (const auto &I : captures()) {
1100 if (!I.capturesVariable())
1103 // This does not handle variable redeclarations. This should be
1104 // extended to capture variables with redeclarations, for example
1105 // a thread-private variable in OpenMP.
1106 if (I.getCapturedVar() == Var)
1113 StmtRange OMPClause::children() {
1114 switch(getClauseKind()) {
1116 #define OPENMP_CLAUSE(Name, Class) \
1117 case OMPC_ ## Name : return static_cast<Class *>(this)->children();
1118 #include "clang/Basic/OpenMPKinds.def"
1120 llvm_unreachable("unknown OMPClause");
1123 OMPPrivateClause *OMPPrivateClause::Create(const ASTContext &C,
1124 SourceLocation StartLoc,
1125 SourceLocation LParenLoc,
1126 SourceLocation EndLoc,
1127 ArrayRef<Expr *> VL) {
1128 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
1129 llvm::alignOf<Expr *>()) +
1130 sizeof(Expr *) * VL.size());
1131 OMPPrivateClause *Clause = new (Mem) OMPPrivateClause(StartLoc, LParenLoc,
1133 Clause->setVarRefs(VL);
1137 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
1139 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
1140 llvm::alignOf<Expr *>()) +
1141 sizeof(Expr *) * N);
1142 return new (Mem) OMPPrivateClause(N);
1145 OMPFirstprivateClause *OMPFirstprivateClause::Create(const ASTContext &C,
1146 SourceLocation StartLoc,
1147 SourceLocation LParenLoc,
1148 SourceLocation EndLoc,
1149 ArrayRef<Expr *> VL) {
1150 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
1151 llvm::alignOf<Expr *>()) +
1152 sizeof(Expr *) * VL.size());
1153 OMPFirstprivateClause *Clause = new (Mem) OMPFirstprivateClause(StartLoc,
1157 Clause->setVarRefs(VL);
1161 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
1163 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
1164 llvm::alignOf<Expr *>()) +
1165 sizeof(Expr *) * N);
1166 return new (Mem) OMPFirstprivateClause(N);
1169 OMPLastprivateClause *OMPLastprivateClause::Create(const ASTContext &C,
1170 SourceLocation StartLoc,
1171 SourceLocation LParenLoc,
1172 SourceLocation EndLoc,
1173 ArrayRef<Expr *> VL) {
1174 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLastprivateClause),
1175 llvm::alignOf<Expr *>()) +
1176 sizeof(Expr *) * VL.size());
1177 OMPLastprivateClause *Clause =
1178 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1179 Clause->setVarRefs(VL);
1183 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
1185 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLastprivateClause),
1186 llvm::alignOf<Expr *>()) +
1187 sizeof(Expr *) * N);
1188 return new (Mem) OMPLastprivateClause(N);
1191 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
1192 SourceLocation StartLoc,
1193 SourceLocation LParenLoc,
1194 SourceLocation EndLoc,
1195 ArrayRef<Expr *> VL) {
1196 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
1197 llvm::alignOf<Expr *>()) +
1198 sizeof(Expr *) * VL.size());
1199 OMPSharedClause *Clause = new (Mem) OMPSharedClause(StartLoc, LParenLoc,
1201 Clause->setVarRefs(VL);
1205 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C,
1207 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
1208 llvm::alignOf<Expr *>()) +
1209 sizeof(Expr *) * N);
1210 return new (Mem) OMPSharedClause(N);
1213 OMPLinearClause *OMPLinearClause::Create(const ASTContext &C,
1214 SourceLocation StartLoc,
1215 SourceLocation LParenLoc,
1216 SourceLocation ColonLoc,
1217 SourceLocation EndLoc,
1218 ArrayRef<Expr *> VL, Expr *Step) {
1219 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLinearClause),
1220 llvm::alignOf<Expr *>()) +
1221 sizeof(Expr *) * (VL.size() + 1));
1222 OMPLinearClause *Clause = new (Mem)
1223 OMPLinearClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
1224 Clause->setVarRefs(VL);
1225 Clause->setStep(Step);
1229 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
1231 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLinearClause),
1232 llvm::alignOf<Expr *>()) +
1233 sizeof(Expr *) * (NumVars + 1));
1234 return new (Mem) OMPLinearClause(NumVars);
1238 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
1239 SourceLocation LParenLoc, SourceLocation ColonLoc,
1240 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
1241 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPAlignedClause),
1242 llvm::alignOf<Expr *>()) +
1243 sizeof(Expr *) * (VL.size() + 1));
1244 OMPAlignedClause *Clause = new (Mem)
1245 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
1246 Clause->setVarRefs(VL);
1247 Clause->setAlignment(A);
1251 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
1253 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPAlignedClause),
1254 llvm::alignOf<Expr *>()) +
1255 sizeof(Expr *) * (NumVars + 1));
1256 return new (Mem) OMPAlignedClause(NumVars);
1259 OMPCopyinClause *OMPCopyinClause::Create(const ASTContext &C,
1260 SourceLocation StartLoc,
1261 SourceLocation LParenLoc,
1262 SourceLocation EndLoc,
1263 ArrayRef<Expr *> VL) {
1264 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyinClause),
1265 llvm::alignOf<Expr *>()) +
1266 sizeof(Expr *) * VL.size());
1267 OMPCopyinClause *Clause = new (Mem) OMPCopyinClause(StartLoc, LParenLoc,
1269 Clause->setVarRefs(VL);
1273 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C,
1275 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyinClause),
1276 llvm::alignOf<Expr *>()) +
1277 sizeof(Expr *) * N);
1278 return new (Mem) OMPCopyinClause(N);
1281 OMPCopyprivateClause *OMPCopyprivateClause::Create(const ASTContext &C,
1282 SourceLocation StartLoc,
1283 SourceLocation LParenLoc,
1284 SourceLocation EndLoc,
1285 ArrayRef<Expr *> VL) {
1286 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyprivateClause),
1287 llvm::alignOf<Expr *>()) +
1288 sizeof(Expr *) * VL.size());
1289 OMPCopyprivateClause *Clause =
1290 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1291 Clause->setVarRefs(VL);
1295 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
1297 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyprivateClause),
1298 llvm::alignOf<Expr *>()) +
1299 sizeof(Expr *) * N);
1300 return new (Mem) OMPCopyprivateClause(N);
1303 void OMPExecutableDirective::setClauses(ArrayRef<OMPClause *> Clauses) {
1304 assert(Clauses.size() == getNumClauses() &&
1305 "Number of clauses is not the same as the preallocated buffer");
1306 std::copy(Clauses.begin(), Clauses.end(), getClauses().begin());
1309 OMPReductionClause *OMPReductionClause::Create(
1310 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1311 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
1312 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo) {
1313 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPReductionClause),
1314 llvm::alignOf<Expr *>()) +
1315 sizeof(Expr *) * VL.size());
1316 OMPReductionClause *Clause = new (Mem) OMPReductionClause(
1317 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
1318 Clause->setVarRefs(VL);
1322 OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
1324 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPReductionClause),
1325 llvm::alignOf<Expr *>()) +
1326 sizeof(Expr *) * N);
1327 return new (Mem) OMPReductionClause(N);
1330 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
1331 SourceLocation StartLoc,
1332 SourceLocation LParenLoc,
1333 SourceLocation EndLoc,
1334 ArrayRef<Expr *> VL) {
1335 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFlushClause),
1336 llvm::alignOf<Expr *>()) +
1337 sizeof(Expr *) * VL.size());
1338 OMPFlushClause *Clause =
1339 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
1340 Clause->setVarRefs(VL);
1344 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
1345 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFlushClause),
1346 llvm::alignOf<Expr *>()) +
1347 sizeof(Expr *) * N);
1348 return new (Mem) OMPFlushClause(N);
1351 OMPParallelDirective *OMPParallelDirective::Create(
1352 const ASTContext &C,
1353 SourceLocation StartLoc,
1354 SourceLocation EndLoc,
1355 ArrayRef<OMPClause *> Clauses,
1356 Stmt *AssociatedStmt) {
1357 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
1358 llvm::alignOf<OMPClause *>());
1359 void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1361 OMPParallelDirective *Dir = new (Mem) OMPParallelDirective(StartLoc, EndLoc,
1363 Dir->setClauses(Clauses);
1364 Dir->setAssociatedStmt(AssociatedStmt);
1368 OMPParallelDirective *OMPParallelDirective::CreateEmpty(const ASTContext &C,
1369 unsigned NumClauses,
1371 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
1372 llvm::alignOf<OMPClause *>());
1373 void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1375 return new (Mem) OMPParallelDirective(NumClauses);
1379 OMPSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1380 SourceLocation EndLoc, unsigned CollapsedNum,
1381 ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1382 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSimdDirective),
1383 llvm::alignOf<OMPClause *>());
1385 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1386 OMPSimdDirective *Dir = new (Mem)
1387 OMPSimdDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1388 Dir->setClauses(Clauses);
1389 Dir->setAssociatedStmt(AssociatedStmt);
1393 OMPSimdDirective *OMPSimdDirective::CreateEmpty(const ASTContext &C,
1394 unsigned NumClauses,
1395 unsigned CollapsedNum,
1397 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSimdDirective),
1398 llvm::alignOf<OMPClause *>());
1400 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1401 return new (Mem) OMPSimdDirective(CollapsedNum, NumClauses);
1405 OMPForDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1406 SourceLocation EndLoc, unsigned CollapsedNum,
1407 ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1408 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForDirective),
1409 llvm::alignOf<OMPClause *>());
1411 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1412 OMPForDirective *Dir =
1413 new (Mem) OMPForDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1414 Dir->setClauses(Clauses);
1415 Dir->setAssociatedStmt(AssociatedStmt);
1419 OMPForDirective *OMPForDirective::CreateEmpty(const ASTContext &C,
1420 unsigned NumClauses,
1421 unsigned CollapsedNum,
1423 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForDirective),
1424 llvm::alignOf<OMPClause *>());
1426 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1427 return new (Mem) OMPForDirective(CollapsedNum, NumClauses);
1430 OMPSectionsDirective *OMPSectionsDirective::Create(
1431 const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1432 ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1433 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1434 llvm::alignOf<OMPClause *>());
1436 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1437 OMPSectionsDirective *Dir =
1438 new (Mem) OMPSectionsDirective(StartLoc, EndLoc, Clauses.size());
1439 Dir->setClauses(Clauses);
1440 Dir->setAssociatedStmt(AssociatedStmt);
1444 OMPSectionsDirective *OMPSectionsDirective::CreateEmpty(const ASTContext &C,
1445 unsigned NumClauses,
1447 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1448 llvm::alignOf<OMPClause *>());
1450 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1451 return new (Mem) OMPSectionsDirective(NumClauses);
1454 OMPSectionDirective *OMPSectionDirective::Create(const ASTContext &C,
1455 SourceLocation StartLoc,
1456 SourceLocation EndLoc,
1457 Stmt *AssociatedStmt) {
1458 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1459 llvm::alignOf<Stmt *>());
1460 void *Mem = C.Allocate(Size + sizeof(Stmt *));
1461 OMPSectionDirective *Dir = new (Mem) OMPSectionDirective(StartLoc, EndLoc);
1462 Dir->setAssociatedStmt(AssociatedStmt);
1466 OMPSectionDirective *OMPSectionDirective::CreateEmpty(const ASTContext &C,
1468 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionDirective),
1469 llvm::alignOf<Stmt *>());
1470 void *Mem = C.Allocate(Size + sizeof(Stmt *));
1471 return new (Mem) OMPSectionDirective();
1474 OMPSingleDirective *OMPSingleDirective::Create(const ASTContext &C,
1475 SourceLocation StartLoc,
1476 SourceLocation EndLoc,
1477 ArrayRef<OMPClause *> Clauses,
1478 Stmt *AssociatedStmt) {
1479 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSingleDirective),
1480 llvm::alignOf<OMPClause *>());
1482 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1483 OMPSingleDirective *Dir =
1484 new (Mem) OMPSingleDirective(StartLoc, EndLoc, Clauses.size());
1485 Dir->setClauses(Clauses);
1486 Dir->setAssociatedStmt(AssociatedStmt);
1490 OMPSingleDirective *OMPSingleDirective::CreateEmpty(const ASTContext &C,
1491 unsigned NumClauses,
1493 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSingleDirective),
1494 llvm::alignOf<OMPClause *>());
1496 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1497 return new (Mem) OMPSingleDirective(NumClauses);
1500 OMPMasterDirective *OMPMasterDirective::Create(const ASTContext &C,
1501 SourceLocation StartLoc,
1502 SourceLocation EndLoc,
1503 Stmt *AssociatedStmt) {
1504 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPMasterDirective),
1505 llvm::alignOf<Stmt *>());
1506 void *Mem = C.Allocate(Size + sizeof(Stmt *));
1507 OMPMasterDirective *Dir = new (Mem) OMPMasterDirective(StartLoc, EndLoc);
1508 Dir->setAssociatedStmt(AssociatedStmt);
1512 OMPMasterDirective *OMPMasterDirective::CreateEmpty(const ASTContext &C,
1514 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPMasterDirective),
1515 llvm::alignOf<Stmt *>());
1516 void *Mem = C.Allocate(Size + sizeof(Stmt *));
1517 return new (Mem) OMPMasterDirective();
1520 OMPCriticalDirective *OMPCriticalDirective::Create(
1521 const ASTContext &C, const DeclarationNameInfo &Name,
1522 SourceLocation StartLoc, SourceLocation EndLoc, Stmt *AssociatedStmt) {
1523 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCriticalDirective),
1524 llvm::alignOf<Stmt *>());
1525 void *Mem = C.Allocate(Size + sizeof(Stmt *));
1526 OMPCriticalDirective *Dir =
1527 new (Mem) OMPCriticalDirective(Name, StartLoc, EndLoc);
1528 Dir->setAssociatedStmt(AssociatedStmt);
1532 OMPCriticalDirective *OMPCriticalDirective::CreateEmpty(const ASTContext &C,
1534 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCriticalDirective),
1535 llvm::alignOf<Stmt *>());
1536 void *Mem = C.Allocate(Size + sizeof(Stmt *));
1537 return new (Mem) OMPCriticalDirective();
1540 OMPParallelForDirective *
1541 OMPParallelForDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1542 SourceLocation EndLoc, unsigned CollapsedNum,
1543 ArrayRef<OMPClause *> Clauses,
1544 Stmt *AssociatedStmt) {
1545 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForDirective),
1546 llvm::alignOf<OMPClause *>());
1548 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1549 OMPParallelForDirective *Dir = new (Mem)
1550 OMPParallelForDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1551 Dir->setClauses(Clauses);
1552 Dir->setAssociatedStmt(AssociatedStmt);
1556 OMPParallelForDirective *
1557 OMPParallelForDirective::CreateEmpty(const ASTContext &C, unsigned NumClauses,
1558 unsigned CollapsedNum, EmptyShell) {
1559 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForDirective),
1560 llvm::alignOf<OMPClause *>());
1562 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1563 return new (Mem) OMPParallelForDirective(CollapsedNum, NumClauses);
1566 OMPParallelSectionsDirective *OMPParallelSectionsDirective::Create(
1567 const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1568 ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1569 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelSectionsDirective),
1570 llvm::alignOf<OMPClause *>());
1572 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1573 OMPParallelSectionsDirective *Dir =
1574 new (Mem) OMPParallelSectionsDirective(StartLoc, EndLoc, Clauses.size());
1575 Dir->setClauses(Clauses);
1576 Dir->setAssociatedStmt(AssociatedStmt);
1580 OMPParallelSectionsDirective *
1581 OMPParallelSectionsDirective::CreateEmpty(const ASTContext &C,
1582 unsigned NumClauses, EmptyShell) {
1583 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelSectionsDirective),
1584 llvm::alignOf<OMPClause *>());
1586 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1587 return new (Mem) OMPParallelSectionsDirective(NumClauses);
1590 OMPTaskDirective *OMPTaskDirective::Create(const ASTContext &C,
1591 SourceLocation StartLoc,
1592 SourceLocation EndLoc,
1593 ArrayRef<OMPClause *> Clauses,
1594 Stmt *AssociatedStmt) {
1595 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskDirective),
1596 llvm::alignOf<OMPClause *>());
1598 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1599 OMPTaskDirective *Dir =
1600 new (Mem) OMPTaskDirective(StartLoc, EndLoc, Clauses.size());
1601 Dir->setClauses(Clauses);
1602 Dir->setAssociatedStmt(AssociatedStmt);
1606 OMPTaskDirective *OMPTaskDirective::CreateEmpty(const ASTContext &C,
1607 unsigned NumClauses,
1609 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskDirective),
1610 llvm::alignOf<OMPClause *>());
1612 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1613 return new (Mem) OMPTaskDirective(NumClauses);
1616 OMPTaskyieldDirective *OMPTaskyieldDirective::Create(const ASTContext &C,
1617 SourceLocation StartLoc,
1618 SourceLocation EndLoc) {
1619 void *Mem = C.Allocate(sizeof(OMPTaskyieldDirective));
1620 OMPTaskyieldDirective *Dir =
1621 new (Mem) OMPTaskyieldDirective(StartLoc, EndLoc);
1625 OMPTaskyieldDirective *OMPTaskyieldDirective::CreateEmpty(const ASTContext &C,
1627 void *Mem = C.Allocate(sizeof(OMPTaskyieldDirective));
1628 return new (Mem) OMPTaskyieldDirective();
1631 OMPBarrierDirective *OMPBarrierDirective::Create(const ASTContext &C,
1632 SourceLocation StartLoc,
1633 SourceLocation EndLoc) {
1634 void *Mem = C.Allocate(sizeof(OMPBarrierDirective));
1635 OMPBarrierDirective *Dir = new (Mem) OMPBarrierDirective(StartLoc, EndLoc);
1639 OMPBarrierDirective *OMPBarrierDirective::CreateEmpty(const ASTContext &C,
1641 void *Mem = C.Allocate(sizeof(OMPBarrierDirective));
1642 return new (Mem) OMPBarrierDirective();
1645 OMPTaskwaitDirective *OMPTaskwaitDirective::Create(const ASTContext &C,
1646 SourceLocation StartLoc,
1647 SourceLocation EndLoc) {
1648 void *Mem = C.Allocate(sizeof(OMPTaskwaitDirective));
1649 OMPTaskwaitDirective *Dir = new (Mem) OMPTaskwaitDirective(StartLoc, EndLoc);
1653 OMPTaskwaitDirective *OMPTaskwaitDirective::CreateEmpty(const ASTContext &C,
1655 void *Mem = C.Allocate(sizeof(OMPTaskwaitDirective));
1656 return new (Mem) OMPTaskwaitDirective();
1659 OMPFlushDirective *OMPFlushDirective::Create(const ASTContext &C,
1660 SourceLocation StartLoc,
1661 SourceLocation EndLoc,
1662 ArrayRef<OMPClause *> Clauses) {
1663 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPFlushDirective),
1664 llvm::alignOf<OMPClause *>());
1665 void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size());
1666 OMPFlushDirective *Dir =
1667 new (Mem) OMPFlushDirective(StartLoc, EndLoc, Clauses.size());
1668 Dir->setClauses(Clauses);
1672 OMPFlushDirective *OMPFlushDirective::CreateEmpty(const ASTContext &C,
1673 unsigned NumClauses,
1675 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPFlushDirective),
1676 llvm::alignOf<OMPClause *>());
1677 void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses);
1678 return new (Mem) OMPFlushDirective(NumClauses);