SemaStmt.cpp revision 8d0bcb090775b3285ac99692db61e349eda03366
1//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===// 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 semantic analysis for statements. 11// 12//===----------------------------------------------------------------------===// 13 14#include "Sema.h" 15#include "clang/AST/ASTContext.h" 16#include "clang/AST/DeclObjC.h" 17#include "clang/AST/Expr.h" 18#include "clang/Basic/TargetInfo.h" 19#include "clang/Basic/Diagnostic.h" 20using namespace clang; 21 22Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) { 23 Expr *E = static_cast<Expr*>(expr); 24 assert(E && "ActOnExprStmt(): missing expression"); 25 26 // C99 6.8.3p2: The expression in an expression statement is evaluated as a 27 // void expression for its side effects. Conversion to void allows any 28 // operand, even incomplete types. 29 30 // Same thing in for stmt first clause (when expr) and third clause. 31 return E; 32} 33 34 35Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) { 36 return new NullStmt(SemiLoc); 37} 38 39Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl, SourceLocation StartLoc, 40 SourceLocation EndLoc) { 41 if (decl == 0) 42 return true; 43 44 ScopedDecl *SD = cast<ScopedDecl>(static_cast<Decl *>(decl)); 45 return new DeclStmt(SD, StartLoc, EndLoc); 46} 47 48Action::StmtResult 49Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, 50 StmtTy **elts, unsigned NumElts, bool isStmtExpr) { 51 Stmt **Elts = reinterpret_cast<Stmt**>(elts); 52 // If we're in C89 mode, check that we don't have any decls after stmts. If 53 // so, emit an extension diagnostic. 54 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) { 55 // Note that __extension__ can be around a decl. 56 unsigned i = 0; 57 // Skip over all declarations. 58 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) 59 /*empty*/; 60 61 // We found the end of the list or a statement. Scan for another declstmt. 62 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) 63 /*empty*/; 64 65 if (i != NumElts) { 66 ScopedDecl *D = cast<DeclStmt>(Elts[i])->getDecl(); 67 Diag(D->getLocation(), diag::ext_mixed_decls_code); 68 } 69 } 70 // Warn about unused expressions in statements. 71 for (unsigned i = 0; i != NumElts; ++i) { 72 Expr *E = dyn_cast<Expr>(Elts[i]); 73 if (!E) continue; 74 75 // Warn about expressions with unused results. 76 if (E->hasLocalSideEffect() || E->getType()->isVoidType()) 77 continue; 78 79 // The last expr in a stmt expr really is used. 80 if (isStmtExpr && i == NumElts-1) 81 continue; 82 83 /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in 84 /// a context where the result is unused. Emit a diagnostic to warn about 85 /// this. 86 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) 87 Diag(BO->getOperatorLoc(), diag::warn_unused_expr, 88 BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange()); 89 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) 90 Diag(UO->getOperatorLoc(), diag::warn_unused_expr, 91 UO->getSubExpr()->getSourceRange()); 92 else 93 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange()); 94 } 95 96 return new CompoundStmt(Elts, NumElts, L, R); 97} 98 99Action::StmtResult 100Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval, 101 SourceLocation DotDotDotLoc, ExprTy *rhsval, 102 SourceLocation ColonLoc, StmtTy *subStmt) { 103 Stmt *SubStmt = static_cast<Stmt*>(subStmt); 104 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval); 105 assert((LHSVal != 0) && "missing expression in case statement"); 106 107 SourceLocation ExpLoc; 108 // C99 6.8.4.2p3: The expression shall be an integer constant. 109 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) { 110 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr, 111 LHSVal->getSourceRange()); 112 return SubStmt; 113 } 114 115 // GCC extension: The expression shall be an integer constant. 116 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) { 117 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr, 118 RHSVal->getSourceRange()); 119 RHSVal = 0; // Recover by just forgetting about it. 120 } 121 122 if (SwitchStack.empty()) { 123 Diag(CaseLoc, diag::err_case_not_in_switch); 124 return SubStmt; 125 } 126 127 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc); 128 SwitchStack.back()->addSwitchCase(CS); 129 return CS; 130} 131 132Action::StmtResult 133Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, 134 StmtTy *subStmt, Scope *CurScope) { 135 Stmt *SubStmt = static_cast<Stmt*>(subStmt); 136 137 if (SwitchStack.empty()) { 138 Diag(DefaultLoc, diag::err_default_not_in_switch); 139 return SubStmt; 140 } 141 142 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt); 143 SwitchStack.back()->addSwitchCase(DS); 144 145 return DS; 146} 147 148Action::StmtResult 149Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, 150 SourceLocation ColonLoc, StmtTy *subStmt) { 151 Stmt *SubStmt = static_cast<Stmt*>(subStmt); 152 // Look up the record for this label identifier. 153 LabelStmt *&LabelDecl = LabelMap[II]; 154 155 // If not forward referenced or defined already, just create a new LabelStmt. 156 if (LabelDecl == 0) 157 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt); 158 159 assert(LabelDecl->getID() == II && "Label mismatch!"); 160 161 // Otherwise, this label was either forward reference or multiply defined. If 162 // multiply defined, reject it now. 163 if (LabelDecl->getSubStmt()) { 164 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName()); 165 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition); 166 return SubStmt; 167 } 168 169 // Otherwise, this label was forward declared, and we just found its real 170 // definition. Fill in the forward definition and return it. 171 LabelDecl->setIdentLoc(IdentLoc); 172 LabelDecl->setSubStmt(SubStmt); 173 return LabelDecl; 174} 175 176Action::StmtResult 177Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal, 178 StmtTy *ThenVal, SourceLocation ElseLoc, 179 StmtTy *ElseVal) { 180 Expr *condExpr = (Expr *)CondVal; 181 Stmt *thenStmt = (Stmt *)ThenVal; 182 183 assert(condExpr && "ActOnIfStmt(): missing expression"); 184 185 DefaultFunctionArrayConversion(condExpr); 186 QualType condType = condExpr->getType(); 187 188 if (!condType->isScalarType()) // C99 6.8.4.1p1 189 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar, 190 condType.getAsString(), condExpr->getSourceRange()); 191 192 // Warn if the if block has a null body without an else value. 193 // this helps prevent bugs due to typos, such as 194 // if (condition); 195 // do_stuff(); 196 if (!ElseVal) { 197 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt)) 198 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body); 199 } 200 201 return new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal); 202} 203 204Action::StmtResult 205Sema::ActOnStartOfSwitchStmt(ExprTy *cond) { 206 Expr *Cond = static_cast<Expr*>(cond); 207 208 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. 209 UsualUnaryConversions(Cond); 210 211 SwitchStmt *SS = new SwitchStmt(Cond); 212 SwitchStack.push_back(SS); 213 return SS; 214} 215 216/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have 217/// the specified width and sign. If an overflow occurs, detect it and emit 218/// the specified diagnostic. 219void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, 220 unsigned NewWidth, bool NewSign, 221 SourceLocation Loc, 222 unsigned DiagID) { 223 // Perform a conversion to the promoted condition type if needed. 224 if (NewWidth > Val.getBitWidth()) { 225 // If this is an extension, just do it. 226 llvm::APSInt OldVal(Val); 227 Val.extend(NewWidth); 228 229 // If the input was signed and negative and the output is unsigned, 230 // warn. 231 if (!NewSign && OldVal.isSigned() && OldVal.isNegative()) 232 Diag(Loc, DiagID, OldVal.toString(10), Val.toString(10)); 233 234 Val.setIsSigned(NewSign); 235 } else if (NewWidth < Val.getBitWidth()) { 236 // If this is a truncation, check for overflow. 237 llvm::APSInt ConvVal(Val); 238 ConvVal.trunc(NewWidth); 239 ConvVal.setIsSigned(NewSign); 240 ConvVal.extend(Val.getBitWidth()); 241 ConvVal.setIsSigned(Val.isSigned()); 242 if (ConvVal != Val) 243 Diag(Loc, DiagID, Val.toString(10), ConvVal.toString(10)); 244 245 // Regardless of whether a diagnostic was emitted, really do the 246 // truncation. 247 Val.trunc(NewWidth); 248 Val.setIsSigned(NewSign); 249 } else if (NewSign != Val.isSigned()) { 250 // Convert the sign to match the sign of the condition. This can cause 251 // overflow as well: unsigned(INTMIN) 252 llvm::APSInt OldVal(Val); 253 Val.setIsSigned(NewSign); 254 255 if (Val.isNegative()) // Sign bit changes meaning. 256 Diag(Loc, DiagID, OldVal.toString(10), Val.toString(10)); 257 } 258} 259 260namespace { 261 struct CaseCompareFunctor { 262 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, 263 const llvm::APSInt &RHS) { 264 return LHS.first < RHS; 265 } 266 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, 267 const std::pair<llvm::APSInt, CaseStmt*> &RHS) { 268 return LHS.first < RHS.first; 269 } 270 bool operator()(const llvm::APSInt &LHS, 271 const std::pair<llvm::APSInt, CaseStmt*> &RHS) { 272 return LHS < RHS.first; 273 } 274 }; 275} 276 277/// CmpCaseVals - Comparison predicate for sorting case values. 278/// 279static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, 280 const std::pair<llvm::APSInt, CaseStmt*>& rhs) { 281 if (lhs.first < rhs.first) 282 return true; 283 284 if (lhs.first == rhs.first && 285 lhs.second->getCaseLoc().getRawEncoding() 286 < rhs.second->getCaseLoc().getRawEncoding()) 287 return true; 288 return false; 289} 290 291Action::StmtResult 292Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, 293 ExprTy *Body) { 294 Stmt *BodyStmt = (Stmt*)Body; 295 296 SwitchStmt *SS = SwitchStack.back(); 297 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!"); 298 299 SS->setBody(BodyStmt, SwitchLoc); 300 SwitchStack.pop_back(); 301 302 Expr *CondExpr = SS->getCond(); 303 QualType CondType = CondExpr->getType(); 304 305 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1 306 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer, 307 CondType.getAsString(), CondExpr->getSourceRange()); 308 return true; 309 } 310 311 // Get the bitwidth of the switched-on value before promotions. We must 312 // convert the integer case values to this width before comparison. 313 unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType)); 314 bool CondIsSigned = CondType->isSignedIntegerType(); 315 316 // Accumulate all of the case values in a vector so that we can sort them 317 // and detect duplicates. This vector contains the APInt for the case after 318 // it has been converted to the condition type. 319 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; 320 CaseValsTy CaseVals; 321 322 // Keep track of any GNU case ranges we see. The APSInt is the low value. 323 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges; 324 325 DefaultStmt *TheDefaultStmt = 0; 326 327 bool CaseListIsErroneous = false; 328 329 for (SwitchCase *SC = SS->getSwitchCaseList(); SC; 330 SC = SC->getNextSwitchCase()) { 331 332 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { 333 if (TheDefaultStmt) { 334 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); 335 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label); 336 337 // FIXME: Remove the default statement from the switch block so that 338 // we'll return a valid AST. This requires recursing down the 339 // AST and finding it, not something we are set up to do right now. For 340 // now, just lop the entire switch stmt out of the AST. 341 CaseListIsErroneous = true; 342 } 343 TheDefaultStmt = DS; 344 345 } else { 346 CaseStmt *CS = cast<CaseStmt>(SC); 347 348 // We already verified that the expression has a i-c-e value (C99 349 // 6.8.4.2p3) - get that value now. 350 Expr *Lo = CS->getLHS(); 351 llvm::APSInt LoVal = Lo->getIntegerConstantExprValue(Context); 352 353 // Convert the value to the same width/sign as the condition. 354 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, 355 CS->getLHS()->getLocStart(), 356 diag::warn_case_value_overflow); 357 358 // If the LHS is not the same type as the condition, insert an implicit 359 // cast. 360 ImpCastExprToType(Lo, CondType); 361 CS->setLHS(Lo); 362 363 // If this is a case range, remember it in CaseRanges, otherwise CaseVals. 364 if (CS->getRHS()) 365 CaseRanges.push_back(std::make_pair(LoVal, CS)); 366 else 367 CaseVals.push_back(std::make_pair(LoVal, CS)); 368 } 369 } 370 371 // Sort all the scalar case values so we can easily detect duplicates. 372 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); 373 374 if (!CaseVals.empty()) { 375 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { 376 if (CaseVals[i].first == CaseVals[i+1].first) { 377 // If we have a duplicate, report it. 378 Diag(CaseVals[i+1].second->getLHS()->getLocStart(), 379 diag::err_duplicate_case, CaseVals[i].first.toString(10)); 380 Diag(CaseVals[i].second->getLHS()->getLocStart(), 381 diag::err_duplicate_case_prev); 382 // FIXME: We really want to remove the bogus case stmt from the substmt, 383 // but we have no way to do this right now. 384 CaseListIsErroneous = true; 385 } 386 } 387 } 388 389 // Detect duplicate case ranges, which usually don't exist at all in the first 390 // place. 391 if (!CaseRanges.empty()) { 392 // Sort all the case ranges by their low value so we can easily detect 393 // overlaps between ranges. 394 std::stable_sort(CaseRanges.begin(), CaseRanges.end()); 395 396 // Scan the ranges, computing the high values and removing empty ranges. 397 std::vector<llvm::APSInt> HiVals; 398 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { 399 CaseStmt *CR = CaseRanges[i].second; 400 Expr *Hi = CR->getRHS(); 401 llvm::APSInt HiVal = Hi->getIntegerConstantExprValue(Context); 402 403 // Convert the value to the same width/sign as the condition. 404 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, 405 CR->getRHS()->getLocStart(), 406 diag::warn_case_value_overflow); 407 408 // If the LHS is not the same type as the condition, insert an implicit 409 // cast. 410 ImpCastExprToType(Hi, CondType); 411 CR->setRHS(Hi); 412 413 // If the low value is bigger than the high value, the case is empty. 414 if (CaseRanges[i].first > HiVal) { 415 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range, 416 SourceRange(CR->getLHS()->getLocStart(), 417 CR->getRHS()->getLocEnd())); 418 CaseRanges.erase(CaseRanges.begin()+i); 419 --i, --e; 420 continue; 421 } 422 HiVals.push_back(HiVal); 423 } 424 425 // Rescan the ranges, looking for overlap with singleton values and other 426 // ranges. Since the range list is sorted, we only need to compare case 427 // ranges with their neighbors. 428 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { 429 llvm::APSInt &CRLo = CaseRanges[i].first; 430 llvm::APSInt &CRHi = HiVals[i]; 431 CaseStmt *CR = CaseRanges[i].second; 432 433 // Check to see whether the case range overlaps with any singleton cases. 434 CaseStmt *OverlapStmt = 0; 435 llvm::APSInt OverlapVal(32); 436 437 // Find the smallest value >= the lower bound. If I is in the case range, 438 // then we have overlap. 439 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), 440 CaseVals.end(), CRLo, 441 CaseCompareFunctor()); 442 if (I != CaseVals.end() && I->first < CRHi) { 443 OverlapVal = I->first; // Found overlap with scalar. 444 OverlapStmt = I->second; 445 } 446 447 // Find the smallest value bigger than the upper bound. 448 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); 449 if (I != CaseVals.begin() && (I-1)->first >= CRLo) { 450 OverlapVal = (I-1)->first; // Found overlap with scalar. 451 OverlapStmt = (I-1)->second; 452 } 453 454 // Check to see if this case stmt overlaps with the subsequent case range. 455 if (i && CRLo <= HiVals[i-1]) { 456 OverlapVal = HiVals[i-1]; // Found overlap with range. 457 OverlapStmt = CaseRanges[i-1].second; 458 } 459 460 if (OverlapStmt) { 461 // If we have a duplicate, report it. 462 Diag(CR->getLHS()->getLocStart(), 463 diag::err_duplicate_case, OverlapVal.toString(10)); 464 Diag(OverlapStmt->getLHS()->getLocStart(), 465 diag::err_duplicate_case_prev); 466 // FIXME: We really want to remove the bogus case stmt from the substmt, 467 // but we have no way to do this right now. 468 CaseListIsErroneous = true; 469 } 470 } 471 } 472 473 // FIXME: If the case list was broken is some way, we don't have a good system 474 // to patch it up. Instead, just return the whole substmt as broken. 475 if (CaseListIsErroneous) 476 return true; 477 478 return SS; 479} 480 481Action::StmtResult 482Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) { 483 Expr *condExpr = (Expr *)Cond; 484 assert(condExpr && "ActOnWhileStmt(): missing expression"); 485 486 DefaultFunctionArrayConversion(condExpr); 487 QualType condType = condExpr->getType(); 488 489 if (!condType->isScalarType()) // C99 6.8.5p2 490 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar, 491 condType.getAsString(), condExpr->getSourceRange()); 492 493 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc); 494} 495 496Action::StmtResult 497Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body, 498 SourceLocation WhileLoc, ExprTy *Cond) { 499 Expr *condExpr = (Expr *)Cond; 500 assert(condExpr && "ActOnDoStmt(): missing expression"); 501 502 DefaultFunctionArrayConversion(condExpr); 503 QualType condType = condExpr->getType(); 504 505 if (!condType->isScalarType()) // C99 6.8.5p2 506 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar, 507 condType.getAsString(), condExpr->getSourceRange()); 508 509 return new DoStmt((Stmt*)Body, condExpr, DoLoc); 510} 511 512Action::StmtResult 513Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, 514 StmtTy *first, ExprTy *second, ExprTy *third, 515 SourceLocation RParenLoc, StmtTy *body) { 516 Stmt *First = static_cast<Stmt*>(first); 517 Expr *Second = static_cast<Expr*>(second); 518 Expr *Third = static_cast<Expr*>(third); 519 Stmt *Body = static_cast<Stmt*>(body); 520 521 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { 522 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare 523 // identifiers for objects having storage class 'auto' or 'register'. 524 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); 525 DI!=DE; ++DI) { 526 VarDecl *VD = dyn_cast<VarDecl>(*DI); 527 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage()) 528 VD = 0; 529 if (VD == 0) 530 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for); 531 // FIXME: mark decl erroneous! 532 } 533 } 534 if (Second) { 535 DefaultFunctionArrayConversion(Second); 536 QualType SecondType = Second->getType(); 537 538 if (!SecondType->isScalarType()) // C99 6.8.5p2 539 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar, 540 SecondType.getAsString(), Second->getSourceRange()); 541 } 542 return new ForStmt(First, Second, Third, Body, ForLoc); 543} 544 545Action::StmtResult 546Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, 547 SourceLocation LParenLoc, 548 StmtTy *first, ExprTy *second, 549 SourceLocation RParenLoc, StmtTy *body) { 550 Stmt *First = static_cast<Stmt*>(first); 551 Expr *Second = static_cast<Expr*>(second); 552 Stmt *Body = static_cast<Stmt*>(body); 553 if (First) { 554 QualType FirstType; 555 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) { 556 FirstType = cast<ValueDecl>(DS->getDecl())->getType(); 557 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare 558 // identifiers for objects having storage class 'auto' or 'register'. 559 ScopedDecl *D = DS->getDecl(); 560 VarDecl *VD = cast<VarDecl>(D); 561 if (VD->isBlockVarDecl() && !VD->hasLocalStorage()) 562 return Diag(VD->getLocation(), diag::err_non_variable_decl_in_for); 563 if (D->getNextDeclarator()) 564 return Diag(D->getLocation(), diag::err_toomany_element_decls); 565 } else { 566 Expr::isLvalueResult lval = cast<Expr>(First)->isLvalue(Context); 567 568 if (lval != Expr::LV_Valid) 569 return Diag(First->getLocStart(), diag::err_selector_element_not_lvalue, 570 First->getSourceRange()); 571 572 FirstType = static_cast<Expr*>(first)->getType(); 573 } 574 if (!Context.isObjCObjectPointerType(FirstType)) 575 Diag(ForLoc, diag::err_selector_element_type, 576 FirstType.getAsString(), First->getSourceRange()); 577 } 578 if (Second) { 579 DefaultFunctionArrayConversion(Second); 580 QualType SecondType = Second->getType(); 581 if (!Context.isObjCObjectPointerType(SecondType)) 582 Diag(ForLoc, diag::err_collection_expr_type, 583 SecondType.getAsString(), Second->getSourceRange()); 584 } 585 return new ObjCForCollectionStmt(First, Second, Body, ForLoc, RParenLoc); 586} 587 588Action::StmtResult 589Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, 590 IdentifierInfo *LabelII) { 591 // Look up the record for this label identifier. 592 LabelStmt *&LabelDecl = LabelMap[LabelII]; 593 594 // If we haven't seen this label yet, create a forward reference. 595 if (LabelDecl == 0) 596 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0); 597 598 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc); 599} 600 601Action::StmtResult 602Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, 603 ExprTy *DestExp) { 604 // FIXME: Verify that the operand is convertible to void*. 605 606 return new IndirectGotoStmt((Expr*)DestExp); 607} 608 609Action::StmtResult 610Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { 611 Scope *S = CurScope->getContinueParent(); 612 if (!S) { 613 // C99 6.8.6.2p1: A break shall appear only in or as a loop body. 614 Diag(ContinueLoc, diag::err_continue_not_in_loop); 615 return true; 616 } 617 618 return new ContinueStmt(ContinueLoc); 619} 620 621Action::StmtResult 622Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { 623 Scope *S = CurScope->getBreakParent(); 624 if (!S) { 625 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. 626 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch); 627 return true; 628 } 629 630 return new BreakStmt(BreakLoc); 631} 632 633 634Action::StmtResult 635Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) { 636 Expr *RetValExp = static_cast<Expr *>(rex); 637 QualType FnRetType = 638 getCurFunctionDecl() ? getCurFunctionDecl()->getResultType() : 639 getCurMethodDecl()->getResultType(); 640 641 if (FnRetType->isVoidType()) { 642 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns) 643 Diag(ReturnLoc, diag::ext_return_has_expr, 644 ( getCurFunctionDecl() ? 645 getCurFunctionDecl()->getIdentifier()->getName() : 646 getCurMethodDecl()->getSelector().getName() ), 647 RetValExp->getSourceRange()); 648 return new ReturnStmt(ReturnLoc, RetValExp); 649 } else { 650 if (!RetValExp) { 651 const char *funcName = 652 getCurFunctionDecl() ? 653 getCurFunctionDecl()->getIdentifier()->getName() : 654 getCurMethodDecl()->getSelector().getName().c_str(); 655 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns) 656 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName); 657 else // C90 6.6.6.4p4 658 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName); 659 return new ReturnStmt(ReturnLoc, (Expr*)0); 660 } 661 } 662 // we have a non-void function with an expression, continue checking 663 QualType RetValType = RetValExp->getType(); 664 665 // C99 6.8.6.4p3(136): The return statement is not an assignment. The 666 // overlap restriction of subclause 6.5.16.1 does not apply to the case of 667 // function return. 668 AssignConvertType ConvTy = CheckSingleAssignmentConstraints(FnRetType, 669 RetValExp); 670 if (DiagnoseAssignmentResult(ConvTy, ReturnLoc, FnRetType, 671 RetValType, RetValExp, "returning")) 672 return true; 673 674 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); 675 676 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp); 677} 678 679Sema::StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, 680 bool IsSimple, 681 bool IsVolatile, 682 unsigned NumOutputs, 683 unsigned NumInputs, 684 std::string *Names, 685 ExprTy **constraints, 686 ExprTy **exprs, 687 ExprTy *asmString, 688 unsigned NumClobbers, 689 ExprTy **clobbers, 690 SourceLocation RParenLoc) { 691 StringLiteral **Constraints = reinterpret_cast<StringLiteral**>(constraints); 692 Expr **Exprs = reinterpret_cast<Expr **>(exprs); 693 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString); 694 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers); 695 696 // The parser verifies that there is a string literal here. 697 if (AsmString->isWide()) 698 // FIXME: We currently leak memory here. 699 return Diag(AsmString->getLocStart(), diag::err_asm_wide_character, 700 AsmString->getSourceRange()); 701 702 703 for (unsigned i = 0; i != NumOutputs; i++) { 704 StringLiteral *Literal = Constraints[i]; 705 if (Literal->isWide()) 706 // FIXME: We currently leak memory here. 707 return Diag(Literal->getLocStart(), diag::err_asm_wide_character, 708 Literal->getSourceRange()); 709 710 std::string OutputConstraint(Literal->getStrData(), 711 Literal->getByteLength()); 712 713 TargetInfo::ConstraintInfo info; 714 if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info)) 715 // FIXME: We currently leak memory here. 716 return Diag(Literal->getLocStart(), 717 diag::err_asm_invalid_output_constraint, OutputConstraint); 718 719 // Check that the output exprs are valid lvalues. 720 ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]); 721 Expr::isLvalueResult Result = OutputExpr->isLvalue(Context); 722 if (Result != Expr::LV_Valid) { 723 // FIXME: We currently leak memory here. 724 return Diag(OutputExpr->getSubExpr()->getLocStart(), 725 diag::err_asm_invalid_lvalue_in_output, 726 OutputExpr->getSubExpr()->getSourceRange()); 727 } 728 } 729 730 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { 731 StringLiteral *Literal = Constraints[i]; 732 if (Literal->isWide()) 733 // FIXME: We currently leak memory here. 734 return Diag(Literal->getLocStart(), diag::err_asm_wide_character, 735 Literal->getSourceRange()); 736 737 std::string InputConstraint(Literal->getStrData(), 738 Literal->getByteLength()); 739 740 TargetInfo::ConstraintInfo info; 741 if (!Context.Target.validateInputConstraint(InputConstraint.c_str(), 742 NumOutputs, info)) { 743 // FIXME: We currently leak memory here. 744 return Diag(Literal->getLocStart(), 745 diag::err_asm_invalid_input_constraint, InputConstraint); 746 } 747 748 // Check that the input exprs aren't of type void. 749 ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]); 750 if (InputExpr->getType()->isVoidType()) { 751 752 // FIXME: We currently leak memory here. 753 return Diag(InputExpr->getSubExpr()->getLocStart(), 754 diag::err_asm_invalid_type_in_input, 755 InputExpr->getType().getAsString(), InputConstraint, 756 InputExpr->getSubExpr()->getSourceRange()); 757 } 758 } 759 760 // Check that the clobbers are valid. 761 for (unsigned i = 0; i != NumClobbers; i++) { 762 StringLiteral *Literal = Clobbers[i]; 763 if (Literal->isWide()) 764 // FIXME: We currently leak memory here. 765 return Diag(Literal->getLocStart(), diag::err_asm_wide_character, 766 Literal->getSourceRange()); 767 768 llvm::SmallString<16> Clobber(Literal->getStrData(), 769 Literal->getStrData() + 770 Literal->getByteLength()); 771 772 if (!Context.Target.isValidGCCRegisterName(Clobber.c_str())) 773 // FIXME: We currently leak memory here. 774 return Diag(Literal->getLocStart(), 775 diag::err_asm_unknown_register_name, Clobber.c_str()); 776 } 777 778 return new AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs, 779 Names, Constraints, Exprs, AsmString, NumClobbers, 780 Clobbers, RParenLoc); 781} 782 783Action::StmtResult 784Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, 785 SourceLocation RParen, StmtTy *Parm, 786 StmtTy *Body, StmtTy *CatchList) { 787 ObjCAtCatchStmt *CS = new ObjCAtCatchStmt(AtLoc, RParen, 788 static_cast<Stmt*>(Parm), static_cast<Stmt*>(Body), 789 static_cast<Stmt*>(CatchList)); 790 return CatchList ? CatchList : CS; 791} 792 793Action::StmtResult 794Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtTy *Body) { 795 ObjCAtFinallyStmt *FS = new ObjCAtFinallyStmt(AtLoc, 796 static_cast<Stmt*>(Body)); 797 return FS; 798} 799 800Action::StmtResult 801Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc, 802 StmtTy *Try, StmtTy *Catch, StmtTy *Finally) { 803 ObjCAtTryStmt *TS = new ObjCAtTryStmt(AtLoc, static_cast<Stmt*>(Try), 804 static_cast<Stmt*>(Catch), 805 static_cast<Stmt*>(Finally)); 806 return TS; 807} 808 809Action::StmtResult 810Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, StmtTy *Throw) { 811 ObjCAtThrowStmt *TS = new ObjCAtThrowStmt(AtLoc, static_cast<Stmt*>(Throw)); 812 return TS; 813} 814 815Action::StmtResult 816Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprTy *SynchExpr, 817 StmtTy *SynchBody) { 818 ObjCAtSynchronizedStmt *SS = new ObjCAtSynchronizedStmt(AtLoc, 819 static_cast<Stmt*>(SynchExpr), static_cast<Stmt*>(SynchBody)); 820 return SS; 821} 822