Stmt.cpp revision 65d78312ce026092cb6e7b1d4d06f05e18d02aa0
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/Type.h" 22#include "clang/Basic/TargetInfo.h" 23#include "clang/Lex/Token.h" 24#include "llvm/ADT/StringExtras.h" 25#include "llvm/Support/raw_ostream.h" 26using namespace clang; 27 28static struct StmtClassNameTable { 29 const char *Name; 30 unsigned Counter; 31 unsigned Size; 32} StmtClassInfo[Stmt::lastStmtConstant+1]; 33 34static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) { 35 static bool Initialized = false; 36 if (Initialized) 37 return StmtClassInfo[E]; 38 39 // Intialize the table on the first use. 40 Initialized = true; 41#define ABSTRACT_STMT(STMT) 42#define STMT(CLASS, PARENT) \ 43 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \ 44 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS); 45#include "clang/AST/StmtNodes.inc" 46 47 return StmtClassInfo[E]; 48} 49 50void *Stmt::operator new(size_t bytes, ASTContext& C, 51 unsigned alignment) throw() { 52 return ::operator new(bytes, C, alignment); 53} 54 55void *Stmt::operator new(size_t bytes, ASTContext* C, 56 unsigned alignment) throw() { 57 return ::operator new(bytes, *C, alignment); 58} 59 60const char *Stmt::getStmtClassName() const { 61 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name; 62} 63 64void Stmt::PrintStats() { 65 // Ensure the table is primed. 66 getStmtInfoTableEntry(Stmt::NullStmtClass); 67 68 unsigned sum = 0; 69 llvm::errs() << "\n*** Stmt/Expr Stats:\n"; 70 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) { 71 if (StmtClassInfo[i].Name == 0) continue; 72 sum += StmtClassInfo[i].Counter; 73 } 74 llvm::errs() << " " << sum << " stmts/exprs total.\n"; 75 sum = 0; 76 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) { 77 if (StmtClassInfo[i].Name == 0) continue; 78 if (StmtClassInfo[i].Counter == 0) continue; 79 llvm::errs() << " " << StmtClassInfo[i].Counter << " " 80 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size 81 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size 82 << " bytes)\n"; 83 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size; 84 } 85 86 llvm::errs() << "Total bytes = " << sum << "\n"; 87} 88 89void Stmt::addStmtClass(StmtClass s) { 90 ++getStmtInfoTableEntry(s).Counter; 91} 92 93bool Stmt::StatisticsEnabled = false; 94void Stmt::EnableStatistics() { 95 StatisticsEnabled = true; 96} 97 98Stmt *Stmt::IgnoreImplicit() { 99 Stmt *s = this; 100 101 if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s)) 102 s = ewc->getSubExpr(); 103 104 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s)) 105 s = ice->getSubExpr(); 106 107 return s; 108} 109 110/// \brief Strip off all label-like statements. 111/// 112/// This will strip off label statements, case statements, attributed 113/// statements and default statements recursively. 114const Stmt *Stmt::stripLabelLikeStatements() const { 115 const Stmt *S = this; 116 while (true) { 117 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S)) 118 S = LS->getSubStmt(); 119 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) 120 S = SC->getSubStmt(); 121 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S)) 122 S = AS->getSubStmt(); 123 else 124 return S; 125 } 126} 127 128namespace { 129 struct good {}; 130 struct bad {}; 131 132 // These silly little functions have to be static inline to suppress 133 // unused warnings, and they have to be defined to suppress other 134 // warnings. 135 static inline good is_good(good) { return good(); } 136 137 typedef Stmt::child_range children_t(); 138 template <class T> good implements_children(children_t T::*) { 139 return good(); 140 } 141 static inline bad implements_children(children_t Stmt::*) { 142 return bad(); 143 } 144 145 typedef SourceLocation getLocStart_t() const; 146 template <class T> good implements_getLocStart(getLocStart_t T::*) { 147 return good(); 148 } 149 static inline bad implements_getLocStart(getLocStart_t Stmt::*) { 150 return bad(); 151 } 152 153 typedef SourceLocation getLocEnd_t() const; 154 template <class T> good implements_getLocEnd(getLocEnd_t T::*) { 155 return good(); 156 } 157 static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) { 158 return bad(); 159 } 160 161#define ASSERT_IMPLEMENTS_children(type) \ 162 (void) sizeof(is_good(implements_children(&type::children))) 163#define ASSERT_IMPLEMENTS_getLocStart(type) \ 164 (void) sizeof(is_good(implements_getLocStart(&type::getLocStart))) 165#define ASSERT_IMPLEMENTS_getLocEnd(type) \ 166 (void) sizeof(is_good(implements_getLocEnd(&type::getLocEnd))) 167} 168 169/// Check whether the various Stmt classes implement their member 170/// functions. 171static inline void check_implementations() { 172#define ABSTRACT_STMT(type) 173#define STMT(type, base) \ 174 ASSERT_IMPLEMENTS_children(type); \ 175 ASSERT_IMPLEMENTS_getLocStart(type); \ 176 ASSERT_IMPLEMENTS_getLocEnd(type); 177#include "clang/AST/StmtNodes.inc" 178} 179 180Stmt::child_range Stmt::children() { 181 switch (getStmtClass()) { 182 case Stmt::NoStmtClass: llvm_unreachable("statement without class"); 183#define ABSTRACT_STMT(type) 184#define STMT(type, base) \ 185 case Stmt::type##Class: \ 186 return static_cast<type*>(this)->children(); 187#include "clang/AST/StmtNodes.inc" 188 } 189 llvm_unreachable("unknown statement kind!"); 190} 191 192// Amusing macro metaprogramming hack: check whether a class provides 193// a more specific implementation of getSourceRange. 194// 195// See also Expr.cpp:getExprLoc(). 196namespace { 197 /// This implementation is used when a class provides a custom 198 /// implementation of getSourceRange. 199 template <class S, class T> 200 SourceRange getSourceRangeImpl(const Stmt *stmt, 201 SourceRange (T::*v)() const) { 202 return static_cast<const S*>(stmt)->getSourceRange(); 203 } 204 205 /// This implementation is used when a class doesn't provide a custom 206 /// implementation of getSourceRange. Overload resolution should pick it over 207 /// the implementation above because it's more specialized according to 208 /// function template partial ordering. 209 template <class S> 210 SourceRange getSourceRangeImpl(const Stmt *stmt, 211 SourceRange (Stmt::*v)() const) { 212 return SourceRange(static_cast<const S*>(stmt)->getLocStart(), 213 static_cast<const S*>(stmt)->getLocEnd()); 214 } 215} 216 217SourceRange Stmt::getSourceRange() const { 218 switch (getStmtClass()) { 219 case Stmt::NoStmtClass: llvm_unreachable("statement without class"); 220#define ABSTRACT_STMT(type) 221#define STMT(type, base) \ 222 case Stmt::type##Class: \ 223 return getSourceRangeImpl<type>(this, &type::getSourceRange); 224#include "clang/AST/StmtNodes.inc" 225 } 226 llvm_unreachable("unknown statement kind!"); 227} 228 229SourceLocation Stmt::getLocStart() const { 230// llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n"; 231 switch (getStmtClass()) { 232 case Stmt::NoStmtClass: llvm_unreachable("statement without class"); 233#define ABSTRACT_STMT(type) 234#define STMT(type, base) \ 235 case Stmt::type##Class: \ 236 return static_cast<const type*>(this)->getLocStart(); 237#include "clang/AST/StmtNodes.inc" 238 } 239 llvm_unreachable("unknown statement kind"); 240} 241 242SourceLocation Stmt::getLocEnd() const { 243 switch (getStmtClass()) { 244 case Stmt::NoStmtClass: llvm_unreachable("statement without class"); 245#define ABSTRACT_STMT(type) 246#define STMT(type, base) \ 247 case Stmt::type##Class: \ 248 return static_cast<const type*>(this)->getLocEnd(); 249#include "clang/AST/StmtNodes.inc" 250 } 251 llvm_unreachable("unknown statement kind"); 252} 253 254CompoundStmt::CompoundStmt(ASTContext &C, Stmt **StmtStart, unsigned NumStmts, 255 SourceLocation LB, SourceLocation RB) 256 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) { 257 CompoundStmtBits.NumStmts = NumStmts; 258 assert(CompoundStmtBits.NumStmts == NumStmts && 259 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!"); 260 261 if (NumStmts == 0) { 262 Body = 0; 263 return; 264 } 265 266 Body = new (C) Stmt*[NumStmts]; 267 memcpy(Body, StmtStart, NumStmts * sizeof(*Body)); 268} 269 270void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) { 271 if (this->Body) 272 C.Deallocate(Body); 273 this->CompoundStmtBits.NumStmts = NumStmts; 274 275 Body = new (C) Stmt*[NumStmts]; 276 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts); 277} 278 279const char *LabelStmt::getName() const { 280 return getDecl()->getIdentifier()->getNameStart(); 281} 282 283AttributedStmt *AttributedStmt::Create(ASTContext &C, SourceLocation Loc, 284 ArrayRef<const Attr*> Attrs, 285 Stmt *SubStmt) { 286 void *Mem = C.Allocate(sizeof(AttributedStmt) + 287 sizeof(Attr*) * (Attrs.size() - 1), 288 llvm::alignOf<AttributedStmt>()); 289 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt); 290} 291 292AttributedStmt *AttributedStmt::CreateEmpty(ASTContext &C, unsigned NumAttrs) { 293 assert(NumAttrs > 0 && "NumAttrs should be greater than zero"); 294 void *Mem = C.Allocate(sizeof(AttributedStmt) + 295 sizeof(Attr*) * (NumAttrs - 1), 296 llvm::alignOf<AttributedStmt>()); 297 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs); 298} 299 300bool Stmt::hasImplicitControlFlow() const { 301 switch (StmtBits.sClass) { 302 default: 303 return false; 304 305 case CallExprClass: 306 case ConditionalOperatorClass: 307 case ChooseExprClass: 308 case StmtExprClass: 309 case DeclStmtClass: 310 return true; 311 312 case Stmt::BinaryOperatorClass: { 313 const BinaryOperator* B = cast<BinaryOperator>(this); 314 if (B->isLogicalOp() || B->getOpcode() == BO_Comma) 315 return true; 316 else 317 return false; 318 } 319 } 320} 321 322std::string AsmStmt::generateAsmString(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 330StringRef 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 338const 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 346StringRef 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 354const 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 362StringRef 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. 372unsigned 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 380StringRef GCCAsmStmt::getClobber(unsigned i) const { 381 return getClobberStringLiteral(i)->getString(); 382} 383 384Expr *GCCAsmStmt::getOutputExpr(unsigned i) { 385 return cast<Expr>(Exprs[i]); 386} 387 388/// getOutputConstraint - Return the constraint string for the specified 389/// output operand. All output constraints are known to be non-empty (either 390/// '=' or '+'). 391StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const { 392 return getOutputConstraintLiteral(i)->getString(); 393} 394 395Expr *GCCAsmStmt::getInputExpr(unsigned i) { 396 return cast<Expr>(Exprs[i + NumOutputs]); 397} 398void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) { 399 Exprs[i + NumOutputs] = E; 400} 401 402/// getInputConstraint - Return the specified input constraint. Unlike output 403/// constraints, these can be empty. 404StringRef GCCAsmStmt::getInputConstraint(unsigned i) const { 405 return getInputConstraintLiteral(i)->getString(); 406} 407 408void GCCAsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C, 409 IdentifierInfo **Names, 410 StringLiteral **Constraints, 411 Stmt **Exprs, 412 unsigned NumOutputs, 413 unsigned NumInputs, 414 StringLiteral **Clobbers, 415 unsigned NumClobbers) { 416 this->NumOutputs = NumOutputs; 417 this->NumInputs = NumInputs; 418 this->NumClobbers = NumClobbers; 419 420 unsigned NumExprs = NumOutputs + NumInputs; 421 422 C.Deallocate(this->Names); 423 this->Names = new (C) IdentifierInfo*[NumExprs]; 424 std::copy(Names, Names + NumExprs, this->Names); 425 426 C.Deallocate(this->Exprs); 427 this->Exprs = new (C) Stmt*[NumExprs]; 428 std::copy(Exprs, Exprs + NumExprs, this->Exprs); 429 430 C.Deallocate(this->Constraints); 431 this->Constraints = new (C) StringLiteral*[NumExprs]; 432 std::copy(Constraints, Constraints + NumExprs, this->Constraints); 433 434 C.Deallocate(this->Clobbers); 435 this->Clobbers = new (C) StringLiteral*[NumClobbers]; 436 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers); 437} 438 439/// getNamedOperand - Given a symbolic operand reference like %[foo], 440/// translate this into a numeric value needed to reference the same operand. 441/// This returns -1 if the operand name is invalid. 442int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const { 443 unsigned NumPlusOperands = 0; 444 445 // Check if this is an output operand. 446 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) { 447 if (getOutputName(i) == SymbolicName) 448 return i; 449 } 450 451 for (unsigned i = 0, e = getNumInputs(); i != e; ++i) 452 if (getInputName(i) == SymbolicName) 453 return getNumOutputs() + NumPlusOperands + i; 454 455 // Not found. 456 return -1; 457} 458 459/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing 460/// it into pieces. If the asm string is erroneous, emit errors and return 461/// true, otherwise return false. 462unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces, 463 ASTContext &C, unsigned &DiagOffs) const { 464 StringRef Str = getAsmString()->getString(); 465 const char *StrStart = Str.begin(); 466 const char *StrEnd = Str.end(); 467 const char *CurPtr = StrStart; 468 469 // "Simple" inline asms have no constraints or operands, just convert the asm 470 // string to escape $'s. 471 if (isSimple()) { 472 std::string Result; 473 for (; CurPtr != StrEnd; ++CurPtr) { 474 switch (*CurPtr) { 475 case '$': 476 Result += "$$"; 477 break; 478 default: 479 Result += *CurPtr; 480 break; 481 } 482 } 483 Pieces.push_back(AsmStringPiece(Result)); 484 return 0; 485 } 486 487 // CurStringPiece - The current string that we are building up as we scan the 488 // asm string. 489 std::string CurStringPiece; 490 491 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants(); 492 493 while (1) { 494 // Done with the string? 495 if (CurPtr == StrEnd) { 496 if (!CurStringPiece.empty()) 497 Pieces.push_back(AsmStringPiece(CurStringPiece)); 498 return 0; 499 } 500 501 char CurChar = *CurPtr++; 502 switch (CurChar) { 503 case '$': CurStringPiece += "$$"; continue; 504 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue; 505 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue; 506 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue; 507 case '%': 508 break; 509 default: 510 CurStringPiece += CurChar; 511 continue; 512 } 513 514 // Escaped "%" character in asm string. 515 if (CurPtr == StrEnd) { 516 // % at end of string is invalid (no escape). 517 DiagOffs = CurPtr-StrStart-1; 518 return diag::err_asm_invalid_escape; 519 } 520 521 char EscapedChar = *CurPtr++; 522 if (EscapedChar == '%') { // %% -> % 523 // Escaped percentage sign. 524 CurStringPiece += '%'; 525 continue; 526 } 527 528 if (EscapedChar == '=') { // %= -> Generate an unique ID. 529 CurStringPiece += "${:uid}"; 530 continue; 531 } 532 533 // Otherwise, we have an operand. If we have accumulated a string so far, 534 // add it to the Pieces list. 535 if (!CurStringPiece.empty()) { 536 Pieces.push_back(AsmStringPiece(CurStringPiece)); 537 CurStringPiece.clear(); 538 } 539 540 // Handle %x4 and %x[foo] by capturing x as the modifier character. 541 char Modifier = '\0'; 542 if (isalpha(EscapedChar)) { 543 if (CurPtr == StrEnd) { // Premature end. 544 DiagOffs = CurPtr-StrStart-1; 545 return diag::err_asm_invalid_escape; 546 } 547 Modifier = EscapedChar; 548 EscapedChar = *CurPtr++; 549 } 550 551 if (isdigit(EscapedChar)) { 552 // %n - Assembler operand n 553 unsigned N = 0; 554 555 --CurPtr; 556 while (CurPtr != StrEnd && isdigit(*CurPtr)) 557 N = N*10 + ((*CurPtr++)-'0'); 558 559 unsigned NumOperands = 560 getNumOutputs() + getNumPlusOperands() + getNumInputs(); 561 if (N >= NumOperands) { 562 DiagOffs = CurPtr-StrStart-1; 563 return diag::err_asm_invalid_operand_number; 564 } 565 566 Pieces.push_back(AsmStringPiece(N, Modifier)); 567 continue; 568 } 569 570 // Handle %[foo], a symbolic operand reference. 571 if (EscapedChar == '[') { 572 DiagOffs = CurPtr-StrStart-1; 573 574 // Find the ']'. 575 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr); 576 if (NameEnd == 0) 577 return diag::err_asm_unterminated_symbolic_operand_name; 578 if (NameEnd == CurPtr) 579 return diag::err_asm_empty_symbolic_operand_name; 580 581 StringRef SymbolicName(CurPtr, NameEnd - CurPtr); 582 583 int N = getNamedOperand(SymbolicName); 584 if (N == -1) { 585 // Verify that an operand with that name exists. 586 DiagOffs = CurPtr-StrStart; 587 return diag::err_asm_unknown_symbolic_operand_name; 588 } 589 Pieces.push_back(AsmStringPiece(N, Modifier)); 590 591 CurPtr = NameEnd+1; 592 continue; 593 } 594 595 DiagOffs = CurPtr-StrStart-1; 596 return diag::err_asm_invalid_escape; 597 } 598} 599 600/// Assemble final IR asm string (GCC-style). 601std::string GCCAsmStmt::generateAsmString(ASTContext &C) const { 602 // Analyze the asm string to decompose it into its pieces. We know that Sema 603 // has already done this, so it is guaranteed to be successful. 604 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces; 605 unsigned DiagOffs; 606 AnalyzeAsmString(Pieces, C, DiagOffs); 607 608 std::string AsmString; 609 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) { 610 if (Pieces[i].isString()) 611 AsmString += Pieces[i].getString(); 612 else if (Pieces[i].getModifier() == '\0') 613 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo()); 614 else 615 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' + 616 Pieces[i].getModifier() + '}'; 617 } 618 return AsmString; 619} 620 621/// Assemble final IR asm string (MS-style). 622std::string MSAsmStmt::generateAsmString(ASTContext &C) const { 623 // FIXME: This needs to be translated into the IR string representation. 624 return AsmStr; 625} 626 627Expr *MSAsmStmt::getOutputExpr(unsigned i) { 628 return cast<Expr>(Exprs[i]); 629} 630 631Expr *MSAsmStmt::getInputExpr(unsigned i) { 632 return cast<Expr>(Exprs[i + NumOutputs]); 633} 634void MSAsmStmt::setInputExpr(unsigned i, Expr *E) { 635 Exprs[i + NumOutputs] = E; 636} 637 638QualType CXXCatchStmt::getCaughtType() const { 639 if (ExceptionDecl) 640 return ExceptionDecl->getType(); 641 return QualType(); 642} 643 644//===----------------------------------------------------------------------===// 645// Constructors 646//===----------------------------------------------------------------------===// 647 648GCCAsmStmt::GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, 649 bool isvolatile, unsigned numoutputs, unsigned numinputs, 650 IdentifierInfo **names, StringLiteral **constraints, 651 Expr **exprs, StringLiteral *asmstr, 652 unsigned numclobbers, StringLiteral **clobbers, 653 SourceLocation rparenloc) 654 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs, 655 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) { 656 657 unsigned NumExprs = NumOutputs + NumInputs; 658 659 Names = new (C) IdentifierInfo*[NumExprs]; 660 std::copy(names, names + NumExprs, Names); 661 662 Exprs = new (C) Stmt*[NumExprs]; 663 std::copy(exprs, exprs + NumExprs, Exprs); 664 665 Constraints = new (C) StringLiteral*[NumExprs]; 666 std::copy(constraints, constraints + NumExprs, Constraints); 667 668 Clobbers = new (C) StringLiteral*[NumClobbers]; 669 std::copy(clobbers, clobbers + NumClobbers, Clobbers); 670} 671 672MSAsmStmt::MSAsmStmt(ASTContext &C, SourceLocation asmloc, 673 SourceLocation lbraceloc, bool issimple, bool isvolatile, 674 ArrayRef<Token> asmtoks, unsigned numoutputs, 675 unsigned numinputs, ArrayRef<IdentifierInfo*> names, 676 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs, 677 StringRef asmstr, ArrayRef<StringRef> clobbers, 678 SourceLocation endloc) 679 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs, 680 numinputs, clobbers.size()), LBraceLoc(lbraceloc), 681 EndLoc(endloc), AsmStr(asmstr.str()), NumAsmToks(asmtoks.size()) { 682 683 unsigned NumExprs = NumOutputs + NumInputs; 684 685 Names = new (C) IdentifierInfo*[NumExprs]; 686 for (unsigned i = 0, e = NumExprs; i != e; ++i) 687 Names[i] = names[i]; 688 689 Exprs = new (C) Stmt*[NumExprs]; 690 for (unsigned i = 0, e = NumExprs; i != e; ++i) 691 Exprs[i] = exprs[i]; 692 693 AsmToks = new (C) Token[NumAsmToks]; 694 for (unsigned i = 0, e = NumAsmToks; i != e; ++i) 695 AsmToks[i] = asmtoks[i]; 696 697 Constraints = new (C) StringRef[NumExprs]; 698 for (unsigned i = 0, e = NumExprs; i != e; ++i) { 699 size_t size = constraints[i].size(); 700 char *dest = new (C) char[size]; 701 std::strncpy(dest, constraints[i].data(), size); 702 Constraints[i] = StringRef(dest, size); 703 } 704 705 Clobbers = new (C) StringRef[NumClobbers]; 706 for (unsigned i = 0, e = NumClobbers; i != e; ++i) { 707 // FIXME: Avoid the allocation/copy if at all possible. 708 size_t size = clobbers[i].size(); 709 char *dest = new (C) char[size]; 710 std::strncpy(dest, clobbers[i].data(), size); 711 Clobbers[i] = StringRef(dest, size); 712 } 713} 714 715ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, 716 Stmt *Body, SourceLocation FCL, 717 SourceLocation RPL) 718: Stmt(ObjCForCollectionStmtClass) { 719 SubExprs[ELEM] = Elem; 720 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect); 721 SubExprs[BODY] = Body; 722 ForLoc = FCL; 723 RParenLoc = RPL; 724} 725 726ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt, 727 Stmt **CatchStmts, unsigned NumCatchStmts, 728 Stmt *atFinallyStmt) 729 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc), 730 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0) 731{ 732 Stmt **Stmts = getStmts(); 733 Stmts[0] = atTryStmt; 734 for (unsigned I = 0; I != NumCatchStmts; ++I) 735 Stmts[I + 1] = CatchStmts[I]; 736 737 if (HasFinally) 738 Stmts[NumCatchStmts + 1] = atFinallyStmt; 739} 740 741ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context, 742 SourceLocation atTryLoc, 743 Stmt *atTryStmt, 744 Stmt **CatchStmts, 745 unsigned NumCatchStmts, 746 Stmt *atFinallyStmt) { 747 unsigned Size = sizeof(ObjCAtTryStmt) + 748 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *); 749 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>()); 750 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts, 751 atFinallyStmt); 752} 753 754ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context, 755 unsigned NumCatchStmts, 756 bool HasFinally) { 757 unsigned Size = sizeof(ObjCAtTryStmt) + 758 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *); 759 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>()); 760 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally); 761} 762 763SourceLocation ObjCAtTryStmt::getLocEnd() const { 764 if (HasFinally) 765 return getFinallyStmt()->getLocEnd(); 766 if (NumCatchStmts) 767 return getCatchStmt(NumCatchStmts - 1)->getLocEnd(); 768 return getTryBody()->getLocEnd(); 769} 770 771CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc, 772 Stmt *tryBlock, Stmt **handlers, 773 unsigned numHandlers) { 774 std::size_t Size = sizeof(CXXTryStmt); 775 Size += ((numHandlers + 1) * sizeof(Stmt)); 776 777 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>()); 778 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers); 779} 780 781CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty, 782 unsigned numHandlers) { 783 std::size_t Size = sizeof(CXXTryStmt); 784 Size += ((numHandlers + 1) * sizeof(Stmt)); 785 786 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>()); 787 return new (Mem) CXXTryStmt(Empty, numHandlers); 788} 789 790CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, 791 Stmt **handlers, unsigned numHandlers) 792 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) { 793 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1); 794 Stmts[0] = tryBlock; 795 std::copy(handlers, handlers + NumHandlers, Stmts + 1); 796} 797 798CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt, 799 Expr *Cond, Expr *Inc, DeclStmt *LoopVar, 800 Stmt *Body, SourceLocation FL, 801 SourceLocation CL, SourceLocation RPL) 802 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) { 803 SubExprs[RANGE] = Range; 804 SubExprs[BEGINEND] = BeginEndStmt; 805 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond); 806 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc); 807 SubExprs[LOOPVAR] = LoopVar; 808 SubExprs[BODY] = Body; 809} 810 811Expr *CXXForRangeStmt::getRangeInit() { 812 DeclStmt *RangeStmt = getRangeStmt(); 813 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl()); 814 assert(RangeDecl &&& "for-range should have a single var decl"); 815 return RangeDecl->getInit(); 816} 817 818const Expr *CXXForRangeStmt::getRangeInit() const { 819 return const_cast<CXXForRangeStmt*>(this)->getRangeInit(); 820} 821 822VarDecl *CXXForRangeStmt::getLoopVariable() { 823 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl(); 824 assert(LV && "No loop variable in CXXForRangeStmt"); 825 return cast<VarDecl>(LV); 826} 827 828const VarDecl *CXXForRangeStmt::getLoopVariable() const { 829 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable(); 830} 831 832IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond, 833 Stmt *then, SourceLocation EL, Stmt *elsev) 834 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL) 835{ 836 setConditionVariable(C, var); 837 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 838 SubExprs[THEN] = then; 839 SubExprs[ELSE] = elsev; 840} 841 842VarDecl *IfStmt::getConditionVariable() const { 843 if (!SubExprs[VAR]) 844 return 0; 845 846 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]); 847 return cast<VarDecl>(DS->getSingleDecl()); 848} 849 850void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) { 851 if (!V) { 852 SubExprs[VAR] = 0; 853 return; 854 } 855 856 SourceRange VarRange = V->getSourceRange(); 857 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(), 858 VarRange.getEnd()); 859} 860 861ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, 862 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP, 863 SourceLocation RP) 864 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP) 865{ 866 SubExprs[INIT] = Init; 867 setConditionVariable(C, condVar); 868 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond); 869 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc); 870 SubExprs[BODY] = Body; 871} 872 873VarDecl *ForStmt::getConditionVariable() const { 874 if (!SubExprs[CONDVAR]) 875 return 0; 876 877 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]); 878 return cast<VarDecl>(DS->getSingleDecl()); 879} 880 881void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) { 882 if (!V) { 883 SubExprs[CONDVAR] = 0; 884 return; 885 } 886 887 SourceRange VarRange = V->getSourceRange(); 888 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(), 889 VarRange.getEnd()); 890} 891 892SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond) 893 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0) 894{ 895 setConditionVariable(C, Var); 896 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 897 SubExprs[BODY] = NULL; 898} 899 900VarDecl *SwitchStmt::getConditionVariable() const { 901 if (!SubExprs[VAR]) 902 return 0; 903 904 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]); 905 return cast<VarDecl>(DS->getSingleDecl()); 906} 907 908void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) { 909 if (!V) { 910 SubExprs[VAR] = 0; 911 return; 912 } 913 914 SourceRange VarRange = V->getSourceRange(); 915 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(), 916 VarRange.getEnd()); 917} 918 919Stmt *SwitchCase::getSubStmt() { 920 if (isa<CaseStmt>(this)) 921 return cast<CaseStmt>(this)->getSubStmt(); 922 return cast<DefaultStmt>(this)->getSubStmt(); 923} 924 925WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body, 926 SourceLocation WL) 927 : Stmt(WhileStmtClass) { 928 setConditionVariable(C, Var); 929 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 930 SubExprs[BODY] = body; 931 WhileLoc = WL; 932} 933 934VarDecl *WhileStmt::getConditionVariable() const { 935 if (!SubExprs[VAR]) 936 return 0; 937 938 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]); 939 return cast<VarDecl>(DS->getSingleDecl()); 940} 941 942void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) { 943 if (!V) { 944 SubExprs[VAR] = 0; 945 return; 946 } 947 948 SourceRange VarRange = V->getSourceRange(); 949 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(), 950 VarRange.getEnd()); 951} 952 953// IndirectGotoStmt 954LabelDecl *IndirectGotoStmt::getConstantTarget() { 955 if (AddrLabelExpr *E = 956 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts())) 957 return E->getLabel(); 958 return 0; 959} 960 961// ReturnStmt 962const Expr* ReturnStmt::getRetValue() const { 963 return cast_or_null<Expr>(RetExpr); 964} 965Expr* ReturnStmt::getRetValue() { 966 return cast_or_null<Expr>(RetExpr); 967} 968 969SEHTryStmt::SEHTryStmt(bool IsCXXTry, 970 SourceLocation TryLoc, 971 Stmt *TryBlock, 972 Stmt *Handler) 973 : Stmt(SEHTryStmtClass), 974 IsCXXTry(IsCXXTry), 975 TryLoc(TryLoc) 976{ 977 Children[TRY] = TryBlock; 978 Children[HANDLER] = Handler; 979} 980 981SEHTryStmt* SEHTryStmt::Create(ASTContext &C, 982 bool IsCXXTry, 983 SourceLocation TryLoc, 984 Stmt *TryBlock, 985 Stmt *Handler) { 986 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler); 987} 988 989SEHExceptStmt* SEHTryStmt::getExceptHandler() const { 990 return dyn_cast<SEHExceptStmt>(getHandler()); 991} 992 993SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const { 994 return dyn_cast<SEHFinallyStmt>(getHandler()); 995} 996 997SEHExceptStmt::SEHExceptStmt(SourceLocation Loc, 998 Expr *FilterExpr, 999 Stmt *Block) 1000 : Stmt(SEHExceptStmtClass), 1001 Loc(Loc) 1002{ 1003 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr); 1004 Children[BLOCK] = Block; 1005} 1006 1007SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C, 1008 SourceLocation Loc, 1009 Expr *FilterExpr, 1010 Stmt *Block) { 1011 return new(C) SEHExceptStmt(Loc,FilterExpr,Block); 1012} 1013 1014SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc, 1015 Stmt *Block) 1016 : Stmt(SEHFinallyStmtClass), 1017 Loc(Loc), 1018 Block(Block) 1019{} 1020 1021SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C, 1022 SourceLocation Loc, 1023 Stmt *Block) { 1024 return new(C)SEHFinallyStmt(Loc,Block); 1025} 1026