SemaStmt.cpp revision 20401698e3bd93a24bb5d9e18e435895cefe5fd1
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file implements semantic analysis for statements. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "Sema.h" 1551fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson#include "clang/AST/APValue.h" 16f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner#include "clang/AST/ASTContext.h" 17c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/Expr.h" 198189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek#include "clang/AST/ASTContext.h" 206fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson#include "clang/Basic/TargetInfo.h" 215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian RedlSema::OwningStmtResult Sema::ActOnExprStmt(ExprArg expr) { 24a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl Expr *E = static_cast<Expr*>(expr.release()); 251b273c403734d343d720acb28f04011807c8aa56Steve Naroff assert(E && "ActOnExprStmt(): missing expression"); 26a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl 27834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // C99 6.8.3p2: The expression in an expression statement is evaluated as a 28834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // void expression for its side effects. Conversion to void allows any 29834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // operand, even incomplete types. 30a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl 31834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // Same thing in for stmt first clause (when expr) and third clause. 32a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl return Owned(static_cast<Stmt*>(E)); 335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 36a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian RedlSema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) { 378189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) NullStmt(SemiLoc)); 385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 40682bf92db408a6cbc3d37b5496a99b6ef85041ecChris LattnerSema::OwningStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg, 41a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl SourceLocation StartLoc, 42a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl SourceLocation EndLoc) { 4320401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner DeclGroupRef DG = dg.getAsVal<DeclGroupRef>(); 4420401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner 45682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner // If we have an invalid decl, just return an error. 4620401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner if (DG.isNull()) return StmtError(); 47682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner 4824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc)); 495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 51a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian RedlAction::OwningStmtResult 521b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, 53a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl MultiStmtArg elts, bool isStmtExpr) { 54a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl unsigned NumElts = elts.size(); 55a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl Stmt **Elts = reinterpret_cast<Stmt**>(elts.release()); 56c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // If we're in C89 mode, check that we don't have any decls after stmts. If 57c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // so, emit an extension diagnostic. 58c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) { 59c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // Note that __extension__ can be around a decl. 60c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner unsigned i = 0; 61c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // Skip over all declarations. 62c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) 63c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner /*empty*/; 64c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner 65c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // We found the end of the list or a statement. Scan for another declstmt. 66c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) 67c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner /*empty*/; 68c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner 69c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner if (i != NumElts) { 704afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin(); 71c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner Diag(D->getLocation(), diag::ext_mixed_decls_code); 72c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner } 73c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner } 7498414c1b7d1944a57156d52e29bd41c005de09acChris Lattner // Warn about unused expressions in statements. 7598414c1b7d1944a57156d52e29bd41c005de09acChris Lattner for (unsigned i = 0; i != NumElts; ++i) { 7698414c1b7d1944a57156d52e29bd41c005de09acChris Lattner Expr *E = dyn_cast<Expr>(Elts[i]); 7798414c1b7d1944a57156d52e29bd41c005de09acChris Lattner if (!E) continue; 7898414c1b7d1944a57156d52e29bd41c005de09acChris Lattner 79026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner // Warn about expressions with unused results if they are non-void and if 80026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner // this not the last stmt in a stmt expr. 81026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner if (E->getType()->isVoidType() || (isStmtExpr && i == NumElts-1)) 8298414c1b7d1944a57156d52e29bd41c005de09acChris Lattner continue; 8398414c1b7d1944a57156d52e29bd41c005de09acChris Lattner 84026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner SourceLocation Loc; 85026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner SourceRange R1, R2; 86026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner if (!E->isUnusedResultAWarning(Loc, R1, R2)) 8798414c1b7d1944a57156d52e29bd41c005de09acChris Lattner continue; 88026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner 89026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner Diag(Loc, diag::warn_unused_expr) << R1 << R2; 9098414c1b7d1944a57156d52e29bd41c005de09acChris Lattner } 91a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl 928189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R)); 935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 95117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian RedlAction::OwningStmtResult 96117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian RedlSema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval, 97117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl SourceLocation DotDotDotLoc, ExprArg rhsval, 9824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner SourceLocation ColonLoc) { 99117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl assert((lhsval.get() != 0) && "missing expression in case statement"); 100117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.4.2p3: The expression shall be an integer constant. 10251fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson // However, GCC allows any evaluatable integer expression. 103117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl Expr *LHSVal = static_cast<Expr*>(lhsval.get()); 104d3a61d5ec5357d19b7c0b6b599231e68dc5e237fAnders Carlsson if (VerifyIntegerConstantExpression(LHSVal)) 10524e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner return StmtError(); 1065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1076c36be5b383875b490684bcf439d6d427298c1afChris Lattner // GCC extension: The expression shall be an integer constant. 108117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 109117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl Expr *RHSVal = static_cast<Expr*>(rhsval.get()); 110117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl if (RHSVal && VerifyIntegerConstantExpression(RHSVal)) { 111f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner RHSVal = 0; // Recover by just forgetting about it. 112117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl rhsval = 0; 113117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl } 114117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 1158a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner if (SwitchStack.empty()) { 1168a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner Diag(CaseLoc, diag::err_case_not_in_switch); 11724e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner return StmtError(); 1188a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner } 1195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 120117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl // Only now release the smart pointers. 121117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl lhsval.release(); 122117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl rhsval.release(); 12324e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc); 1248a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner SwitchStack.back()->addSwitchCase(CS); 125117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(CS); 1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner/// ActOnCaseStmtBody - This installs a statement as the body of a case. 12924e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattnervoid Sema::ActOnCaseStmtBody(StmtTy *caseStmt, StmtArg subStmt) { 13024e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner CaseStmt *CS = static_cast<CaseStmt*>(caseStmt); 13124e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner Stmt *SubStmt = static_cast<Stmt*>(subStmt.release()); 13224e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner CS->setSubStmt(SubStmt); 13324e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner} 13424e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner 135117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian RedlAction::OwningStmtResult 1361b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, 137117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl StmtArg subStmt, Scope *CurScope) { 138117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl Stmt *SubStmt = static_cast<Stmt*>(subStmt.release()); 139117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 1408a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner if (SwitchStack.empty()) { 1410fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner Diag(DefaultLoc, diag::err_default_not_in_switch); 142117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(SubStmt); 1430fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner } 144117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 1458189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, SubStmt); 1468a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner SwitchStack.back()->addSwitchCase(DS); 147117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(DS); 1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 150de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult 1511b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, 152de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl SourceLocation ColonLoc, StmtArg subStmt) { 153de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl Stmt *SubStmt = static_cast<Stmt*>(subStmt.release()); 1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Look up the record for this label identifier. 155d8eb456289a81728f9aac77ac0a4db0ea9cc231aSteve Naroff LabelStmt *&LabelDecl = CurBlock ? CurBlock->LabelMap[II] : LabelMap[II]; 156de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If not forward referenced or defined already, just create a new LabelStmt. 158caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff if (LabelDecl == 0) 159caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt)); 160de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(LabelDecl->getID() == II && "Label mismatch!"); 162de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this label was either forward reference or multiply defined. If 1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // multiply defined, reject it now. 1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (LabelDecl->getSubStmt()) { 16608631c5fa053867146b5ee8be658c229f6bf127cChris Lattner Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID(); 1675f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition); 168de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return Owned(SubStmt); 1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 170de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this label was forward declared, and we just found its real 1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // definition. Fill in the forward definition and return it. 1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LabelDecl->setIdentLoc(IdentLoc); 1740fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner LabelDecl->setSubStmt(SubStmt); 175de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return Owned(LabelDecl); 1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 178de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult 179de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlSema::ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal, 180de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl StmtArg ThenVal, SourceLocation ElseLoc, 181de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl StmtArg ElseVal) { 182de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl Expr *condExpr = (Expr *)CondVal.release(); 183de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 1841b273c403734d343d720acb28f04011807c8aa56Steve Naroff assert(condExpr && "ActOnIfStmt(): missing expression"); 185de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 186c80b4eefa221f10871e7fed0ab9dc7d9d3d07dd0Steve Naroff DefaultFunctionArrayConversion(condExpr); 187de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl // Take ownership again until we're past the error checking. 188de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl CondVal = condExpr; 189c80b4eefa221f10871e7fed0ab9dc7d9d3d07dd0Steve Naroff QualType condType = condExpr->getType(); 190de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 1915921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (getLangOptions().CPlusPlus) { 1925921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4 193de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return StmtError(); 1945921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis } else if (!condType->isScalarType()) // C99 6.8.4.1p1 195de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return StmtError(Diag(IfLoc, diag::err_typecheck_statement_requires_scalar) 196de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl << condType << condExpr->getSourceRange()); 197de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 198de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl Stmt *thenStmt = (Stmt *)ThenVal.release(); 1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2002d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson // Warn if the if block has a null body without an else value. 2012d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson // this helps prevent bugs due to typos, such as 2022d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson // if (condition); 2032d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson // do_stuff(); 204de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl if (!ElseVal.get()) { 2052d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt)) 2062d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson Diag(stmt->getSemiLoc(), diag::warn_empty_if_body); 2072d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson } 2082d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson 209de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl CondVal.release(); 2108189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt, 2118189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek (Stmt*)ElseVal.release())); 2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 214de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult 215de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlSema::ActOnStartOfSwitchStmt(ExprArg cond) { 216de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl Expr *Cond = static_cast<Expr*>(cond.release()); 217de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 2185921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (getLangOptions().CPlusPlus) { 2195921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // C++ 6.4.2.p2: 2205921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // The condition shall be of integral type, enumeration type, or of a class 2215921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // type for which a single conversion function to integral or enumeration 2225921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // type exists (12.3). If the condition is of class type, the condition is 2235921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // converted by calling that conversion function, and the result of the 2245921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // conversion is used in place of the original condition for the remainder 2255921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // of this section. Integral promotions are performed. 2265921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis 2275921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis QualType Ty = Cond->getType(); 2285921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis 2295921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // FIXME: Handle class types. 2305921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis 2315921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // If the type is wrong a diagnostic will be emitted later at 2325921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // ActOnFinishSwitchStmt. 2335921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (Ty->isIntegralType() || Ty->isEnumeralType()) { 2345921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // Integral promotions are performed. 2355921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // FIXME: Integral promotions for C++ are not complete. 2365921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis UsualUnaryConversions(Cond); 2375921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis } 2385921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis } else { 2395921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. 2405921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis UsualUnaryConversions(Cond); 2415921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis } 242de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 2438189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek SwitchStmt *SS = new (Context) SwitchStmt(Cond); 244c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson SwitchStack.push_back(SS); 245de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return Owned(SS); 246c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson} 2476c36be5b383875b490684bcf439d6d427298c1afChris Lattner 248f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have 249f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified width and sign. If an overflow occurs, detect it and emit 250f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified diagnostic. 251f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattnervoid Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, 252f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner unsigned NewWidth, bool NewSign, 253f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner SourceLocation Loc, 254f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner unsigned DiagID) { 255f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Perform a conversion to the promoted condition type if needed. 256f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (NewWidth > Val.getBitWidth()) { 257f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // If this is an extension, just do it. 258f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner llvm::APSInt OldVal(Val); 259f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Val.extend(NewWidth); 260f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 261f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // If the input was signed and negative and the output is unsigned, 262f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // warn. 263f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (!NewSign && OldVal.isSigned() && OldVal.isNegative()) 264d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10); 265f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 266f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Val.setIsSigned(NewSign); 267f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else if (NewWidth < Val.getBitWidth()) { 268f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // If this is a truncation, check for overflow. 269f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner llvm::APSInt ConvVal(Val); 270f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner ConvVal.trunc(NewWidth); 271b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner ConvVal.setIsSigned(NewSign); 272f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner ConvVal.extend(Val.getBitWidth()); 273b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner ConvVal.setIsSigned(Val.isSigned()); 274f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (ConvVal != Val) 275d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10); 276f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 277f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Regardless of whether a diagnostic was emitted, really do the 278f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // truncation. 279f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Val.trunc(NewWidth); 280b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner Val.setIsSigned(NewSign); 281f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else if (NewSign != Val.isSigned()) { 282f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Convert the sign to match the sign of the condition. This can cause 283f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // overflow as well: unsigned(INTMIN) 284f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner llvm::APSInt OldVal(Val); 285f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Val.setIsSigned(NewSign); 286f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 287f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (Val.isNegative()) // Sign bit changes meaning. 288d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10); 289f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 290f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner} 291f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 2920471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattnernamespace { 2930471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner struct CaseCompareFunctor { 2940471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, 2950471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner const llvm::APSInt &RHS) { 2960471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner return LHS.first < RHS; 2970471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 2980e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, 2990e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner const std::pair<llvm::APSInt, CaseStmt*> &RHS) { 3000e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner return LHS.first < RHS.first; 3010e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner } 3020471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner bool operator()(const llvm::APSInt &LHS, 3030471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner const std::pair<llvm::APSInt, CaseStmt*> &RHS) { 3040471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner return LHS < RHS.first; 3050471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 3060471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner }; 3070471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner} 3080471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner 309764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// CmpCaseVals - Comparison predicate for sorting case values. 310764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// 311764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattnerstatic bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, 312764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner const std::pair<llvm::APSInt, CaseStmt*>& rhs) { 313764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner if (lhs.first < rhs.first) 314764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return true; 315764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner 316764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner if (lhs.first == rhs.first && 317764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner lhs.second->getCaseLoc().getRawEncoding() 318764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner < rhs.second->getCaseLoc().getRawEncoding()) 319764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return true; 320764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return false; 321764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner} 322764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner 323de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult 324de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlSema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch, 325de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl StmtArg Body) { 326de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl Stmt *BodyStmt = (Stmt*)Body.release(); 327de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 328c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson SwitchStmt *SS = SwitchStack.back(); 329de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl assert(SS == (SwitchStmt*)Switch.get() && "switch stack missing push/pop!"); 330de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 3319dcbfa450d751bd68fc4af8b75da381d4f6984b9Steve Naroff SS->setBody(BodyStmt, SwitchLoc); 332c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson SwitchStack.pop_back(); 333c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson 334f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Expr *CondExpr = SS->getCond(); 335f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner QualType CondType = CondExpr->getType(); 336de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 337f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (!CondType->isIntegerType()) { // C99 6.8.4.2p1 338d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer) 339d162584991885ab004a02573a73ce06422b921fcChris Lattner << CondType << CondExpr->getSourceRange(); 340de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return StmtError(); 341c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson } 342de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 343f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Get the bitwidth of the switched-on value before promotions. We must 344f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // convert the integer case values to this width before comparison. 34598be4943e8dc4f3905629a7102668960873cf863Chris Lattner unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType)); 346f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner bool CondIsSigned = CondType->isSignedIntegerType(); 347f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 348f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Accumulate all of the case values in a vector so that we can sort them 349f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // and detect duplicates. This vector contains the APInt for the case after 350f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // it has been converted to the condition type. 3510471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; 3520471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner CaseValsTy CaseVals; 353f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 354f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Keep track of any GNU case ranges we see. The APSInt is the low value. 355f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges; 356f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 357f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner DefaultStmt *TheDefaultStmt = 0; 358c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson 359b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner bool CaseListIsErroneous = false; 360b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner 361b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner for (SwitchCase *SC = SS->getSwitchCaseList(); SC; 362c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson SC = SC->getNextSwitchCase()) { 363b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner 364c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { 365f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (TheDefaultStmt) { 366f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); 3675f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev); 368de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 369f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // FIXME: Remove the default statement from the switch block so that 370f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // we'll return a valid AST. This requires recursing down the 371f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // AST and finding it, not something we are set up to do right now. For 372f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // now, just lop the entire switch stmt out of the AST. 373b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner CaseListIsErroneous = true; 374c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson } 375f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner TheDefaultStmt = DS; 376c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson 377f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else { 378f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner CaseStmt *CS = cast<CaseStmt>(SC); 379f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 380f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // We already verified that the expression has a i-c-e value (C99 381f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // 6.8.4.2p3) - get that value now. 3821e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner Expr *Lo = CS->getLHS(); 38351fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson llvm::APSInt LoVal = Lo->EvaluateAsInt(Context); 384f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 385f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Convert the value to the same width/sign as the condition. 386f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, 387f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner CS->getLHS()->getLocStart(), 388f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner diag::warn_case_value_overflow); 3896c36be5b383875b490684bcf439d6d427298c1afChris Lattner 3901e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner // If the LHS is not the same type as the condition, insert an implicit 3911e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner // cast. 3921e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner ImpCastExprToType(Lo, CondType); 3931e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner CS->setLHS(Lo); 3941e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner 395b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // If this is a case range, remember it in CaseRanges, otherwise CaseVals. 396f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (CS->getRHS()) 397f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner CaseRanges.push_back(std::make_pair(LoVal, CS)); 398b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner else 399b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner CaseVals.push_back(std::make_pair(LoVal, CS)); 400f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 401f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 402f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 403b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // Sort all the scalar case values so we can easily detect duplicates. 404764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); 405b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner 406f334850cd2fc2d4ac9b585f80d97b4f0097a0348Chris Lattner if (!CaseVals.empty()) { 407f334850cd2fc2d4ac9b585f80d97b4f0097a0348Chris Lattner for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { 408f334850cd2fc2d4ac9b585f80d97b4f0097a0348Chris Lattner if (CaseVals[i].first == CaseVals[i+1].first) { 409f334850cd2fc2d4ac9b585f80d97b4f0097a0348Chris Lattner // If we have a duplicate, report it. 410f334850cd2fc2d4ac9b585f80d97b4f0097a0348Chris Lattner Diag(CaseVals[i+1].second->getLHS()->getLocStart(), 411d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner diag::err_duplicate_case) << CaseVals[i].first.toString(10); 412f334850cd2fc2d4ac9b585f80d97b4f0097a0348Chris Lattner Diag(CaseVals[i].second->getLHS()->getLocStart(), 4135f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner diag::note_duplicate_case_prev); 414f334850cd2fc2d4ac9b585f80d97b4f0097a0348Chris Lattner // FIXME: We really want to remove the bogus case stmt from the substmt, 415f334850cd2fc2d4ac9b585f80d97b4f0097a0348Chris Lattner // but we have no way to do this right now. 416f334850cd2fc2d4ac9b585f80d97b4f0097a0348Chris Lattner CaseListIsErroneous = true; 417f334850cd2fc2d4ac9b585f80d97b4f0097a0348Chris Lattner } 418b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 419b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 420f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 421b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // Detect duplicate case ranges, which usually don't exist at all in the first 422b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // place. 423b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner if (!CaseRanges.empty()) { 424b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // Sort all the case ranges by their low value so we can easily detect 425b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // overlaps between ranges. 4260471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner std::stable_sort(CaseRanges.begin(), CaseRanges.end()); 427b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner 428b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // Scan the ranges, computing the high values and removing empty ranges. 429b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner std::vector<llvm::APSInt> HiVals; 4306efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { 431b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner CaseStmt *CR = CaseRanges[i].second; 4321e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner Expr *Hi = CR->getRHS(); 43351fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson llvm::APSInt HiVal = Hi->EvaluateAsInt(Context); 434b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner 435b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // Convert the value to the same width/sign as the condition. 436b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, 437b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner CR->getRHS()->getLocStart(), 438b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner diag::warn_case_value_overflow); 439b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner 4401e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner // If the LHS is not the same type as the condition, insert an implicit 4411e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner // cast. 4421e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner ImpCastExprToType(Hi, CondType); 4431e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner CR->setRHS(Hi); 4441e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner 4456efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner // If the low value is bigger than the high value, the case is empty. 4466efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner if (CaseRanges[i].first > HiVal) { 447dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range) 448dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner << SourceRange(CR->getLHS()->getLocStart(), 449dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner CR->getRHS()->getLocEnd()); 4506efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner CaseRanges.erase(CaseRanges.begin()+i); 4516efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner --i, --e; 4526efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner continue; 4536efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner } 454b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner HiVals.push_back(HiVal); 455b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 456b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner 457b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // Rescan the ranges, looking for overlap with singleton values and other 4580471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner // ranges. Since the range list is sorted, we only need to compare case 4590471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner // ranges with their neighbors. 460b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { 4610471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner llvm::APSInt &CRLo = CaseRanges[i].first; 4620471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner llvm::APSInt &CRHi = HiVals[i]; 4630471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner CaseStmt *CR = CaseRanges[i].second; 4640471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner 4650471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner // Check to see whether the case range overlaps with any singleton cases. 4660471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner CaseStmt *OverlapStmt = 0; 4670471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner llvm::APSInt OverlapVal(32); 4680471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner 4690471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner // Find the smallest value >= the lower bound. If I is in the case range, 4700471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner // then we have overlap. 4710471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), 4720471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner CaseVals.end(), CRLo, 4730471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner CaseCompareFunctor()); 4740471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner if (I != CaseVals.end() && I->first < CRHi) { 4750471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner OverlapVal = I->first; // Found overlap with scalar. 4760471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner OverlapStmt = I->second; 4770471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 4780471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner 4790471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner // Find the smallest value bigger than the upper bound. 4800471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); 4810471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner if (I != CaseVals.begin() && (I-1)->first >= CRLo) { 4820471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner OverlapVal = (I-1)->first; // Found overlap with scalar. 4830471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner OverlapStmt = (I-1)->second; 4840471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 4850471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner 4860471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner // Check to see if this case stmt overlaps with the subsequent case range. 4870471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner if (i && CRLo <= HiVals[i-1]) { 4880471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner OverlapVal = HiVals[i-1]; // Found overlap with range. 4890471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner OverlapStmt = CaseRanges[i-1].second; 4900471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 491b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner 4920471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner if (OverlapStmt) { 4930471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner // If we have a duplicate, report it. 494d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case) 495d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner << OverlapVal.toString(10); 4960471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner Diag(OverlapStmt->getLHS()->getLocStart(), 4975f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner diag::note_duplicate_case_prev); 4980471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner // FIXME: We really want to remove the bogus case stmt from the substmt, 4990471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner // but we have no way to do this right now. 5000471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner CaseListIsErroneous = true; 5010471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 502b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 503b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 504f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 505b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // FIXME: If the case list was broken is some way, we don't have a good system 506b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // to patch it up. Instead, just return the whole substmt as broken. 507b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner if (CaseListIsErroneous) 508de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return StmtError(); 509de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 510de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl Switch.release(); 511de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return Owned(SS); 5125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 5135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 514f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult 515f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnWhileStmt(SourceLocation WhileLoc, ExprArg Cond, StmtArg Body) { 516f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl Expr *condExpr = (Expr *)Cond.release(); 5171b273c403734d343d720acb28f04011807c8aa56Steve Naroff assert(condExpr && "ActOnWhileStmt(): missing expression"); 518f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 519c80b4eefa221f10871e7fed0ab9dc7d9d3d07dd0Steve Naroff DefaultFunctionArrayConversion(condExpr); 520f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl Cond = condExpr; 521c80b4eefa221f10871e7fed0ab9dc7d9d3d07dd0Steve Naroff QualType condType = condExpr->getType(); 522f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 5235921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (getLangOptions().CPlusPlus) { 5245921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4 525f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(); 5265921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis } else if (!condType->isScalarType()) // C99 6.8.5p2 527f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag(WhileLoc, 528f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_typecheck_statement_requires_scalar) 529f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl << condType << condExpr->getSourceRange()); 5305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 531f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl Cond.release(); 5328189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) WhileStmt(condExpr, (Stmt*)Body.release(), 5338189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek WhileLoc)); 5345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 5355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 536f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult 537f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body, 538f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl SourceLocation WhileLoc, ExprArg Cond) { 539f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl Expr *condExpr = (Expr *)Cond.release(); 5401b273c403734d343d720acb28f04011807c8aa56Steve Naroff assert(condExpr && "ActOnDoStmt(): missing expression"); 541f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 542c80b4eefa221f10871e7fed0ab9dc7d9d3d07dd0Steve Naroff DefaultFunctionArrayConversion(condExpr); 543f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl Cond = condExpr; 544c80b4eefa221f10871e7fed0ab9dc7d9d3d07dd0Steve Naroff QualType condType = condExpr->getType(); 545f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 5466314ff264c345b5babb9faf50dad0bc68d7bd74eArgyrios Kyrtzidis if (getLangOptions().CPlusPlus) { 5476314ff264c345b5babb9faf50dad0bc68d7bd74eArgyrios Kyrtzidis if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4 548f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(); 5496314ff264c345b5babb9faf50dad0bc68d7bd74eArgyrios Kyrtzidis } else if (!condType->isScalarType()) // C99 6.8.5p2 550f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag(DoLoc, diag::err_typecheck_statement_requires_scalar) 551f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl << condType << condExpr->getSourceRange()); 5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 553f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl Cond.release(); 5548189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) DoStmt((Stmt*)Body.release(), condExpr, DoLoc)); 5555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 5565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 557f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult 558f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, 559f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl StmtArg first, ExprArg second, ExprArg third, 560f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl SourceLocation RParenLoc, StmtArg body) { 561f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl Stmt *First = static_cast<Stmt*>(first.get()); 562f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl Expr *Second = static_cast<Expr*>(second.get()); 563f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl Expr *Third = static_cast<Expr*>(third.get()); 564f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl Stmt *Body = static_cast<Stmt*>(body.get()); 565f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 5665921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (!getLangOptions().CPlusPlus) { 5675921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { 568f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // C99 6.8.5p3: The declaration part of a 'for' statement shall only 569f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // declare identifiers for objects having storage class 'auto' or 570f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // 'register'. 5715921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); 5725921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis DI!=DE; ++DI) { 5735921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis VarDecl *VD = dyn_cast<VarDecl>(*DI); 5745921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage()) 5755921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis VD = 0; 5765921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (VD == 0) 5775921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for); 5785921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // FIXME: mark decl erroneous! 5795921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis } 580ae3b701f59e78e058b83344be17206af3bf5d277Chris Lattner } 5815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 5825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Second) { 58336c4b0ec13453bc003bbac380770b45ae35ef19cChris Lattner DefaultFunctionArrayConversion(Second); 58436c4b0ec13453bc003bbac380770b45ae35ef19cChris Lattner QualType SecondType = Second->getType(); 585f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 5865921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (getLangOptions().CPlusPlus) { 5875921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4 588f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(); 5895921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis } else if (!SecondType->isScalarType()) // C99 6.8.5p2 590f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag(ForLoc, 591f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_typecheck_statement_requires_scalar) 592f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl << SecondType << Second->getSourceRange()); 5935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 594f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl first.release(); 595f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl second.release(); 596f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl third.release(); 597f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl body.release(); 5988189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ForStmt(First, Second, Third, Body, ForLoc)); 5995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 6005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 601f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult 602f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, 603f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl SourceLocation LParenLoc, 604f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl StmtArg first, ExprArg second, 605f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl SourceLocation RParenLoc, StmtArg body) { 606f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl Stmt *First = static_cast<Stmt*>(first.get()); 607f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl Expr *Second = static_cast<Expr*>(second.get()); 608f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl Stmt *Body = static_cast<Stmt*>(body.get()); 60920552d2842245692b649e0d25380670922f954a2Fariborz Jahanian if (First) { 61020552d2842245692b649e0d25380670922f954a2Fariborz Jahanian QualType FirstType; 61120552d2842245692b649e0d25380670922f954a2Fariborz Jahanian if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) { 6127e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner if (!DS->isSingleDecl()) 613f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag((*DS->decl_begin())->getLocation(), 614f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_toomany_element_decls)); 615f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 6167e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner Decl *D = DS->getSingleDecl(); 617f34afeed9a0112bf31fee185b6c80556111d3834Ted Kremenek FirstType = cast<ValueDecl>(D)->getType(); 618f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // C99 6.8.5p3: The declaration part of a 'for' statement shall only 619f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // declare identifiers for objects having storage class 'auto' or 620f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // 'register'. 621248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff VarDecl *VD = cast<VarDecl>(D); 622248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff if (VD->isBlockVarDecl() && !VD->hasLocalStorage()) 623f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag(VD->getLocation(), 624f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_non_variable_decl_in_for)); 6251fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson } else { 626810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (cast<Expr>(First)->isLvalue(Context) != Expr::LV_Valid) 627f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag(First->getLocStart(), 628f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_selector_element_not_lvalue) 629f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl << First->getSourceRange()); 6301fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson 631f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl FirstType = static_cast<Expr*>(First)->getType(); 6321fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson } 633b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek if (!Context.isObjCObjectPointerType(FirstType)) 634dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner Diag(ForLoc, diag::err_selector_element_type) 635d162584991885ab004a02573a73ce06422b921fcChris Lattner << FirstType << First->getSourceRange(); 6363ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian } 6373ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian if (Second) { 6383ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian DefaultFunctionArrayConversion(Second); 6393ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian QualType SecondType = Second->getType(); 640b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek if (!Context.isObjCObjectPointerType(SecondType)) 641dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner Diag(ForLoc, diag::err_collection_expr_type) 642d162584991885ab004a02573a73ce06422b921fcChris Lattner << SecondType << Second->getSourceRange(); 6433ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian } 644f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl first.release(); 645f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl second.release(); 646f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl body.release(); 6478189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body, 6488189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek ForLoc, RParenLoc)); 6493ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian} 6505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6514cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult 6521b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, 6535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IdentifierInfo *LabelII) { 6544eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // If we are in a block, reject all gotos for now. 6554eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff if (CurBlock) 6564cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(Diag(GotoLoc, diag::err_goto_in_block)); 6574eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff 6585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Look up the record for this label identifier. 659d8eb456289a81728f9aac77ac0a4db0ea9cc231aSteve Naroff LabelStmt *&LabelDecl = CurBlock ? CurBlock->LabelMap[LabelII] : 660d8eb456289a81728f9aac77ac0a4db0ea9cc231aSteve Naroff LabelMap[LabelII]; 6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 662caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff // If we haven't seen this label yet, create a forward reference. 663caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff if (LabelDecl == 0) 6648189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0); 6654cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 6668189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc)); 6675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 6685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6694cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult 6701b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, 6714cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl ExprArg DestExp) { 672bbf462314b1dc8e422b7c4dd4cac47e566aedf6dEli Friedman // Convert operand to void* 67333083823342649b1fccec856c1f239c09fc0d7e1Eli Friedman Expr* E = DestExp.takeAs<Expr>(); 67433083823342649b1fccec856c1f239c09fc0d7e1Eli Friedman QualType ETy = E->getType(); 67533083823342649b1fccec856c1f239c09fc0d7e1Eli Friedman AssignConvertType ConvTy = 67633083823342649b1fccec856c1f239c09fc0d7e1Eli Friedman CheckSingleAssignmentConstraints(Context.VoidPtrTy, E); 67733083823342649b1fccec856c1f239c09fc0d7e1Eli Friedman if (DiagnoseAssignmentResult(ConvTy, StarLoc, Context.VoidPtrTy, ETy, 67833083823342649b1fccec856c1f239c09fc0d7e1Eli Friedman E, "passing")) 67933083823342649b1fccec856c1f239c09fc0d7e1Eli Friedman return StmtError(); 680bbf462314b1dc8e422b7c4dd4cac47e566aedf6dEli Friedman return Owned(new (Context) IndirectGotoStmt(E)); 6815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 6825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6834cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult 6841b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { 6855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Scope *S = CurScope->getContinueParent(); 6865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!S) { 6875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.6.2p1: A break shall appear only in or as a loop body. 6884cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop)); 6895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 6904cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 6918189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ContinueStmt(ContinueLoc)); 6925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 6935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6944cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult 6951b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { 6965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Scope *S = CurScope->getBreakParent(); 6975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!S) { 6985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. 6994cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch)); 7005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 7014cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 7028189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) BreakStmt(BreakLoc)); 7035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 7045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 70527c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor/// ActOnBlockReturnStmt - Utility routine to figure out block's return type. 7064eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff/// 7074cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult 7084eb206bebcdab28ababe8df55c6185cec2cdc071Steve NaroffSema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { 7094cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 7104eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // If this is the first return we've seen in the block, infer the type of 7114eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // the block from it. 7124eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff if (CurBlock->ReturnType == 0) { 713c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff if (RetValExp) { 71416564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff // Don't call UsualUnaryConversions(), since we don't want to do 71516564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff // integer promotions here. 71616564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff DefaultFunctionArrayConversion(RetValExp); 7174eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff CurBlock->ReturnType = RetValExp->getType().getTypePtr(); 718c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff } else 7194eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff CurBlock->ReturnType = Context.VoidTy.getTypePtr(); 7204eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff } 72198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump QualType FnRetType = QualType(CurBlock->ReturnType, 0); 7224cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 7234eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // Otherwise, verify that this result type matches the previous one. We are 7244eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // pickier with blocks than for normal functions because we don't have GCC 7254eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // compatibility to worry about here. 7264eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff if (CurBlock->ReturnType->isVoidType()) { 7274eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff if (RetValExp) { 7284eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff Diag(ReturnLoc, diag::err_return_block_has_expr); 7298189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek RetValExp->Destroy(Context); 7304eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff RetValExp = 0; 7314eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff } 7328189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); 7334eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff } 7344cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 7354cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl if (!RetValExp) 7364cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr)); 7374cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 73898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) { 73998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump // we have a non-void block with an expression, continue checking 74098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump QualType RetValType = RetValExp->getType(); 74198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump 74298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump // C99 6.8.6.4p3(136): The return statement is not an assignment. The 74398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump // overlap restriction of subclause 6.5.16.1 does not apply to the case of 74498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump // function return. 7454cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 74698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump // In C++ the return statement is handled via a copy initialization. 74798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump // the C version of which boils down to CheckSingleAssignmentConstraints. 74898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump // FIXME: Leaks RetValExp. 74998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump if (PerformCopyInitialization(RetValExp, FnRetType, "returning")) 75098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump return StmtError(); 75198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump 75298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); 75398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump } 7544cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 7558189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); 7564eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff} 7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 758e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl/// IsReturnCopyElidable - Whether returning @p RetExpr from a function that 759e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl/// returns a @p RetType fulfills the criteria for copy elision (C++0x 12.8p15). 760e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redlstatic bool IsReturnCopyElidable(ASTContext &Ctx, QualType RetType, 761e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl Expr *RetExpr) { 762e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl QualType ExprType = RetExpr->getType(); 763e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // - in a return statement in a function with ... 764e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // ... a class return type ... 765e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl if (!RetType->isRecordType()) 766e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl return false; 767e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // ... the same cv-unqualified type as the function return type ... 768e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl if (Ctx.getCanonicalType(RetType).getUnqualifiedType() != 769e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl Ctx.getCanonicalType(ExprType).getUnqualifiedType()) 770e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl return false; 771e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // ... the expression is the name of a non-volatile automatic object ... 772e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // We ignore parentheses here. 773e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // FIXME: Is this compliant? 774e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetExpr->IgnoreParens()); 775e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl if (!DR) 776e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl return false; 777e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); 778e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl if (!VD) 779e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl return false; 780e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl return VD->hasLocalStorage() && !VD->getType()->isReferenceType() 781e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl && !VD->getType().isVolatileQualified(); 782e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl} 783e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl 7844cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult 7854cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlSema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) { 7864cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl Expr *RetValExp = static_cast<Expr *>(rex.release()); 7874eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff if (CurBlock) 7884eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff return ActOnBlockReturnStmt(ReturnLoc, RetValExp); 7894cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 790371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner QualType FnRetType; 791371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner if (FunctionDecl *FD = getCurFunctionDecl()) 792371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner FnRetType = FD->getResultType(); 793c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff else if (ObjCMethodDecl *MD = getCurMethodDecl()) 794c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff FnRetType = MD->getResultType(); 795c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff else // If we don't have a function/method context, bail. 796c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff return StmtError(); 797c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff 7985cf216b7fa64b933b60743b0b26053e8e7aa87beChris Lattner if (FnRetType->isVoidType()) { 7993c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns) 80065ce04bef06696379682410f399f37b43996d824Chris Lattner unsigned D = diag::ext_return_has_expr; 80165ce04bef06696379682410f399f37b43996d824Chris Lattner if (RetValExp->getType()->isVoidType()) 80265ce04bef06696379682410f399f37b43996d824Chris Lattner D = diag::ext_return_has_void_expr; 8034cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 804e878eb035b343d7d819c092102364ec9849716aeChris Lattner // return (some void expression); is legal in C++. 805e878eb035b343d7d819c092102364ec9849716aeChris Lattner if (D != diag::ext_return_has_void_expr || 806e878eb035b343d7d819c092102364ec9849716aeChris Lattner !getLangOptions().CPlusPlus) { 807e878eb035b343d7d819c092102364ec9849716aeChris Lattner NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); 808e878eb035b343d7d819c092102364ec9849716aeChris Lattner Diag(ReturnLoc, D) 809e878eb035b343d7d819c092102364ec9849716aeChris Lattner << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl) 810e878eb035b343d7d819c092102364ec9849716aeChris Lattner << RetValExp->getSourceRange(); 811e878eb035b343d7d819c092102364ec9849716aeChris Lattner } 8125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8138189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); 8145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8154cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 8163c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner if (!RetValExp) { 8173c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4 8183c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner // C99 6.8.6.4p1 (ext_ since GCC warns) 8193c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr; 8203c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner 8213c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner if (FunctionDecl *FD = getCurFunctionDecl()) 82208631c5fa053867146b5ee8be658c229f6bf127cChris Lattner Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/; 8233c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner else 82408631c5fa053867146b5ee8be658c229f6bf127cChris Lattner Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/; 8258189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ReturnStmt(ReturnLoc, (Expr*)0)); 8263c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner } 8274cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 828898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) { 829898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor // we have a non-void function with an expression, continue checking 8304cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 831898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor // C99 6.8.6.4p3(136): The return statement is not an assignment. The 832898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor // overlap restriction of subclause 6.5.16.1 does not apply to the case of 8334cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl // function return. 8344cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 835e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // C++0x 12.8p15: When certain criteria are met, an implementation is 836e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // allowed to omit the copy construction of a class object, [...] 837e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // - in a return statement in a function with a class return type, when 838e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // the expression is the name of a non-volatile automatic object with 839e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // the same cv-unqualified type as the function return type, the copy 840e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // operation can be omitted [...] 841e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // C++0x 12.8p16: When the criteria for elision of a copy operation are met 842e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // and the object to be copied is designated by an lvalue, overload 843e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // resolution to select the constructor for the copy is first performed 844e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // as if the object were designated by an rvalue. 845e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // Note that we only compute Elidable if we're in C++0x, since we don't 846e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // care otherwise. 847e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl bool Elidable = getLangOptions().CPlusPlus0x ? 848e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl IsReturnCopyElidable(Context, FnRetType, RetValExp) : 849e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl false; 850e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl 851898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor // In C++ the return statement is handled via a copy initialization. 8524cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl // the C version of which boils down to CheckSingleAssignmentConstraints. 853e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl // FIXME: Leaks RetValExp on error. 854e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl if (PerformCopyInitialization(RetValExp, FnRetType, "returning", Elidable)) 8554cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(); 8564cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 857898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); 858898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor } 859898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 8608189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); 8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 863810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently 864810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// ignore "noop" casts in places where an lvalue is required by an inline asm. 865810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// We emulate this behavior when -fheinous-gnu-extensions is specified, but 866810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// provide a strong guidance to not use it. 867810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// 868810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// This method checks to see if the argument is an acceptable l-value and 869810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// returns false if it is a case we can handle. 870810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattnerstatic bool CheckAsmLValue(const Expr *E, Sema &S) { 871810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (E->isLvalue(S.Context) == Expr::LV_Valid) 872810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner return false; // Cool, this is an lvalue. 873810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 874810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // Okay, this is not an lvalue, but perhaps it is the result of a cast that we 875810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // are supposed to allow. 876810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner const Expr *E2 = E->IgnoreParenNoopCasts(S.Context); 877810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (E != E2 && E2->isLvalue(S.Context) == Expr::LV_Valid) { 878810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (!S.getLangOptions().HeinousExtensions) 879810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue) 880810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner << E->getSourceRange(); 881810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner else 882810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue) 883810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner << E->getSourceRange(); 884810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // Accept, even if we emitted an error diagnostic. 885810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner return false; 886810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner } 887810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 888810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner // None of the above, just randomly invalid non-lvalue. 889810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner return true; 890810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner} 891810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 892810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner 8933037ed0a27dba62e522304183718efc149e8b6d9Sebastian RedlSema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, 8943037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl bool IsSimple, 8953037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl bool IsVolatile, 8963037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl unsigned NumOutputs, 8973037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl unsigned NumInputs, 8983037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl std::string *Names, 8993037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl MultiExprArg constraints, 9003037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl MultiExprArg exprs, 9013037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl ExprArg asmString, 9023037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl MultiExprArg clobbers, 9033037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl SourceLocation RParenLoc) { 9043037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl unsigned NumClobbers = clobbers.size(); 9053037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl StringLiteral **Constraints = 9063037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl reinterpret_cast<StringLiteral**>(constraints.get()); 9073037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl Expr **Exprs = reinterpret_cast<Expr **>(exprs.get()); 9083037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString.get()); 9093037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get()); 9103037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 91103eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; 91203eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson 9131708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner // The parser verifies that there is a string literal here. 9146bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner if (AsmString->isWide()) 9153037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character) 9163037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << AsmString->getSourceRange()); 9173037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 9183037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 9191708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner for (unsigned i = 0; i != NumOutputs; i++) { 9201708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner StringLiteral *Literal = Constraints[i]; 9216bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner if (Literal->isWide()) 9223037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 9233037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << Literal->getSourceRange()); 9243037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 925d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson std::string OutputConstraint(Literal->getStrData(), 926d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson Literal->getByteLength()); 9273037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 928d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson TargetInfo::ConstraintInfo info; 9296bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info)) 9303037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(), 9313037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl diag::err_asm_invalid_output_constraint) << OutputConstraint); 9323037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 933d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson // Check that the output exprs are valid lvalues. 9341708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]); 935810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (CheckAsmLValue(OutputExpr, *this)) { 9363037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(OutputExpr->getSubExpr()->getLocStart(), 937dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner diag::err_asm_invalid_lvalue_in_output) 9383037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << OutputExpr->getSubExpr()->getSourceRange()); 93904728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 94003eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson 94103eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson OutputConstraintInfos.push_back(info); 94204728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 9433037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 94404728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { 9451708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner StringLiteral *Literal = Constraints[i]; 9466bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner if (Literal->isWide()) 9473037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 9483037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << Literal->getSourceRange()); 9493037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 9503037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl std::string InputConstraint(Literal->getStrData(), 951d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson Literal->getByteLength()); 9523037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 953d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson TargetInfo::ConstraintInfo info; 954d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson if (!Context.Target.validateInputConstraint(InputConstraint.c_str(), 95545b050e72d058131e6f169fe54888bb91a003fb5Anders Carlsson &Names[0], 95603eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson &Names[0] + NumOutputs, 95703eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson &OutputConstraintInfos[0], 95803eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson info)) { 9593037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(), 9603037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl diag::err_asm_invalid_input_constraint) << InputConstraint); 961d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson } 9623037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 9631708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]); 9643037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 965d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson // Only allow void types for memory constraints. 966e6ea27995fb15add0de47588b4226049fa0753e5Anders Carlsson if ((info & TargetInfo::CI_AllowsMemory) 967e6ea27995fb15add0de47588b4226049fa0753e5Anders Carlsson && !(info & TargetInfo::CI_AllowsRegister)) { 968810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner if (CheckAsmLValue(InputExpr, *this)) 969d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(), 970d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson diag::err_asm_invalid_lvalue_in_input) 971d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson << InputConstraint << InputExpr->getSubExpr()->getSourceRange()); 97204728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 9733037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 974d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson if (info & TargetInfo::CI_AllowsRegister) { 975d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson if (InputExpr->getType()->isVoidType()) { 976d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(), 977d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson diag::err_asm_invalid_type_in_input) 978d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson << InputExpr->getType() << InputConstraint 979d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson << InputExpr->getSubExpr()->getSourceRange()); 980d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson } 981d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson } 9826032979a5d091e5607c7c467400449727d9beb5aAnders Carlsson 9836032979a5d091e5607c7c467400449727d9beb5aAnders Carlsson DefaultFunctionArrayConversion(Exprs[i]); 98404728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson } 9853037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 9866fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson // Check that the clobbers are valid. 9871708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner for (unsigned i = 0; i != NumClobbers; i++) { 9881708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner StringLiteral *Literal = Clobbers[i]; 9896bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner if (Literal->isWide()) 9903037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 9913037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl << Literal->getSourceRange()); 9923037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 9933037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl llvm::SmallString<16> Clobber(Literal->getStrData(), 9943037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl Literal->getStrData() + 9956fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson Literal->getByteLength()); 9963037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 9976bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner if (!Context.Target.isValidGCCRegisterName(Clobber.c_str())) 9983037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl return StmtError(Diag(Literal->getLocStart(), 9993037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl diag::err_asm_unknown_register_name) << Clobber.c_str()); 10006fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson } 10013037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl 10023037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl constraints.release(); 10033037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl exprs.release(); 10043037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl asmString.release(); 10053037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl clobbers.release(); 1006fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner AsmStmt *NS = 1007fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner new (Context) AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs, 1008fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner Names, Constraints, Exprs, AsmString, NumClobbers, 1009fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner Clobbers, RParenLoc); 1010fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner // Validate the asm string, ensuring it makes sense given the operands we 1011fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner // have. 1012fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner llvm::SmallVector<AsmStmt::AsmStringPiece, 8> Pieces; 1013fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner unsigned DiagOffs; 1014fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { 10152ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) 10162ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner << AsmString->getSourceRange(); 1017fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner DeleteStmt(NS); 1018fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner return StmtError(); 1019fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner } 1020fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner 1021fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner 1022fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner return Owned(NS); 1023fe795956194141c91ae555985c9b930595bff43fChris Lattner} 10243b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian 1025431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult 1026431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, 1027b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner SourceLocation RParen, DeclPtrTy Parm, 1028431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl StmtArg Body, StmtArg catchList) { 1029431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl Stmt *CatchList = static_cast<Stmt*>(catchList.release()); 1030b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner ParmVarDecl *PVD = cast_or_null<ParmVarDecl>(Parm.getAs<Decl>()); 1031f50cb369273c6bd26c9629df92ee53f1d8af4149Steve Naroff 1032f50cb369273c6bd26c9629df92ee53f1d8af4149Steve Naroff // PVD == 0 implies @catch(...). 10339d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff if (PVD) { 10349d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff if (!Context.isObjCObjectPointerType(PVD->getType())) 10359d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff return StmtError(Diag(PVD->getLocation(), 10369d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff diag::err_catch_param_not_objc_type)); 10379d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff if (PVD->getType()->isObjCQualifiedIdType()) 10389d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff return StmtError(Diag(PVD->getLocation(), 1039d198abae5938262e501a409e833bf01ca8b8253eSteve Naroff diag::err_illegal_qualifiers_on_catch_parm)); 10409d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff } 1041f50cb369273c6bd26c9629df92ee53f1d8af4149Steve Naroff 10428189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek ObjCAtCatchStmt *CS = new (Context) ObjCAtCatchStmt(AtLoc, RParen, 1043f50cb369273c6bd26c9629df92ee53f1d8af4149Steve Naroff PVD, static_cast<Stmt*>(Body.release()), CatchList); 1044431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl return Owned(CatchList ? CatchList : CS); 10453b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian} 10463b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian 1047431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult 1048431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) { 10498189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ObjCAtFinallyStmt(AtLoc, 10508189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek static_cast<Stmt*>(Body.release()))); 1051161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian} 1052bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 1053431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult 1054431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtTryStmt(SourceLocation AtLoc, 1055431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl StmtArg Try, StmtArg Catch, StmtArg Finally) { 10568189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ObjCAtTryStmt(AtLoc, 10578189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek static_cast<Stmt*>(Try.release()), 1058431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl static_cast<Stmt*>(Catch.release()), 1059431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl static_cast<Stmt*>(Finally.release()))); 1060bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian} 1061bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 1062431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult 10633dcfe10a6801eb52f4c20f1242bea0a3a98aa146Steve NaroffSema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg expr,Scope *CurScope) { 10647151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff Expr *ThrowExpr = static_cast<Expr*>(expr.release()); 10657151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff if (!ThrowExpr) { 1066e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff // @throw without an expression designates a rethrow (which much occur 1067e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff // in the context of an @catch clause). 1068e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff Scope *AtCatchParent = CurScope; 1069e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff while (AtCatchParent && !AtCatchParent->isAtCatchScope()) 1070e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff AtCatchParent = AtCatchParent->getParent(); 1071e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff if (!AtCatchParent) 10724ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch)); 10737151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff } else { 10747151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff QualType ThrowType = ThrowExpr->getType(); 10757151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff // Make sure the expression type is an ObjC pointer or "void *". 10767151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff if (!Context.isObjCObjectPointerType(ThrowType)) { 10777151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff const PointerType *PT = ThrowType->getAsPointerType(); 10787151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff if (!PT || !PT->getPointeeType()->isVoidType()) 10794ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object) 10804ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff << ThrowExpr->getType() << ThrowExpr->getSourceRange()); 10817151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff } 10827151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff } 10837151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff return Owned(new (Context) ObjCAtThrowStmt(AtLoc, ThrowExpr)); 108439f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian} 1085bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 1086431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult 1087431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr, 1088431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl StmtArg SynchBody) { 10898189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc, 1090431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl static_cast<Stmt*>(SynchExpr.release()), 1091431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl static_cast<Stmt*>(SynchBody.release()))); 1092fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian} 10934b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl 10944b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block 10954b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// and creates a proper catch handler from them. 10964b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian RedlAction::OwningStmtResult 1097b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerSema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclPtrTy ExDecl, 10984b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl StmtArg HandlerBlock) { 10994b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl // There's nothing to test that ActOnExceptionDecl didn't already test. 11008189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) CXXCatchStmt(CatchLoc, 1101b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner cast_or_null<VarDecl>(ExDecl.getAs<Decl>()), 11028189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek static_cast<Stmt*>(HandlerBlock.release()))); 11034b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl} 11048351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 11058351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// ActOnCXXTryBlock - Takes a try compound-statement and a number of 11068351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// handlers and creates a try statement from them. 11078351da06ca3082dfd49dd8e3c1785a986920f57cSebastian RedlAction::OwningStmtResult 11088351da06ca3082dfd49dd8e3c1785a986920f57cSebastian RedlSema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock, 11098351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl MultiStmtArg RawHandlers) { 11108351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl unsigned NumHandlers = RawHandlers.size(); 11118351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl assert(NumHandlers > 0 && 11128351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl "The parser shouldn't call this if there are no handlers."); 11138351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get()); 11148351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 11158351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl for(unsigned i = 0; i < NumHandlers - 1; ++i) { 11168351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]); 11178351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl if (!Handler->getExceptionDecl()) 11188351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl return StmtError(Diag(Handler->getLocStart(), diag::err_early_catch_all)); 11198351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl } 11208351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // FIXME: We should detect handlers for the same type as an earlier one. 11218351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // This one is rather easy. 11228351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // FIXME: We should detect handlers that cannot catch anything because an 11238351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // earlier handler catches a superclass. Need to find a method that is not 11248351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // quadratic for this. 11258351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // Neither of these are explicitly forbidden, but every compiler detects them 11268351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // and warns. 11278351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 11288351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl RawHandlers.release(); 11298189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) CXXTryStmt(TryLoc, 11308189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek static_cast<Stmt*>(TryBlock.release()), 11318189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek Handlers, NumHandlers)); 11328351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl} 1133