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