1 //===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Stmt class and statement subclasses.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/Stmt.h"
15 #include "clang/AST/ExprCXX.h"
16 #include "clang/AST/ExprObjC.h"
17 #include "clang/AST/StmtCXX.h"
18 #include "clang/AST/StmtObjC.h"
19 #include "clang/AST/Type.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/ASTDiagnostic.h"
22 #include "clang/Basic/TargetInfo.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
27 static struct StmtClassNameTable {
31 } StmtClassInfo[Stmt::lastStmtConstant+1];
33 static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
34 static bool Initialized = false;
36 return StmtClassInfo[E];
38 // Intialize the table on the first use.
40 #define ABSTRACT_STMT(STMT)
41 #define STMT(CLASS, PARENT) \
42 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
43 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
44 #include "clang/AST/StmtNodes.inc"
46 return StmtClassInfo[E];
49 const char *Stmt::getStmtClassName() const {
50 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
53 void Stmt::PrintStats() {
54 // Ensure the table is primed.
55 getStmtInfoTableEntry(Stmt::NullStmtClass);
58 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
59 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
60 if (StmtClassInfo[i].Name == 0) continue;
61 sum += StmtClassInfo[i].Counter;
63 llvm::errs() << " " << sum << " stmts/exprs total.\n";
65 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
66 if (StmtClassInfo[i].Name == 0) continue;
67 if (StmtClassInfo[i].Counter == 0) continue;
68 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
69 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
70 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
72 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
75 llvm::errs() << "Total bytes = " << sum << "\n";
78 void Stmt::addStmtClass(StmtClass s) {
79 ++getStmtInfoTableEntry(s).Counter;
82 bool Stmt::StatisticsEnabled = false;
83 void Stmt::EnableStatistics() {
84 StatisticsEnabled = true;
87 Stmt *Stmt::IgnoreImplicit() {
90 if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s))
91 s = ewc->getSubExpr();
93 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s))
94 s = ice->getSubExpr();
99 /// \brief Strip off all label-like statements.
101 /// This will strip off label statements, case statements, attributed
102 /// statements and default statements recursively.
103 const Stmt *Stmt::stripLabelLikeStatements() const {
104 const Stmt *S = this;
106 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
107 S = LS->getSubStmt();
108 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
109 S = SC->getSubStmt();
110 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
111 S = AS->getSubStmt();
121 // These silly little functions have to be static inline to suppress
122 // unused warnings, and they have to be defined to suppress other
124 static inline good is_good(good) { return good(); }
126 typedef Stmt::child_range children_t();
127 template <class T> good implements_children(children_t T::*) {
130 static inline bad implements_children(children_t Stmt::*) {
134 typedef SourceRange getSourceRange_t() const;
135 template <class T> good implements_getSourceRange(getSourceRange_t T::*) {
138 static inline bad implements_getSourceRange(getSourceRange_t Stmt::*) {
142 #define ASSERT_IMPLEMENTS_children(type) \
143 (void) sizeof(is_good(implements_children(&type::children)))
144 #define ASSERT_IMPLEMENTS_getSourceRange(type) \
145 (void) sizeof(is_good(implements_getSourceRange(&type::getSourceRange)))
148 /// Check whether the various Stmt classes implement their member
150 static inline void check_implementations() {
151 #define ABSTRACT_STMT(type)
152 #define STMT(type, base) \
153 ASSERT_IMPLEMENTS_children(type); \
154 ASSERT_IMPLEMENTS_getSourceRange(type);
155 #include "clang/AST/StmtNodes.inc"
158 Stmt::child_range Stmt::children() {
159 switch (getStmtClass()) {
160 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
161 #define ABSTRACT_STMT(type)
162 #define STMT(type, base) \
163 case Stmt::type##Class: \
164 return static_cast<type*>(this)->children();
165 #include "clang/AST/StmtNodes.inc"
167 llvm_unreachable("unknown statement kind!");
170 SourceRange Stmt::getSourceRange() const {
171 switch (getStmtClass()) {
172 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
173 #define ABSTRACT_STMT(type)
174 #define STMT(type, base) \
175 case Stmt::type##Class: \
176 return static_cast<const type*>(this)->getSourceRange();
177 #include "clang/AST/StmtNodes.inc"
179 llvm_unreachable("unknown statement kind!");
182 // Amusing macro metaprogramming hack: check whether a class provides
183 // a more specific implementation of getLocStart() and getLocEnd().
185 // See also Expr.cpp:getExprLoc().
187 /// This implementation is used when a class provides a custom
188 /// implementation of getLocStart.
189 template <class S, class T>
190 SourceLocation getLocStartImpl(const Stmt *stmt,
191 SourceLocation (T::*v)() const) {
192 return static_cast<const S*>(stmt)->getLocStart();
195 /// This implementation is used when a class doesn't provide a custom
196 /// implementation of getLocStart. Overload resolution should pick it over
197 /// the implementation above because it's more specialized according to
198 /// function template partial ordering.
200 SourceLocation getLocStartImpl(const Stmt *stmt,
201 SourceLocation (Stmt::*v)() const) {
202 return static_cast<const S*>(stmt)->getSourceRange().getBegin();
205 /// This implementation is used when a class provides a custom
206 /// implementation of getLocEnd.
207 template <class S, class T>
208 SourceLocation getLocEndImpl(const Stmt *stmt,
209 SourceLocation (T::*v)() const) {
210 return static_cast<const S*>(stmt)->getLocEnd();
213 /// This implementation is used when a class doesn't provide a custom
214 /// implementation of getLocEnd. Overload resolution should pick it over
215 /// the implementation above because it's more specialized according to
216 /// function template partial ordering.
218 SourceLocation getLocEndImpl(const Stmt *stmt,
219 SourceLocation (Stmt::*v)() const) {
220 return static_cast<const S*>(stmt)->getSourceRange().getEnd();
224 SourceLocation Stmt::getLocStart() const {
225 switch (getStmtClass()) {
226 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
227 #define ABSTRACT_STMT(type)
228 #define STMT(type, base) \
229 case Stmt::type##Class: \
230 return getLocStartImpl<type>(this, &type::getLocStart);
231 #include "clang/AST/StmtNodes.inc"
233 llvm_unreachable("unknown statement kind");
236 SourceLocation Stmt::getLocEnd() const {
237 switch (getStmtClass()) {
238 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
239 #define ABSTRACT_STMT(type)
240 #define STMT(type, base) \
241 case Stmt::type##Class: \
242 return getLocEndImpl<type>(this, &type::getLocEnd);
243 #include "clang/AST/StmtNodes.inc"
245 llvm_unreachable("unknown statement kind");
248 CompoundStmt::CompoundStmt(ASTContext &C, Stmt **StmtStart, unsigned NumStmts,
249 SourceLocation LB, SourceLocation RB)
250 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
251 CompoundStmtBits.NumStmts = NumStmts;
252 assert(CompoundStmtBits.NumStmts == NumStmts &&
253 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
260 Body = new (C) Stmt*[NumStmts];
261 memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
264 void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
267 this->CompoundStmtBits.NumStmts = NumStmts;
269 Body = new (C) Stmt*[NumStmts];
270 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
273 const char *LabelStmt::getName() const {
274 return getDecl()->getIdentifier()->getNameStart();
277 AttributedStmt *AttributedStmt::Create(ASTContext &C, SourceLocation Loc,
278 ArrayRef<const Attr*> Attrs,
280 void *Mem = C.Allocate(sizeof(AttributedStmt) +
281 sizeof(Attr*) * (Attrs.size() - 1),
282 llvm::alignOf<AttributedStmt>());
283 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
286 AttributedStmt *AttributedStmt::CreateEmpty(ASTContext &C, unsigned NumAttrs) {
287 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
288 void *Mem = C.Allocate(sizeof(AttributedStmt) +
289 sizeof(Attr*) * (NumAttrs - 1),
290 llvm::alignOf<AttributedStmt>());
291 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
294 // This is defined here to avoid polluting Stmt.h with importing Expr.h
295 SourceRange ReturnStmt::getSourceRange() const {
297 return SourceRange(RetLoc, RetExpr->getLocEnd());
299 return SourceRange(RetLoc);
302 bool Stmt::hasImplicitControlFlow() const {
303 switch (StmtBits.sClass) {
308 case ConditionalOperatorClass:
309 case ChooseExprClass:
314 case Stmt::BinaryOperatorClass: {
315 const BinaryOperator* B = cast<BinaryOperator>(this);
316 if (B->isLogicalOp() || B->getOpcode() == BO_Comma)
324 std::string AsmStmt::generateAsmString(ASTContext &C) const {
325 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
326 return gccAsmStmt->generateAsmString(C);
327 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
328 return msAsmStmt->generateAsmString(C);
329 llvm_unreachable("unknown asm statement kind!");
332 StringRef AsmStmt::getOutputConstraint(unsigned i) const {
333 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
334 return gccAsmStmt->getOutputConstraint(i);
335 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
336 return msAsmStmt->getOutputConstraint(i);
337 llvm_unreachable("unknown asm statement kind!");
340 const Expr *AsmStmt::getOutputExpr(unsigned i) const {
341 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
342 return gccAsmStmt->getOutputExpr(i);
343 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
344 return msAsmStmt->getOutputExpr(i);
345 llvm_unreachable("unknown asm statement kind!");
348 StringRef AsmStmt::getInputConstraint(unsigned i) const {
349 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
350 return gccAsmStmt->getInputConstraint(i);
351 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
352 return msAsmStmt->getInputConstraint(i);
353 llvm_unreachable("unknown asm statement kind!");
356 const Expr *AsmStmt::getInputExpr(unsigned i) const {
357 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
358 return gccAsmStmt->getInputExpr(i);
359 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
360 return msAsmStmt->getInputExpr(i);
361 llvm_unreachable("unknown asm statement kind!");
364 StringRef AsmStmt::getClobber(unsigned i) const {
365 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
366 return gccAsmStmt->getClobber(i);
367 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
368 return msAsmStmt->getClobber(i);
369 llvm_unreachable("unknown asm statement kind!");
372 /// getNumPlusOperands - Return the number of output operands that have a "+"
374 unsigned AsmStmt::getNumPlusOperands() const {
376 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
377 if (isOutputPlusConstraint(i))
382 StringRef GCCAsmStmt::getClobber(unsigned i) const {
383 return getClobberStringLiteral(i)->getString();
386 Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
387 return cast<Expr>(Exprs[i]);
390 /// getOutputConstraint - Return the constraint string for the specified
391 /// output operand. All output constraints are known to be non-empty (either
393 StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
394 return getOutputConstraintLiteral(i)->getString();
397 Expr *GCCAsmStmt::getInputExpr(unsigned i) {
398 return cast<Expr>(Exprs[i + NumOutputs]);
400 void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
401 Exprs[i + NumOutputs] = E;
404 /// getInputConstraint - Return the specified input constraint. Unlike output
405 /// constraints, these can be empty.
406 StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
407 return getInputConstraintLiteral(i)->getString();
410 void GCCAsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
411 IdentifierInfo **Names,
412 StringLiteral **Constraints,
416 StringLiteral **Clobbers,
417 unsigned NumClobbers) {
418 this->NumOutputs = NumOutputs;
419 this->NumInputs = NumInputs;
420 this->NumClobbers = NumClobbers;
422 unsigned NumExprs = NumOutputs + NumInputs;
424 C.Deallocate(this->Names);
425 this->Names = new (C) IdentifierInfo*[NumExprs];
426 std::copy(Names, Names + NumExprs, this->Names);
428 C.Deallocate(this->Exprs);
429 this->Exprs = new (C) Stmt*[NumExprs];
430 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
432 C.Deallocate(this->Constraints);
433 this->Constraints = new (C) StringLiteral*[NumExprs];
434 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
436 C.Deallocate(this->Clobbers);
437 this->Clobbers = new (C) StringLiteral*[NumClobbers];
438 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
441 /// getNamedOperand - Given a symbolic operand reference like %[foo],
442 /// translate this into a numeric value needed to reference the same operand.
443 /// This returns -1 if the operand name is invalid.
444 int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
445 unsigned NumPlusOperands = 0;
447 // Check if this is an output operand.
448 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
449 if (getOutputName(i) == SymbolicName)
453 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
454 if (getInputName(i) == SymbolicName)
455 return getNumOutputs() + NumPlusOperands + i;
461 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
462 /// it into pieces. If the asm string is erroneous, emit errors and return
463 /// true, otherwise return false.
464 unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
465 ASTContext &C, unsigned &DiagOffs) const {
466 StringRef Str = getAsmString()->getString();
467 const char *StrStart = Str.begin();
468 const char *StrEnd = Str.end();
469 const char *CurPtr = StrStart;
471 // "Simple" inline asms have no constraints or operands, just convert the asm
472 // string to escape $'s.
475 for (; CurPtr != StrEnd; ++CurPtr) {
485 Pieces.push_back(AsmStringPiece(Result));
489 // CurStringPiece - The current string that we are building up as we scan the
491 std::string CurStringPiece;
493 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
496 // Done with the string?
497 if (CurPtr == StrEnd) {
498 if (!CurStringPiece.empty())
499 Pieces.push_back(AsmStringPiece(CurStringPiece));
503 char CurChar = *CurPtr++;
505 case '$': CurStringPiece += "$$"; continue;
506 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
507 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
508 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
512 CurStringPiece += CurChar;
516 // Escaped "%" character in asm string.
517 if (CurPtr == StrEnd) {
518 // % at end of string is invalid (no escape).
519 DiagOffs = CurPtr-StrStart-1;
520 return diag::err_asm_invalid_escape;
523 char EscapedChar = *CurPtr++;
524 if (EscapedChar == '%') { // %% -> %
525 // Escaped percentage sign.
526 CurStringPiece += '%';
530 if (EscapedChar == '=') { // %= -> Generate an unique ID.
531 CurStringPiece += "${:uid}";
535 // Otherwise, we have an operand. If we have accumulated a string so far,
536 // add it to the Pieces list.
537 if (!CurStringPiece.empty()) {
538 Pieces.push_back(AsmStringPiece(CurStringPiece));
539 CurStringPiece.clear();
542 // Handle %x4 and %x[foo] by capturing x as the modifier character.
543 char Modifier = '\0';
544 if (isalpha(EscapedChar)) {
545 if (CurPtr == StrEnd) { // Premature end.
546 DiagOffs = CurPtr-StrStart-1;
547 return diag::err_asm_invalid_escape;
549 Modifier = EscapedChar;
550 EscapedChar = *CurPtr++;
553 if (isdigit(EscapedChar)) {
554 // %n - Assembler operand n
558 while (CurPtr != StrEnd && isdigit(*CurPtr))
559 N = N*10 + ((*CurPtr++)-'0');
561 unsigned NumOperands =
562 getNumOutputs() + getNumPlusOperands() + getNumInputs();
563 if (N >= NumOperands) {
564 DiagOffs = CurPtr-StrStart-1;
565 return diag::err_asm_invalid_operand_number;
568 Pieces.push_back(AsmStringPiece(N, Modifier));
572 // Handle %[foo], a symbolic operand reference.
573 if (EscapedChar == '[') {
574 DiagOffs = CurPtr-StrStart-1;
577 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
579 return diag::err_asm_unterminated_symbolic_operand_name;
580 if (NameEnd == CurPtr)
581 return diag::err_asm_empty_symbolic_operand_name;
583 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
585 int N = getNamedOperand(SymbolicName);
587 // Verify that an operand with that name exists.
588 DiagOffs = CurPtr-StrStart;
589 return diag::err_asm_unknown_symbolic_operand_name;
591 Pieces.push_back(AsmStringPiece(N, Modifier));
597 DiagOffs = CurPtr-StrStart-1;
598 return diag::err_asm_invalid_escape;
602 /// Assemble final IR asm string (GCC-style).
603 std::string GCCAsmStmt::generateAsmString(ASTContext &C) const {
604 // Analyze the asm string to decompose it into its pieces. We know that Sema
605 // has already done this, so it is guaranteed to be successful.
606 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
608 AnalyzeAsmString(Pieces, C, DiagOffs);
610 std::string AsmString;
611 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
612 if (Pieces[i].isString())
613 AsmString += Pieces[i].getString();
614 else if (Pieces[i].getModifier() == '\0')
615 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
617 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
618 Pieces[i].getModifier() + '}';
623 /// Assemble final IR asm string (MS-style).
624 std::string MSAsmStmt::generateAsmString(ASTContext &C) const {
625 // FIXME: This needs to be translated into the IR string representation.
629 Expr *MSAsmStmt::getOutputExpr(unsigned i) {
630 return cast<Expr>(Exprs[i]);
633 Expr *MSAsmStmt::getInputExpr(unsigned i) {
634 return cast<Expr>(Exprs[i + NumOutputs]);
636 void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
637 Exprs[i + NumOutputs] = E;
640 QualType CXXCatchStmt::getCaughtType() const {
642 return ExceptionDecl->getType();
646 //===----------------------------------------------------------------------===//
648 //===----------------------------------------------------------------------===//
650 GCCAsmStmt::GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
651 bool isvolatile, unsigned numoutputs, unsigned numinputs,
652 IdentifierInfo **names, StringLiteral **constraints,
653 Expr **exprs, StringLiteral *asmstr,
654 unsigned numclobbers, StringLiteral **clobbers,
655 SourceLocation rparenloc)
656 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
657 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
659 unsigned NumExprs = NumOutputs + NumInputs;
661 Names = new (C) IdentifierInfo*[NumExprs];
662 std::copy(names, names + NumExprs, Names);
664 Exprs = new (C) Stmt*[NumExprs];
665 std::copy(exprs, exprs + NumExprs, Exprs);
667 Constraints = new (C) StringLiteral*[NumExprs];
668 std::copy(constraints, constraints + NumExprs, Constraints);
670 Clobbers = new (C) StringLiteral*[NumClobbers];
671 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
674 MSAsmStmt::MSAsmStmt(ASTContext &C, SourceLocation asmloc,
675 SourceLocation lbraceloc, bool issimple, bool isvolatile,
676 ArrayRef<Token> asmtoks, unsigned numoutputs,
677 unsigned numinputs, ArrayRef<IdentifierInfo*> names,
678 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
679 StringRef asmstr, ArrayRef<StringRef> clobbers,
680 SourceLocation endloc)
681 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
682 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
683 EndLoc(endloc), AsmStr(asmstr.str()), NumAsmToks(asmtoks.size()) {
685 unsigned NumExprs = NumOutputs + NumInputs;
687 Names = new (C) IdentifierInfo*[NumExprs];
688 for (unsigned i = 0, e = NumExprs; i != e; ++i)
691 Exprs = new (C) Stmt*[NumExprs];
692 for (unsigned i = 0, e = NumExprs; i != e; ++i)
695 AsmToks = new (C) Token[NumAsmToks];
696 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
697 AsmToks[i] = asmtoks[i];
699 Constraints = new (C) StringRef[NumExprs];
700 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
701 size_t size = constraints[i].size();
702 char *dest = new (C) char[size];
703 std::strncpy(dest, constraints[i].data(), size);
704 Constraints[i] = StringRef(dest, size);
707 Clobbers = new (C) StringRef[NumClobbers];
708 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
709 // FIXME: Avoid the allocation/copy if at all possible.
710 size_t size = clobbers[i].size();
711 char *dest = new (C) char[size];
712 std::strncpy(dest, clobbers[i].data(), size);
713 Clobbers[i] = StringRef(dest, size);
717 ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
718 Stmt *Body, SourceLocation FCL,
720 : Stmt(ObjCForCollectionStmtClass) {
721 SubExprs[ELEM] = Elem;
722 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
723 SubExprs[BODY] = Body;
728 ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
729 Stmt **CatchStmts, unsigned NumCatchStmts,
731 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
732 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
734 Stmt **Stmts = getStmts();
735 Stmts[0] = atTryStmt;
736 for (unsigned I = 0; I != NumCatchStmts; ++I)
737 Stmts[I + 1] = CatchStmts[I];
740 Stmts[NumCatchStmts + 1] = atFinallyStmt;
743 ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
744 SourceLocation atTryLoc,
747 unsigned NumCatchStmts,
748 Stmt *atFinallyStmt) {
749 unsigned Size = sizeof(ObjCAtTryStmt) +
750 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
751 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
752 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
756 ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
757 unsigned NumCatchStmts,
759 unsigned Size = sizeof(ObjCAtTryStmt) +
760 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
761 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
762 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
765 SourceRange ObjCAtTryStmt::getSourceRange() const {
766 SourceLocation EndLoc;
768 EndLoc = getFinallyStmt()->getLocEnd();
769 else if (NumCatchStmts)
770 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
772 EndLoc = getTryBody()->getLocEnd();
774 return SourceRange(AtTryLoc, EndLoc);
777 CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
778 Stmt *tryBlock, Stmt **handlers,
779 unsigned numHandlers) {
780 std::size_t Size = sizeof(CXXTryStmt);
781 Size += ((numHandlers + 1) * sizeof(Stmt));
783 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
784 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
787 CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
788 unsigned numHandlers) {
789 std::size_t Size = sizeof(CXXTryStmt);
790 Size += ((numHandlers + 1) * sizeof(Stmt));
792 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
793 return new (Mem) CXXTryStmt(Empty, numHandlers);
796 CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
797 Stmt **handlers, unsigned numHandlers)
798 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
799 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
801 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
804 CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
805 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
806 Stmt *Body, SourceLocation FL,
807 SourceLocation CL, SourceLocation RPL)
808 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
809 SubExprs[RANGE] = Range;
810 SubExprs[BEGINEND] = BeginEndStmt;
811 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
812 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
813 SubExprs[LOOPVAR] = LoopVar;
814 SubExprs[BODY] = Body;
817 Expr *CXXForRangeStmt::getRangeInit() {
818 DeclStmt *RangeStmt = getRangeStmt();
819 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
820 assert(RangeDecl &&& "for-range should have a single var decl");
821 return RangeDecl->getInit();
824 const Expr *CXXForRangeStmt::getRangeInit() const {
825 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
828 VarDecl *CXXForRangeStmt::getLoopVariable() {
829 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
830 assert(LV && "No loop variable in CXXForRangeStmt");
831 return cast<VarDecl>(LV);
834 const VarDecl *CXXForRangeStmt::getLoopVariable() const {
835 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
838 IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
839 Stmt *then, SourceLocation EL, Stmt *elsev)
840 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
842 setConditionVariable(C, var);
843 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
844 SubExprs[THEN] = then;
845 SubExprs[ELSE] = elsev;
848 VarDecl *IfStmt::getConditionVariable() const {
852 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
853 return cast<VarDecl>(DS->getSingleDecl());
856 void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
862 SourceRange VarRange = V->getSourceRange();
863 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
867 ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
868 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
870 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
872 SubExprs[INIT] = Init;
873 setConditionVariable(C, condVar);
874 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
875 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
876 SubExprs[BODY] = Body;
879 VarDecl *ForStmt::getConditionVariable() const {
880 if (!SubExprs[CONDVAR])
883 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
884 return cast<VarDecl>(DS->getSingleDecl());
887 void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
889 SubExprs[CONDVAR] = 0;
893 SourceRange VarRange = V->getSourceRange();
894 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
898 SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
899 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
901 setConditionVariable(C, Var);
902 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
903 SubExprs[BODY] = NULL;
906 VarDecl *SwitchStmt::getConditionVariable() const {
910 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
911 return cast<VarDecl>(DS->getSingleDecl());
914 void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
920 SourceRange VarRange = V->getSourceRange();
921 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
925 Stmt *SwitchCase::getSubStmt() {
926 if (isa<CaseStmt>(this))
927 return cast<CaseStmt>(this)->getSubStmt();
928 return cast<DefaultStmt>(this)->getSubStmt();
931 WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
933 : Stmt(WhileStmtClass) {
934 setConditionVariable(C, Var);
935 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
936 SubExprs[BODY] = body;
940 VarDecl *WhileStmt::getConditionVariable() const {
944 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
945 return cast<VarDecl>(DS->getSingleDecl());
948 void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
954 SourceRange VarRange = V->getSourceRange();
955 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
960 LabelDecl *IndirectGotoStmt::getConstantTarget() {
961 if (AddrLabelExpr *E =
962 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
963 return E->getLabel();
968 const Expr* ReturnStmt::getRetValue() const {
969 return cast_or_null<Expr>(RetExpr);
971 Expr* ReturnStmt::getRetValue() {
972 return cast_or_null<Expr>(RetExpr);
975 SEHTryStmt::SEHTryStmt(bool IsCXXTry,
976 SourceLocation TryLoc,
979 : Stmt(SEHTryStmtClass),
983 Children[TRY] = TryBlock;
984 Children[HANDLER] = Handler;
987 SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
989 SourceLocation TryLoc,
992 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
995 SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
996 return dyn_cast<SEHExceptStmt>(getHandler());
999 SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
1000 return dyn_cast<SEHFinallyStmt>(getHandler());
1003 SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
1006 : Stmt(SEHExceptStmtClass),
1009 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
1010 Children[BLOCK] = Block;
1013 SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
1017 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1020 SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1022 : Stmt(SEHFinallyStmtClass),
1027 SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
1030 return new(C)SEHFinallyStmt(Loc,Block);