xref: /llvm-project-15.0.7/clang/lib/AST/Stmt.cpp (revision dd55b95f)
1 //===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
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 implements the Stmt class and statement subclasses.
11 //
12 //===----------------------------------------------------------------------===//
13 
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;
29 
30 static struct StmtClassNameTable {
31   const char *Name;
32   unsigned Counter;
33   unsigned Size;
34 } StmtClassInfo[Stmt::lastStmtConstant+1];
35 
36 static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
37   static bool Initialized = false;
38   if (Initialized)
39     return StmtClassInfo[E];
40 
41   // Intialize the table on the first use.
42   Initialized = true;
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"
48 
49   return StmtClassInfo[E];
50 }
51 
52 void *Stmt::operator new(size_t bytes, const ASTContext& C,
53                          unsigned alignment) {
54   return ::operator new(bytes, C, alignment);
55 }
56 
57 const char *Stmt::getStmtClassName() const {
58   return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
59 }
60 
61 void Stmt::PrintStats() {
62   // Ensure the table is primed.
63   getStmtInfoTableEntry(Stmt::NullStmtClass);
64 
65   unsigned sum = 0;
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;
70   }
71   llvm::errs() << "  " << sum << " stmts/exprs total.\n";
72   sum = 0;
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
79                  << " bytes)\n";
80     sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
81   }
82 
83   llvm::errs() << "Total bytes = " << sum << "\n";
84 }
85 
86 void Stmt::addStmtClass(StmtClass s) {
87   ++getStmtInfoTableEntry(s).Counter;
88 }
89 
90 bool Stmt::StatisticsEnabled = false;
91 void Stmt::EnableStatistics() {
92   StatisticsEnabled = true;
93 }
94 
95 Stmt *Stmt::IgnoreImplicit() {
96   Stmt *s = this;
97 
98   if (auto *ewc = dyn_cast<ExprWithCleanups>(s))
99     s = ewc->getSubExpr();
100 
101   if (auto *mte = dyn_cast<MaterializeTemporaryExpr>(s))
102     s = mte->GetTemporaryExpr();
103 
104   if (auto *bte = dyn_cast<CXXBindTemporaryExpr>(s))
105     s = bte->getSubExpr();
106 
107   while (auto *ice = dyn_cast<ImplicitCastExpr>(s))
108     s = ice->getSubExpr();
109 
110   return s;
111 }
112 
113 /// \brief Skip no-op (attributed, compound) container stmts and skip captured
114 /// stmt at the top, if \a IgnoreCaptured is true.
115 Stmt *Stmt::IgnoreContainers(bool IgnoreCaptured) {
116   Stmt *S = this;
117   if (IgnoreCaptured)
118     if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
119       S = CapS->getCapturedStmt();
120   while (true) {
121     if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
122       S = AS->getSubStmt();
123     else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
124       if (CS->size() != 1)
125         break;
126       S = CS->body_back();
127     } else
128       break;
129   }
130   return S;
131 }
132 
133 /// \brief Strip off all label-like statements.
134 ///
135 /// This will strip off label statements, case statements, attributed
136 /// statements and default statements recursively.
137 const Stmt *Stmt::stripLabelLikeStatements() const {
138   const Stmt *S = this;
139   while (true) {
140     if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
141       S = LS->getSubStmt();
142     else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
143       S = SC->getSubStmt();
144     else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
145       S = AS->getSubStmt();
146     else
147       return S;
148   }
149 }
150 
151 namespace {
152   struct good {};
153   struct bad {};
154 
155   // These silly little functions have to be static inline to suppress
156   // unused warnings, and they have to be defined to suppress other
157   // warnings.
158   static inline good is_good(good) { return good(); }
159 
160   typedef Stmt::child_range children_t();
161   template <class T> good implements_children(children_t T::*) {
162     return good();
163   }
164   LLVM_ATTRIBUTE_UNUSED
165   static inline bad implements_children(children_t Stmt::*) {
166     return bad();
167   }
168 
169   typedef SourceLocation getLocStart_t() const;
170   template <class T> good implements_getLocStart(getLocStart_t T::*) {
171     return good();
172   }
173   LLVM_ATTRIBUTE_UNUSED
174   static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
175     return bad();
176   }
177 
178   typedef SourceLocation getLocEnd_t() const;
179   template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
180     return good();
181   }
182   LLVM_ATTRIBUTE_UNUSED
183   static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
184     return bad();
185   }
186 
187 #define ASSERT_IMPLEMENTS_children(type) \
188   (void) is_good(implements_children(&type::children))
189 #define ASSERT_IMPLEMENTS_getLocStart(type) \
190   (void) is_good(implements_getLocStart(&type::getLocStart))
191 #define ASSERT_IMPLEMENTS_getLocEnd(type) \
192   (void) is_good(implements_getLocEnd(&type::getLocEnd))
193 }
194 
195 /// Check whether the various Stmt classes implement their member
196 /// functions.
197 LLVM_ATTRIBUTE_UNUSED
198 static inline void check_implementations() {
199 #define ABSTRACT_STMT(type)
200 #define STMT(type, base) \
201   ASSERT_IMPLEMENTS_children(type); \
202   ASSERT_IMPLEMENTS_getLocStart(type); \
203   ASSERT_IMPLEMENTS_getLocEnd(type);
204 #include "clang/AST/StmtNodes.inc"
205 }
206 
207 Stmt::child_range Stmt::children() {
208   switch (getStmtClass()) {
209   case Stmt::NoStmtClass: llvm_unreachable("statement without class");
210 #define ABSTRACT_STMT(type)
211 #define STMT(type, base) \
212   case Stmt::type##Class: \
213     return static_cast<type*>(this)->children();
214 #include "clang/AST/StmtNodes.inc"
215   }
216   llvm_unreachable("unknown statement kind!");
217 }
218 
219 // Amusing macro metaprogramming hack: check whether a class provides
220 // a more specific implementation of getSourceRange.
221 //
222 // See also Expr.cpp:getExprLoc().
223 namespace {
224   /// This implementation is used when a class provides a custom
225   /// implementation of getSourceRange.
226   template <class S, class T>
227   SourceRange getSourceRangeImpl(const Stmt *stmt,
228                                  SourceRange (T::*v)() const) {
229     return static_cast<const S*>(stmt)->getSourceRange();
230   }
231 
232   /// This implementation is used when a class doesn't provide a custom
233   /// implementation of getSourceRange.  Overload resolution should pick it over
234   /// the implementation above because it's more specialized according to
235   /// function template partial ordering.
236   template <class S>
237   SourceRange getSourceRangeImpl(const Stmt *stmt,
238                                  SourceRange (Stmt::*v)() const) {
239     return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
240                        static_cast<const S*>(stmt)->getLocEnd());
241   }
242 }
243 
244 SourceRange Stmt::getSourceRange() const {
245   switch (getStmtClass()) {
246   case Stmt::NoStmtClass: llvm_unreachable("statement without class");
247 #define ABSTRACT_STMT(type)
248 #define STMT(type, base) \
249   case Stmt::type##Class: \
250     return getSourceRangeImpl<type>(this, &type::getSourceRange);
251 #include "clang/AST/StmtNodes.inc"
252   }
253   llvm_unreachable("unknown statement kind!");
254 }
255 
256 SourceLocation Stmt::getLocStart() const {
257 //  llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
258   switch (getStmtClass()) {
259   case Stmt::NoStmtClass: llvm_unreachable("statement without class");
260 #define ABSTRACT_STMT(type)
261 #define STMT(type, base) \
262   case Stmt::type##Class: \
263     return static_cast<const type*>(this)->getLocStart();
264 #include "clang/AST/StmtNodes.inc"
265   }
266   llvm_unreachable("unknown statement kind");
267 }
268 
269 SourceLocation Stmt::getLocEnd() const {
270   switch (getStmtClass()) {
271   case Stmt::NoStmtClass: llvm_unreachable("statement without class");
272 #define ABSTRACT_STMT(type)
273 #define STMT(type, base) \
274   case Stmt::type##Class: \
275     return static_cast<const type*>(this)->getLocEnd();
276 #include "clang/AST/StmtNodes.inc"
277   }
278   llvm_unreachable("unknown statement kind");
279 }
280 
281 CompoundStmt::CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
282                            SourceLocation LB, SourceLocation RB)
283   : Stmt(CompoundStmtClass), LBraceLoc(LB), RBraceLoc(RB) {
284   CompoundStmtBits.NumStmts = Stmts.size();
285   assert(CompoundStmtBits.NumStmts == Stmts.size() &&
286          "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
287 
288   if (Stmts.size() == 0) {
289     Body = nullptr;
290     return;
291   }
292 
293   Body = new (C) Stmt*[Stmts.size()];
294   std::copy(Stmts.begin(), Stmts.end(), Body);
295 }
296 
297 void CompoundStmt::setStmts(const ASTContext &C, Stmt **Stmts,
298                             unsigned NumStmts) {
299   if (this->Body)
300     C.Deallocate(Body);
301   this->CompoundStmtBits.NumStmts = NumStmts;
302 
303   Body = new (C) Stmt*[NumStmts];
304   memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
305 }
306 
307 const char *LabelStmt::getName() const {
308   return getDecl()->getIdentifier()->getNameStart();
309 }
310 
311 AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
312                                        ArrayRef<const Attr*> Attrs,
313                                        Stmt *SubStmt) {
314   assert(!Attrs.empty() && "Attrs should not be empty");
315   void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * Attrs.size(),
316                          llvm::alignOf<AttributedStmt>());
317   return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
318 }
319 
320 AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
321                                             unsigned NumAttrs) {
322   assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
323   void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * NumAttrs,
324                          llvm::alignOf<AttributedStmt>());
325   return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
326 }
327 
328 std::string AsmStmt::generateAsmString(const ASTContext &C) const {
329   if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
330     return gccAsmStmt->generateAsmString(C);
331   if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
332     return msAsmStmt->generateAsmString(C);
333   llvm_unreachable("unknown asm statement kind!");
334 }
335 
336 StringRef AsmStmt::getOutputConstraint(unsigned i) const {
337   if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
338     return gccAsmStmt->getOutputConstraint(i);
339   if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
340     return msAsmStmt->getOutputConstraint(i);
341   llvm_unreachable("unknown asm statement kind!");
342 }
343 
344 const Expr *AsmStmt::getOutputExpr(unsigned i) const {
345   if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
346     return gccAsmStmt->getOutputExpr(i);
347   if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
348     return msAsmStmt->getOutputExpr(i);
349   llvm_unreachable("unknown asm statement kind!");
350 }
351 
352 StringRef AsmStmt::getInputConstraint(unsigned i) const {
353   if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
354     return gccAsmStmt->getInputConstraint(i);
355   if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
356     return msAsmStmt->getInputConstraint(i);
357   llvm_unreachable("unknown asm statement kind!");
358 }
359 
360 const Expr *AsmStmt::getInputExpr(unsigned i) const {
361   if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
362     return gccAsmStmt->getInputExpr(i);
363   if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
364     return msAsmStmt->getInputExpr(i);
365   llvm_unreachable("unknown asm statement kind!");
366 }
367 
368 StringRef AsmStmt::getClobber(unsigned i) const {
369   if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
370     return gccAsmStmt->getClobber(i);
371   if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
372     return msAsmStmt->getClobber(i);
373   llvm_unreachable("unknown asm statement kind!");
374 }
375 
376 /// getNumPlusOperands - Return the number of output operands that have a "+"
377 /// constraint.
378 unsigned AsmStmt::getNumPlusOperands() const {
379   unsigned Res = 0;
380   for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
381     if (isOutputPlusConstraint(i))
382       ++Res;
383   return Res;
384 }
385 
386 char GCCAsmStmt::AsmStringPiece::getModifier() const {
387   assert(isOperand() && "Only Operands can have modifiers.");
388   return isLetter(Str[0]) ? Str[0] : '\0';
389 }
390 
391 StringRef GCCAsmStmt::getClobber(unsigned i) const {
392   return getClobberStringLiteral(i)->getString();
393 }
394 
395 Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
396   return cast<Expr>(Exprs[i]);
397 }
398 
399 /// getOutputConstraint - Return the constraint string for the specified
400 /// output operand.  All output constraints are known to be non-empty (either
401 /// '=' or '+').
402 StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
403   return getOutputConstraintLiteral(i)->getString();
404 }
405 
406 Expr *GCCAsmStmt::getInputExpr(unsigned i) {
407   return cast<Expr>(Exprs[i + NumOutputs]);
408 }
409 void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
410   Exprs[i + NumOutputs] = E;
411 }
412 
413 /// getInputConstraint - Return the specified input constraint.  Unlike output
414 /// constraints, these can be empty.
415 StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
416   return getInputConstraintLiteral(i)->getString();
417 }
418 
419 void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
420                                                 IdentifierInfo **Names,
421                                                 StringLiteral **Constraints,
422                                                 Stmt **Exprs,
423                                                 unsigned NumOutputs,
424                                                 unsigned NumInputs,
425                                                 StringLiteral **Clobbers,
426                                                 unsigned NumClobbers) {
427   this->NumOutputs = NumOutputs;
428   this->NumInputs = NumInputs;
429   this->NumClobbers = NumClobbers;
430 
431   unsigned NumExprs = NumOutputs + NumInputs;
432 
433   C.Deallocate(this->Names);
434   this->Names = new (C) IdentifierInfo*[NumExprs];
435   std::copy(Names, Names + NumExprs, this->Names);
436 
437   C.Deallocate(this->Exprs);
438   this->Exprs = new (C) Stmt*[NumExprs];
439   std::copy(Exprs, Exprs + NumExprs, this->Exprs);
440 
441   C.Deallocate(this->Constraints);
442   this->Constraints = new (C) StringLiteral*[NumExprs];
443   std::copy(Constraints, Constraints + NumExprs, this->Constraints);
444 
445   C.Deallocate(this->Clobbers);
446   this->Clobbers = new (C) StringLiteral*[NumClobbers];
447   std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
448 }
449 
450 /// getNamedOperand - Given a symbolic operand reference like %[foo],
451 /// translate this into a numeric value needed to reference the same operand.
452 /// This returns -1 if the operand name is invalid.
453 int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
454   unsigned NumPlusOperands = 0;
455 
456   // Check if this is an output operand.
457   for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
458     if (getOutputName(i) == SymbolicName)
459       return i;
460   }
461 
462   for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
463     if (getInputName(i) == SymbolicName)
464       return getNumOutputs() + NumPlusOperands + i;
465 
466   // Not found.
467   return -1;
468 }
469 
470 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
471 /// it into pieces.  If the asm string is erroneous, emit errors and return
472 /// true, otherwise return false.
473 unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
474                                 const ASTContext &C, unsigned &DiagOffs) const {
475   StringRef Str = getAsmString()->getString();
476   const char *StrStart = Str.begin();
477   const char *StrEnd = Str.end();
478   const char *CurPtr = StrStart;
479 
480   // "Simple" inline asms have no constraints or operands, just convert the asm
481   // string to escape $'s.
482   if (isSimple()) {
483     std::string Result;
484     for (; CurPtr != StrEnd; ++CurPtr) {
485       switch (*CurPtr) {
486       case '$':
487         Result += "$$";
488         break;
489       default:
490         Result += *CurPtr;
491         break;
492       }
493     }
494     Pieces.push_back(AsmStringPiece(Result));
495     return 0;
496   }
497 
498   // CurStringPiece - The current string that we are building up as we scan the
499   // asm string.
500   std::string CurStringPiece;
501 
502   bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
503 
504   while (1) {
505     // Done with the string?
506     if (CurPtr == StrEnd) {
507       if (!CurStringPiece.empty())
508         Pieces.push_back(AsmStringPiece(CurStringPiece));
509       return 0;
510     }
511 
512     char CurChar = *CurPtr++;
513     switch (CurChar) {
514     case '$': CurStringPiece += "$$"; continue;
515     case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
516     case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
517     case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
518     case '%':
519       break;
520     default:
521       CurStringPiece += CurChar;
522       continue;
523     }
524 
525     // Escaped "%" character in asm string.
526     if (CurPtr == StrEnd) {
527       // % at end of string is invalid (no escape).
528       DiagOffs = CurPtr-StrStart-1;
529       return diag::err_asm_invalid_escape;
530     }
531 
532     char EscapedChar = *CurPtr++;
533     if (EscapedChar == '%') {  // %% -> %
534       // Escaped percentage sign.
535       CurStringPiece += '%';
536       continue;
537     }
538 
539     if (EscapedChar == '=') {  // %= -> Generate an unique ID.
540       CurStringPiece += "${:uid}";
541       continue;
542     }
543 
544     // Otherwise, we have an operand.  If we have accumulated a string so far,
545     // add it to the Pieces list.
546     if (!CurStringPiece.empty()) {
547       Pieces.push_back(AsmStringPiece(CurStringPiece));
548       CurStringPiece.clear();
549     }
550 
551     // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
552     // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
553 
554     const char *Begin = CurPtr - 1; // Points to the character following '%'.
555     const char *Percent = Begin - 1; // Points to '%'.
556 
557     if (isLetter(EscapedChar)) {
558       if (CurPtr == StrEnd) { // Premature end.
559         DiagOffs = CurPtr-StrStart-1;
560         return diag::err_asm_invalid_escape;
561       }
562       EscapedChar = *CurPtr++;
563     }
564 
565     const TargetInfo &TI = C.getTargetInfo();
566     const SourceManager &SM = C.getSourceManager();
567     const LangOptions &LO = C.getLangOpts();
568 
569     // Handle operands that don't have asmSymbolicName (e.g., %x4).
570     if (isDigit(EscapedChar)) {
571       // %n - Assembler operand n
572       unsigned N = 0;
573 
574       --CurPtr;
575       while (CurPtr != StrEnd && isDigit(*CurPtr))
576         N = N*10 + ((*CurPtr++)-'0');
577 
578       unsigned NumOperands =
579         getNumOutputs() + getNumPlusOperands() + getNumInputs();
580       if (N >= NumOperands) {
581         DiagOffs = CurPtr-StrStart-1;
582         return diag::err_asm_invalid_operand_number;
583       }
584 
585       // Str contains "x4" (Operand without the leading %).
586       std::string Str(Begin, CurPtr - Begin);
587 
588       // (BeginLoc, EndLoc) represents the range of the operand we are currently
589       // processing. Unlike Str, the range includes the leading '%'.
590       SourceLocation BeginLoc =
591           getAsmString()->getLocationOfByte(Percent - StrStart, SM, LO, TI);
592       SourceLocation EndLoc =
593           getAsmString()->getLocationOfByte(CurPtr - StrStart, SM, LO, TI);
594 
595       Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
596       continue;
597     }
598 
599     // Handle operands that have asmSymbolicName (e.g., %x[foo]).
600     if (EscapedChar == '[') {
601       DiagOffs = CurPtr-StrStart-1;
602 
603       // Find the ']'.
604       const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
605       if (NameEnd == nullptr)
606         return diag::err_asm_unterminated_symbolic_operand_name;
607       if (NameEnd == CurPtr)
608         return diag::err_asm_empty_symbolic_operand_name;
609 
610       StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
611 
612       int N = getNamedOperand(SymbolicName);
613       if (N == -1) {
614         // Verify that an operand with that name exists.
615         DiagOffs = CurPtr-StrStart;
616         return diag::err_asm_unknown_symbolic_operand_name;
617       }
618 
619       // Str contains "x[foo]" (Operand without the leading %).
620       std::string Str(Begin, NameEnd + 1 - Begin);
621 
622       // (BeginLoc, EndLoc) represents the range of the operand we are currently
623       // processing. Unlike Str, the range includes the leading '%'.
624       SourceLocation BeginLoc =
625           getAsmString()->getLocationOfByte(Percent - StrStart, SM, LO, TI);
626       SourceLocation EndLoc =
627           getAsmString()->getLocationOfByte(NameEnd + 1 - StrStart, SM, LO, TI);
628 
629       Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
630 
631       CurPtr = NameEnd+1;
632       continue;
633     }
634 
635     DiagOffs = CurPtr-StrStart-1;
636     return diag::err_asm_invalid_escape;
637   }
638 }
639 
640 /// Assemble final IR asm string (GCC-style).
641 std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
642   // Analyze the asm string to decompose it into its pieces.  We know that Sema
643   // has already done this, so it is guaranteed to be successful.
644   SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
645   unsigned DiagOffs;
646   AnalyzeAsmString(Pieces, C, DiagOffs);
647 
648   std::string AsmString;
649   for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
650     if (Pieces[i].isString())
651       AsmString += Pieces[i].getString();
652     else if (Pieces[i].getModifier() == '\0')
653       AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
654     else
655       AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
656                    Pieces[i].getModifier() + '}';
657   }
658   return AsmString;
659 }
660 
661 /// Assemble final IR asm string (MS-style).
662 std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
663   // FIXME: This needs to be translated into the IR string representation.
664   return AsmStr;
665 }
666 
667 Expr *MSAsmStmt::getOutputExpr(unsigned i) {
668   return cast<Expr>(Exprs[i]);
669 }
670 
671 Expr *MSAsmStmt::getInputExpr(unsigned i) {
672   return cast<Expr>(Exprs[i + NumOutputs]);
673 }
674 void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
675   Exprs[i + NumOutputs] = E;
676 }
677 
678 QualType CXXCatchStmt::getCaughtType() const {
679   if (ExceptionDecl)
680     return ExceptionDecl->getType();
681   return QualType();
682 }
683 
684 //===----------------------------------------------------------------------===//
685 // Constructors
686 //===----------------------------------------------------------------------===//
687 
688 GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
689                        bool issimple, bool isvolatile, unsigned numoutputs,
690                        unsigned numinputs, IdentifierInfo **names,
691                        StringLiteral **constraints, Expr **exprs,
692                        StringLiteral *asmstr, unsigned numclobbers,
693                        StringLiteral **clobbers, SourceLocation rparenloc)
694   : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
695             numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
696 
697   unsigned NumExprs = NumOutputs + NumInputs;
698 
699   Names = new (C) IdentifierInfo*[NumExprs];
700   std::copy(names, names + NumExprs, Names);
701 
702   Exprs = new (C) Stmt*[NumExprs];
703   std::copy(exprs, exprs + NumExprs, Exprs);
704 
705   Constraints = new (C) StringLiteral*[NumExprs];
706   std::copy(constraints, constraints + NumExprs, Constraints);
707 
708   Clobbers = new (C) StringLiteral*[NumClobbers];
709   std::copy(clobbers, clobbers + NumClobbers, Clobbers);
710 }
711 
712 MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
713                      SourceLocation lbraceloc, bool issimple, bool isvolatile,
714                      ArrayRef<Token> asmtoks, unsigned numoutputs,
715                      unsigned numinputs,
716                      ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
717                      StringRef asmstr, ArrayRef<StringRef> clobbers,
718                      SourceLocation endloc)
719   : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
720             numinputs, clobbers.size()), LBraceLoc(lbraceloc),
721             EndLoc(endloc), NumAsmToks(asmtoks.size()) {
722 
723   initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
724 }
725 
726 static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
727   return str.copy(C);
728 }
729 
730 void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
731                            ArrayRef<Token> asmtoks,
732                            ArrayRef<StringRef> constraints,
733                            ArrayRef<Expr*> exprs,
734                            ArrayRef<StringRef> clobbers) {
735   assert(NumAsmToks == asmtoks.size());
736   assert(NumClobbers == clobbers.size());
737 
738   unsigned NumExprs = exprs.size();
739   assert(NumExprs == NumOutputs + NumInputs);
740   assert(NumExprs == constraints.size());
741 
742   AsmStr = copyIntoContext(C, asmstr);
743 
744   Exprs = new (C) Stmt*[NumExprs];
745   for (unsigned i = 0, e = NumExprs; i != e; ++i)
746     Exprs[i] = exprs[i];
747 
748   AsmToks = new (C) Token[NumAsmToks];
749   for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
750     AsmToks[i] = asmtoks[i];
751 
752   Constraints = new (C) StringRef[NumExprs];
753   for (unsigned i = 0, e = NumExprs; i != e; ++i) {
754     Constraints[i] = copyIntoContext(C, constraints[i]);
755   }
756 
757   Clobbers = new (C) StringRef[NumClobbers];
758   for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
759     // FIXME: Avoid the allocation/copy if at all possible.
760     Clobbers[i] = copyIntoContext(C, clobbers[i]);
761   }
762 }
763 
764 ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
765                                              Stmt *Body,  SourceLocation FCL,
766                                              SourceLocation RPL)
767 : Stmt(ObjCForCollectionStmtClass) {
768   SubExprs[ELEM] = Elem;
769   SubExprs[COLLECTION] = Collect;
770   SubExprs[BODY] = Body;
771   ForLoc = FCL;
772   RParenLoc = RPL;
773 }
774 
775 ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
776                              Stmt **CatchStmts, unsigned NumCatchStmts,
777                              Stmt *atFinallyStmt)
778   : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
779     NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != nullptr) {
780   Stmt **Stmts = getStmts();
781   Stmts[0] = atTryStmt;
782   for (unsigned I = 0; I != NumCatchStmts; ++I)
783     Stmts[I + 1] = CatchStmts[I];
784 
785   if (HasFinally)
786     Stmts[NumCatchStmts + 1] = atFinallyStmt;
787 }
788 
789 ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
790                                      SourceLocation atTryLoc,
791                                      Stmt *atTryStmt,
792                                      Stmt **CatchStmts,
793                                      unsigned NumCatchStmts,
794                                      Stmt *atFinallyStmt) {
795   unsigned Size = sizeof(ObjCAtTryStmt) +
796     (1 + NumCatchStmts + (atFinallyStmt != nullptr)) * sizeof(Stmt *);
797   void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
798   return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
799                                  atFinallyStmt);
800 }
801 
802 ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
803                                           unsigned NumCatchStmts,
804                                           bool HasFinally) {
805   unsigned Size = sizeof(ObjCAtTryStmt) +
806     (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
807   void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
808   return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
809 }
810 
811 SourceLocation ObjCAtTryStmt::getLocEnd() const {
812   if (HasFinally)
813     return getFinallyStmt()->getLocEnd();
814   if (NumCatchStmts)
815     return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
816   return getTryBody()->getLocEnd();
817 }
818 
819 CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
820                                Stmt *tryBlock, ArrayRef<Stmt*> handlers) {
821   std::size_t Size = sizeof(CXXTryStmt);
822   Size += ((handlers.size() + 1) * sizeof(Stmt *));
823 
824   void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
825   return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
826 }
827 
828 CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
829                                unsigned numHandlers) {
830   std::size_t Size = sizeof(CXXTryStmt);
831   Size += ((numHandlers + 1) * sizeof(Stmt *));
832 
833   void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
834   return new (Mem) CXXTryStmt(Empty, numHandlers);
835 }
836 
837 CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
838                        ArrayRef<Stmt*> handlers)
839   : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
840   Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
841   Stmts[0] = tryBlock;
842   std::copy(handlers.begin(), handlers.end(), Stmts + 1);
843 }
844 
845 CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
846                                  Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
847                                  Stmt *Body, SourceLocation FL,
848                                  SourceLocation CL, SourceLocation RPL)
849   : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
850   SubExprs[RANGE] = Range;
851   SubExprs[BEGINEND] = BeginEndStmt;
852   SubExprs[COND] = Cond;
853   SubExprs[INC] = Inc;
854   SubExprs[LOOPVAR] = LoopVar;
855   SubExprs[BODY] = Body;
856 }
857 
858 Expr *CXXForRangeStmt::getRangeInit() {
859   DeclStmt *RangeStmt = getRangeStmt();
860   VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
861   assert(RangeDecl && "for-range should have a single var decl");
862   return RangeDecl->getInit();
863 }
864 
865 const Expr *CXXForRangeStmt::getRangeInit() const {
866   return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
867 }
868 
869 VarDecl *CXXForRangeStmt::getLoopVariable() {
870   Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
871   assert(LV && "No loop variable in CXXForRangeStmt");
872   return cast<VarDecl>(LV);
873 }
874 
875 const VarDecl *CXXForRangeStmt::getLoopVariable() const {
876   return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
877 }
878 
879 IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
880                Stmt *then, SourceLocation EL, Stmt *elsev)
881   : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
882 {
883   setConditionVariable(C, var);
884   SubExprs[COND] = cond;
885   SubExprs[THEN] = then;
886   SubExprs[ELSE] = elsev;
887 }
888 
889 VarDecl *IfStmt::getConditionVariable() const {
890   if (!SubExprs[VAR])
891     return nullptr;
892 
893   DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
894   return cast<VarDecl>(DS->getSingleDecl());
895 }
896 
897 void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
898   if (!V) {
899     SubExprs[VAR] = nullptr;
900     return;
901   }
902 
903   SourceRange VarRange = V->getSourceRange();
904   SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
905                                    VarRange.getEnd());
906 }
907 
908 ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
909                  Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
910                  SourceLocation RP)
911   : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
912 {
913   SubExprs[INIT] = Init;
914   setConditionVariable(C, condVar);
915   SubExprs[COND] = Cond;
916   SubExprs[INC] = Inc;
917   SubExprs[BODY] = Body;
918 }
919 
920 VarDecl *ForStmt::getConditionVariable() const {
921   if (!SubExprs[CONDVAR])
922     return nullptr;
923 
924   DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
925   return cast<VarDecl>(DS->getSingleDecl());
926 }
927 
928 void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
929   if (!V) {
930     SubExprs[CONDVAR] = nullptr;
931     return;
932   }
933 
934   SourceRange VarRange = V->getSourceRange();
935   SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
936                                        VarRange.getEnd());
937 }
938 
939 SwitchStmt::SwitchStmt(const ASTContext &C, VarDecl *Var, Expr *cond)
940     : Stmt(SwitchStmtClass), FirstCase(nullptr, false) {
941   setConditionVariable(C, Var);
942   SubExprs[COND] = cond;
943   SubExprs[BODY] = nullptr;
944 }
945 
946 VarDecl *SwitchStmt::getConditionVariable() const {
947   if (!SubExprs[VAR])
948     return nullptr;
949 
950   DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
951   return cast<VarDecl>(DS->getSingleDecl());
952 }
953 
954 void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
955   if (!V) {
956     SubExprs[VAR] = nullptr;
957     return;
958   }
959 
960   SourceRange VarRange = V->getSourceRange();
961   SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
962                                    VarRange.getEnd());
963 }
964 
965 Stmt *SwitchCase::getSubStmt() {
966   if (isa<CaseStmt>(this))
967     return cast<CaseStmt>(this)->getSubStmt();
968   return cast<DefaultStmt>(this)->getSubStmt();
969 }
970 
971 WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
972                      SourceLocation WL)
973   : Stmt(WhileStmtClass) {
974   setConditionVariable(C, Var);
975   SubExprs[COND] = cond;
976   SubExprs[BODY] = body;
977   WhileLoc = WL;
978 }
979 
980 VarDecl *WhileStmt::getConditionVariable() const {
981   if (!SubExprs[VAR])
982     return nullptr;
983 
984   DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
985   return cast<VarDecl>(DS->getSingleDecl());
986 }
987 
988 void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
989   if (!V) {
990     SubExprs[VAR] = nullptr;
991     return;
992   }
993 
994   SourceRange VarRange = V->getSourceRange();
995   SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
996                                    VarRange.getEnd());
997 }
998 
999 // IndirectGotoStmt
1000 LabelDecl *IndirectGotoStmt::getConstantTarget() {
1001   if (AddrLabelExpr *E =
1002         dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
1003     return E->getLabel();
1004   return nullptr;
1005 }
1006 
1007 // ReturnStmt
1008 const Expr* ReturnStmt::getRetValue() const {
1009   return cast_or_null<Expr>(RetExpr);
1010 }
1011 Expr* ReturnStmt::getRetValue() {
1012   return cast_or_null<Expr>(RetExpr);
1013 }
1014 
1015 SEHTryStmt::SEHTryStmt(bool IsCXXTry,
1016                        SourceLocation TryLoc,
1017                        Stmt *TryBlock,
1018                        Stmt *Handler)
1019   : Stmt(SEHTryStmtClass),
1020     IsCXXTry(IsCXXTry),
1021     TryLoc(TryLoc)
1022 {
1023   Children[TRY]     = TryBlock;
1024   Children[HANDLER] = Handler;
1025 }
1026 
1027 SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
1028                                SourceLocation TryLoc, Stmt *TryBlock,
1029                                Stmt *Handler) {
1030   return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
1031 }
1032 
1033 SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
1034   return dyn_cast<SEHExceptStmt>(getHandler());
1035 }
1036 
1037 SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
1038   return dyn_cast<SEHFinallyStmt>(getHandler());
1039 }
1040 
1041 SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
1042                              Expr *FilterExpr,
1043                              Stmt *Block)
1044   : Stmt(SEHExceptStmtClass),
1045     Loc(Loc)
1046 {
1047   Children[FILTER_EXPR] = FilterExpr;
1048   Children[BLOCK]       = Block;
1049 }
1050 
1051 SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
1052                                      Expr *FilterExpr, Stmt *Block) {
1053   return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1054 }
1055 
1056 SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1057                                Stmt *Block)
1058   : Stmt(SEHFinallyStmtClass),
1059     Loc(Loc),
1060     Block(Block)
1061 {}
1062 
1063 SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
1064                                        Stmt *Block) {
1065   return new(C)SEHFinallyStmt(Loc,Block);
1066 }
1067 
1068 CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1069   unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1070 
1071   // Offset of the first Capture object.
1072   unsigned FirstCaptureOffset =
1073     llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1074 
1075   return reinterpret_cast<Capture *>(
1076       reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1077       + FirstCaptureOffset);
1078 }
1079 
1080 CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1081                            ArrayRef<Capture> Captures,
1082                            ArrayRef<Expr *> CaptureInits,
1083                            CapturedDecl *CD,
1084                            RecordDecl *RD)
1085   : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
1086     CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
1087   assert( S && "null captured statement");
1088   assert(CD && "null captured declaration for captured statement");
1089   assert(RD && "null record declaration for captured statement");
1090 
1091   // Copy initialization expressions.
1092   Stmt **Stored = getStoredStmts();
1093   for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1094     *Stored++ = CaptureInits[I];
1095 
1096   // Copy the statement being captured.
1097   *Stored = S;
1098 
1099   // Copy all Capture objects.
1100   Capture *Buffer = getStoredCaptures();
1101   std::copy(Captures.begin(), Captures.end(), Buffer);
1102 }
1103 
1104 CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1105   : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
1106     CapDeclAndKind(nullptr, CR_Default), TheRecordDecl(nullptr) {
1107   getStoredStmts()[NumCaptures] = nullptr;
1108 }
1109 
1110 CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
1111                                    CapturedRegionKind Kind,
1112                                    ArrayRef<Capture> Captures,
1113                                    ArrayRef<Expr *> CaptureInits,
1114                                    CapturedDecl *CD,
1115                                    RecordDecl *RD) {
1116   // The layout is
1117   //
1118   // -----------------------------------------------------------
1119   // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1120   // ----------------^-------------------^----------------------
1121   //                 getStoredStmts()    getStoredCaptures()
1122   //
1123   // where S is the statement being captured.
1124   //
1125   assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1126 
1127   unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1128   if (!Captures.empty()) {
1129     // Realign for the following Capture array.
1130     Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1131     Size += sizeof(Capture) * Captures.size();
1132   }
1133 
1134   void *Mem = Context.Allocate(Size);
1135   return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
1136 }
1137 
1138 CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
1139                                                unsigned NumCaptures) {
1140   unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1141   if (NumCaptures > 0) {
1142     // Realign for the following Capture array.
1143     Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1144     Size += sizeof(Capture) * NumCaptures;
1145   }
1146 
1147   void *Mem = Context.Allocate(Size);
1148   return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1149 }
1150 
1151 Stmt::child_range CapturedStmt::children() {
1152   // Children are captured field initilizers.
1153   return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
1154 }
1155 
1156 bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
1157   for (const auto &I : captures()) {
1158     if (!I.capturesVariable())
1159       continue;
1160 
1161     // This does not handle variable redeclarations. This should be
1162     // extended to capture variables with redeclarations, for example
1163     // a thread-private variable in OpenMP.
1164     if (I.getCapturedVar() == Var)
1165       return true;
1166   }
1167 
1168   return false;
1169 }
1170 
1171 OMPClause::child_range OMPClause::children() {
1172   switch(getClauseKind()) {
1173   default : break;
1174 #define OPENMP_CLAUSE(Name, Class)                                       \
1175   case OMPC_ ## Name : return static_cast<Class *>(this)->children();
1176 #include "clang/Basic/OpenMPKinds.def"
1177   }
1178   llvm_unreachable("unknown OMPClause");
1179 }
1180 
1181 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
1182   assert(VL.size() == varlist_size() &&
1183          "Number of private copies is not the same as the preallocated buffer");
1184   std::copy(VL.begin(), VL.end(), varlist_end());
1185 }
1186 
1187 OMPPrivateClause *
1188 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
1189                          SourceLocation LParenLoc, SourceLocation EndLoc,
1190                          ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
1191   // Allocate space for private variables and initializer expressions.
1192   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
1193                                                   llvm::alignOf<Expr *>()) +
1194                          2 * sizeof(Expr *) * VL.size());
1195   OMPPrivateClause *Clause =
1196       new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1197   Clause->setVarRefs(VL);
1198   Clause->setPrivateCopies(PrivateVL);
1199   return Clause;
1200 }
1201 
1202 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
1203                                                 unsigned N) {
1204   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
1205                                                   llvm::alignOf<Expr *>()) +
1206                          2 * sizeof(Expr *) * N);
1207   return new (Mem) OMPPrivateClause(N);
1208 }
1209 
1210 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
1211   assert(VL.size() == varlist_size() &&
1212          "Number of private copies is not the same as the preallocated buffer");
1213   std::copy(VL.begin(), VL.end(), varlist_end());
1214 }
1215 
1216 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
1217   assert(VL.size() == varlist_size() &&
1218          "Number of inits is not the same as the preallocated buffer");
1219   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
1220 }
1221 
1222 OMPFirstprivateClause *
1223 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
1224                               SourceLocation LParenLoc, SourceLocation EndLoc,
1225                               ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
1226                               ArrayRef<Expr *> InitVL) {
1227   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
1228                                                   llvm::alignOf<Expr *>()) +
1229                          3 * sizeof(Expr *) * VL.size());
1230   OMPFirstprivateClause *Clause =
1231       new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1232   Clause->setVarRefs(VL);
1233   Clause->setPrivateCopies(PrivateVL);
1234   Clause->setInits(InitVL);
1235   return Clause;
1236 }
1237 
1238 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
1239                                                           unsigned N) {
1240   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
1241                                                   llvm::alignOf<Expr *>()) +
1242                          3 * sizeof(Expr *) * N);
1243   return new (Mem) OMPFirstprivateClause(N);
1244 }
1245 
1246 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
1247   assert(PrivateCopies.size() == varlist_size() &&
1248          "Number of private copies is not the same as the preallocated buffer");
1249   std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
1250 }
1251 
1252 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
1253   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
1254                                               "not the same as the "
1255                                               "preallocated buffer");
1256   std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
1257 }
1258 
1259 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
1260   assert(DstExprs.size() == varlist_size() && "Number of destination "
1261                                               "expressions is not the same as "
1262                                               "the preallocated buffer");
1263   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
1264 }
1265 
1266 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
1267   assert(AssignmentOps.size() == varlist_size() &&
1268          "Number of assignment expressions is not the same as the preallocated "
1269          "buffer");
1270   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
1271             getDestinationExprs().end());
1272 }
1273 
1274 OMPLastprivateClause *OMPLastprivateClause::Create(
1275     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1276     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
1277     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
1278   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLastprivateClause),
1279                                                   llvm::alignOf<Expr *>()) +
1280                          5 * sizeof(Expr *) * VL.size());
1281   OMPLastprivateClause *Clause =
1282       new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1283   Clause->setVarRefs(VL);
1284   Clause->setSourceExprs(SrcExprs);
1285   Clause->setDestinationExprs(DstExprs);
1286   Clause->setAssignmentOps(AssignmentOps);
1287   return Clause;
1288 }
1289 
1290 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
1291                                                         unsigned N) {
1292   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLastprivateClause),
1293                                                   llvm::alignOf<Expr *>()) +
1294                          5 * sizeof(Expr *) * N);
1295   return new (Mem) OMPLastprivateClause(N);
1296 }
1297 
1298 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
1299                                          SourceLocation StartLoc,
1300                                          SourceLocation LParenLoc,
1301                                          SourceLocation EndLoc,
1302                                          ArrayRef<Expr *> VL) {
1303   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
1304                                                   llvm::alignOf<Expr *>()) +
1305                          sizeof(Expr *) * VL.size());
1306   OMPSharedClause *Clause = new (Mem) OMPSharedClause(StartLoc, LParenLoc,
1307                                                       EndLoc, VL.size());
1308   Clause->setVarRefs(VL);
1309   return Clause;
1310 }
1311 
1312 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C,
1313                                               unsigned N) {
1314   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
1315                                                   llvm::alignOf<Expr *>()) +
1316                          sizeof(Expr *) * N);
1317   return new (Mem) OMPSharedClause(N);
1318 }
1319 
1320 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
1321   assert(IL.size() == varlist_size() &&
1322          "Number of inits is not the same as the preallocated buffer");
1323   std::copy(IL.begin(), IL.end(), varlist_end());
1324 }
1325 
1326 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
1327   assert(UL.size() == varlist_size() &&
1328          "Number of updates is not the same as the preallocated buffer");
1329   std::copy(UL.begin(), UL.end(), getInits().end());
1330 }
1331 
1332 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
1333   assert(FL.size() == varlist_size() &&
1334          "Number of final updates is not the same as the preallocated buffer");
1335   std::copy(FL.begin(), FL.end(), getUpdates().end());
1336 }
1337 
1338 OMPLinearClause *
1339 OMPLinearClause::Create(const ASTContext &C, SourceLocation StartLoc,
1340                         SourceLocation LParenLoc, SourceLocation ColonLoc,
1341                         SourceLocation EndLoc, ArrayRef<Expr *> VL,
1342                         ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep) {
1343   // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
1344   // (Step and CalcStep).
1345   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLinearClause),
1346                                                   llvm::alignOf<Expr *>()) +
1347                          (4 * VL.size() + 2) * sizeof(Expr *));
1348   OMPLinearClause *Clause = new (Mem)
1349       OMPLinearClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
1350   Clause->setVarRefs(VL);
1351   Clause->setInits(IL);
1352   // Fill update and final expressions with zeroes, they are provided later,
1353   // after the directive construction.
1354   std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
1355             nullptr);
1356   std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
1357             nullptr);
1358   Clause->setStep(Step);
1359   Clause->setCalcStep(CalcStep);
1360   return Clause;
1361 }
1362 
1363 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
1364                                               unsigned NumVars) {
1365   // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
1366   // (Step and CalcStep).
1367   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLinearClause),
1368                                                   llvm::alignOf<Expr *>()) +
1369                          (4 * NumVars + 2) * sizeof(Expr *));
1370   return new (Mem) OMPLinearClause(NumVars);
1371 }
1372 
1373 OMPAlignedClause *
1374 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
1375                          SourceLocation LParenLoc, SourceLocation ColonLoc,
1376                          SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
1377   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPAlignedClause),
1378                                                   llvm::alignOf<Expr *>()) +
1379                          sizeof(Expr *) * (VL.size() + 1));
1380   OMPAlignedClause *Clause = new (Mem)
1381       OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
1382   Clause->setVarRefs(VL);
1383   Clause->setAlignment(A);
1384   return Clause;
1385 }
1386 
1387 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
1388                                                 unsigned NumVars) {
1389   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPAlignedClause),
1390                                                   llvm::alignOf<Expr *>()) +
1391                          sizeof(Expr *) * (NumVars + 1));
1392   return new (Mem) OMPAlignedClause(NumVars);
1393 }
1394 
1395 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
1396   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
1397                                               "not the same as the "
1398                                               "preallocated buffer");
1399   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
1400 }
1401 
1402 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
1403   assert(DstExprs.size() == varlist_size() && "Number of destination "
1404                                               "expressions is not the same as "
1405                                               "the preallocated buffer");
1406   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
1407 }
1408 
1409 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
1410   assert(AssignmentOps.size() == varlist_size() &&
1411          "Number of assignment expressions is not the same as the preallocated "
1412          "buffer");
1413   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
1414             getDestinationExprs().end());
1415 }
1416 
1417 OMPCopyinClause *OMPCopyinClause::Create(
1418     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1419     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
1420     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
1421   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyinClause),
1422                                                   llvm::alignOf<Expr *>()) +
1423                          4 * sizeof(Expr *) * VL.size());
1424   OMPCopyinClause *Clause = new (Mem) OMPCopyinClause(StartLoc, LParenLoc,
1425                                                       EndLoc, VL.size());
1426   Clause->setVarRefs(VL);
1427   Clause->setSourceExprs(SrcExprs);
1428   Clause->setDestinationExprs(DstExprs);
1429   Clause->setAssignmentOps(AssignmentOps);
1430   return Clause;
1431 }
1432 
1433 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C,
1434                                               unsigned N) {
1435   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyinClause),
1436                                                   llvm::alignOf<Expr *>()) +
1437                          4 * sizeof(Expr *) * N);
1438   return new (Mem) OMPCopyinClause(N);
1439 }
1440 
1441 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
1442   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
1443                                               "not the same as the "
1444                                               "preallocated buffer");
1445   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
1446 }
1447 
1448 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
1449   assert(DstExprs.size() == varlist_size() && "Number of destination "
1450                                               "expressions is not the same as "
1451                                               "the preallocated buffer");
1452   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
1453 }
1454 
1455 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
1456   assert(AssignmentOps.size() == varlist_size() &&
1457          "Number of assignment expressions is not the same as the preallocated "
1458          "buffer");
1459   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
1460             getDestinationExprs().end());
1461 }
1462 
1463 OMPCopyprivateClause *OMPCopyprivateClause::Create(
1464     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1465     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
1466     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
1467   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyprivateClause),
1468                                                   llvm::alignOf<Expr *>()) +
1469                          4 * sizeof(Expr *) * VL.size());
1470   OMPCopyprivateClause *Clause =
1471       new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1472   Clause->setVarRefs(VL);
1473   Clause->setSourceExprs(SrcExprs);
1474   Clause->setDestinationExprs(DstExprs);
1475   Clause->setAssignmentOps(AssignmentOps);
1476   return Clause;
1477 }
1478 
1479 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
1480                                                         unsigned N) {
1481   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyprivateClause),
1482                                                   llvm::alignOf<Expr *>()) +
1483                          4 * sizeof(Expr *) * N);
1484   return new (Mem) OMPCopyprivateClause(N);
1485 }
1486 
1487 void OMPExecutableDirective::setClauses(ArrayRef<OMPClause *> Clauses) {
1488   assert(Clauses.size() == getNumClauses() &&
1489          "Number of clauses is not the same as the preallocated buffer");
1490   std::copy(Clauses.begin(), Clauses.end(), getClauses().begin());
1491 }
1492 
1493 void OMPLoopDirective::setCounters(ArrayRef<Expr *> A) {
1494   assert(A.size() == getCollapsedNumber() &&
1495          "Number of loop counters is not the same as the collapsed number");
1496   std::copy(A.begin(), A.end(), getCounters().begin());
1497 }
1498 
1499 void OMPLoopDirective::setPrivateCounters(ArrayRef<Expr *> A) {
1500   assert(A.size() == getCollapsedNumber() && "Number of loop private counters "
1501                                              "is not the same as the collapsed "
1502                                              "number");
1503   std::copy(A.begin(), A.end(), getPrivateCounters().begin());
1504 }
1505 
1506 void OMPLoopDirective::setUpdates(ArrayRef<Expr *> A) {
1507   assert(A.size() == getCollapsedNumber() &&
1508          "Number of counter updates is not the same as the collapsed number");
1509   std::copy(A.begin(), A.end(), getUpdates().begin());
1510 }
1511 
1512 void OMPLoopDirective::setFinals(ArrayRef<Expr *> A) {
1513   assert(A.size() == getCollapsedNumber() &&
1514          "Number of counter finals is not the same as the collapsed number");
1515   std::copy(A.begin(), A.end(), getFinals().begin());
1516 }
1517 
1518 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
1519   assert(
1520       LHSExprs.size() == varlist_size() &&
1521       "Number of LHS expressions is not the same as the preallocated buffer");
1522   std::copy(LHSExprs.begin(), LHSExprs.end(), varlist_end());
1523 }
1524 
1525 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
1526   assert(
1527       RHSExprs.size() == varlist_size() &&
1528       "Number of RHS expressions is not the same as the preallocated buffer");
1529   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
1530 }
1531 
1532 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
1533   assert(ReductionOps.size() == varlist_size() && "Number of reduction "
1534                                                   "expressions is not the same "
1535                                                   "as the preallocated buffer");
1536   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
1537 }
1538 
1539 OMPReductionClause *OMPReductionClause::Create(
1540     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1541     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
1542     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
1543     ArrayRef<Expr *> LHSExprs, ArrayRef<Expr *> RHSExprs,
1544     ArrayRef<Expr *> ReductionOps) {
1545   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPReductionClause),
1546                                                   llvm::alignOf<Expr *>()) +
1547                          4 * sizeof(Expr *) * VL.size());
1548   OMPReductionClause *Clause = new (Mem) OMPReductionClause(
1549       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
1550   Clause->setVarRefs(VL);
1551   Clause->setLHSExprs(LHSExprs);
1552   Clause->setRHSExprs(RHSExprs);
1553   Clause->setReductionOps(ReductionOps);
1554   return Clause;
1555 }
1556 
1557 OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
1558                                                     unsigned N) {
1559   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPReductionClause),
1560                                                   llvm::alignOf<Expr *>()) +
1561                          4 * sizeof(Expr *) * N);
1562   return new (Mem) OMPReductionClause(N);
1563 }
1564 
1565 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
1566                                        SourceLocation StartLoc,
1567                                        SourceLocation LParenLoc,
1568                                        SourceLocation EndLoc,
1569                                        ArrayRef<Expr *> VL) {
1570   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFlushClause),
1571                                                   llvm::alignOf<Expr *>()) +
1572                          sizeof(Expr *) * VL.size());
1573   OMPFlushClause *Clause =
1574       new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
1575   Clause->setVarRefs(VL);
1576   return Clause;
1577 }
1578 
1579 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
1580   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFlushClause),
1581                                                   llvm::alignOf<Expr *>()) +
1582                          sizeof(Expr *) * N);
1583   return new (Mem) OMPFlushClause(N);
1584 }
1585 
1586 OMPDependClause *
1587 OMPDependClause::Create(const ASTContext &C, SourceLocation StartLoc,
1588                         SourceLocation LParenLoc, SourceLocation EndLoc,
1589                         OpenMPDependClauseKind DepKind, SourceLocation DepLoc,
1590                         SourceLocation ColonLoc, ArrayRef<Expr *> VL) {
1591   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPDependClause),
1592                                                   llvm::alignOf<Expr *>()) +
1593                          sizeof(Expr *) * VL.size());
1594   OMPDependClause *Clause =
1595       new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size());
1596   Clause->setVarRefs(VL);
1597   Clause->setDependencyKind(DepKind);
1598   Clause->setDependencyLoc(DepLoc);
1599   Clause->setColonLoc(ColonLoc);
1600   return Clause;
1601 }
1602 
1603 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) {
1604   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPDependClause),
1605                                                   llvm::alignOf<Expr *>()) +
1606                          sizeof(Expr *) * N);
1607   return new (Mem) OMPDependClause(N);
1608 }
1609 
1610 const OMPClause *
1611 OMPExecutableDirective::getSingleClause(OpenMPClauseKind K) const {
1612   auto &&I = getClausesOfKind(K);
1613 
1614   if (I) {
1615     auto *Clause = *I;
1616     assert(!++I && "There are at least 2 clauses of the specified kind");
1617     return Clause;
1618   }
1619   return nullptr;
1620 }
1621 
1622 OMPParallelDirective *OMPParallelDirective::Create(
1623                                               const ASTContext &C,
1624                                               SourceLocation StartLoc,
1625                                               SourceLocation EndLoc,
1626                                               ArrayRef<OMPClause *> Clauses,
1627                                               Stmt *AssociatedStmt) {
1628   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
1629                                            llvm::alignOf<OMPClause *>());
1630   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1631                          sizeof(Stmt *));
1632   OMPParallelDirective *Dir = new (Mem) OMPParallelDirective(StartLoc, EndLoc,
1633                                                              Clauses.size());
1634   Dir->setClauses(Clauses);
1635   Dir->setAssociatedStmt(AssociatedStmt);
1636   return Dir;
1637 }
1638 
1639 OMPParallelDirective *OMPParallelDirective::CreateEmpty(const ASTContext &C,
1640                                                         unsigned NumClauses,
1641                                                         EmptyShell) {
1642   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
1643                                            llvm::alignOf<OMPClause *>());
1644   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1645                          sizeof(Stmt *));
1646   return new (Mem) OMPParallelDirective(NumClauses);
1647 }
1648 
1649 OMPSimdDirective *
1650 OMPSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1651                          SourceLocation EndLoc, unsigned CollapsedNum,
1652                          ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
1653                          const HelperExprs &Exprs) {
1654   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSimdDirective),
1655                                            llvm::alignOf<OMPClause *>());
1656   void *Mem =
1657       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1658                  sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_simd));
1659   OMPSimdDirective *Dir = new (Mem)
1660       OMPSimdDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1661   Dir->setClauses(Clauses);
1662   Dir->setAssociatedStmt(AssociatedStmt);
1663   Dir->setIterationVariable(Exprs.IterationVarRef);
1664   Dir->setLastIteration(Exprs.LastIteration);
1665   Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1666   Dir->setPreCond(Exprs.PreCond);
1667   Dir->setCond(Exprs.Cond);
1668   Dir->setInit(Exprs.Init);
1669   Dir->setInc(Exprs.Inc);
1670   Dir->setCounters(Exprs.Counters);
1671   Dir->setPrivateCounters(Exprs.PrivateCounters);
1672   Dir->setUpdates(Exprs.Updates);
1673   Dir->setFinals(Exprs.Finals);
1674   return Dir;
1675 }
1676 
1677 OMPSimdDirective *OMPSimdDirective::CreateEmpty(const ASTContext &C,
1678                                                 unsigned NumClauses,
1679                                                 unsigned CollapsedNum,
1680                                                 EmptyShell) {
1681   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSimdDirective),
1682                                            llvm::alignOf<OMPClause *>());
1683   void *Mem =
1684       C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1685                  sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_simd));
1686   return new (Mem) OMPSimdDirective(CollapsedNum, NumClauses);
1687 }
1688 
1689 OMPForDirective *
1690 OMPForDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1691                         SourceLocation EndLoc, unsigned CollapsedNum,
1692                         ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
1693                         const HelperExprs &Exprs) {
1694   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForDirective),
1695                                            llvm::alignOf<OMPClause *>());
1696   void *Mem =
1697       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1698                  sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for));
1699   OMPForDirective *Dir =
1700       new (Mem) OMPForDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1701   Dir->setClauses(Clauses);
1702   Dir->setAssociatedStmt(AssociatedStmt);
1703   Dir->setIterationVariable(Exprs.IterationVarRef);
1704   Dir->setLastIteration(Exprs.LastIteration);
1705   Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1706   Dir->setPreCond(Exprs.PreCond);
1707   Dir->setCond(Exprs.Cond);
1708   Dir->setInit(Exprs.Init);
1709   Dir->setInc(Exprs.Inc);
1710   Dir->setIsLastIterVariable(Exprs.IL);
1711   Dir->setLowerBoundVariable(Exprs.LB);
1712   Dir->setUpperBoundVariable(Exprs.UB);
1713   Dir->setStrideVariable(Exprs.ST);
1714   Dir->setEnsureUpperBound(Exprs.EUB);
1715   Dir->setNextLowerBound(Exprs.NLB);
1716   Dir->setNextUpperBound(Exprs.NUB);
1717   Dir->setCounters(Exprs.Counters);
1718   Dir->setPrivateCounters(Exprs.PrivateCounters);
1719   Dir->setUpdates(Exprs.Updates);
1720   Dir->setFinals(Exprs.Finals);
1721   return Dir;
1722 }
1723 
1724 OMPForDirective *OMPForDirective::CreateEmpty(const ASTContext &C,
1725                                               unsigned NumClauses,
1726                                               unsigned CollapsedNum,
1727                                               EmptyShell) {
1728   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForDirective),
1729                                            llvm::alignOf<OMPClause *>());
1730   void *Mem =
1731       C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1732                  sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for));
1733   return new (Mem) OMPForDirective(CollapsedNum, NumClauses);
1734 }
1735 
1736 OMPForSimdDirective *
1737 OMPForSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1738                             SourceLocation EndLoc, unsigned CollapsedNum,
1739                             ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
1740                             const HelperExprs &Exprs) {
1741   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForSimdDirective),
1742                                            llvm::alignOf<OMPClause *>());
1743   void *Mem =
1744       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1745                  sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for_simd));
1746   OMPForSimdDirective *Dir = new (Mem)
1747       OMPForSimdDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1748   Dir->setClauses(Clauses);
1749   Dir->setAssociatedStmt(AssociatedStmt);
1750   Dir->setIterationVariable(Exprs.IterationVarRef);
1751   Dir->setLastIteration(Exprs.LastIteration);
1752   Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1753   Dir->setPreCond(Exprs.PreCond);
1754   Dir->setCond(Exprs.Cond);
1755   Dir->setInit(Exprs.Init);
1756   Dir->setInc(Exprs.Inc);
1757   Dir->setIsLastIterVariable(Exprs.IL);
1758   Dir->setLowerBoundVariable(Exprs.LB);
1759   Dir->setUpperBoundVariable(Exprs.UB);
1760   Dir->setStrideVariable(Exprs.ST);
1761   Dir->setEnsureUpperBound(Exprs.EUB);
1762   Dir->setNextLowerBound(Exprs.NLB);
1763   Dir->setNextUpperBound(Exprs.NUB);
1764   Dir->setCounters(Exprs.Counters);
1765   Dir->setPrivateCounters(Exprs.PrivateCounters);
1766   Dir->setUpdates(Exprs.Updates);
1767   Dir->setFinals(Exprs.Finals);
1768   return Dir;
1769 }
1770 
1771 OMPForSimdDirective *OMPForSimdDirective::CreateEmpty(const ASTContext &C,
1772                                                       unsigned NumClauses,
1773                                                       unsigned CollapsedNum,
1774                                                       EmptyShell) {
1775   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForSimdDirective),
1776                                            llvm::alignOf<OMPClause *>());
1777   void *Mem =
1778       C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1779                  sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for_simd));
1780   return new (Mem) OMPForSimdDirective(CollapsedNum, NumClauses);
1781 }
1782 
1783 OMPSectionsDirective *OMPSectionsDirective::Create(
1784     const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1785     ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1786   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1787                                            llvm::alignOf<OMPClause *>());
1788   void *Mem =
1789       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1790   OMPSectionsDirective *Dir =
1791       new (Mem) OMPSectionsDirective(StartLoc, EndLoc, Clauses.size());
1792   Dir->setClauses(Clauses);
1793   Dir->setAssociatedStmt(AssociatedStmt);
1794   return Dir;
1795 }
1796 
1797 OMPSectionsDirective *OMPSectionsDirective::CreateEmpty(const ASTContext &C,
1798                                                         unsigned NumClauses,
1799                                                         EmptyShell) {
1800   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1801                                            llvm::alignOf<OMPClause *>());
1802   void *Mem =
1803       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1804   return new (Mem) OMPSectionsDirective(NumClauses);
1805 }
1806 
1807 OMPSectionDirective *OMPSectionDirective::Create(const ASTContext &C,
1808                                                  SourceLocation StartLoc,
1809                                                  SourceLocation EndLoc,
1810                                                  Stmt *AssociatedStmt) {
1811   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionDirective),
1812                                            llvm::alignOf<Stmt *>());
1813   void *Mem = C.Allocate(Size + sizeof(Stmt *));
1814   OMPSectionDirective *Dir = new (Mem) OMPSectionDirective(StartLoc, EndLoc);
1815   Dir->setAssociatedStmt(AssociatedStmt);
1816   return Dir;
1817 }
1818 
1819 OMPSectionDirective *OMPSectionDirective::CreateEmpty(const ASTContext &C,
1820                                                       EmptyShell) {
1821   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionDirective),
1822                                            llvm::alignOf<Stmt *>());
1823   void *Mem = C.Allocate(Size + sizeof(Stmt *));
1824   return new (Mem) OMPSectionDirective();
1825 }
1826 
1827 OMPSingleDirective *OMPSingleDirective::Create(const ASTContext &C,
1828                                                SourceLocation StartLoc,
1829                                                SourceLocation EndLoc,
1830                                                ArrayRef<OMPClause *> Clauses,
1831                                                Stmt *AssociatedStmt) {
1832   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSingleDirective),
1833                                            llvm::alignOf<OMPClause *>());
1834   void *Mem =
1835       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1836   OMPSingleDirective *Dir =
1837       new (Mem) OMPSingleDirective(StartLoc, EndLoc, Clauses.size());
1838   Dir->setClauses(Clauses);
1839   Dir->setAssociatedStmt(AssociatedStmt);
1840   return Dir;
1841 }
1842 
1843 OMPSingleDirective *OMPSingleDirective::CreateEmpty(const ASTContext &C,
1844                                                     unsigned NumClauses,
1845                                                     EmptyShell) {
1846   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSingleDirective),
1847                                            llvm::alignOf<OMPClause *>());
1848   void *Mem =
1849       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1850   return new (Mem) OMPSingleDirective(NumClauses);
1851 }
1852 
1853 OMPMasterDirective *OMPMasterDirective::Create(const ASTContext &C,
1854                                                SourceLocation StartLoc,
1855                                                SourceLocation EndLoc,
1856                                                Stmt *AssociatedStmt) {
1857   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPMasterDirective),
1858                                            llvm::alignOf<Stmt *>());
1859   void *Mem = C.Allocate(Size + sizeof(Stmt *));
1860   OMPMasterDirective *Dir = new (Mem) OMPMasterDirective(StartLoc, EndLoc);
1861   Dir->setAssociatedStmt(AssociatedStmt);
1862   return Dir;
1863 }
1864 
1865 OMPMasterDirective *OMPMasterDirective::CreateEmpty(const ASTContext &C,
1866                                                     EmptyShell) {
1867   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPMasterDirective),
1868                                            llvm::alignOf<Stmt *>());
1869   void *Mem = C.Allocate(Size + sizeof(Stmt *));
1870   return new (Mem) OMPMasterDirective();
1871 }
1872 
1873 OMPCriticalDirective *OMPCriticalDirective::Create(
1874     const ASTContext &C, const DeclarationNameInfo &Name,
1875     SourceLocation StartLoc, SourceLocation EndLoc, Stmt *AssociatedStmt) {
1876   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCriticalDirective),
1877                                            llvm::alignOf<Stmt *>());
1878   void *Mem = C.Allocate(Size + sizeof(Stmt *));
1879   OMPCriticalDirective *Dir =
1880       new (Mem) OMPCriticalDirective(Name, StartLoc, EndLoc);
1881   Dir->setAssociatedStmt(AssociatedStmt);
1882   return Dir;
1883 }
1884 
1885 OMPCriticalDirective *OMPCriticalDirective::CreateEmpty(const ASTContext &C,
1886                                                         EmptyShell) {
1887   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCriticalDirective),
1888                                            llvm::alignOf<Stmt *>());
1889   void *Mem = C.Allocate(Size + sizeof(Stmt *));
1890   return new (Mem) OMPCriticalDirective();
1891 }
1892 
1893 OMPParallelForDirective *OMPParallelForDirective::Create(
1894     const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1895     unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
1896     const HelperExprs &Exprs) {
1897   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForDirective),
1898                                            llvm::alignOf<OMPClause *>());
1899   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1900                          sizeof(Stmt *) *
1901                              numLoopChildren(CollapsedNum, OMPD_parallel_for));
1902   OMPParallelForDirective *Dir = new (Mem)
1903       OMPParallelForDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1904   Dir->setClauses(Clauses);
1905   Dir->setAssociatedStmt(AssociatedStmt);
1906   Dir->setIterationVariable(Exprs.IterationVarRef);
1907   Dir->setLastIteration(Exprs.LastIteration);
1908   Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1909   Dir->setPreCond(Exprs.PreCond);
1910   Dir->setCond(Exprs.Cond);
1911   Dir->setInit(Exprs.Init);
1912   Dir->setInc(Exprs.Inc);
1913   Dir->setIsLastIterVariable(Exprs.IL);
1914   Dir->setLowerBoundVariable(Exprs.LB);
1915   Dir->setUpperBoundVariable(Exprs.UB);
1916   Dir->setStrideVariable(Exprs.ST);
1917   Dir->setEnsureUpperBound(Exprs.EUB);
1918   Dir->setNextLowerBound(Exprs.NLB);
1919   Dir->setNextUpperBound(Exprs.NUB);
1920   Dir->setCounters(Exprs.Counters);
1921   Dir->setPrivateCounters(Exprs.PrivateCounters);
1922   Dir->setUpdates(Exprs.Updates);
1923   Dir->setFinals(Exprs.Finals);
1924   return Dir;
1925 }
1926 
1927 OMPParallelForDirective *
1928 OMPParallelForDirective::CreateEmpty(const ASTContext &C, unsigned NumClauses,
1929                                      unsigned CollapsedNum, EmptyShell) {
1930   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForDirective),
1931                                            llvm::alignOf<OMPClause *>());
1932   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1933                          sizeof(Stmt *) *
1934                              numLoopChildren(CollapsedNum, OMPD_parallel_for));
1935   return new (Mem) OMPParallelForDirective(CollapsedNum, NumClauses);
1936 }
1937 
1938 OMPParallelForSimdDirective *OMPParallelForSimdDirective::Create(
1939     const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1940     unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
1941     const HelperExprs &Exprs) {
1942   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForSimdDirective),
1943                                            llvm::alignOf<OMPClause *>());
1944   void *Mem = C.Allocate(
1945       Size + sizeof(OMPClause *) * Clauses.size() +
1946       sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_parallel_for_simd));
1947   OMPParallelForSimdDirective *Dir = new (Mem) OMPParallelForSimdDirective(
1948       StartLoc, EndLoc, CollapsedNum, Clauses.size());
1949   Dir->setClauses(Clauses);
1950   Dir->setAssociatedStmt(AssociatedStmt);
1951   Dir->setIterationVariable(Exprs.IterationVarRef);
1952   Dir->setLastIteration(Exprs.LastIteration);
1953   Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1954   Dir->setPreCond(Exprs.PreCond);
1955   Dir->setCond(Exprs.Cond);
1956   Dir->setInit(Exprs.Init);
1957   Dir->setInc(Exprs.Inc);
1958   Dir->setIsLastIterVariable(Exprs.IL);
1959   Dir->setLowerBoundVariable(Exprs.LB);
1960   Dir->setUpperBoundVariable(Exprs.UB);
1961   Dir->setStrideVariable(Exprs.ST);
1962   Dir->setEnsureUpperBound(Exprs.EUB);
1963   Dir->setNextLowerBound(Exprs.NLB);
1964   Dir->setNextUpperBound(Exprs.NUB);
1965   Dir->setCounters(Exprs.Counters);
1966   Dir->setPrivateCounters(Exprs.PrivateCounters);
1967   Dir->setUpdates(Exprs.Updates);
1968   Dir->setFinals(Exprs.Finals);
1969   return Dir;
1970 }
1971 
1972 OMPParallelForSimdDirective *
1973 OMPParallelForSimdDirective::CreateEmpty(const ASTContext &C,
1974                                          unsigned NumClauses,
1975                                          unsigned CollapsedNum, EmptyShell) {
1976   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForSimdDirective),
1977                                            llvm::alignOf<OMPClause *>());
1978   void *Mem = C.Allocate(
1979       Size + sizeof(OMPClause *) * NumClauses +
1980       sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_parallel_for_simd));
1981   return new (Mem) OMPParallelForSimdDirective(CollapsedNum, NumClauses);
1982 }
1983 
1984 OMPParallelSectionsDirective *OMPParallelSectionsDirective::Create(
1985     const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1986     ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1987   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelSectionsDirective),
1988                                            llvm::alignOf<OMPClause *>());
1989   void *Mem =
1990       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1991   OMPParallelSectionsDirective *Dir =
1992       new (Mem) OMPParallelSectionsDirective(StartLoc, EndLoc, Clauses.size());
1993   Dir->setClauses(Clauses);
1994   Dir->setAssociatedStmt(AssociatedStmt);
1995   return Dir;
1996 }
1997 
1998 OMPParallelSectionsDirective *
1999 OMPParallelSectionsDirective::CreateEmpty(const ASTContext &C,
2000                                           unsigned NumClauses, EmptyShell) {
2001   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelSectionsDirective),
2002                                            llvm::alignOf<OMPClause *>());
2003   void *Mem =
2004       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
2005   return new (Mem) OMPParallelSectionsDirective(NumClauses);
2006 }
2007 
2008 OMPTaskDirective *OMPTaskDirective::Create(const ASTContext &C,
2009                                            SourceLocation StartLoc,
2010                                            SourceLocation EndLoc,
2011                                            ArrayRef<OMPClause *> Clauses,
2012                                            Stmt *AssociatedStmt) {
2013   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskDirective),
2014                                            llvm::alignOf<OMPClause *>());
2015   void *Mem =
2016       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
2017   OMPTaskDirective *Dir =
2018       new (Mem) OMPTaskDirective(StartLoc, EndLoc, Clauses.size());
2019   Dir->setClauses(Clauses);
2020   Dir->setAssociatedStmt(AssociatedStmt);
2021   return Dir;
2022 }
2023 
2024 OMPTaskDirective *OMPTaskDirective::CreateEmpty(const ASTContext &C,
2025                                                 unsigned NumClauses,
2026                                                 EmptyShell) {
2027   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskDirective),
2028                                            llvm::alignOf<OMPClause *>());
2029   void *Mem =
2030       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
2031   return new (Mem) OMPTaskDirective(NumClauses);
2032 }
2033 
2034 OMPTaskyieldDirective *OMPTaskyieldDirective::Create(const ASTContext &C,
2035                                                      SourceLocation StartLoc,
2036                                                      SourceLocation EndLoc) {
2037   void *Mem = C.Allocate(sizeof(OMPTaskyieldDirective));
2038   OMPTaskyieldDirective *Dir =
2039       new (Mem) OMPTaskyieldDirective(StartLoc, EndLoc);
2040   return Dir;
2041 }
2042 
2043 OMPTaskyieldDirective *OMPTaskyieldDirective::CreateEmpty(const ASTContext &C,
2044                                                           EmptyShell) {
2045   void *Mem = C.Allocate(sizeof(OMPTaskyieldDirective));
2046   return new (Mem) OMPTaskyieldDirective();
2047 }
2048 
2049 OMPBarrierDirective *OMPBarrierDirective::Create(const ASTContext &C,
2050                                                  SourceLocation StartLoc,
2051                                                  SourceLocation EndLoc) {
2052   void *Mem = C.Allocate(sizeof(OMPBarrierDirective));
2053   OMPBarrierDirective *Dir = new (Mem) OMPBarrierDirective(StartLoc, EndLoc);
2054   return Dir;
2055 }
2056 
2057 OMPBarrierDirective *OMPBarrierDirective::CreateEmpty(const ASTContext &C,
2058                                                       EmptyShell) {
2059   void *Mem = C.Allocate(sizeof(OMPBarrierDirective));
2060   return new (Mem) OMPBarrierDirective();
2061 }
2062 
2063 OMPTaskwaitDirective *OMPTaskwaitDirective::Create(const ASTContext &C,
2064                                                    SourceLocation StartLoc,
2065                                                    SourceLocation EndLoc) {
2066   void *Mem = C.Allocate(sizeof(OMPTaskwaitDirective));
2067   OMPTaskwaitDirective *Dir = new (Mem) OMPTaskwaitDirective(StartLoc, EndLoc);
2068   return Dir;
2069 }
2070 
2071 OMPTaskwaitDirective *OMPTaskwaitDirective::CreateEmpty(const ASTContext &C,
2072                                                         EmptyShell) {
2073   void *Mem = C.Allocate(sizeof(OMPTaskwaitDirective));
2074   return new (Mem) OMPTaskwaitDirective();
2075 }
2076 
2077 OMPTaskgroupDirective *OMPTaskgroupDirective::Create(const ASTContext &C,
2078                                                      SourceLocation StartLoc,
2079                                                      SourceLocation EndLoc,
2080                                                      Stmt *AssociatedStmt) {
2081   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskgroupDirective),
2082                                            llvm::alignOf<Stmt *>());
2083   void *Mem = C.Allocate(Size + sizeof(Stmt *));
2084   OMPTaskgroupDirective *Dir =
2085       new (Mem) OMPTaskgroupDirective(StartLoc, EndLoc);
2086   Dir->setAssociatedStmt(AssociatedStmt);
2087   return Dir;
2088 }
2089 
2090 OMPTaskgroupDirective *OMPTaskgroupDirective::CreateEmpty(const ASTContext &C,
2091                                                           EmptyShell) {
2092   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskgroupDirective),
2093                                            llvm::alignOf<Stmt *>());
2094   void *Mem = C.Allocate(Size + sizeof(Stmt *));
2095   return new (Mem) OMPTaskgroupDirective();
2096 }
2097 
2098 OMPCancellationPointDirective *OMPCancellationPointDirective::Create(
2099     const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
2100     OpenMPDirectiveKind CancelRegion) {
2101   unsigned Size = llvm::RoundUpToAlignment(
2102       sizeof(OMPCancellationPointDirective), llvm::alignOf<Stmt *>());
2103   void *Mem = C.Allocate(Size);
2104   OMPCancellationPointDirective *Dir =
2105       new (Mem) OMPCancellationPointDirective(StartLoc, EndLoc);
2106   Dir->setCancelRegion(CancelRegion);
2107   return Dir;
2108 }
2109 
2110 OMPCancellationPointDirective *
2111 OMPCancellationPointDirective::CreateEmpty(const ASTContext &C, EmptyShell) {
2112   unsigned Size = llvm::RoundUpToAlignment(
2113       sizeof(OMPCancellationPointDirective), llvm::alignOf<Stmt *>());
2114   void *Mem = C.Allocate(Size);
2115   return new (Mem) OMPCancellationPointDirective();
2116 }
2117 
2118 OMPCancelDirective *
2119 OMPCancelDirective::Create(const ASTContext &C, SourceLocation StartLoc,
2120                            SourceLocation EndLoc,
2121                            OpenMPDirectiveKind CancelRegion) {
2122   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCancelDirective),
2123                                            llvm::alignOf<Stmt *>());
2124   void *Mem = C.Allocate(Size);
2125   OMPCancelDirective *Dir = new (Mem) OMPCancelDirective(StartLoc, EndLoc);
2126   Dir->setCancelRegion(CancelRegion);
2127   return Dir;
2128 }
2129 
2130 OMPCancelDirective *OMPCancelDirective::CreateEmpty(const ASTContext &C,
2131                                                     EmptyShell) {
2132   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCancelDirective),
2133                                            llvm::alignOf<Stmt *>());
2134   void *Mem = C.Allocate(Size);
2135   return new (Mem) OMPCancelDirective();
2136 }
2137 
2138 OMPFlushDirective *OMPFlushDirective::Create(const ASTContext &C,
2139                                              SourceLocation StartLoc,
2140                                              SourceLocation EndLoc,
2141                                              ArrayRef<OMPClause *> Clauses) {
2142   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPFlushDirective),
2143                                            llvm::alignOf<OMPClause *>());
2144   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size());
2145   OMPFlushDirective *Dir =
2146       new (Mem) OMPFlushDirective(StartLoc, EndLoc, Clauses.size());
2147   Dir->setClauses(Clauses);
2148   return Dir;
2149 }
2150 
2151 OMPFlushDirective *OMPFlushDirective::CreateEmpty(const ASTContext &C,
2152                                                   unsigned NumClauses,
2153                                                   EmptyShell) {
2154   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPFlushDirective),
2155                                            llvm::alignOf<OMPClause *>());
2156   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses);
2157   return new (Mem) OMPFlushDirective(NumClauses);
2158 }
2159 
2160 OMPOrderedDirective *OMPOrderedDirective::Create(const ASTContext &C,
2161                                                  SourceLocation StartLoc,
2162                                                  SourceLocation EndLoc,
2163                                                  Stmt *AssociatedStmt) {
2164   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPOrderedDirective),
2165                                            llvm::alignOf<Stmt *>());
2166   void *Mem = C.Allocate(Size + sizeof(Stmt *));
2167   OMPOrderedDirective *Dir = new (Mem) OMPOrderedDirective(StartLoc, EndLoc);
2168   Dir->setAssociatedStmt(AssociatedStmt);
2169   return Dir;
2170 }
2171 
2172 OMPOrderedDirective *OMPOrderedDirective::CreateEmpty(const ASTContext &C,
2173                                                       EmptyShell) {
2174   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPOrderedDirective),
2175                                            llvm::alignOf<Stmt *>());
2176   void *Mem = C.Allocate(Size + sizeof(Stmt *));
2177   return new (Mem) OMPOrderedDirective();
2178 }
2179 
2180 OMPAtomicDirective *OMPAtomicDirective::Create(
2181     const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
2182     ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt, Expr *X, Expr *V,
2183     Expr *E, Expr *UE, bool IsXLHSInRHSPart, bool IsPostfixUpdate) {
2184   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPAtomicDirective),
2185                                            llvm::alignOf<OMPClause *>());
2186   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
2187                          5 * sizeof(Stmt *));
2188   OMPAtomicDirective *Dir =
2189       new (Mem) OMPAtomicDirective(StartLoc, EndLoc, Clauses.size());
2190   Dir->setClauses(Clauses);
2191   Dir->setAssociatedStmt(AssociatedStmt);
2192   Dir->setX(X);
2193   Dir->setV(V);
2194   Dir->setExpr(E);
2195   Dir->setUpdateExpr(UE);
2196   Dir->IsXLHSInRHSPart = IsXLHSInRHSPart;
2197   Dir->IsPostfixUpdate = IsPostfixUpdate;
2198   return Dir;
2199 }
2200 
2201 OMPAtomicDirective *OMPAtomicDirective::CreateEmpty(const ASTContext &C,
2202                                                     unsigned NumClauses,
2203                                                     EmptyShell) {
2204   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPAtomicDirective),
2205                                            llvm::alignOf<OMPClause *>());
2206   void *Mem =
2207       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + 5 * sizeof(Stmt *));
2208   return new (Mem) OMPAtomicDirective(NumClauses);
2209 }
2210 
2211 OMPTargetDirective *OMPTargetDirective::Create(const ASTContext &C,
2212                                                SourceLocation StartLoc,
2213                                                SourceLocation EndLoc,
2214                                                ArrayRef<OMPClause *> Clauses,
2215                                                Stmt *AssociatedStmt) {
2216   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTargetDirective),
2217                                            llvm::alignOf<OMPClause *>());
2218   void *Mem =
2219       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
2220   OMPTargetDirective *Dir =
2221       new (Mem) OMPTargetDirective(StartLoc, EndLoc, Clauses.size());
2222   Dir->setClauses(Clauses);
2223   Dir->setAssociatedStmt(AssociatedStmt);
2224   return Dir;
2225 }
2226 
2227 OMPTargetDirective *OMPTargetDirective::CreateEmpty(const ASTContext &C,
2228                                                     unsigned NumClauses,
2229                                                     EmptyShell) {
2230   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTargetDirective),
2231                                            llvm::alignOf<OMPClause *>());
2232   void *Mem =
2233       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
2234   return new (Mem) OMPTargetDirective(NumClauses);
2235 }
2236 
2237 OMPTargetDataDirective *OMPTargetDataDirective::Create(
2238     const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
2239     ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
2240   void *Mem =
2241       C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPTargetDataDirective),
2242                                           llvm::alignOf<OMPClause *>()) +
2243                  sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
2244   OMPTargetDataDirective *Dir =
2245       new (Mem) OMPTargetDataDirective(StartLoc, EndLoc, Clauses.size());
2246   Dir->setClauses(Clauses);
2247   Dir->setAssociatedStmt(AssociatedStmt);
2248   return Dir;
2249 }
2250 
2251 OMPTargetDataDirective *OMPTargetDataDirective::CreateEmpty(const ASTContext &C,
2252                                                             unsigned N,
2253                                                             EmptyShell) {
2254   void *Mem =
2255       C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPTargetDataDirective),
2256                                           llvm::alignOf<OMPClause *>()) +
2257                  sizeof(OMPClause *) * N + sizeof(Stmt *));
2258   return new (Mem) OMPTargetDataDirective(N);
2259 }
2260 
2261 OMPTeamsDirective *OMPTeamsDirective::Create(const ASTContext &C,
2262                                              SourceLocation StartLoc,
2263                                              SourceLocation EndLoc,
2264                                              ArrayRef<OMPClause *> Clauses,
2265                                              Stmt *AssociatedStmt) {
2266   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTeamsDirective),
2267                                            llvm::alignOf<OMPClause *>());
2268   void *Mem =
2269       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
2270   OMPTeamsDirective *Dir =
2271       new (Mem) OMPTeamsDirective(StartLoc, EndLoc, Clauses.size());
2272   Dir->setClauses(Clauses);
2273   Dir->setAssociatedStmt(AssociatedStmt);
2274   return Dir;
2275 }
2276 
2277 OMPTeamsDirective *OMPTeamsDirective::CreateEmpty(const ASTContext &C,
2278                                                   unsigned NumClauses,
2279                                                   EmptyShell) {
2280   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTeamsDirective),
2281                                            llvm::alignOf<OMPClause *>());
2282   void *Mem =
2283       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
2284   return new (Mem) OMPTeamsDirective(NumClauses);
2285 }
2286