xref: /llvm-project-15.0.7/clang/lib/AST/Stmt.cpp (revision 885e78cb)
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.push_back(AsmStringPiece(N, 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.push_back(AsmStringPiece(N, 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   size_t size = str.size();
728   char *buffer = new (C) char[size];
729   memcpy(buffer, str.data(), size);
730   return StringRef(buffer, size);
731 }
732 
733 void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
734                            ArrayRef<Token> asmtoks,
735                            ArrayRef<StringRef> constraints,
736                            ArrayRef<Expr*> exprs,
737                            ArrayRef<StringRef> clobbers) {
738   assert(NumAsmToks == asmtoks.size());
739   assert(NumClobbers == clobbers.size());
740 
741   unsigned NumExprs = exprs.size();
742   assert(NumExprs == NumOutputs + NumInputs);
743   assert(NumExprs == constraints.size());
744 
745   AsmStr = copyIntoContext(C, asmstr);
746 
747   Exprs = new (C) Stmt*[NumExprs];
748   for (unsigned i = 0, e = NumExprs; i != e; ++i)
749     Exprs[i] = exprs[i];
750 
751   AsmToks = new (C) Token[NumAsmToks];
752   for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
753     AsmToks[i] = asmtoks[i];
754 
755   Constraints = new (C) StringRef[NumExprs];
756   for (unsigned i = 0, e = NumExprs; i != e; ++i) {
757     Constraints[i] = copyIntoContext(C, constraints[i]);
758   }
759 
760   Clobbers = new (C) StringRef[NumClobbers];
761   for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
762     // FIXME: Avoid the allocation/copy if at all possible.
763     Clobbers[i] = copyIntoContext(C, clobbers[i]);
764   }
765 }
766 
767 ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
768                                              Stmt *Body,  SourceLocation FCL,
769                                              SourceLocation RPL)
770 : Stmt(ObjCForCollectionStmtClass) {
771   SubExprs[ELEM] = Elem;
772   SubExprs[COLLECTION] = Collect;
773   SubExprs[BODY] = Body;
774   ForLoc = FCL;
775   RParenLoc = RPL;
776 }
777 
778 ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
779                              Stmt **CatchStmts, unsigned NumCatchStmts,
780                              Stmt *atFinallyStmt)
781   : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
782     NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != nullptr) {
783   Stmt **Stmts = getStmts();
784   Stmts[0] = atTryStmt;
785   for (unsigned I = 0; I != NumCatchStmts; ++I)
786     Stmts[I + 1] = CatchStmts[I];
787 
788   if (HasFinally)
789     Stmts[NumCatchStmts + 1] = atFinallyStmt;
790 }
791 
792 ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
793                                      SourceLocation atTryLoc,
794                                      Stmt *atTryStmt,
795                                      Stmt **CatchStmts,
796                                      unsigned NumCatchStmts,
797                                      Stmt *atFinallyStmt) {
798   unsigned Size = sizeof(ObjCAtTryStmt) +
799     (1 + NumCatchStmts + (atFinallyStmt != nullptr)) * sizeof(Stmt *);
800   void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
801   return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
802                                  atFinallyStmt);
803 }
804 
805 ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
806                                           unsigned NumCatchStmts,
807                                           bool HasFinally) {
808   unsigned Size = sizeof(ObjCAtTryStmt) +
809     (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
810   void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
811   return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
812 }
813 
814 SourceLocation ObjCAtTryStmt::getLocEnd() const {
815   if (HasFinally)
816     return getFinallyStmt()->getLocEnd();
817   if (NumCatchStmts)
818     return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
819   return getTryBody()->getLocEnd();
820 }
821 
822 CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
823                                Stmt *tryBlock, ArrayRef<Stmt*> handlers) {
824   std::size_t Size = sizeof(CXXTryStmt);
825   Size += ((handlers.size() + 1) * sizeof(Stmt));
826 
827   void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
828   return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
829 }
830 
831 CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
832                                unsigned numHandlers) {
833   std::size_t Size = sizeof(CXXTryStmt);
834   Size += ((numHandlers + 1) * sizeof(Stmt));
835 
836   void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
837   return new (Mem) CXXTryStmt(Empty, numHandlers);
838 }
839 
840 CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
841                        ArrayRef<Stmt*> handlers)
842   : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
843   Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
844   Stmts[0] = tryBlock;
845   std::copy(handlers.begin(), handlers.end(), Stmts + 1);
846 }
847 
848 CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
849                                  Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
850                                  Stmt *Body, SourceLocation FL,
851                                  SourceLocation CL, SourceLocation RPL)
852   : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
853   SubExprs[RANGE] = Range;
854   SubExprs[BEGINEND] = BeginEndStmt;
855   SubExprs[COND] = Cond;
856   SubExprs[INC] = Inc;
857   SubExprs[LOOPVAR] = LoopVar;
858   SubExprs[BODY] = Body;
859 }
860 
861 Expr *CXXForRangeStmt::getRangeInit() {
862   DeclStmt *RangeStmt = getRangeStmt();
863   VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
864   assert(RangeDecl && "for-range should have a single var decl");
865   return RangeDecl->getInit();
866 }
867 
868 const Expr *CXXForRangeStmt::getRangeInit() const {
869   return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
870 }
871 
872 VarDecl *CXXForRangeStmt::getLoopVariable() {
873   Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
874   assert(LV && "No loop variable in CXXForRangeStmt");
875   return cast<VarDecl>(LV);
876 }
877 
878 const VarDecl *CXXForRangeStmt::getLoopVariable() const {
879   return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
880 }
881 
882 IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
883                Stmt *then, SourceLocation EL, Stmt *elsev)
884   : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
885 {
886   setConditionVariable(C, var);
887   SubExprs[COND] = cond;
888   SubExprs[THEN] = then;
889   SubExprs[ELSE] = elsev;
890 }
891 
892 VarDecl *IfStmt::getConditionVariable() const {
893   if (!SubExprs[VAR])
894     return nullptr;
895 
896   DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
897   return cast<VarDecl>(DS->getSingleDecl());
898 }
899 
900 void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
901   if (!V) {
902     SubExprs[VAR] = nullptr;
903     return;
904   }
905 
906   SourceRange VarRange = V->getSourceRange();
907   SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
908                                    VarRange.getEnd());
909 }
910 
911 ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
912                  Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
913                  SourceLocation RP)
914   : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
915 {
916   SubExprs[INIT] = Init;
917   setConditionVariable(C, condVar);
918   SubExprs[COND] = Cond;
919   SubExprs[INC] = Inc;
920   SubExprs[BODY] = Body;
921 }
922 
923 VarDecl *ForStmt::getConditionVariable() const {
924   if (!SubExprs[CONDVAR])
925     return nullptr;
926 
927   DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
928   return cast<VarDecl>(DS->getSingleDecl());
929 }
930 
931 void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
932   if (!V) {
933     SubExprs[CONDVAR] = nullptr;
934     return;
935   }
936 
937   SourceRange VarRange = V->getSourceRange();
938   SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
939                                        VarRange.getEnd());
940 }
941 
942 SwitchStmt::SwitchStmt(const ASTContext &C, VarDecl *Var, Expr *cond)
943   : Stmt(SwitchStmtClass), FirstCase(nullptr), AllEnumCasesCovered(0)
944 {
945   setConditionVariable(C, Var);
946   SubExprs[COND] = cond;
947   SubExprs[BODY] = nullptr;
948 }
949 
950 VarDecl *SwitchStmt::getConditionVariable() const {
951   if (!SubExprs[VAR])
952     return nullptr;
953 
954   DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
955   return cast<VarDecl>(DS->getSingleDecl());
956 }
957 
958 void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
959   if (!V) {
960     SubExprs[VAR] = nullptr;
961     return;
962   }
963 
964   SourceRange VarRange = V->getSourceRange();
965   SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
966                                    VarRange.getEnd());
967 }
968 
969 Stmt *SwitchCase::getSubStmt() {
970   if (isa<CaseStmt>(this))
971     return cast<CaseStmt>(this)->getSubStmt();
972   return cast<DefaultStmt>(this)->getSubStmt();
973 }
974 
975 WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
976                      SourceLocation WL)
977   : Stmt(WhileStmtClass) {
978   setConditionVariable(C, Var);
979   SubExprs[COND] = cond;
980   SubExprs[BODY] = body;
981   WhileLoc = WL;
982 }
983 
984 VarDecl *WhileStmt::getConditionVariable() const {
985   if (!SubExprs[VAR])
986     return nullptr;
987 
988   DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
989   return cast<VarDecl>(DS->getSingleDecl());
990 }
991 
992 void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
993   if (!V) {
994     SubExprs[VAR] = nullptr;
995     return;
996   }
997 
998   SourceRange VarRange = V->getSourceRange();
999   SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
1000                                    VarRange.getEnd());
1001 }
1002 
1003 // IndirectGotoStmt
1004 LabelDecl *IndirectGotoStmt::getConstantTarget() {
1005   if (AddrLabelExpr *E =
1006         dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
1007     return E->getLabel();
1008   return nullptr;
1009 }
1010 
1011 // ReturnStmt
1012 const Expr* ReturnStmt::getRetValue() const {
1013   return cast_or_null<Expr>(RetExpr);
1014 }
1015 Expr* ReturnStmt::getRetValue() {
1016   return cast_or_null<Expr>(RetExpr);
1017 }
1018 
1019 SEHTryStmt::SEHTryStmt(bool IsCXXTry,
1020                        SourceLocation TryLoc,
1021                        Stmt *TryBlock,
1022                        Stmt *Handler)
1023   : Stmt(SEHTryStmtClass),
1024     IsCXXTry(IsCXXTry),
1025     TryLoc(TryLoc)
1026 {
1027   Children[TRY]     = TryBlock;
1028   Children[HANDLER] = Handler;
1029 }
1030 
1031 SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
1032                                SourceLocation TryLoc, Stmt *TryBlock,
1033                                Stmt *Handler) {
1034   return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
1035 }
1036 
1037 SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
1038   return dyn_cast<SEHExceptStmt>(getHandler());
1039 }
1040 
1041 SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
1042   return dyn_cast<SEHFinallyStmt>(getHandler());
1043 }
1044 
1045 SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
1046                              Expr *FilterExpr,
1047                              Stmt *Block)
1048   : Stmt(SEHExceptStmtClass),
1049     Loc(Loc)
1050 {
1051   Children[FILTER_EXPR] = FilterExpr;
1052   Children[BLOCK]       = Block;
1053 }
1054 
1055 SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
1056                                      Expr *FilterExpr, Stmt *Block) {
1057   return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1058 }
1059 
1060 SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1061                                Stmt *Block)
1062   : Stmt(SEHFinallyStmtClass),
1063     Loc(Loc),
1064     Block(Block)
1065 {}
1066 
1067 SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
1068                                        Stmt *Block) {
1069   return new(C)SEHFinallyStmt(Loc,Block);
1070 }
1071 
1072 CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1073   unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1074 
1075   // Offset of the first Capture object.
1076   unsigned FirstCaptureOffset =
1077     llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1078 
1079   return reinterpret_cast<Capture *>(
1080       reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1081       + FirstCaptureOffset);
1082 }
1083 
1084 CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1085                            ArrayRef<Capture> Captures,
1086                            ArrayRef<Expr *> CaptureInits,
1087                            CapturedDecl *CD,
1088                            RecordDecl *RD)
1089   : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
1090     CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
1091   assert( S && "null captured statement");
1092   assert(CD && "null captured declaration for captured statement");
1093   assert(RD && "null record declaration for captured statement");
1094 
1095   // Copy initialization expressions.
1096   Stmt **Stored = getStoredStmts();
1097   for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1098     *Stored++ = CaptureInits[I];
1099 
1100   // Copy the statement being captured.
1101   *Stored = S;
1102 
1103   // Copy all Capture objects.
1104   Capture *Buffer = getStoredCaptures();
1105   std::copy(Captures.begin(), Captures.end(), Buffer);
1106 }
1107 
1108 CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1109   : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
1110     CapDeclAndKind(nullptr, CR_Default), TheRecordDecl(nullptr) {
1111   getStoredStmts()[NumCaptures] = nullptr;
1112 }
1113 
1114 CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
1115                                    CapturedRegionKind Kind,
1116                                    ArrayRef<Capture> Captures,
1117                                    ArrayRef<Expr *> CaptureInits,
1118                                    CapturedDecl *CD,
1119                                    RecordDecl *RD) {
1120   // The layout is
1121   //
1122   // -----------------------------------------------------------
1123   // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1124   // ----------------^-------------------^----------------------
1125   //                 getStoredStmts()    getStoredCaptures()
1126   //
1127   // where S is the statement being captured.
1128   //
1129   assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1130 
1131   unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1132   if (!Captures.empty()) {
1133     // Realign for the following Capture array.
1134     Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1135     Size += sizeof(Capture) * Captures.size();
1136   }
1137 
1138   void *Mem = Context.Allocate(Size);
1139   return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
1140 }
1141 
1142 CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
1143                                                unsigned NumCaptures) {
1144   unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1145   if (NumCaptures > 0) {
1146     // Realign for the following Capture array.
1147     Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1148     Size += sizeof(Capture) * NumCaptures;
1149   }
1150 
1151   void *Mem = Context.Allocate(Size);
1152   return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1153 }
1154 
1155 Stmt::child_range CapturedStmt::children() {
1156   // Children are captured field initilizers.
1157   return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
1158 }
1159 
1160 bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
1161   for (const auto &I : captures()) {
1162     if (!I.capturesVariable())
1163       continue;
1164 
1165     // This does not handle variable redeclarations. This should be
1166     // extended to capture variables with redeclarations, for example
1167     // a thread-private variable in OpenMP.
1168     if (I.getCapturedVar() == Var)
1169       return true;
1170   }
1171 
1172   return false;
1173 }
1174 
1175 StmtRange OMPClause::children() {
1176   switch(getClauseKind()) {
1177   default : break;
1178 #define OPENMP_CLAUSE(Name, Class)                                       \
1179   case OMPC_ ## Name : return static_cast<Class *>(this)->children();
1180 #include "clang/Basic/OpenMPKinds.def"
1181   }
1182   llvm_unreachable("unknown OMPClause");
1183 }
1184 
1185 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
1186   assert(VL.size() == varlist_size() &&
1187          "Number of private copies is not the same as the preallocated buffer");
1188   std::copy(VL.begin(), VL.end(), varlist_end());
1189 }
1190 
1191 OMPPrivateClause *
1192 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
1193                          SourceLocation LParenLoc, SourceLocation EndLoc,
1194                          ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
1195   // Allocate space for private variables and initializer expressions.
1196   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
1197                                                   llvm::alignOf<Expr *>()) +
1198                          2 * sizeof(Expr *) * VL.size());
1199   OMPPrivateClause *Clause =
1200       new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1201   Clause->setVarRefs(VL);
1202   Clause->setPrivateCopies(PrivateVL);
1203   return Clause;
1204 }
1205 
1206 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
1207                                                 unsigned N) {
1208   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
1209                                                   llvm::alignOf<Expr *>()) +
1210                          2 * sizeof(Expr *) * N);
1211   return new (Mem) OMPPrivateClause(N);
1212 }
1213 
1214 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
1215   assert(VL.size() == varlist_size() &&
1216          "Number of private copies is not the same as the preallocated buffer");
1217   std::copy(VL.begin(), VL.end(), varlist_end());
1218 }
1219 
1220 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
1221   assert(VL.size() == varlist_size() &&
1222          "Number of inits is not the same as the preallocated buffer");
1223   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
1224 }
1225 
1226 OMPFirstprivateClause *
1227 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
1228                               SourceLocation LParenLoc, SourceLocation EndLoc,
1229                               ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
1230                               ArrayRef<Expr *> InitVL) {
1231   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
1232                                                   llvm::alignOf<Expr *>()) +
1233                          3 * sizeof(Expr *) * VL.size());
1234   OMPFirstprivateClause *Clause =
1235       new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1236   Clause->setVarRefs(VL);
1237   Clause->setPrivateCopies(PrivateVL);
1238   Clause->setInits(InitVL);
1239   return Clause;
1240 }
1241 
1242 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
1243                                                           unsigned N) {
1244   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
1245                                                   llvm::alignOf<Expr *>()) +
1246                          3 * sizeof(Expr *) * N);
1247   return new (Mem) OMPFirstprivateClause(N);
1248 }
1249 
1250 OMPLastprivateClause *OMPLastprivateClause::Create(const ASTContext &C,
1251                                                    SourceLocation StartLoc,
1252                                                    SourceLocation LParenLoc,
1253                                                    SourceLocation EndLoc,
1254                                                    ArrayRef<Expr *> VL) {
1255   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLastprivateClause),
1256                                                   llvm::alignOf<Expr *>()) +
1257                          sizeof(Expr *) * VL.size());
1258   OMPLastprivateClause *Clause =
1259       new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1260   Clause->setVarRefs(VL);
1261   return Clause;
1262 }
1263 
1264 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
1265                                                         unsigned N) {
1266   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLastprivateClause),
1267                                                   llvm::alignOf<Expr *>()) +
1268                          sizeof(Expr *) * N);
1269   return new (Mem) OMPLastprivateClause(N);
1270 }
1271 
1272 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
1273                                          SourceLocation StartLoc,
1274                                          SourceLocation LParenLoc,
1275                                          SourceLocation EndLoc,
1276                                          ArrayRef<Expr *> VL) {
1277   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
1278                                                   llvm::alignOf<Expr *>()) +
1279                          sizeof(Expr *) * VL.size());
1280   OMPSharedClause *Clause = new (Mem) OMPSharedClause(StartLoc, LParenLoc,
1281                                                       EndLoc, VL.size());
1282   Clause->setVarRefs(VL);
1283   return Clause;
1284 }
1285 
1286 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C,
1287                                               unsigned N) {
1288   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
1289                                                   llvm::alignOf<Expr *>()) +
1290                          sizeof(Expr *) * N);
1291   return new (Mem) OMPSharedClause(N);
1292 }
1293 
1294 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
1295   assert(IL.size() == varlist_size() &&
1296          "Number of inits is not the same as the preallocated buffer");
1297   std::copy(IL.begin(), IL.end(), varlist_end());
1298 }
1299 
1300 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
1301   assert(UL.size() == varlist_size() &&
1302          "Number of updates is not the same as the preallocated buffer");
1303   std::copy(UL.begin(), UL.end(), getInits().end());
1304 }
1305 
1306 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
1307   assert(FL.size() == varlist_size() &&
1308          "Number of final updates is not the same as the preallocated buffer");
1309   std::copy(FL.begin(), FL.end(), getUpdates().end());
1310 }
1311 
1312 OMPLinearClause *
1313 OMPLinearClause::Create(const ASTContext &C, SourceLocation StartLoc,
1314                         SourceLocation LParenLoc, SourceLocation ColonLoc,
1315                         SourceLocation EndLoc, ArrayRef<Expr *> VL,
1316                         ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep) {
1317   // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
1318   // (Step and CalcStep).
1319   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLinearClause),
1320                                                   llvm::alignOf<Expr *>()) +
1321                          (4 * VL.size() + 2) * sizeof(Expr *));
1322   OMPLinearClause *Clause = new (Mem)
1323       OMPLinearClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
1324   Clause->setVarRefs(VL);
1325   Clause->setInits(IL);
1326   // Fill update and final expressions with zeroes, they are provided later,
1327   // after the directive construction.
1328   std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
1329             nullptr);
1330   std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
1331             nullptr);
1332   Clause->setStep(Step);
1333   Clause->setCalcStep(CalcStep);
1334   return Clause;
1335 }
1336 
1337 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
1338                                               unsigned NumVars) {
1339   // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
1340   // (Step and CalcStep).
1341   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLinearClause),
1342                                                   llvm::alignOf<Expr *>()) +
1343                          (4 * NumVars + 2) * sizeof(Expr *));
1344   return new (Mem) OMPLinearClause(NumVars);
1345 }
1346 
1347 OMPAlignedClause *
1348 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
1349                          SourceLocation LParenLoc, SourceLocation ColonLoc,
1350                          SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
1351   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPAlignedClause),
1352                                                   llvm::alignOf<Expr *>()) +
1353                          sizeof(Expr *) * (VL.size() + 1));
1354   OMPAlignedClause *Clause = new (Mem)
1355       OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
1356   Clause->setVarRefs(VL);
1357   Clause->setAlignment(A);
1358   return Clause;
1359 }
1360 
1361 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
1362                                                 unsigned NumVars) {
1363   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPAlignedClause),
1364                                                   llvm::alignOf<Expr *>()) +
1365                          sizeof(Expr *) * (NumVars + 1));
1366   return new (Mem) OMPAlignedClause(NumVars);
1367 }
1368 
1369 OMPCopyinClause *OMPCopyinClause::Create(const ASTContext &C,
1370                                          SourceLocation StartLoc,
1371                                          SourceLocation LParenLoc,
1372                                          SourceLocation EndLoc,
1373                                          ArrayRef<Expr *> VL) {
1374   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyinClause),
1375                                                   llvm::alignOf<Expr *>()) +
1376                          sizeof(Expr *) * VL.size());
1377   OMPCopyinClause *Clause = new (Mem) OMPCopyinClause(StartLoc, LParenLoc,
1378                                                       EndLoc, VL.size());
1379   Clause->setVarRefs(VL);
1380   return Clause;
1381 }
1382 
1383 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C,
1384                                               unsigned N) {
1385   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyinClause),
1386                                                   llvm::alignOf<Expr *>()) +
1387                          sizeof(Expr *) * N);
1388   return new (Mem) OMPCopyinClause(N);
1389 }
1390 
1391 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
1392   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
1393                                               "not the same as the "
1394                                               "preallocated buffer");
1395   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
1396 }
1397 
1398 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
1399   assert(DstExprs.size() == varlist_size() && "Number of destination "
1400                                               "expressions is not the same as "
1401                                               "the preallocated buffer");
1402   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
1403 }
1404 
1405 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
1406   assert(AssignmentOps.size() == varlist_size() &&
1407          "Number of assignment expressions is not the same as the preallocated "
1408          "buffer");
1409   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
1410             getDestinationExprs().end());
1411 }
1412 
1413 OMPCopyprivateClause *OMPCopyprivateClause::Create(
1414     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1415     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
1416     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
1417   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyprivateClause),
1418                                                   llvm::alignOf<Expr *>()) +
1419                          4 * sizeof(Expr *) * VL.size());
1420   OMPCopyprivateClause *Clause =
1421       new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1422   Clause->setVarRefs(VL);
1423   Clause->setSourceExprs(SrcExprs);
1424   Clause->setDestinationExprs(DstExprs);
1425   Clause->setAssignmentOps(AssignmentOps);
1426   return Clause;
1427 }
1428 
1429 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
1430                                                         unsigned N) {
1431   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyprivateClause),
1432                                                   llvm::alignOf<Expr *>()) +
1433                          4 * sizeof(Expr *) * N);
1434   return new (Mem) OMPCopyprivateClause(N);
1435 }
1436 
1437 void OMPExecutableDirective::setClauses(ArrayRef<OMPClause *> Clauses) {
1438   assert(Clauses.size() == getNumClauses() &&
1439          "Number of clauses is not the same as the preallocated buffer");
1440   std::copy(Clauses.begin(), Clauses.end(), getClauses().begin());
1441 }
1442 
1443 void OMPLoopDirective::setCounters(ArrayRef<Expr *> A) {
1444   assert(A.size() == getCollapsedNumber() &&
1445          "Number of loop counters is not the same as the collapsed number");
1446   std::copy(A.begin(), A.end(), getCounters().begin());
1447 }
1448 
1449 void OMPLoopDirective::setUpdates(ArrayRef<Expr *> A) {
1450   assert(A.size() == getCollapsedNumber() &&
1451          "Number of counter updates is not the same as the collapsed number");
1452   std::copy(A.begin(), A.end(), getUpdates().begin());
1453 }
1454 
1455 void OMPLoopDirective::setFinals(ArrayRef<Expr *> A) {
1456   assert(A.size() == getCollapsedNumber() &&
1457          "Number of counter finals is not the same as the collapsed number");
1458   std::copy(A.begin(), A.end(), getFinals().begin());
1459 }
1460 
1461 OMPReductionClause *OMPReductionClause::Create(
1462     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1463     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
1464     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo) {
1465   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPReductionClause),
1466                                                   llvm::alignOf<Expr *>()) +
1467                          sizeof(Expr *) * VL.size());
1468   OMPReductionClause *Clause = new (Mem) OMPReductionClause(
1469       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
1470   Clause->setVarRefs(VL);
1471   return Clause;
1472 }
1473 
1474 OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
1475                                                     unsigned N) {
1476   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPReductionClause),
1477                                                   llvm::alignOf<Expr *>()) +
1478                          sizeof(Expr *) * N);
1479   return new (Mem) OMPReductionClause(N);
1480 }
1481 
1482 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
1483                                        SourceLocation StartLoc,
1484                                        SourceLocation LParenLoc,
1485                                        SourceLocation EndLoc,
1486                                        ArrayRef<Expr *> VL) {
1487   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFlushClause),
1488                                                   llvm::alignOf<Expr *>()) +
1489                          sizeof(Expr *) * VL.size());
1490   OMPFlushClause *Clause =
1491       new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
1492   Clause->setVarRefs(VL);
1493   return Clause;
1494 }
1495 
1496 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
1497   void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFlushClause),
1498                                                   llvm::alignOf<Expr *>()) +
1499                          sizeof(Expr *) * N);
1500   return new (Mem) OMPFlushClause(N);
1501 }
1502 
1503 const OMPClause *
1504 OMPExecutableDirective::getSingleClause(OpenMPClauseKind K) const {
1505   auto ClauseFilter =
1506       [=](const OMPClause *C) -> bool { return C->getClauseKind() == K; };
1507   OMPExecutableDirective::filtered_clause_iterator<decltype(ClauseFilter)> I(
1508       clauses(), ClauseFilter);
1509 
1510   if (I) {
1511     auto *Clause = *I;
1512     assert(!++I && "There are at least 2 clauses of the specified kind");
1513     return Clause;
1514   }
1515   return nullptr;
1516 }
1517 
1518 OMPParallelDirective *OMPParallelDirective::Create(
1519                                               const ASTContext &C,
1520                                               SourceLocation StartLoc,
1521                                               SourceLocation EndLoc,
1522                                               ArrayRef<OMPClause *> Clauses,
1523                                               Stmt *AssociatedStmt) {
1524   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
1525                                            llvm::alignOf<OMPClause *>());
1526   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1527                          sizeof(Stmt *));
1528   OMPParallelDirective *Dir = new (Mem) OMPParallelDirective(StartLoc, EndLoc,
1529                                                              Clauses.size());
1530   Dir->setClauses(Clauses);
1531   Dir->setAssociatedStmt(AssociatedStmt);
1532   return Dir;
1533 }
1534 
1535 OMPParallelDirective *OMPParallelDirective::CreateEmpty(const ASTContext &C,
1536                                                         unsigned NumClauses,
1537                                                         EmptyShell) {
1538   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
1539                                            llvm::alignOf<OMPClause *>());
1540   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1541                          sizeof(Stmt *));
1542   return new (Mem) OMPParallelDirective(NumClauses);
1543 }
1544 
1545 OMPSimdDirective *
1546 OMPSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1547                          SourceLocation EndLoc, unsigned CollapsedNum,
1548                          ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
1549                          const HelperExprs &Exprs) {
1550   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSimdDirective),
1551                                            llvm::alignOf<OMPClause *>());
1552   void *Mem =
1553       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1554                  sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_simd));
1555   OMPSimdDirective *Dir = new (Mem)
1556       OMPSimdDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1557   Dir->setClauses(Clauses);
1558   Dir->setAssociatedStmt(AssociatedStmt);
1559   Dir->setIterationVariable(Exprs.IterationVarRef);
1560   Dir->setLastIteration(Exprs.LastIteration);
1561   Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1562   Dir->setPreCond(Exprs.PreCond);
1563   Dir->setCond(Exprs.Cond, Exprs.SeparatedCond);
1564   Dir->setInit(Exprs.Init);
1565   Dir->setInc(Exprs.Inc);
1566   Dir->setCounters(Exprs.Counters);
1567   Dir->setUpdates(Exprs.Updates);
1568   Dir->setFinals(Exprs.Finals);
1569   return Dir;
1570 }
1571 
1572 OMPSimdDirective *OMPSimdDirective::CreateEmpty(const ASTContext &C,
1573                                                 unsigned NumClauses,
1574                                                 unsigned CollapsedNum,
1575                                                 EmptyShell) {
1576   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSimdDirective),
1577                                            llvm::alignOf<OMPClause *>());
1578   void *Mem =
1579       C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1580                  sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_simd));
1581   return new (Mem) OMPSimdDirective(CollapsedNum, NumClauses);
1582 }
1583 
1584 OMPForDirective *
1585 OMPForDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1586                         SourceLocation EndLoc, unsigned CollapsedNum,
1587                         ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
1588                         const HelperExprs &Exprs) {
1589   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForDirective),
1590                                            llvm::alignOf<OMPClause *>());
1591   void *Mem =
1592       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1593                  sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for));
1594   OMPForDirective *Dir =
1595       new (Mem) OMPForDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1596   Dir->setClauses(Clauses);
1597   Dir->setAssociatedStmt(AssociatedStmt);
1598   Dir->setIterationVariable(Exprs.IterationVarRef);
1599   Dir->setLastIteration(Exprs.LastIteration);
1600   Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1601   Dir->setPreCond(Exprs.PreCond);
1602   Dir->setCond(Exprs.Cond, Exprs.SeparatedCond);
1603   Dir->setInit(Exprs.Init);
1604   Dir->setInc(Exprs.Inc);
1605   Dir->setIsLastIterVariable(Exprs.IL);
1606   Dir->setLowerBoundVariable(Exprs.LB);
1607   Dir->setUpperBoundVariable(Exprs.UB);
1608   Dir->setStrideVariable(Exprs.ST);
1609   Dir->setEnsureUpperBound(Exprs.EUB);
1610   Dir->setNextLowerBound(Exprs.NLB);
1611   Dir->setNextUpperBound(Exprs.NUB);
1612   Dir->setCounters(Exprs.Counters);
1613   Dir->setUpdates(Exprs.Updates);
1614   Dir->setFinals(Exprs.Finals);
1615   return Dir;
1616 }
1617 
1618 OMPForDirective *OMPForDirective::CreateEmpty(const ASTContext &C,
1619                                               unsigned NumClauses,
1620                                               unsigned CollapsedNum,
1621                                               EmptyShell) {
1622   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForDirective),
1623                                            llvm::alignOf<OMPClause *>());
1624   void *Mem =
1625       C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1626                  sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for));
1627   return new (Mem) OMPForDirective(CollapsedNum, NumClauses);
1628 }
1629 
1630 OMPForSimdDirective *
1631 OMPForSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1632                             SourceLocation EndLoc, unsigned CollapsedNum,
1633                             ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
1634                             const HelperExprs &Exprs) {
1635   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForSimdDirective),
1636                                            llvm::alignOf<OMPClause *>());
1637   void *Mem =
1638       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1639                  sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for_simd));
1640   OMPForSimdDirective *Dir = new (Mem)
1641       OMPForSimdDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1642   Dir->setClauses(Clauses);
1643   Dir->setAssociatedStmt(AssociatedStmt);
1644   Dir->setIterationVariable(Exprs.IterationVarRef);
1645   Dir->setLastIteration(Exprs.LastIteration);
1646   Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1647   Dir->setPreCond(Exprs.PreCond);
1648   Dir->setCond(Exprs.Cond, Exprs.SeparatedCond);
1649   Dir->setInit(Exprs.Init);
1650   Dir->setInc(Exprs.Inc);
1651   Dir->setIsLastIterVariable(Exprs.IL);
1652   Dir->setLowerBoundVariable(Exprs.LB);
1653   Dir->setUpperBoundVariable(Exprs.UB);
1654   Dir->setStrideVariable(Exprs.ST);
1655   Dir->setEnsureUpperBound(Exprs.EUB);
1656   Dir->setNextLowerBound(Exprs.NLB);
1657   Dir->setNextUpperBound(Exprs.NUB);
1658   Dir->setCounters(Exprs.Counters);
1659   Dir->setUpdates(Exprs.Updates);
1660   Dir->setFinals(Exprs.Finals);
1661   return Dir;
1662 }
1663 
1664 OMPForSimdDirective *OMPForSimdDirective::CreateEmpty(const ASTContext &C,
1665                                                       unsigned NumClauses,
1666                                                       unsigned CollapsedNum,
1667                                                       EmptyShell) {
1668   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForSimdDirective),
1669                                            llvm::alignOf<OMPClause *>());
1670   void *Mem =
1671       C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1672                  sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for_simd));
1673   return new (Mem) OMPForSimdDirective(CollapsedNum, NumClauses);
1674 }
1675 
1676 OMPSectionsDirective *OMPSectionsDirective::Create(
1677     const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1678     ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1679   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1680                                            llvm::alignOf<OMPClause *>());
1681   void *Mem =
1682       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1683   OMPSectionsDirective *Dir =
1684       new (Mem) OMPSectionsDirective(StartLoc, EndLoc, Clauses.size());
1685   Dir->setClauses(Clauses);
1686   Dir->setAssociatedStmt(AssociatedStmt);
1687   return Dir;
1688 }
1689 
1690 OMPSectionsDirective *OMPSectionsDirective::CreateEmpty(const ASTContext &C,
1691                                                         unsigned NumClauses,
1692                                                         EmptyShell) {
1693   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1694                                            llvm::alignOf<OMPClause *>());
1695   void *Mem =
1696       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1697   return new (Mem) OMPSectionsDirective(NumClauses);
1698 }
1699 
1700 OMPSectionDirective *OMPSectionDirective::Create(const ASTContext &C,
1701                                                  SourceLocation StartLoc,
1702                                                  SourceLocation EndLoc,
1703                                                  Stmt *AssociatedStmt) {
1704   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1705                                            llvm::alignOf<Stmt *>());
1706   void *Mem = C.Allocate(Size + sizeof(Stmt *));
1707   OMPSectionDirective *Dir = new (Mem) OMPSectionDirective(StartLoc, EndLoc);
1708   Dir->setAssociatedStmt(AssociatedStmt);
1709   return Dir;
1710 }
1711 
1712 OMPSectionDirective *OMPSectionDirective::CreateEmpty(const ASTContext &C,
1713                                                       EmptyShell) {
1714   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionDirective),
1715                                            llvm::alignOf<Stmt *>());
1716   void *Mem = C.Allocate(Size + sizeof(Stmt *));
1717   return new (Mem) OMPSectionDirective();
1718 }
1719 
1720 OMPSingleDirective *OMPSingleDirective::Create(const ASTContext &C,
1721                                                SourceLocation StartLoc,
1722                                                SourceLocation EndLoc,
1723                                                ArrayRef<OMPClause *> Clauses,
1724                                                Stmt *AssociatedStmt) {
1725   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSingleDirective),
1726                                            llvm::alignOf<OMPClause *>());
1727   void *Mem =
1728       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1729   OMPSingleDirective *Dir =
1730       new (Mem) OMPSingleDirective(StartLoc, EndLoc, Clauses.size());
1731   Dir->setClauses(Clauses);
1732   Dir->setAssociatedStmt(AssociatedStmt);
1733   return Dir;
1734 }
1735 
1736 OMPSingleDirective *OMPSingleDirective::CreateEmpty(const ASTContext &C,
1737                                                     unsigned NumClauses,
1738                                                     EmptyShell) {
1739   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSingleDirective),
1740                                            llvm::alignOf<OMPClause *>());
1741   void *Mem =
1742       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1743   return new (Mem) OMPSingleDirective(NumClauses);
1744 }
1745 
1746 OMPMasterDirective *OMPMasterDirective::Create(const ASTContext &C,
1747                                                SourceLocation StartLoc,
1748                                                SourceLocation EndLoc,
1749                                                Stmt *AssociatedStmt) {
1750   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPMasterDirective),
1751                                            llvm::alignOf<Stmt *>());
1752   void *Mem = C.Allocate(Size + sizeof(Stmt *));
1753   OMPMasterDirective *Dir = new (Mem) OMPMasterDirective(StartLoc, EndLoc);
1754   Dir->setAssociatedStmt(AssociatedStmt);
1755   return Dir;
1756 }
1757 
1758 OMPMasterDirective *OMPMasterDirective::CreateEmpty(const ASTContext &C,
1759                                                     EmptyShell) {
1760   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPMasterDirective),
1761                                            llvm::alignOf<Stmt *>());
1762   void *Mem = C.Allocate(Size + sizeof(Stmt *));
1763   return new (Mem) OMPMasterDirective();
1764 }
1765 
1766 OMPCriticalDirective *OMPCriticalDirective::Create(
1767     const ASTContext &C, const DeclarationNameInfo &Name,
1768     SourceLocation StartLoc, SourceLocation EndLoc, Stmt *AssociatedStmt) {
1769   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCriticalDirective),
1770                                            llvm::alignOf<Stmt *>());
1771   void *Mem = C.Allocate(Size + sizeof(Stmt *));
1772   OMPCriticalDirective *Dir =
1773       new (Mem) OMPCriticalDirective(Name, StartLoc, EndLoc);
1774   Dir->setAssociatedStmt(AssociatedStmt);
1775   return Dir;
1776 }
1777 
1778 OMPCriticalDirective *OMPCriticalDirective::CreateEmpty(const ASTContext &C,
1779                                                         EmptyShell) {
1780   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCriticalDirective),
1781                                            llvm::alignOf<Stmt *>());
1782   void *Mem = C.Allocate(Size + sizeof(Stmt *));
1783   return new (Mem) OMPCriticalDirective();
1784 }
1785 
1786 OMPParallelForDirective *OMPParallelForDirective::Create(
1787     const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1788     unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
1789     const HelperExprs &Exprs) {
1790   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForDirective),
1791                                            llvm::alignOf<OMPClause *>());
1792   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1793                          sizeof(Stmt *) *
1794                              numLoopChildren(CollapsedNum, OMPD_parallel_for));
1795   OMPParallelForDirective *Dir = new (Mem)
1796       OMPParallelForDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1797   Dir->setClauses(Clauses);
1798   Dir->setAssociatedStmt(AssociatedStmt);
1799   Dir->setIterationVariable(Exprs.IterationVarRef);
1800   Dir->setLastIteration(Exprs.LastIteration);
1801   Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1802   Dir->setPreCond(Exprs.PreCond);
1803   Dir->setCond(Exprs.Cond, Exprs.SeparatedCond);
1804   Dir->setInit(Exprs.Init);
1805   Dir->setInc(Exprs.Inc);
1806   Dir->setIsLastIterVariable(Exprs.IL);
1807   Dir->setLowerBoundVariable(Exprs.LB);
1808   Dir->setUpperBoundVariable(Exprs.UB);
1809   Dir->setStrideVariable(Exprs.ST);
1810   Dir->setEnsureUpperBound(Exprs.EUB);
1811   Dir->setNextLowerBound(Exprs.NLB);
1812   Dir->setNextUpperBound(Exprs.NUB);
1813   Dir->setCounters(Exprs.Counters);
1814   Dir->setUpdates(Exprs.Updates);
1815   Dir->setFinals(Exprs.Finals);
1816   return Dir;
1817 }
1818 
1819 OMPParallelForDirective *
1820 OMPParallelForDirective::CreateEmpty(const ASTContext &C, unsigned NumClauses,
1821                                      unsigned CollapsedNum, EmptyShell) {
1822   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForDirective),
1823                                            llvm::alignOf<OMPClause *>());
1824   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1825                          sizeof(Stmt *) *
1826                              numLoopChildren(CollapsedNum, OMPD_parallel_for));
1827   return new (Mem) OMPParallelForDirective(CollapsedNum, NumClauses);
1828 }
1829 
1830 OMPParallelForSimdDirective *OMPParallelForSimdDirective::Create(
1831     const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1832     unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
1833     const HelperExprs &Exprs) {
1834   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForSimdDirective),
1835                                            llvm::alignOf<OMPClause *>());
1836   void *Mem = C.Allocate(
1837       Size + sizeof(OMPClause *) * Clauses.size() +
1838       sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_parallel_for_simd));
1839   OMPParallelForSimdDirective *Dir = new (Mem) OMPParallelForSimdDirective(
1840       StartLoc, EndLoc, CollapsedNum, Clauses.size());
1841   Dir->setClauses(Clauses);
1842   Dir->setAssociatedStmt(AssociatedStmt);
1843   Dir->setIterationVariable(Exprs.IterationVarRef);
1844   Dir->setLastIteration(Exprs.LastIteration);
1845   Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1846   Dir->setPreCond(Exprs.PreCond);
1847   Dir->setCond(Exprs.Cond, Exprs.SeparatedCond);
1848   Dir->setInit(Exprs.Init);
1849   Dir->setInc(Exprs.Inc);
1850   Dir->setIsLastIterVariable(Exprs.IL);
1851   Dir->setLowerBoundVariable(Exprs.LB);
1852   Dir->setUpperBoundVariable(Exprs.UB);
1853   Dir->setStrideVariable(Exprs.ST);
1854   Dir->setEnsureUpperBound(Exprs.EUB);
1855   Dir->setNextLowerBound(Exprs.NLB);
1856   Dir->setNextUpperBound(Exprs.NUB);
1857   Dir->setCounters(Exprs.Counters);
1858   Dir->setUpdates(Exprs.Updates);
1859   Dir->setFinals(Exprs.Finals);
1860   return Dir;
1861 }
1862 
1863 OMPParallelForSimdDirective *
1864 OMPParallelForSimdDirective::CreateEmpty(const ASTContext &C,
1865                                          unsigned NumClauses,
1866                                          unsigned CollapsedNum, EmptyShell) {
1867   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForSimdDirective),
1868                                            llvm::alignOf<OMPClause *>());
1869   void *Mem = C.Allocate(
1870       Size + sizeof(OMPClause *) * NumClauses +
1871       sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_parallel_for_simd));
1872   return new (Mem) OMPParallelForSimdDirective(CollapsedNum, NumClauses);
1873 }
1874 
1875 OMPParallelSectionsDirective *OMPParallelSectionsDirective::Create(
1876     const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1877     ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1878   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelSectionsDirective),
1879                                            llvm::alignOf<OMPClause *>());
1880   void *Mem =
1881       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1882   OMPParallelSectionsDirective *Dir =
1883       new (Mem) OMPParallelSectionsDirective(StartLoc, EndLoc, Clauses.size());
1884   Dir->setClauses(Clauses);
1885   Dir->setAssociatedStmt(AssociatedStmt);
1886   return Dir;
1887 }
1888 
1889 OMPParallelSectionsDirective *
1890 OMPParallelSectionsDirective::CreateEmpty(const ASTContext &C,
1891                                           unsigned NumClauses, EmptyShell) {
1892   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelSectionsDirective),
1893                                            llvm::alignOf<OMPClause *>());
1894   void *Mem =
1895       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1896   return new (Mem) OMPParallelSectionsDirective(NumClauses);
1897 }
1898 
1899 OMPTaskDirective *OMPTaskDirective::Create(const ASTContext &C,
1900                                            SourceLocation StartLoc,
1901                                            SourceLocation EndLoc,
1902                                            ArrayRef<OMPClause *> Clauses,
1903                                            Stmt *AssociatedStmt) {
1904   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskDirective),
1905                                            llvm::alignOf<OMPClause *>());
1906   void *Mem =
1907       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1908   OMPTaskDirective *Dir =
1909       new (Mem) OMPTaskDirective(StartLoc, EndLoc, Clauses.size());
1910   Dir->setClauses(Clauses);
1911   Dir->setAssociatedStmt(AssociatedStmt);
1912   return Dir;
1913 }
1914 
1915 OMPTaskDirective *OMPTaskDirective::CreateEmpty(const ASTContext &C,
1916                                                 unsigned NumClauses,
1917                                                 EmptyShell) {
1918   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskDirective),
1919                                            llvm::alignOf<OMPClause *>());
1920   void *Mem =
1921       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1922   return new (Mem) OMPTaskDirective(NumClauses);
1923 }
1924 
1925 OMPTaskyieldDirective *OMPTaskyieldDirective::Create(const ASTContext &C,
1926                                                      SourceLocation StartLoc,
1927                                                      SourceLocation EndLoc) {
1928   void *Mem = C.Allocate(sizeof(OMPTaskyieldDirective));
1929   OMPTaskyieldDirective *Dir =
1930       new (Mem) OMPTaskyieldDirective(StartLoc, EndLoc);
1931   return Dir;
1932 }
1933 
1934 OMPTaskyieldDirective *OMPTaskyieldDirective::CreateEmpty(const ASTContext &C,
1935                                                           EmptyShell) {
1936   void *Mem = C.Allocate(sizeof(OMPTaskyieldDirective));
1937   return new (Mem) OMPTaskyieldDirective();
1938 }
1939 
1940 OMPBarrierDirective *OMPBarrierDirective::Create(const ASTContext &C,
1941                                                  SourceLocation StartLoc,
1942                                                  SourceLocation EndLoc) {
1943   void *Mem = C.Allocate(sizeof(OMPBarrierDirective));
1944   OMPBarrierDirective *Dir = new (Mem) OMPBarrierDirective(StartLoc, EndLoc);
1945   return Dir;
1946 }
1947 
1948 OMPBarrierDirective *OMPBarrierDirective::CreateEmpty(const ASTContext &C,
1949                                                       EmptyShell) {
1950   void *Mem = C.Allocate(sizeof(OMPBarrierDirective));
1951   return new (Mem) OMPBarrierDirective();
1952 }
1953 
1954 OMPTaskwaitDirective *OMPTaskwaitDirective::Create(const ASTContext &C,
1955                                                    SourceLocation StartLoc,
1956                                                    SourceLocation EndLoc) {
1957   void *Mem = C.Allocate(sizeof(OMPTaskwaitDirective));
1958   OMPTaskwaitDirective *Dir = new (Mem) OMPTaskwaitDirective(StartLoc, EndLoc);
1959   return Dir;
1960 }
1961 
1962 OMPTaskwaitDirective *OMPTaskwaitDirective::CreateEmpty(const ASTContext &C,
1963                                                         EmptyShell) {
1964   void *Mem = C.Allocate(sizeof(OMPTaskwaitDirective));
1965   return new (Mem) OMPTaskwaitDirective();
1966 }
1967 
1968 OMPFlushDirective *OMPFlushDirective::Create(const ASTContext &C,
1969                                              SourceLocation StartLoc,
1970                                              SourceLocation EndLoc,
1971                                              ArrayRef<OMPClause *> Clauses) {
1972   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPFlushDirective),
1973                                            llvm::alignOf<OMPClause *>());
1974   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size());
1975   OMPFlushDirective *Dir =
1976       new (Mem) OMPFlushDirective(StartLoc, EndLoc, Clauses.size());
1977   Dir->setClauses(Clauses);
1978   return Dir;
1979 }
1980 
1981 OMPFlushDirective *OMPFlushDirective::CreateEmpty(const ASTContext &C,
1982                                                   unsigned NumClauses,
1983                                                   EmptyShell) {
1984   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPFlushDirective),
1985                                            llvm::alignOf<OMPClause *>());
1986   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses);
1987   return new (Mem) OMPFlushDirective(NumClauses);
1988 }
1989 
1990 OMPOrderedDirective *OMPOrderedDirective::Create(const ASTContext &C,
1991                                                  SourceLocation StartLoc,
1992                                                  SourceLocation EndLoc,
1993                                                  Stmt *AssociatedStmt) {
1994   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPOrderedDirective),
1995                                            llvm::alignOf<Stmt *>());
1996   void *Mem = C.Allocate(Size + sizeof(Stmt *));
1997   OMPOrderedDirective *Dir = new (Mem) OMPOrderedDirective(StartLoc, EndLoc);
1998   Dir->setAssociatedStmt(AssociatedStmt);
1999   return Dir;
2000 }
2001 
2002 OMPOrderedDirective *OMPOrderedDirective::CreateEmpty(const ASTContext &C,
2003                                                       EmptyShell) {
2004   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPOrderedDirective),
2005                                            llvm::alignOf<Stmt *>());
2006   void *Mem = C.Allocate(Size + sizeof(Stmt *));
2007   return new (Mem) OMPOrderedDirective();
2008 }
2009 
2010 OMPAtomicDirective *
2011 OMPAtomicDirective::Create(const ASTContext &C, SourceLocation StartLoc,
2012                            SourceLocation EndLoc, ArrayRef<OMPClause *> Clauses,
2013                            Stmt *AssociatedStmt, BinaryOperatorKind OpKind,
2014                            Expr *X, Expr *XRVal, Expr *V, Expr *E) {
2015   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPAtomicDirective),
2016                                            llvm::alignOf<OMPClause *>());
2017   void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
2018                          5 * sizeof(Stmt *));
2019   OMPAtomicDirective *Dir =
2020       new (Mem) OMPAtomicDirective(StartLoc, EndLoc, Clauses.size());
2021   Dir->setOpKind(OpKind);
2022   Dir->setClauses(Clauses);
2023   Dir->setAssociatedStmt(AssociatedStmt);
2024   Dir->setX(X);
2025   Dir->setXRVal(X);
2026   Dir->setV(V);
2027   Dir->setExpr(E);
2028   return Dir;
2029 }
2030 
2031 OMPAtomicDirective *OMPAtomicDirective::CreateEmpty(const ASTContext &C,
2032                                                     unsigned NumClauses,
2033                                                     EmptyShell) {
2034   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPAtomicDirective),
2035                                            llvm::alignOf<OMPClause *>());
2036   void *Mem =
2037       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + 5 * sizeof(Stmt *));
2038   return new (Mem) OMPAtomicDirective(NumClauses);
2039 }
2040 
2041 OMPTargetDirective *OMPTargetDirective::Create(const ASTContext &C,
2042                                                SourceLocation StartLoc,
2043                                                SourceLocation EndLoc,
2044                                                ArrayRef<OMPClause *> Clauses,
2045                                                Stmt *AssociatedStmt) {
2046   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTargetDirective),
2047                                            llvm::alignOf<OMPClause *>());
2048   void *Mem =
2049       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
2050   OMPTargetDirective *Dir =
2051       new (Mem) OMPTargetDirective(StartLoc, EndLoc, Clauses.size());
2052   Dir->setClauses(Clauses);
2053   Dir->setAssociatedStmt(AssociatedStmt);
2054   return Dir;
2055 }
2056 
2057 OMPTargetDirective *OMPTargetDirective::CreateEmpty(const ASTContext &C,
2058                                                     unsigned NumClauses,
2059                                                     EmptyShell) {
2060   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTargetDirective),
2061                                            llvm::alignOf<OMPClause *>());
2062   void *Mem =
2063       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
2064   return new (Mem) OMPTargetDirective(NumClauses);
2065 }
2066 
2067 OMPTeamsDirective *OMPTeamsDirective::Create(const ASTContext &C,
2068                                              SourceLocation StartLoc,
2069                                              SourceLocation EndLoc,
2070                                              ArrayRef<OMPClause *> Clauses,
2071                                              Stmt *AssociatedStmt) {
2072   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTeamsDirective),
2073                                            llvm::alignOf<OMPClause *>());
2074   void *Mem =
2075       C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
2076   OMPTeamsDirective *Dir =
2077       new (Mem) OMPTeamsDirective(StartLoc, EndLoc, Clauses.size());
2078   Dir->setClauses(Clauses);
2079   Dir->setAssociatedStmt(AssociatedStmt);
2080   return Dir;
2081 }
2082 
2083 OMPTeamsDirective *OMPTeamsDirective::CreateEmpty(const ASTContext &C,
2084                                                   unsigned NumClauses,
2085                                                   EmptyShell) {
2086   unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTeamsDirective),
2087                                            llvm::alignOf<OMPClause *>());
2088   void *Mem =
2089       C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
2090   return new (Mem) OMPTeamsDirective(NumClauses);
2091 }
2092 
2093