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"
28using namespace clang;
29
30static struct StmtClassNameTable {
31  const char *Name;
32  unsigned Counter;
33  unsigned Size;
34} StmtClassInfo[Stmt::lastStmtConstant+1];
35
36static 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
52void *Stmt::operator new(size_t bytes, const ASTContext& C,
53                         unsigned alignment) {
54  return ::operator new(bytes, C, alignment);
55}
56
57const char *Stmt::getStmtClassName() const {
58  return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
59}
60
61void 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
86void Stmt::addStmtClass(StmtClass s) {
87  ++getStmtInfoTableEntry(s).Counter;
88}
89
90bool Stmt::StatisticsEnabled = false;
91void Stmt::EnableStatistics() {
92  StatisticsEnabled = true;
93}
94
95Stmt *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 Strip off all label-like statements.
108///
109/// This will strip off label statements, case statements, attributed
110/// statements and default statements recursively.
111const Stmt *Stmt::stripLabelLikeStatements() const {
112  const Stmt *S = this;
113  while (true) {
114    if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
115      S = LS->getSubStmt();
116    else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
117      S = SC->getSubStmt();
118    else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
119      S = AS->getSubStmt();
120    else
121      return S;
122  }
123}
124
125namespace {
126  struct good {};
127  struct bad {};
128
129  // These silly little functions have to be static inline to suppress
130  // unused warnings, and they have to be defined to suppress other
131  // warnings.
132  static inline good is_good(good) { return good(); }
133
134  typedef Stmt::child_range children_t();
135  template <class T> good implements_children(children_t T::*) {
136    return good();
137  }
138  LLVM_ATTRIBUTE_UNUSED
139  static inline bad implements_children(children_t Stmt::*) {
140    return bad();
141  }
142
143  typedef SourceLocation getLocStart_t() const;
144  template <class T> good implements_getLocStart(getLocStart_t T::*) {
145    return good();
146  }
147  LLVM_ATTRIBUTE_UNUSED
148  static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
149    return bad();
150  }
151
152  typedef SourceLocation getLocEnd_t() const;
153  template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
154    return good();
155  }
156  LLVM_ATTRIBUTE_UNUSED
157  static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
158    return bad();
159  }
160
161#define ASSERT_IMPLEMENTS_children(type) \
162  (void) is_good(implements_children(&type::children))
163#define ASSERT_IMPLEMENTS_getLocStart(type) \
164  (void) is_good(implements_getLocStart(&type::getLocStart))
165#define ASSERT_IMPLEMENTS_getLocEnd(type) \
166  (void) is_good(implements_getLocEnd(&type::getLocEnd))
167}
168
169/// Check whether the various Stmt classes implement their member
170/// functions.
171LLVM_ATTRIBUTE_UNUSED
172static inline void check_implementations() {
173#define ABSTRACT_STMT(type)
174#define STMT(type, base) \
175  ASSERT_IMPLEMENTS_children(type); \
176  ASSERT_IMPLEMENTS_getLocStart(type); \
177  ASSERT_IMPLEMENTS_getLocEnd(type);
178#include "clang/AST/StmtNodes.inc"
179}
180
181Stmt::child_range Stmt::children() {
182  switch (getStmtClass()) {
183  case Stmt::NoStmtClass: llvm_unreachable("statement without class");
184#define ABSTRACT_STMT(type)
185#define STMT(type, base) \
186  case Stmt::type##Class: \
187    return static_cast<type*>(this)->children();
188#include "clang/AST/StmtNodes.inc"
189  }
190  llvm_unreachable("unknown statement kind!");
191}
192
193// Amusing macro metaprogramming hack: check whether a class provides
194// a more specific implementation of getSourceRange.
195//
196// See also Expr.cpp:getExprLoc().
197namespace {
198  /// This implementation is used when a class provides a custom
199  /// implementation of getSourceRange.
200  template <class S, class T>
201  SourceRange getSourceRangeImpl(const Stmt *stmt,
202                                 SourceRange (T::*v)() const) {
203    return static_cast<const S*>(stmt)->getSourceRange();
204  }
205
206  /// This implementation is used when a class doesn't provide a custom
207  /// implementation of getSourceRange.  Overload resolution should pick it over
208  /// the implementation above because it's more specialized according to
209  /// function template partial ordering.
210  template <class S>
211  SourceRange getSourceRangeImpl(const Stmt *stmt,
212                                 SourceRange (Stmt::*v)() const) {
213    return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
214                       static_cast<const S*>(stmt)->getLocEnd());
215  }
216}
217
218SourceRange Stmt::getSourceRange() const {
219  switch (getStmtClass()) {
220  case Stmt::NoStmtClass: llvm_unreachable("statement without class");
221#define ABSTRACT_STMT(type)
222#define STMT(type, base) \
223  case Stmt::type##Class: \
224    return getSourceRangeImpl<type>(this, &type::getSourceRange);
225#include "clang/AST/StmtNodes.inc"
226  }
227  llvm_unreachable("unknown statement kind!");
228}
229
230SourceLocation Stmt::getLocStart() const {
231//  llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
232  switch (getStmtClass()) {
233  case Stmt::NoStmtClass: llvm_unreachable("statement without class");
234#define ABSTRACT_STMT(type)
235#define STMT(type, base) \
236  case Stmt::type##Class: \
237    return static_cast<const type*>(this)->getLocStart();
238#include "clang/AST/StmtNodes.inc"
239  }
240  llvm_unreachable("unknown statement kind");
241}
242
243SourceLocation Stmt::getLocEnd() const {
244  switch (getStmtClass()) {
245  case Stmt::NoStmtClass: llvm_unreachable("statement without class");
246#define ABSTRACT_STMT(type)
247#define STMT(type, base) \
248  case Stmt::type##Class: \
249    return static_cast<const type*>(this)->getLocEnd();
250#include "clang/AST/StmtNodes.inc"
251  }
252  llvm_unreachable("unknown statement kind");
253}
254
255CompoundStmt::CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
256                           SourceLocation LB, SourceLocation RB)
257  : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
258  CompoundStmtBits.NumStmts = Stmts.size();
259  assert(CompoundStmtBits.NumStmts == Stmts.size() &&
260         "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
261
262  if (Stmts.size() == 0) {
263    Body = nullptr;
264    return;
265  }
266
267  Body = new (C) Stmt*[Stmts.size()];
268  std::copy(Stmts.begin(), Stmts.end(), Body);
269}
270
271void CompoundStmt::setStmts(const ASTContext &C, Stmt **Stmts,
272                            unsigned NumStmts) {
273  if (this->Body)
274    C.Deallocate(Body);
275  this->CompoundStmtBits.NumStmts = NumStmts;
276
277  Body = new (C) Stmt*[NumStmts];
278  memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
279}
280
281const char *LabelStmt::getName() const {
282  return getDecl()->getIdentifier()->getNameStart();
283}
284
285AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
286                                       ArrayRef<const Attr*> Attrs,
287                                       Stmt *SubStmt) {
288  assert(!Attrs.empty() && "Attrs should not be empty");
289  void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * Attrs.size(),
290                         llvm::alignOf<AttributedStmt>());
291  return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
292}
293
294AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
295                                            unsigned NumAttrs) {
296  assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
297  void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * NumAttrs,
298                         llvm::alignOf<AttributedStmt>());
299  return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
300}
301
302std::string AsmStmt::generateAsmString(const ASTContext &C) const {
303  if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
304    return gccAsmStmt->generateAsmString(C);
305  if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
306    return msAsmStmt->generateAsmString(C);
307  llvm_unreachable("unknown asm statement kind!");
308}
309
310StringRef AsmStmt::getOutputConstraint(unsigned i) const {
311  if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
312    return gccAsmStmt->getOutputConstraint(i);
313  if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
314    return msAsmStmt->getOutputConstraint(i);
315  llvm_unreachable("unknown asm statement kind!");
316}
317
318const Expr *AsmStmt::getOutputExpr(unsigned i) const {
319  if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
320    return gccAsmStmt->getOutputExpr(i);
321  if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
322    return msAsmStmt->getOutputExpr(i);
323  llvm_unreachable("unknown asm statement kind!");
324}
325
326StringRef AsmStmt::getInputConstraint(unsigned i) const {
327  if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
328    return gccAsmStmt->getInputConstraint(i);
329  if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
330    return msAsmStmt->getInputConstraint(i);
331  llvm_unreachable("unknown asm statement kind!");
332}
333
334const Expr *AsmStmt::getInputExpr(unsigned i) const {
335  if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
336    return gccAsmStmt->getInputExpr(i);
337  if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
338    return msAsmStmt->getInputExpr(i);
339  llvm_unreachable("unknown asm statement kind!");
340}
341
342StringRef AsmStmt::getClobber(unsigned i) const {
343  if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
344    return gccAsmStmt->getClobber(i);
345  if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
346    return msAsmStmt->getClobber(i);
347  llvm_unreachable("unknown asm statement kind!");
348}
349
350/// getNumPlusOperands - Return the number of output operands that have a "+"
351/// constraint.
352unsigned AsmStmt::getNumPlusOperands() const {
353  unsigned Res = 0;
354  for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
355    if (isOutputPlusConstraint(i))
356      ++Res;
357  return Res;
358}
359
360StringRef GCCAsmStmt::getClobber(unsigned i) const {
361  return getClobberStringLiteral(i)->getString();
362}
363
364Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
365  return cast<Expr>(Exprs[i]);
366}
367
368/// getOutputConstraint - Return the constraint string for the specified
369/// output operand.  All output constraints are known to be non-empty (either
370/// '=' or '+').
371StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
372  return getOutputConstraintLiteral(i)->getString();
373}
374
375Expr *GCCAsmStmt::getInputExpr(unsigned i) {
376  return cast<Expr>(Exprs[i + NumOutputs]);
377}
378void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
379  Exprs[i + NumOutputs] = E;
380}
381
382/// getInputConstraint - Return the specified input constraint.  Unlike output
383/// constraints, these can be empty.
384StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
385  return getInputConstraintLiteral(i)->getString();
386}
387
388void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
389                                                IdentifierInfo **Names,
390                                                StringLiteral **Constraints,
391                                                Stmt **Exprs,
392                                                unsigned NumOutputs,
393                                                unsigned NumInputs,
394                                                StringLiteral **Clobbers,
395                                                unsigned NumClobbers) {
396  this->NumOutputs = NumOutputs;
397  this->NumInputs = NumInputs;
398  this->NumClobbers = NumClobbers;
399
400  unsigned NumExprs = NumOutputs + NumInputs;
401
402  C.Deallocate(this->Names);
403  this->Names = new (C) IdentifierInfo*[NumExprs];
404  std::copy(Names, Names + NumExprs, this->Names);
405
406  C.Deallocate(this->Exprs);
407  this->Exprs = new (C) Stmt*[NumExprs];
408  std::copy(Exprs, Exprs + NumExprs, this->Exprs);
409
410  C.Deallocate(this->Constraints);
411  this->Constraints = new (C) StringLiteral*[NumExprs];
412  std::copy(Constraints, Constraints + NumExprs, this->Constraints);
413
414  C.Deallocate(this->Clobbers);
415  this->Clobbers = new (C) StringLiteral*[NumClobbers];
416  std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
417}
418
419/// getNamedOperand - Given a symbolic operand reference like %[foo],
420/// translate this into a numeric value needed to reference the same operand.
421/// This returns -1 if the operand name is invalid.
422int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
423  unsigned NumPlusOperands = 0;
424
425  // Check if this is an output operand.
426  for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
427    if (getOutputName(i) == SymbolicName)
428      return i;
429  }
430
431  for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
432    if (getInputName(i) == SymbolicName)
433      return getNumOutputs() + NumPlusOperands + i;
434
435  // Not found.
436  return -1;
437}
438
439/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
440/// it into pieces.  If the asm string is erroneous, emit errors and return
441/// true, otherwise return false.
442unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
443                                const ASTContext &C, unsigned &DiagOffs) const {
444  StringRef Str = getAsmString()->getString();
445  const char *StrStart = Str.begin();
446  const char *StrEnd = Str.end();
447  const char *CurPtr = StrStart;
448
449  // "Simple" inline asms have no constraints or operands, just convert the asm
450  // string to escape $'s.
451  if (isSimple()) {
452    std::string Result;
453    for (; CurPtr != StrEnd; ++CurPtr) {
454      switch (*CurPtr) {
455      case '$':
456        Result += "$$";
457        break;
458      default:
459        Result += *CurPtr;
460        break;
461      }
462    }
463    Pieces.push_back(AsmStringPiece(Result));
464    return 0;
465  }
466
467  // CurStringPiece - The current string that we are building up as we scan the
468  // asm string.
469  std::string CurStringPiece;
470
471  bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
472
473  while (1) {
474    // Done with the string?
475    if (CurPtr == StrEnd) {
476      if (!CurStringPiece.empty())
477        Pieces.push_back(AsmStringPiece(CurStringPiece));
478      return 0;
479    }
480
481    char CurChar = *CurPtr++;
482    switch (CurChar) {
483    case '$': CurStringPiece += "$$"; continue;
484    case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
485    case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
486    case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
487    case '%':
488      break;
489    default:
490      CurStringPiece += CurChar;
491      continue;
492    }
493
494    // Escaped "%" character in asm string.
495    if (CurPtr == StrEnd) {
496      // % at end of string is invalid (no escape).
497      DiagOffs = CurPtr-StrStart-1;
498      return diag::err_asm_invalid_escape;
499    }
500
501    char EscapedChar = *CurPtr++;
502    if (EscapedChar == '%') {  // %% -> %
503      // Escaped percentage sign.
504      CurStringPiece += '%';
505      continue;
506    }
507
508    if (EscapedChar == '=') {  // %= -> Generate an unique ID.
509      CurStringPiece += "${:uid}";
510      continue;
511    }
512
513    // Otherwise, we have an operand.  If we have accumulated a string so far,
514    // add it to the Pieces list.
515    if (!CurStringPiece.empty()) {
516      Pieces.push_back(AsmStringPiece(CurStringPiece));
517      CurStringPiece.clear();
518    }
519
520    // Handle %x4 and %x[foo] by capturing x as the modifier character.
521    char Modifier = '\0';
522    if (isLetter(EscapedChar)) {
523      if (CurPtr == StrEnd) { // Premature end.
524        DiagOffs = CurPtr-StrStart-1;
525        return diag::err_asm_invalid_escape;
526      }
527      Modifier = EscapedChar;
528      EscapedChar = *CurPtr++;
529    }
530
531    if (isDigit(EscapedChar)) {
532      // %n - Assembler operand n
533      unsigned N = 0;
534
535      --CurPtr;
536      while (CurPtr != StrEnd && isDigit(*CurPtr))
537        N = N*10 + ((*CurPtr++)-'0');
538
539      unsigned NumOperands =
540        getNumOutputs() + getNumPlusOperands() + getNumInputs();
541      if (N >= NumOperands) {
542        DiagOffs = CurPtr-StrStart-1;
543        return diag::err_asm_invalid_operand_number;
544      }
545
546      Pieces.push_back(AsmStringPiece(N, Modifier));
547      continue;
548    }
549
550    // Handle %[foo], a symbolic operand reference.
551    if (EscapedChar == '[') {
552      DiagOffs = CurPtr-StrStart-1;
553
554      // Find the ']'.
555      const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
556      if (NameEnd == nullptr)
557        return diag::err_asm_unterminated_symbolic_operand_name;
558      if (NameEnd == CurPtr)
559        return diag::err_asm_empty_symbolic_operand_name;
560
561      StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
562
563      int N = getNamedOperand(SymbolicName);
564      if (N == -1) {
565        // Verify that an operand with that name exists.
566        DiagOffs = CurPtr-StrStart;
567        return diag::err_asm_unknown_symbolic_operand_name;
568      }
569      Pieces.push_back(AsmStringPiece(N, Modifier));
570
571      CurPtr = NameEnd+1;
572      continue;
573    }
574
575    DiagOffs = CurPtr-StrStart-1;
576    return diag::err_asm_invalid_escape;
577  }
578}
579
580/// Assemble final IR asm string (GCC-style).
581std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
582  // Analyze the asm string to decompose it into its pieces.  We know that Sema
583  // has already done this, so it is guaranteed to be successful.
584  SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
585  unsigned DiagOffs;
586  AnalyzeAsmString(Pieces, C, DiagOffs);
587
588  std::string AsmString;
589  for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
590    if (Pieces[i].isString())
591      AsmString += Pieces[i].getString();
592    else if (Pieces[i].getModifier() == '\0')
593      AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
594    else
595      AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
596                   Pieces[i].getModifier() + '}';
597  }
598  return AsmString;
599}
600
601/// Assemble final IR asm string (MS-style).
602std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
603  // FIXME: This needs to be translated into the IR string representation.
604  return AsmStr;
605}
606
607Expr *MSAsmStmt::getOutputExpr(unsigned i) {
608  return cast<Expr>(Exprs[i]);
609}
610
611Expr *MSAsmStmt::getInputExpr(unsigned i) {
612  return cast<Expr>(Exprs[i + NumOutputs]);
613}
614void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
615  Exprs[i + NumOutputs] = E;
616}
617
618QualType CXXCatchStmt::getCaughtType() const {
619  if (ExceptionDecl)
620    return ExceptionDecl->getType();
621  return QualType();
622}
623
624//===----------------------------------------------------------------------===//
625// Constructors
626//===----------------------------------------------------------------------===//
627
628GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
629                       bool issimple, bool isvolatile, unsigned numoutputs,
630                       unsigned numinputs, IdentifierInfo **names,
631                       StringLiteral **constraints, Expr **exprs,
632                       StringLiteral *asmstr, unsigned numclobbers,
633                       StringLiteral **clobbers, SourceLocation rparenloc)
634  : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
635            numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
636
637  unsigned NumExprs = NumOutputs + NumInputs;
638
639  Names = new (C) IdentifierInfo*[NumExprs];
640  std::copy(names, names + NumExprs, Names);
641
642  Exprs = new (C) Stmt*[NumExprs];
643  std::copy(exprs, exprs + NumExprs, Exprs);
644
645  Constraints = new (C) StringLiteral*[NumExprs];
646  std::copy(constraints, constraints + NumExprs, Constraints);
647
648  Clobbers = new (C) StringLiteral*[NumClobbers];
649  std::copy(clobbers, clobbers + NumClobbers, Clobbers);
650}
651
652MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
653                     SourceLocation lbraceloc, bool issimple, bool isvolatile,
654                     ArrayRef<Token> asmtoks, unsigned numoutputs,
655                     unsigned numinputs,
656                     ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
657                     StringRef asmstr, ArrayRef<StringRef> clobbers,
658                     SourceLocation endloc)
659  : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
660            numinputs, clobbers.size()), LBraceLoc(lbraceloc),
661            EndLoc(endloc), NumAsmToks(asmtoks.size()) {
662
663  initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
664}
665
666static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
667  size_t size = str.size();
668  char *buffer = new (C) char[size];
669  memcpy(buffer, str.data(), size);
670  return StringRef(buffer, size);
671}
672
673void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
674                           ArrayRef<Token> asmtoks,
675                           ArrayRef<StringRef> constraints,
676                           ArrayRef<Expr*> exprs,
677                           ArrayRef<StringRef> clobbers) {
678  assert(NumAsmToks == asmtoks.size());
679  assert(NumClobbers == clobbers.size());
680
681  unsigned NumExprs = exprs.size();
682  assert(NumExprs == NumOutputs + NumInputs);
683  assert(NumExprs == constraints.size());
684
685  AsmStr = copyIntoContext(C, asmstr);
686
687  Exprs = new (C) Stmt*[NumExprs];
688  for (unsigned i = 0, e = NumExprs; i != e; ++i)
689    Exprs[i] = exprs[i];
690
691  AsmToks = new (C) Token[NumAsmToks];
692  for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
693    AsmToks[i] = asmtoks[i];
694
695  Constraints = new (C) StringRef[NumExprs];
696  for (unsigned i = 0, e = NumExprs; i != e; ++i) {
697    Constraints[i] = copyIntoContext(C, constraints[i]);
698  }
699
700  Clobbers = new (C) StringRef[NumClobbers];
701  for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
702    // FIXME: Avoid the allocation/copy if at all possible.
703    Clobbers[i] = copyIntoContext(C, clobbers[i]);
704  }
705}
706
707ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
708                                             Stmt *Body,  SourceLocation FCL,
709                                             SourceLocation RPL)
710: Stmt(ObjCForCollectionStmtClass) {
711  SubExprs[ELEM] = Elem;
712  SubExprs[COLLECTION] = Collect;
713  SubExprs[BODY] = Body;
714  ForLoc = FCL;
715  RParenLoc = RPL;
716}
717
718ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
719                             Stmt **CatchStmts, unsigned NumCatchStmts,
720                             Stmt *atFinallyStmt)
721  : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
722    NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != nullptr) {
723  Stmt **Stmts = getStmts();
724  Stmts[0] = atTryStmt;
725  for (unsigned I = 0; I != NumCatchStmts; ++I)
726    Stmts[I + 1] = CatchStmts[I];
727
728  if (HasFinally)
729    Stmts[NumCatchStmts + 1] = atFinallyStmt;
730}
731
732ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
733                                     SourceLocation atTryLoc,
734                                     Stmt *atTryStmt,
735                                     Stmt **CatchStmts,
736                                     unsigned NumCatchStmts,
737                                     Stmt *atFinallyStmt) {
738  unsigned Size = sizeof(ObjCAtTryStmt) +
739    (1 + NumCatchStmts + (atFinallyStmt != nullptr)) * sizeof(Stmt *);
740  void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
741  return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
742                                 atFinallyStmt);
743}
744
745ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
746                                          unsigned NumCatchStmts,
747                                          bool HasFinally) {
748  unsigned Size = sizeof(ObjCAtTryStmt) +
749    (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
750  void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
751  return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
752}
753
754SourceLocation ObjCAtTryStmt::getLocEnd() const {
755  if (HasFinally)
756    return getFinallyStmt()->getLocEnd();
757  if (NumCatchStmts)
758    return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
759  return getTryBody()->getLocEnd();
760}
761
762CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
763                               Stmt *tryBlock, ArrayRef<Stmt*> handlers) {
764  std::size_t Size = sizeof(CXXTryStmt);
765  Size += ((handlers.size() + 1) * sizeof(Stmt));
766
767  void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
768  return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
769}
770
771CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
772                               unsigned numHandlers) {
773  std::size_t Size = sizeof(CXXTryStmt);
774  Size += ((numHandlers + 1) * sizeof(Stmt));
775
776  void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
777  return new (Mem) CXXTryStmt(Empty, numHandlers);
778}
779
780CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
781                       ArrayRef<Stmt*> handlers)
782  : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
783  Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
784  Stmts[0] = tryBlock;
785  std::copy(handlers.begin(), handlers.end(), Stmts + 1);
786}
787
788CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
789                                 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
790                                 Stmt *Body, SourceLocation FL,
791                                 SourceLocation CL, SourceLocation RPL)
792  : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
793  SubExprs[RANGE] = Range;
794  SubExprs[BEGINEND] = BeginEndStmt;
795  SubExprs[COND] = Cond;
796  SubExprs[INC] = Inc;
797  SubExprs[LOOPVAR] = LoopVar;
798  SubExprs[BODY] = Body;
799}
800
801Expr *CXXForRangeStmt::getRangeInit() {
802  DeclStmt *RangeStmt = getRangeStmt();
803  VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
804  assert(RangeDecl && "for-range should have a single var decl");
805  return RangeDecl->getInit();
806}
807
808const Expr *CXXForRangeStmt::getRangeInit() const {
809  return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
810}
811
812VarDecl *CXXForRangeStmt::getLoopVariable() {
813  Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
814  assert(LV && "No loop variable in CXXForRangeStmt");
815  return cast<VarDecl>(LV);
816}
817
818const VarDecl *CXXForRangeStmt::getLoopVariable() const {
819  return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
820}
821
822IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
823               Stmt *then, SourceLocation EL, Stmt *elsev)
824  : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
825{
826  setConditionVariable(C, var);
827  SubExprs[COND] = cond;
828  SubExprs[THEN] = then;
829  SubExprs[ELSE] = elsev;
830}
831
832VarDecl *IfStmt::getConditionVariable() const {
833  if (!SubExprs[VAR])
834    return nullptr;
835
836  DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
837  return cast<VarDecl>(DS->getSingleDecl());
838}
839
840void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
841  if (!V) {
842    SubExprs[VAR] = nullptr;
843    return;
844  }
845
846  SourceRange VarRange = V->getSourceRange();
847  SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
848                                   VarRange.getEnd());
849}
850
851ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
852                 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
853                 SourceLocation RP)
854  : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
855{
856  SubExprs[INIT] = Init;
857  setConditionVariable(C, condVar);
858  SubExprs[COND] = Cond;
859  SubExprs[INC] = Inc;
860  SubExprs[BODY] = Body;
861}
862
863VarDecl *ForStmt::getConditionVariable() const {
864  if (!SubExprs[CONDVAR])
865    return nullptr;
866
867  DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
868  return cast<VarDecl>(DS->getSingleDecl());
869}
870
871void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
872  if (!V) {
873    SubExprs[CONDVAR] = nullptr;
874    return;
875  }
876
877  SourceRange VarRange = V->getSourceRange();
878  SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
879                                       VarRange.getEnd());
880}
881
882SwitchStmt::SwitchStmt(const ASTContext &C, VarDecl *Var, Expr *cond)
883  : Stmt(SwitchStmtClass), FirstCase(nullptr), AllEnumCasesCovered(0)
884{
885  setConditionVariable(C, Var);
886  SubExprs[COND] = cond;
887  SubExprs[BODY] = nullptr;
888}
889
890VarDecl *SwitchStmt::getConditionVariable() const {
891  if (!SubExprs[VAR])
892    return nullptr;
893
894  DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
895  return cast<VarDecl>(DS->getSingleDecl());
896}
897
898void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
899  if (!V) {
900    SubExprs[VAR] = nullptr;
901    return;
902  }
903
904  SourceRange VarRange = V->getSourceRange();
905  SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
906                                   VarRange.getEnd());
907}
908
909Stmt *SwitchCase::getSubStmt() {
910  if (isa<CaseStmt>(this))
911    return cast<CaseStmt>(this)->getSubStmt();
912  return cast<DefaultStmt>(this)->getSubStmt();
913}
914
915WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
916                     SourceLocation WL)
917  : Stmt(WhileStmtClass) {
918  setConditionVariable(C, Var);
919  SubExprs[COND] = cond;
920  SubExprs[BODY] = body;
921  WhileLoc = WL;
922}
923
924VarDecl *WhileStmt::getConditionVariable() const {
925  if (!SubExprs[VAR])
926    return nullptr;
927
928  DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
929  return cast<VarDecl>(DS->getSingleDecl());
930}
931
932void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
933  if (!V) {
934    SubExprs[VAR] = nullptr;
935    return;
936  }
937
938  SourceRange VarRange = V->getSourceRange();
939  SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
940                                   VarRange.getEnd());
941}
942
943// IndirectGotoStmt
944LabelDecl *IndirectGotoStmt::getConstantTarget() {
945  if (AddrLabelExpr *E =
946        dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
947    return E->getLabel();
948  return nullptr;
949}
950
951// ReturnStmt
952const Expr* ReturnStmt::getRetValue() const {
953  return cast_or_null<Expr>(RetExpr);
954}
955Expr* ReturnStmt::getRetValue() {
956  return cast_or_null<Expr>(RetExpr);
957}
958
959SEHTryStmt::SEHTryStmt(bool IsCXXTry,
960                       SourceLocation TryLoc,
961                       Stmt *TryBlock,
962                       Stmt *Handler)
963  : Stmt(SEHTryStmtClass),
964    IsCXXTry(IsCXXTry),
965    TryLoc(TryLoc)
966{
967  Children[TRY]     = TryBlock;
968  Children[HANDLER] = Handler;
969}
970
971SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
972                               SourceLocation TryLoc, Stmt *TryBlock,
973                               Stmt *Handler) {
974  return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
975}
976
977SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
978  return dyn_cast<SEHExceptStmt>(getHandler());
979}
980
981SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
982  return dyn_cast<SEHFinallyStmt>(getHandler());
983}
984
985SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
986                             Expr *FilterExpr,
987                             Stmt *Block)
988  : Stmt(SEHExceptStmtClass),
989    Loc(Loc)
990{
991  Children[FILTER_EXPR] = FilterExpr;
992  Children[BLOCK]       = Block;
993}
994
995SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
996                                     Expr *FilterExpr, Stmt *Block) {
997  return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
998}
999
1000SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1001                               Stmt *Block)
1002  : Stmt(SEHFinallyStmtClass),
1003    Loc(Loc),
1004    Block(Block)
1005{}
1006
1007SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
1008                                       Stmt *Block) {
1009  return new(C)SEHFinallyStmt(Loc,Block);
1010}
1011
1012CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1013  unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1014
1015  // Offset of the first Capture object.
1016  unsigned FirstCaptureOffset =
1017    llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1018
1019  return reinterpret_cast<Capture *>(
1020      reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1021      + FirstCaptureOffset);
1022}
1023
1024CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1025                           ArrayRef<Capture> Captures,
1026                           ArrayRef<Expr *> CaptureInits,
1027                           CapturedDecl *CD,
1028                           RecordDecl *RD)
1029  : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
1030    CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
1031  assert( S && "null captured statement");
1032  assert(CD && "null captured declaration for captured statement");
1033  assert(RD && "null record declaration for captured statement");
1034
1035  // Copy initialization expressions.
1036  Stmt **Stored = getStoredStmts();
1037  for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1038    *Stored++ = CaptureInits[I];
1039
1040  // Copy the statement being captured.
1041  *Stored = S;
1042
1043  // Copy all Capture objects.
1044  Capture *Buffer = getStoredCaptures();
1045  std::copy(Captures.begin(), Captures.end(), Buffer);
1046}
1047
1048CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1049  : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
1050    CapDeclAndKind(nullptr, CR_Default), TheRecordDecl(nullptr) {
1051  getStoredStmts()[NumCaptures] = nullptr;
1052}
1053
1054CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
1055                                   CapturedRegionKind Kind,
1056                                   ArrayRef<Capture> Captures,
1057                                   ArrayRef<Expr *> CaptureInits,
1058                                   CapturedDecl *CD,
1059                                   RecordDecl *RD) {
1060  // The layout is
1061  //
1062  // -----------------------------------------------------------
1063  // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1064  // ----------------^-------------------^----------------------
1065  //                 getStoredStmts()    getStoredCaptures()
1066  //
1067  // where S is the statement being captured.
1068  //
1069  assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1070
1071  unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1072  if (!Captures.empty()) {
1073    // Realign for the following Capture array.
1074    Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1075    Size += sizeof(Capture) * Captures.size();
1076  }
1077
1078  void *Mem = Context.Allocate(Size);
1079  return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
1080}
1081
1082CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
1083                                               unsigned NumCaptures) {
1084  unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1085  if (NumCaptures > 0) {
1086    // Realign for the following Capture array.
1087    Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1088    Size += sizeof(Capture) * NumCaptures;
1089  }
1090
1091  void *Mem = Context.Allocate(Size);
1092  return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1093}
1094
1095Stmt::child_range CapturedStmt::children() {
1096  // Children are captured field initilizers.
1097  return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
1098}
1099
1100bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
1101  for (const auto &I : captures()) {
1102    if (!I.capturesVariable())
1103      continue;
1104
1105    // This does not handle variable redeclarations. This should be
1106    // extended to capture variables with redeclarations, for example
1107    // a thread-private variable in OpenMP.
1108    if (I.getCapturedVar() == Var)
1109      return true;
1110  }
1111
1112  return false;
1113}
1114
1115StmtRange OMPClause::children() {
1116  switch(getClauseKind()) {
1117  default : break;
1118#define OPENMP_CLAUSE(Name, Class)                                       \
1119  case OMPC_ ## Name : return static_cast<Class *>(this)->children();
1120#include "clang/Basic/OpenMPKinds.def"
1121  }
1122  llvm_unreachable("unknown OMPClause");
1123}
1124
1125OMPPrivateClause *OMPPrivateClause::Create(const ASTContext &C,
1126                                           SourceLocation StartLoc,
1127                                           SourceLocation LParenLoc,
1128                                           SourceLocation EndLoc,
1129                                           ArrayRef<Expr *> VL) {
1130  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
1131                                                  llvm::alignOf<Expr *>()) +
1132                         sizeof(Expr *) * VL.size());
1133  OMPPrivateClause *Clause = new (Mem) OMPPrivateClause(StartLoc, LParenLoc,
1134                                                        EndLoc, VL.size());
1135  Clause->setVarRefs(VL);
1136  return Clause;
1137}
1138
1139OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
1140                                                unsigned N) {
1141  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
1142                                                  llvm::alignOf<Expr *>()) +
1143                         sizeof(Expr *) * N);
1144  return new (Mem) OMPPrivateClause(N);
1145}
1146
1147OMPFirstprivateClause *OMPFirstprivateClause::Create(const ASTContext &C,
1148                                                     SourceLocation StartLoc,
1149                                                     SourceLocation LParenLoc,
1150                                                     SourceLocation EndLoc,
1151                                                     ArrayRef<Expr *> VL) {
1152  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
1153                                                  llvm::alignOf<Expr *>()) +
1154                         sizeof(Expr *) * VL.size());
1155  OMPFirstprivateClause *Clause = new (Mem) OMPFirstprivateClause(StartLoc,
1156                                                                  LParenLoc,
1157                                                                  EndLoc,
1158                                                                  VL.size());
1159  Clause->setVarRefs(VL);
1160  return Clause;
1161}
1162
1163OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
1164                                                          unsigned N) {
1165  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
1166                                                  llvm::alignOf<Expr *>()) +
1167                         sizeof(Expr *) * N);
1168  return new (Mem) OMPFirstprivateClause(N);
1169}
1170
1171OMPLastprivateClause *OMPLastprivateClause::Create(const ASTContext &C,
1172                                                   SourceLocation StartLoc,
1173                                                   SourceLocation LParenLoc,
1174                                                   SourceLocation EndLoc,
1175                                                   ArrayRef<Expr *> VL) {
1176  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLastprivateClause),
1177                                                  llvm::alignOf<Expr *>()) +
1178                         sizeof(Expr *) * VL.size());
1179  OMPLastprivateClause *Clause =
1180      new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1181  Clause->setVarRefs(VL);
1182  return Clause;
1183}
1184
1185OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
1186                                                        unsigned N) {
1187  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLastprivateClause),
1188                                                  llvm::alignOf<Expr *>()) +
1189                         sizeof(Expr *) * N);
1190  return new (Mem) OMPLastprivateClause(N);
1191}
1192
1193OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
1194                                         SourceLocation StartLoc,
1195                                         SourceLocation LParenLoc,
1196                                         SourceLocation EndLoc,
1197                                         ArrayRef<Expr *> VL) {
1198  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
1199                                                  llvm::alignOf<Expr *>()) +
1200                         sizeof(Expr *) * VL.size());
1201  OMPSharedClause *Clause = new (Mem) OMPSharedClause(StartLoc, LParenLoc,
1202                                                      EndLoc, VL.size());
1203  Clause->setVarRefs(VL);
1204  return Clause;
1205}
1206
1207OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C,
1208                                              unsigned N) {
1209  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
1210                                                  llvm::alignOf<Expr *>()) +
1211                         sizeof(Expr *) * N);
1212  return new (Mem) OMPSharedClause(N);
1213}
1214
1215OMPLinearClause *OMPLinearClause::Create(const ASTContext &C,
1216                                         SourceLocation StartLoc,
1217                                         SourceLocation LParenLoc,
1218                                         SourceLocation ColonLoc,
1219                                         SourceLocation EndLoc,
1220                                         ArrayRef<Expr *> VL, Expr *Step) {
1221  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLinearClause),
1222                                                  llvm::alignOf<Expr *>()) +
1223                         sizeof(Expr *) * (VL.size() + 1));
1224  OMPLinearClause *Clause = new (Mem)
1225      OMPLinearClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
1226  Clause->setVarRefs(VL);
1227  Clause->setStep(Step);
1228  return Clause;
1229}
1230
1231OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
1232                                              unsigned NumVars) {
1233  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLinearClause),
1234                                                  llvm::alignOf<Expr *>()) +
1235                         sizeof(Expr *) * (NumVars + 1));
1236  return new (Mem) OMPLinearClause(NumVars);
1237}
1238
1239OMPAlignedClause *
1240OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
1241                         SourceLocation LParenLoc, SourceLocation ColonLoc,
1242                         SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
1243  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPAlignedClause),
1244                                                  llvm::alignOf<Expr *>()) +
1245                         sizeof(Expr *) * (VL.size() + 1));
1246  OMPAlignedClause *Clause = new (Mem)
1247      OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
1248  Clause->setVarRefs(VL);
1249  Clause->setAlignment(A);
1250  return Clause;
1251}
1252
1253OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
1254                                                unsigned NumVars) {
1255  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPAlignedClause),
1256                                                  llvm::alignOf<Expr *>()) +
1257                         sizeof(Expr *) * (NumVars + 1));
1258  return new (Mem) OMPAlignedClause(NumVars);
1259}
1260
1261OMPCopyinClause *OMPCopyinClause::Create(const ASTContext &C,
1262                                         SourceLocation StartLoc,
1263                                         SourceLocation LParenLoc,
1264                                         SourceLocation EndLoc,
1265                                         ArrayRef<Expr *> VL) {
1266  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyinClause),
1267                                                  llvm::alignOf<Expr *>()) +
1268                         sizeof(Expr *) * VL.size());
1269  OMPCopyinClause *Clause = new (Mem) OMPCopyinClause(StartLoc, LParenLoc,
1270                                                      EndLoc, VL.size());
1271  Clause->setVarRefs(VL);
1272  return Clause;
1273}
1274
1275OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C,
1276                                              unsigned N) {
1277  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyinClause),
1278                                                  llvm::alignOf<Expr *>()) +
1279                         sizeof(Expr *) * N);
1280  return new (Mem) OMPCopyinClause(N);
1281}
1282
1283OMPCopyprivateClause *OMPCopyprivateClause::Create(const ASTContext &C,
1284                                                   SourceLocation StartLoc,
1285                                                   SourceLocation LParenLoc,
1286                                                   SourceLocation EndLoc,
1287                                                   ArrayRef<Expr *> VL) {
1288  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyprivateClause),
1289                                                  llvm::alignOf<Expr *>()) +
1290                         sizeof(Expr *) * VL.size());
1291  OMPCopyprivateClause *Clause =
1292      new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1293  Clause->setVarRefs(VL);
1294  return Clause;
1295}
1296
1297OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
1298                                                        unsigned N) {
1299  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyprivateClause),
1300                                                  llvm::alignOf<Expr *>()) +
1301                         sizeof(Expr *) * N);
1302  return new (Mem) OMPCopyprivateClause(N);
1303}
1304
1305void OMPExecutableDirective::setClauses(ArrayRef<OMPClause *> Clauses) {
1306  assert(Clauses.size() == getNumClauses() &&
1307         "Number of clauses is not the same as the preallocated buffer");
1308  std::copy(Clauses.begin(), Clauses.end(), getClauses().begin());
1309}
1310
1311OMPReductionClause *OMPReductionClause::Create(
1312    const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1313    SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
1314    NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo) {
1315  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPReductionClause),
1316                                                  llvm::alignOf<Expr *>()) +
1317                         sizeof(Expr *) * VL.size());
1318  OMPReductionClause *Clause = new (Mem) OMPReductionClause(
1319      StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
1320  Clause->setVarRefs(VL);
1321  return Clause;
1322}
1323
1324OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
1325                                                    unsigned N) {
1326  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPReductionClause),
1327                                                  llvm::alignOf<Expr *>()) +
1328                         sizeof(Expr *) * N);
1329  return new (Mem) OMPReductionClause(N);
1330}
1331
1332OMPParallelDirective *OMPParallelDirective::Create(
1333                                              const ASTContext &C,
1334                                              SourceLocation StartLoc,
1335                                              SourceLocation EndLoc,
1336                                              ArrayRef<OMPClause *> Clauses,
1337                                              Stmt *AssociatedStmt) {
1338  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
1339                                           llvm::alignOf<OMPClause *>());
1340  void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1341                         sizeof(Stmt *));
1342  OMPParallelDirective *Dir = new (Mem) OMPParallelDirective(StartLoc, EndLoc,
1343                                                             Clauses.size());
1344  Dir->setClauses(Clauses);
1345  Dir->setAssociatedStmt(AssociatedStmt);
1346  return Dir;
1347}
1348
1349OMPParallelDirective *OMPParallelDirective::CreateEmpty(const ASTContext &C,
1350                                                        unsigned NumClauses,
1351                                                        EmptyShell) {
1352  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
1353                                           llvm::alignOf<OMPClause *>());
1354  void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1355                         sizeof(Stmt *));
1356  return new (Mem) OMPParallelDirective(NumClauses);
1357}
1358
1359OMPSimdDirective *
1360OMPSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1361                         SourceLocation EndLoc, unsigned CollapsedNum,
1362                         ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1363  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSimdDirective),
1364                                           llvm::alignOf<OMPClause *>());
1365  void *Mem =
1366      C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1367  OMPSimdDirective *Dir = new (Mem)
1368      OMPSimdDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1369  Dir->setClauses(Clauses);
1370  Dir->setAssociatedStmt(AssociatedStmt);
1371  return Dir;
1372}
1373
1374OMPSimdDirective *OMPSimdDirective::CreateEmpty(const ASTContext &C,
1375                                                unsigned NumClauses,
1376                                                unsigned CollapsedNum,
1377                                                EmptyShell) {
1378  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSimdDirective),
1379                                           llvm::alignOf<OMPClause *>());
1380  void *Mem =
1381      C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1382  return new (Mem) OMPSimdDirective(CollapsedNum, NumClauses);
1383}
1384
1385OMPForDirective *
1386OMPForDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1387                        SourceLocation EndLoc, unsigned CollapsedNum,
1388                        ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1389  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForDirective),
1390                                           llvm::alignOf<OMPClause *>());
1391  void *Mem =
1392      C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1393  OMPForDirective *Dir =
1394      new (Mem) OMPForDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1395  Dir->setClauses(Clauses);
1396  Dir->setAssociatedStmt(AssociatedStmt);
1397  return Dir;
1398}
1399
1400OMPForDirective *OMPForDirective::CreateEmpty(const ASTContext &C,
1401                                              unsigned NumClauses,
1402                                              unsigned CollapsedNum,
1403                                              EmptyShell) {
1404  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForDirective),
1405                                           llvm::alignOf<OMPClause *>());
1406  void *Mem =
1407      C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1408  return new (Mem) OMPForDirective(CollapsedNum, NumClauses);
1409}
1410
1411OMPSectionsDirective *OMPSectionsDirective::Create(
1412    const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1413    ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1414  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1415                                           llvm::alignOf<OMPClause *>());
1416  void *Mem =
1417      C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1418  OMPSectionsDirective *Dir =
1419      new (Mem) OMPSectionsDirective(StartLoc, EndLoc, Clauses.size());
1420  Dir->setClauses(Clauses);
1421  Dir->setAssociatedStmt(AssociatedStmt);
1422  return Dir;
1423}
1424
1425OMPSectionsDirective *OMPSectionsDirective::CreateEmpty(const ASTContext &C,
1426                                                        unsigned NumClauses,
1427                                                        EmptyShell) {
1428  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1429                                           llvm::alignOf<OMPClause *>());
1430  void *Mem =
1431      C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1432  return new (Mem) OMPSectionsDirective(NumClauses);
1433}
1434
1435OMPSectionDirective *OMPSectionDirective::Create(const ASTContext &C,
1436                                                 SourceLocation StartLoc,
1437                                                 SourceLocation EndLoc,
1438                                                 Stmt *AssociatedStmt) {
1439  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1440                                           llvm::alignOf<Stmt *>());
1441  void *Mem = C.Allocate(Size + sizeof(Stmt *));
1442  OMPSectionDirective *Dir = new (Mem) OMPSectionDirective(StartLoc, EndLoc);
1443  Dir->setAssociatedStmt(AssociatedStmt);
1444  return Dir;
1445}
1446
1447OMPSectionDirective *OMPSectionDirective::CreateEmpty(const ASTContext &C,
1448                                                      EmptyShell) {
1449  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionDirective),
1450                                           llvm::alignOf<Stmt *>());
1451  void *Mem = C.Allocate(Size + sizeof(Stmt *));
1452  return new (Mem) OMPSectionDirective();
1453}
1454
1455OMPSingleDirective *OMPSingleDirective::Create(const ASTContext &C,
1456                                               SourceLocation StartLoc,
1457                                               SourceLocation EndLoc,
1458                                               ArrayRef<OMPClause *> Clauses,
1459                                               Stmt *AssociatedStmt) {
1460  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSingleDirective),
1461                                           llvm::alignOf<OMPClause *>());
1462  void *Mem =
1463      C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1464  OMPSingleDirective *Dir =
1465      new (Mem) OMPSingleDirective(StartLoc, EndLoc, Clauses.size());
1466  Dir->setClauses(Clauses);
1467  Dir->setAssociatedStmt(AssociatedStmt);
1468  return Dir;
1469}
1470
1471OMPSingleDirective *OMPSingleDirective::CreateEmpty(const ASTContext &C,
1472                                                    unsigned NumClauses,
1473                                                    EmptyShell) {
1474  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSingleDirective),
1475                                           llvm::alignOf<OMPClause *>());
1476  void *Mem =
1477      C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1478  return new (Mem) OMPSingleDirective(NumClauses);
1479}
1480
1481OMPParallelForDirective *
1482OMPParallelForDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1483                                SourceLocation EndLoc, unsigned CollapsedNum,
1484                                ArrayRef<OMPClause *> Clauses,
1485                                Stmt *AssociatedStmt) {
1486  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForDirective),
1487                                           llvm::alignOf<OMPClause *>());
1488  void *Mem =
1489      C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1490  OMPParallelForDirective *Dir = new (Mem)
1491      OMPParallelForDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1492  Dir->setClauses(Clauses);
1493  Dir->setAssociatedStmt(AssociatedStmt);
1494  return Dir;
1495}
1496
1497OMPParallelForDirective *
1498OMPParallelForDirective::CreateEmpty(const ASTContext &C, unsigned NumClauses,
1499                                     unsigned CollapsedNum, EmptyShell) {
1500  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForDirective),
1501                                           llvm::alignOf<OMPClause *>());
1502  void *Mem =
1503      C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1504  return new (Mem) OMPParallelForDirective(CollapsedNum, NumClauses);
1505}
1506
1507OMPParallelSectionsDirective *OMPParallelSectionsDirective::Create(
1508    const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1509    ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1510  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelSectionsDirective),
1511                                           llvm::alignOf<OMPClause *>());
1512  void *Mem =
1513      C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1514  OMPParallelSectionsDirective *Dir =
1515      new (Mem) OMPParallelSectionsDirective(StartLoc, EndLoc, Clauses.size());
1516  Dir->setClauses(Clauses);
1517  Dir->setAssociatedStmt(AssociatedStmt);
1518  return Dir;
1519}
1520
1521OMPParallelSectionsDirective *
1522OMPParallelSectionsDirective::CreateEmpty(const ASTContext &C,
1523                                          unsigned NumClauses, EmptyShell) {
1524  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelSectionsDirective),
1525                                           llvm::alignOf<OMPClause *>());
1526  void *Mem =
1527      C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1528  return new (Mem) OMPParallelSectionsDirective(NumClauses);
1529}
1530
1531