Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for statements. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 15 | #include "SemaInit.h" |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 16 | #include "clang/AST/APValue.h" |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 84fb9c0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 419cfb3 | 2009-08-16 16:57:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprObjC.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtObjC.h" |
| 22 | #include "clang/AST/StmtCXX.h" |
John McCall | 209acbd | 2010-04-06 22:24:14 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 84fb9c0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 24 | #include "clang/Lex/Preprocessor.h" |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
| 27 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
Anders Carlsson | 6b1d283 | 2009-05-17 21:11:30 +0000 | [diff] [blame] | 30 | Sema::OwningStmtResult Sema::ActOnExprStmt(FullExprArg expr) { |
| 31 | Expr *E = expr->takeAs<Expr>(); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 32 | assert(E && "ActOnExprStmt(): missing expression"); |
Fariborz Jahanian | 16b1037 | 2009-09-03 00:43:07 +0000 | [diff] [blame] | 33 | if (E->getType()->isObjCInterfaceType()) { |
| 34 | if (LangOpts.ObjCNonFragileABI) |
| 35 | Diag(E->getLocEnd(), diag::err_indirection_requires_nonfragile_object) |
| 36 | << E->getType(); |
| 37 | else |
| 38 | Diag(E->getLocEnd(), diag::err_direct_interface_unsupported) |
| 39 | << E->getType(); |
| 40 | return StmtError(); |
| 41 | } |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 42 | // C99 6.8.3p2: The expression in an expression statement is evaluated as a |
| 43 | // void expression for its side effects. Conversion to void allows any |
| 44 | // operand, even incomplete types. |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 46 | // Same thing in for stmt first clause (when expr) and third clause. |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 47 | return Owned(static_cast<Stmt*>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 51 | Sema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 52 | return Owned(new (Context) NullStmt(SemiLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 55 | Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg, |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 56 | SourceLocation StartLoc, |
| 57 | SourceLocation EndLoc) { |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 58 | DeclGroupRef DG = dg.getAsVal<DeclGroupRef>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 2040169 | 2009-04-12 20:13:14 +0000 | [diff] [blame] | 60 | // If we have an invalid decl, just return an error. |
| 61 | if (DG.isNull()) return StmtError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 63 | return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Fariborz Jahanian | a7cf23a | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 66 | void Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) { |
| 67 | DeclGroupRef DG = dg.getAsVal<DeclGroupRef>(); |
| 68 | |
| 69 | // If we have an invalid decl, just return. |
| 70 | if (DG.isNull() || !DG.isSingleDecl()) return; |
| 71 | // suppress any potential 'unused variable' warning. |
| 72 | DG.getSingleDecl()->setUsed(); |
| 73 | } |
| 74 | |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 75 | void Sema::DiagnoseUnusedExprResult(const Stmt *S) { |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 76 | const Expr *E = dyn_cast_or_null<Expr>(S); |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 77 | if (!E) |
| 78 | return; |
| 79 | |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 80 | SourceLocation Loc; |
| 81 | SourceRange R1, R2; |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 82 | if (!E->isUnusedResultAWarning(Loc, R1, R2, Context)) |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 83 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 419cfb3 | 2009-08-16 16:57:27 +0000 | [diff] [blame] | 85 | // Okay, we have an unused result. Depending on what the base expression is, |
| 86 | // we might want to make a more specific diagnostic. Check for one of these |
| 87 | // cases now. |
| 88 | unsigned DiagID = diag::warn_unused_expr; |
| 89 | E = E->IgnoreParens(); |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 90 | if (isa<ObjCImplicitSetterGetterRefExpr>(E)) |
Chris Lattner | 419cfb3 | 2009-08-16 16:57:27 +0000 | [diff] [blame] | 91 | DiagID = diag::warn_unused_property_expr; |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 92 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 93 | if (const CXXExprWithTemporaries *Temps = dyn_cast<CXXExprWithTemporaries>(E)) |
| 94 | E = Temps->getSubExpr(); |
| 95 | if (const CXXZeroInitValueExpr *Zero = dyn_cast<CXXZeroInitValueExpr>(E)) { |
| 96 | if (const RecordType *RecordT = Zero->getType()->getAs<RecordType>()) |
| 97 | if (CXXRecordDecl *RecordD = dyn_cast<CXXRecordDecl>(RecordT->getDecl())) |
| 98 | if (!RecordD->hasTrivialDestructor()) |
| 99 | return; |
| 100 | } |
| 101 | |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 102 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { |
John McCall | 0faede6 | 2010-03-12 07:11:26 +0000 | [diff] [blame] | 103 | if (E->getType()->isVoidType()) |
| 104 | return; |
| 105 | |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 106 | // If the callee has attribute pure, const, or warn_unused_result, warn with |
| 107 | // a more specific message to make it clear what is happening. |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 108 | if (const Decl *FD = CE->getCalleeDecl()) { |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 109 | if (FD->getAttr<WarnUnusedResultAttr>()) { |
| 110 | Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result"; |
| 111 | return; |
| 112 | } |
| 113 | if (FD->getAttr<PureAttr>()) { |
| 114 | Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure"; |
| 115 | return; |
| 116 | } |
| 117 | if (FD->getAttr<ConstAttr>()) { |
| 118 | Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const"; |
| 119 | return; |
| 120 | } |
| 121 | } |
| 122 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 123 | else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) { |
| 124 | const ObjCMethodDecl *MD = ME->getMethodDecl(); |
| 125 | if (MD && MD->getAttr<WarnUnusedResultAttr>()) { |
| 126 | Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result"; |
| 127 | return; |
| 128 | } |
| 129 | } |
John McCall | 209acbd | 2010-04-06 22:24:14 +0000 | [diff] [blame] | 130 | |
| 131 | // Diagnose "(void*) blah" as a typo for "(void) blah". |
| 132 | else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) { |
| 133 | TypeSourceInfo *TI = CE->getTypeInfoAsWritten(); |
| 134 | QualType T = TI->getType(); |
| 135 | |
| 136 | // We really do want to use the non-canonical type here. |
| 137 | if (T == Context.VoidPtrTy) { |
| 138 | PointerTypeLoc TL = cast<PointerTypeLoc>(TI->getTypeLoc()); |
| 139 | |
| 140 | Diag(Loc, diag::warn_unused_voidptr) |
| 141 | << FixItHint::CreateRemoval(TL.getStarLoc()); |
| 142 | return; |
| 143 | } |
| 144 | } |
| 145 | |
Chris Lattner | 419cfb3 | 2009-08-16 16:57:27 +0000 | [diff] [blame] | 146 | Diag(Loc, DiagID) << R1 << R2; |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 149 | Action::OwningStmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 150 | Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 151 | MultiStmtArg elts, bool isStmtExpr) { |
| 152 | unsigned NumElts = elts.size(); |
| 153 | Stmt **Elts = reinterpret_cast<Stmt**>(elts.release()); |
Chris Lattner | c30ebfb | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 154 | // If we're in C89 mode, check that we don't have any decls after stmts. If |
| 155 | // so, emit an extension diagnostic. |
| 156 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) { |
| 157 | // Note that __extension__ can be around a decl. |
| 158 | unsigned i = 0; |
| 159 | // Skip over all declarations. |
| 160 | for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) |
| 161 | /*empty*/; |
| 162 | |
| 163 | // We found the end of the list or a statement. Scan for another declstmt. |
| 164 | for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) |
| 165 | /*empty*/; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Chris Lattner | c30ebfb | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 167 | if (i != NumElts) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 168 | Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin(); |
Chris Lattner | c30ebfb | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 169 | Diag(D->getLocation(), diag::ext_mixed_decls_code); |
| 170 | } |
| 171 | } |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 172 | // Warn about unused expressions in statements. |
| 173 | for (unsigned i = 0; i != NumElts; ++i) { |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 174 | // Ignore statements that are last in a statement expression. |
| 175 | if (isStmtExpr && i == NumElts - 1) |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 176 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 178 | DiagnoseUnusedExprResult(Elts[i]); |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 179 | } |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 180 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 181 | return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 184 | Action::OwningStmtResult |
| 185 | Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval, |
| 186 | SourceLocation DotDotDotLoc, ExprArg rhsval, |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 187 | SourceLocation ColonLoc) { |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 188 | assert((lhsval.get() != 0) && "missing expression in case statement"); |
| 189 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | // However, GCC allows any evaluatable integer expression. |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 192 | Expr *LHSVal = static_cast<Expr*>(lhsval.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent() && |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 194 | VerifyIntegerConstantExpression(LHSVal)) |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 195 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 197 | // GCC extension: The expression shall be an integer constant. |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 198 | |
| 199 | Expr *RHSVal = static_cast<Expr*>(rhsval.get()); |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 200 | if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent() && |
| 201 | VerifyIntegerConstantExpression(RHSVal)) { |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 202 | RHSVal = 0; // Recover by just forgetting about it. |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 203 | rhsval = 0; |
| 204 | } |
| 205 | |
Chris Lattner | bcfce66 | 2009-04-18 20:10:59 +0000 | [diff] [blame] | 206 | if (getSwitchStack().empty()) { |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 207 | Diag(CaseLoc, diag::err_case_not_in_switch); |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 208 | return StmtError(); |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 209 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 210 | |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 211 | // Only now release the smart pointers. |
| 212 | lhsval.release(); |
| 213 | rhsval.release(); |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 214 | CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc, |
| 215 | ColonLoc); |
Chris Lattner | bcfce66 | 2009-04-18 20:10:59 +0000 | [diff] [blame] | 216 | getSwitchStack().back()->addSwitchCase(CS); |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 217 | return Owned(CS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 220 | /// ActOnCaseStmtBody - This installs a statement as the body of a case. |
| 221 | void Sema::ActOnCaseStmtBody(StmtTy *caseStmt, StmtArg subStmt) { |
| 222 | CaseStmt *CS = static_cast<CaseStmt*>(caseStmt); |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 223 | Stmt *SubStmt = subStmt.takeAs<Stmt>(); |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 224 | CS->setSubStmt(SubStmt); |
| 225 | } |
| 226 | |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 227 | Action::OwningStmtResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 229 | StmtArg subStmt, Scope *CurScope) { |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 230 | Stmt *SubStmt = subStmt.takeAs<Stmt>(); |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 231 | |
Chris Lattner | bcfce66 | 2009-04-18 20:10:59 +0000 | [diff] [blame] | 232 | if (getSwitchStack().empty()) { |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 233 | Diag(DefaultLoc, diag::err_default_not_in_switch); |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 234 | return Owned(SubStmt); |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 235 | } |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 237 | DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt); |
Chris Lattner | bcfce66 | 2009-04-18 20:10:59 +0000 | [diff] [blame] | 238 | getSwitchStack().back()->addSwitchCase(DS); |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 239 | return Owned(DS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 242 | Action::OwningStmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 243 | Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 244 | SourceLocation ColonLoc, StmtArg subStmt) { |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 245 | Stmt *SubStmt = subStmt.takeAs<Stmt>(); |
Steve Naroff | f3cf897 | 2009-02-28 16:48:43 +0000 | [diff] [blame] | 246 | // Look up the record for this label identifier. |
Chris Lattner | ea29a3a | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 247 | LabelStmt *&LabelDecl = getLabelMap()[II]; |
Steve Naroff | f3cf897 | 2009-02-28 16:48:43 +0000 | [diff] [blame] | 248 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | // If not forward referenced or defined already, just create a new LabelStmt. |
Steve Naroff | caaacec | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 250 | if (LabelDecl == 0) |
| 251 | return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt)); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 252 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | assert(LabelDecl->getID() == II && "Label mismatch!"); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 254 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 255 | // Otherwise, this label was either forward reference or multiply defined. If |
| 256 | // multiply defined, reject it now. |
| 257 | if (LabelDecl->getSubStmt()) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 258 | Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 259 | Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 260 | return Owned(SubStmt); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | } |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 262 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | // Otherwise, this label was forward declared, and we just found its real |
| 264 | // definition. Fill in the forward definition and return it. |
| 265 | LabelDecl->setIdentLoc(IdentLoc); |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 266 | LabelDecl->setSubStmt(SubStmt); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 267 | return Owned(LabelDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 270 | Action::OwningStmtResult |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 271 | Sema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, DeclPtrTy CondVar, |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 272 | StmtArg ThenVal, SourceLocation ElseLoc, |
| 273 | StmtArg ElseVal) { |
Anders Carlsson | a99fad8 | 2009-05-17 18:26:53 +0000 | [diff] [blame] | 274 | OwningExprResult CondResult(CondVal.release()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 276 | VarDecl *ConditionVar = 0; |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 277 | if (CondVar.get()) { |
| 278 | ConditionVar = CondVar.getAs<VarDecl>(); |
| 279 | CondResult = CheckConditionVariable(ConditionVar); |
| 280 | if (CondResult.isInvalid()) |
| 281 | return StmtError(); |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 282 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 283 | Expr *ConditionExpr = CondResult.takeAs<Expr>(); |
| 284 | if (!ConditionExpr) |
| 285 | return StmtError(); |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 286 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 287 | if (CheckBooleanCondition(ConditionExpr, IfLoc)) { |
| 288 | CondResult = ConditionExpr; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 289 | return StmtError(); |
Douglas Gregor | d06f6ca | 2009-05-15 18:53:42 +0000 | [diff] [blame] | 290 | } |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 291 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 292 | Stmt *thenStmt = ThenVal.takeAs<Stmt>(); |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 293 | DiagnoseUnusedExprResult(thenStmt); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | |
Anders Carlsson | 2d85f8b | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 295 | // Warn if the if block has a null body without an else value. |
| 296 | // this helps prevent bugs due to typos, such as |
| 297 | // if (condition); |
| 298 | // do_stuff(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | if (!ElseVal.get()) { |
Anders Carlsson | 2d85f8b | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 300 | if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt)) |
| 301 | Diag(stmt->getSemiLoc(), diag::warn_empty_if_body); |
| 302 | } |
| 303 | |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 304 | Stmt *elseStmt = ElseVal.takeAs<Stmt>(); |
| 305 | DiagnoseUnusedExprResult(elseStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Anders Carlsson | a99fad8 | 2009-05-17 18:26:53 +0000 | [diff] [blame] | 307 | CondResult.release(); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 308 | return Owned(new (Context) IfStmt(IfLoc, ConditionVar, ConditionExpr, |
| 309 | thenStmt, ElseLoc, elseStmt)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 312 | Action::OwningStmtResult |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 313 | Sema::ActOnStartOfSwitchStmt(FullExprArg cond, DeclPtrTy CondVar) { |
| 314 | OwningExprResult CondResult(cond.release()); |
| 315 | |
Douglas Gregor | d3d5301 | 2009-11-24 17:07:59 +0000 | [diff] [blame] | 316 | VarDecl *ConditionVar = 0; |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 317 | if (CondVar.get()) { |
| 318 | ConditionVar = CondVar.getAs<VarDecl>(); |
| 319 | CondResult = CheckConditionVariable(ConditionVar); |
| 320 | if (CondResult.isInvalid()) |
| 321 | return StmtError(); |
Douglas Gregor | d3d5301 | 2009-11-24 17:07:59 +0000 | [diff] [blame] | 322 | } |
Douglas Gregor | be724ba | 2009-11-25 06:20:02 +0000 | [diff] [blame] | 323 | SwitchStmt *SS = new (Context) SwitchStmt(ConditionVar, |
| 324 | CondResult.takeAs<Expr>()); |
Chris Lattner | bcfce66 | 2009-04-18 20:10:59 +0000 | [diff] [blame] | 325 | getSwitchStack().push_back(SS); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 326 | return Owned(SS); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 327 | } |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 328 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 329 | /// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have |
| 330 | /// the specified width and sign. If an overflow occurs, detect it and emit |
| 331 | /// the specified diagnostic. |
| 332 | void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, |
| 333 | unsigned NewWidth, bool NewSign, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | SourceLocation Loc, |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 335 | unsigned DiagID) { |
| 336 | // Perform a conversion to the promoted condition type if needed. |
| 337 | if (NewWidth > Val.getBitWidth()) { |
| 338 | // If this is an extension, just do it. |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 339 | Val.extend(NewWidth); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 340 | Val.setIsSigned(NewSign); |
Douglas Gregor | f9f627d | 2010-03-01 01:04:55 +0000 | [diff] [blame] | 341 | |
| 342 | // If the input was signed and negative and the output is |
| 343 | // unsigned, don't bother to warn: this is implementation-defined |
| 344 | // behavior. |
| 345 | // FIXME: Introduce a second, default-ignored warning for this case? |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 346 | } else if (NewWidth < Val.getBitWidth()) { |
| 347 | // If this is a truncation, check for overflow. |
| 348 | llvm::APSInt ConvVal(Val); |
| 349 | ConvVal.trunc(NewWidth); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 350 | ConvVal.setIsSigned(NewSign); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 351 | ConvVal.extend(Val.getBitWidth()); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 352 | ConvVal.setIsSigned(Val.isSigned()); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 353 | if (ConvVal != Val) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 354 | Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 356 | // Regardless of whether a diagnostic was emitted, really do the |
| 357 | // truncation. |
| 358 | Val.trunc(NewWidth); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 359 | Val.setIsSigned(NewSign); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 360 | } else if (NewSign != Val.isSigned()) { |
| 361 | // Convert the sign to match the sign of the condition. This can cause |
| 362 | // overflow as well: unsigned(INTMIN) |
Douglas Gregor | 2853eac | 2010-02-18 00:56:01 +0000 | [diff] [blame] | 363 | // We don't diagnose this overflow, because it is implementation-defined |
| 364 | // behavior. |
| 365 | // FIXME: Introduce a second, default-ignored warning for this case? |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 366 | llvm::APSInt OldVal(Val); |
| 367 | Val.setIsSigned(NewSign); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 371 | namespace { |
| 372 | struct CaseCompareFunctor { |
| 373 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 374 | const llvm::APSInt &RHS) { |
| 375 | return LHS.first < RHS; |
| 376 | } |
Chris Lattner | 0e85a27 | 2007-09-03 18:31:57 +0000 | [diff] [blame] | 377 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 378 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 379 | return LHS.first < RHS.first; |
| 380 | } |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 381 | bool operator()(const llvm::APSInt &LHS, |
| 382 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 383 | return LHS < RHS.first; |
| 384 | } |
| 385 | }; |
| 386 | } |
| 387 | |
Chris Lattner | 764a7ce | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 388 | /// CmpCaseVals - Comparison predicate for sorting case values. |
| 389 | /// |
| 390 | static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, |
| 391 | const std::pair<llvm::APSInt, CaseStmt*>& rhs) { |
| 392 | if (lhs.first < rhs.first) |
| 393 | return true; |
| 394 | |
| 395 | if (lhs.first == rhs.first && |
| 396 | lhs.second->getCaseLoc().getRawEncoding() |
| 397 | < rhs.second->getCaseLoc().getRawEncoding()) |
| 398 | return true; |
| 399 | return false; |
| 400 | } |
| 401 | |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 402 | /// CmpEnumVals - Comparison predicate for sorting enumeration values. |
| 403 | /// |
| 404 | static bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, |
| 405 | const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) |
| 406 | { |
| 407 | return lhs.first < rhs.first; |
| 408 | } |
| 409 | |
| 410 | /// EqEnumVals - Comparison preficate for uniqing enumeration values. |
| 411 | /// |
| 412 | static bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, |
| 413 | const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) |
| 414 | { |
| 415 | return lhs.first == rhs.first; |
| 416 | } |
| 417 | |
Chris Lattner | 5f04881 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 418 | /// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of |
| 419 | /// potentially integral-promoted expression @p expr. |
| 420 | static QualType GetTypeBeforeIntegralPromotion(const Expr* expr) { |
| 421 | const ImplicitCastExpr *ImplicitCast = |
| 422 | dyn_cast_or_null<ImplicitCastExpr>(expr); |
| 423 | if (ImplicitCast != NULL) { |
| 424 | const Expr *ExprBeforePromotion = ImplicitCast->getSubExpr(); |
| 425 | QualType TypeBeforePromotion = ExprBeforePromotion->getType(); |
| 426 | if (TypeBeforePromotion->isIntegralType()) { |
| 427 | return TypeBeforePromotion; |
| 428 | } |
| 429 | } |
| 430 | return expr->getType(); |
| 431 | } |
| 432 | |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 433 | /// \brief Check (and possibly convert) the condition in a switch |
| 434 | /// statement in C++. |
| 435 | static bool CheckCXXSwitchCondition(Sema &S, SourceLocation SwitchLoc, |
| 436 | Expr *&CondExpr) { |
| 437 | if (CondExpr->isTypeDependent()) |
| 438 | return false; |
| 439 | |
| 440 | QualType CondType = CondExpr->getType(); |
| 441 | |
| 442 | // C++ 6.4.2.p2: |
| 443 | // The condition shall be of integral type, enumeration type, or of a class |
| 444 | // type for which a single conversion function to integral or enumeration |
| 445 | // type exists (12.3). If the condition is of class type, the condition is |
| 446 | // converted by calling that conversion function, and the result of the |
| 447 | // conversion is used in place of the original condition for the remainder |
| 448 | // of this section. Integral promotions are performed. |
| 449 | |
| 450 | // Make sure that the condition expression has a complete type, |
| 451 | // otherwise we'll never find any conversions. |
| 452 | if (S.RequireCompleteType(SwitchLoc, CondType, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 453 | S.PDiag(diag::err_switch_incomplete_class_type) |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 454 | << CondExpr->getSourceRange())) |
| 455 | return true; |
| 456 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 457 | UnresolvedSet<4> ViableConversions; |
| 458 | UnresolvedSet<4> ExplicitConversions; |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 459 | if (const RecordType *RecordTy = CondType->getAs<RecordType>()) { |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 460 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 461 | = cast<CXXRecordDecl>(RecordTy->getDecl()) |
| 462 | ->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 463 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 464 | E = Conversions->end(); I != E; ++I) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 465 | if (CXXConversionDecl *Conversion |
| 466 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 467 | if (Conversion->getConversionType().getNonReferenceType() |
| 468 | ->isIntegralType()) { |
| 469 | if (Conversion->isExplicit()) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 470 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 471 | else |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 472 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | switch (ViableConversions.size()) { |
| 477 | case 0: |
| 478 | if (ExplicitConversions.size() == 1) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 479 | DeclAccessPair Found = ExplicitConversions[0]; |
| 480 | CXXConversionDecl *Conversion = |
| 481 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 482 | // The user probably meant to invoke the given explicit |
| 483 | // conversion; use it. |
| 484 | QualType ConvTy |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 485 | = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 486 | std::string TypeStr; |
| 487 | ConvTy.getAsStringInternal(TypeStr, S.Context.PrintingPolicy); |
| 488 | |
| 489 | S.Diag(SwitchLoc, diag::err_switch_explicit_conversion) |
| 490 | << CondType << ConvTy << CondExpr->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 491 | << FixItHint::CreateInsertion(CondExpr->getLocStart(), |
| 492 | "static_cast<" + TypeStr + ">(") |
| 493 | << FixItHint::CreateInsertion( |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 494 | S.PP.getLocForEndOfToken(CondExpr->getLocEnd()), |
| 495 | ")"); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 496 | S.Diag(Conversion->getLocation(), diag::note_switch_conversion) |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 497 | << ConvTy->isEnumeralType() << ConvTy; |
| 498 | |
| 499 | // If we aren't in a SFINAE context, build a call to the |
| 500 | // explicit conversion function. |
| 501 | if (S.isSFINAEContext()) |
| 502 | return true; |
| 503 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 504 | S.CheckMemberOperatorAccess(CondExpr->getExprLoc(), |
| 505 | CondExpr, 0, Found); |
| 506 | CondExpr = S.BuildCXXMemberCallExpr(CondExpr, Found, Conversion); |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | // We'll complain below about a non-integral condition type. |
| 510 | break; |
| 511 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 512 | case 1: { |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 513 | // Apply this conversion. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 514 | DeclAccessPair Found = ViableConversions[0]; |
| 515 | S.CheckMemberOperatorAccess(CondExpr->getExprLoc(), |
| 516 | CondExpr, 0, Found); |
| 517 | CondExpr = S.BuildCXXMemberCallExpr(CondExpr, Found, |
| 518 | cast<CXXConversionDecl>(Found->getUnderlyingDecl())); |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 519 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 520 | } |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 521 | |
| 522 | default: |
| 523 | S.Diag(SwitchLoc, diag::err_switch_multiple_conversions) |
| 524 | << CondType << CondExpr->getSourceRange(); |
| 525 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 526 | CXXConversionDecl *Conv |
| 527 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 528 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 529 | S.Diag(Conv->getLocation(), diag::note_switch_conversion) |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 530 | << ConvTy->isEnumeralType() << ConvTy; |
| 531 | } |
| 532 | return true; |
| 533 | } |
| 534 | } |
Douglas Gregor | 0be3193 | 2009-11-25 04:55:54 +0000 | [diff] [blame] | 535 | |
| 536 | return false; |
| 537 | } |
| 538 | |
Chris Lattner | 7e52de4 | 2010-01-24 01:50:29 +0000 | [diff] [blame] | 539 | /// ActOnSwitchBodyError - This is called if there is an error parsing the |
| 540 | /// body of the switch stmt instead of ActOnFinishSwitchStmt. |
| 541 | void Sema::ActOnSwitchBodyError(SourceLocation SwitchLoc, StmtArg Switch, |
| 542 | StmtArg Body) { |
| 543 | // Keep the switch stack balanced. |
| 544 | assert(getSwitchStack().back() == (SwitchStmt*)Switch.get() && |
| 545 | "switch stack missing push/pop!"); |
| 546 | getSwitchStack().pop_back(); |
| 547 | } |
| 548 | |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 549 | Action::OwningStmtResult |
| 550 | Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch, |
| 551 | StmtArg Body) { |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 552 | Stmt *BodyStmt = Body.takeAs<Stmt>(); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 553 | |
Chris Lattner | bcfce66 | 2009-04-18 20:10:59 +0000 | [diff] [blame] | 554 | SwitchStmt *SS = getSwitchStack().back(); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 555 | assert(SS == (SwitchStmt*)Switch.get() && "switch stack missing push/pop!"); |
| 556 | |
Steve Naroff | 9dcbfa4 | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 557 | SS->setBody(BodyStmt, SwitchLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | getSwitchStack().pop_back(); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 559 | |
Douglas Gregor | be724ba | 2009-11-25 06:20:02 +0000 | [diff] [blame] | 560 | if (SS->getCond() == 0) { |
| 561 | SS->Destroy(Context); |
| 562 | return StmtError(); |
| 563 | } |
| 564 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 565 | Expr *CondExpr = SS->getCond(); |
Douglas Gregor | 84fb9c0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 566 | QualType CondTypeBeforePromotion = |
| 567 | GetTypeBeforeIntegralPromotion(CondExpr); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 568 | |
Douglas Gregor | 0de55e7 | 2009-11-25 15:17:36 +0000 | [diff] [blame] | 569 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | a0d3ca1 | 2009-11-25 05:02:21 +0000 | [diff] [blame] | 570 | CheckCXXSwitchCondition(*this, SwitchLoc, CondExpr)) |
Douglas Gregor | 0de55e7 | 2009-11-25 15:17:36 +0000 | [diff] [blame] | 571 | return StmtError(); |
Douglas Gregor | a0d3ca1 | 2009-11-25 05:02:21 +0000 | [diff] [blame] | 572 | |
Douglas Gregor | 0de55e7 | 2009-11-25 15:17:36 +0000 | [diff] [blame] | 573 | // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. |
| 574 | UsualUnaryConversions(CondExpr); |
Douglas Gregor | a0d3ca1 | 2009-11-25 05:02:21 +0000 | [diff] [blame] | 575 | QualType CondType = CondExpr->getType(); |
Douglas Gregor | 84fb9c0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 576 | SS->setCond(CondExpr); |
| 577 | |
Chris Lattner | 5f04881 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 578 | // C++ 6.4.2.p2: |
| 579 | // Integral promotions are performed (on the switch condition). |
| 580 | // |
| 581 | // A case value unrepresentable by the original switch condition |
| 582 | // type (before the promotion) doesn't make sense, even when it can |
| 583 | // be represented by the promoted type. Therefore we need to find |
| 584 | // the pre-promotion type of the switch condition. |
Edward O'Callaghan | 12356b1 | 2009-10-17 19:32:54 +0000 | [diff] [blame] | 585 | if (!CondExpr->isTypeDependent()) { |
| 586 | if (!CondType->isIntegerType()) { // C99 6.8.4.2p1 |
| 587 | Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer) |
| 588 | << CondType << CondExpr->getSourceRange(); |
| 589 | return StmtError(); |
| 590 | } |
| 591 | |
| 592 | if (CondTypeBeforePromotion->isBooleanType()) { |
| 593 | // switch(bool_expr) {...} is often a programmer error, e.g. |
| 594 | // switch(n && mask) { ... } // Doh - should be "n & mask". |
| 595 | // One can always use an if statement instead of switch(bool_expr). |
| 596 | Diag(SwitchLoc, diag::warn_bool_switch_condition) |
| 597 | << CondExpr->getSourceRange(); |
| 598 | } |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 599 | } |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 600 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 601 | // Get the bitwidth of the switched-on value before promotions. We must |
| 602 | // convert the integer case values to this width before comparison. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 603 | bool HasDependentValue |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 604 | = CondExpr->isTypeDependent() || CondExpr->isValueDependent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 | unsigned CondWidth |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 606 | = HasDependentValue? 0 |
Chris Lattner | 5f04881 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 607 | : static_cast<unsigned>(Context.getTypeSize(CondTypeBeforePromotion)); |
| 608 | bool CondIsSigned = CondTypeBeforePromotion->isSignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 610 | // Accumulate all of the case values in a vector so that we can sort them |
| 611 | // and detect duplicates. This vector contains the APInt for the case after |
| 612 | // it has been converted to the condition type. |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 613 | typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; |
| 614 | CaseValsTy CaseVals; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 616 | // Keep track of any GNU case ranges we see. The APSInt is the low value. |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 617 | typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy; |
| 618 | CaseRangesTy CaseRanges; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 619 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 620 | DefaultStmt *TheDefaultStmt = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 621 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 622 | bool CaseListIsErroneous = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 624 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 625 | SC = SC->getNextSwitchCase()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 627 | if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 628 | if (TheDefaultStmt) { |
| 629 | Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 630 | Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 631 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 632 | // FIXME: Remove the default statement from the switch block so that |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 633 | // we'll return a valid AST. This requires recursing down the AST and |
| 634 | // finding it, not something we are set up to do right now. For now, |
| 635 | // just lop the entire switch stmt out of the AST. |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 636 | CaseListIsErroneous = true; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 637 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 638 | TheDefaultStmt = DS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 640 | } else { |
| 641 | CaseStmt *CS = cast<CaseStmt>(SC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 643 | // We already verified that the expression has a i-c-e value (C99 |
| 644 | // 6.8.4.2p3) - get that value now. |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 645 | Expr *Lo = CS->getLHS(); |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 646 | |
| 647 | if (Lo->isTypeDependent() || Lo->isValueDependent()) { |
| 648 | HasDependentValue = true; |
| 649 | break; |
| 650 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 651 | |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 652 | llvm::APSInt LoVal = Lo->EvaluateAsInt(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 653 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 654 | // Convert the value to the same width/sign as the condition. |
| 655 | ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, |
| 656 | CS->getLHS()->getLocStart(), |
| 657 | diag::warn_case_value_overflow); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 658 | |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 659 | // If the LHS is not the same type as the condition, insert an implicit |
| 660 | // cast. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 661 | ImpCastExprToType(Lo, CondType, CastExpr::CK_IntegralCast); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 662 | CS->setLHS(Lo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 663 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 664 | // If this is a case range, remember it in CaseRanges, otherwise CaseVals. |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 665 | if (CS->getRHS()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 666 | if (CS->getRHS()->isTypeDependent() || |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 667 | CS->getRHS()->isValueDependent()) { |
| 668 | HasDependentValue = true; |
| 669 | break; |
| 670 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 671 | CaseRanges.push_back(std::make_pair(LoVal, CS)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | } else |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 673 | CaseVals.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 676 | |
| 677 | if (!HasDependentValue) { |
| 678 | // Sort all the scalar case values so we can easily detect duplicates. |
| 679 | std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); |
| 680 | |
| 681 | if (!CaseVals.empty()) { |
| 682 | for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { |
| 683 | if (CaseVals[i].first == CaseVals[i+1].first) { |
| 684 | // If we have a duplicate, report it. |
| 685 | Diag(CaseVals[i+1].second->getLHS()->getLocStart(), |
| 686 | diag::err_duplicate_case) << CaseVals[i].first.toString(10); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 687 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 688 | diag::note_duplicate_case_prev); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 689 | // FIXME: We really want to remove the bogus case stmt from the |
| 690 | // substmt, but we have no way to do this right now. |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 691 | CaseListIsErroneous = true; |
| 692 | } |
| 693 | } |
| 694 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 695 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 696 | // Detect duplicate case ranges, which usually don't exist at all in |
| 697 | // the first place. |
| 698 | if (!CaseRanges.empty()) { |
| 699 | // Sort all the case ranges by their low value so we can easily detect |
| 700 | // overlaps between ranges. |
| 701 | std::stable_sort(CaseRanges.begin(), CaseRanges.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 703 | // Scan the ranges, computing the high values and removing empty ranges. |
| 704 | std::vector<llvm::APSInt> HiVals; |
| 705 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
| 706 | CaseStmt *CR = CaseRanges[i].second; |
| 707 | Expr *Hi = CR->getRHS(); |
| 708 | llvm::APSInt HiVal = Hi->EvaluateAsInt(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 710 | // Convert the value to the same width/sign as the condition. |
| 711 | ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, |
| 712 | CR->getRHS()->getLocStart(), |
| 713 | diag::warn_case_value_overflow); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 715 | // If the LHS is not the same type as the condition, insert an implicit |
| 716 | // cast. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 717 | ImpCastExprToType(Hi, CondType, CastExpr::CK_IntegralCast); |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 718 | CR->setRHS(Hi); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 719 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 720 | // If the low value is bigger than the high value, the case is empty. |
| 721 | if (CaseRanges[i].first > HiVal) { |
| 722 | Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range) |
| 723 | << SourceRange(CR->getLHS()->getLocStart(), |
| 724 | CR->getRHS()->getLocEnd()); |
| 725 | CaseRanges.erase(CaseRanges.begin()+i); |
| 726 | --i, --e; |
| 727 | continue; |
| 728 | } |
| 729 | HiVals.push_back(HiVal); |
| 730 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 731 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 732 | // Rescan the ranges, looking for overlap with singleton values and other |
| 733 | // ranges. Since the range list is sorted, we only need to compare case |
| 734 | // ranges with their neighbors. |
| 735 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
| 736 | llvm::APSInt &CRLo = CaseRanges[i].first; |
| 737 | llvm::APSInt &CRHi = HiVals[i]; |
| 738 | CaseStmt *CR = CaseRanges[i].second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 740 | // Check to see whether the case range overlaps with any |
| 741 | // singleton cases. |
| 742 | CaseStmt *OverlapStmt = 0; |
| 743 | llvm::APSInt OverlapVal(32); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 745 | // Find the smallest value >= the lower bound. If I is in the |
| 746 | // case range, then we have overlap. |
| 747 | CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), |
| 748 | CaseVals.end(), CRLo, |
| 749 | CaseCompareFunctor()); |
| 750 | if (I != CaseVals.end() && I->first < CRHi) { |
| 751 | OverlapVal = I->first; // Found overlap with scalar. |
| 752 | OverlapStmt = I->second; |
| 753 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 755 | // Find the smallest value bigger than the upper bound. |
| 756 | I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); |
| 757 | if (I != CaseVals.begin() && (I-1)->first >= CRLo) { |
| 758 | OverlapVal = (I-1)->first; // Found overlap with scalar. |
| 759 | OverlapStmt = (I-1)->second; |
| 760 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 762 | // Check to see if this case stmt overlaps with the subsequent |
| 763 | // case range. |
| 764 | if (i && CRLo <= HiVals[i-1]) { |
| 765 | OverlapVal = HiVals[i-1]; // Found overlap with range. |
| 766 | OverlapStmt = CaseRanges[i-1].second; |
| 767 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 769 | if (OverlapStmt) { |
| 770 | // If we have a duplicate, report it. |
| 771 | Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case) |
| 772 | << OverlapVal.toString(10); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | Diag(OverlapStmt->getLHS()->getLocStart(), |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 774 | diag::note_duplicate_case_prev); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 775 | // FIXME: We really want to remove the bogus case stmt from the |
| 776 | // substmt, but we have no way to do this right now. |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 777 | CaseListIsErroneous = true; |
| 778 | } |
Chris Lattner | f334850 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 779 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 780 | } |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 781 | |
| 782 | // Check to see if switch is over an Enum and handles all of its |
| 783 | // values |
Douglas Gregor | 30ab371 | 2010-02-17 23:29:11 +0000 | [diff] [blame] | 784 | const EnumType* ET = CondTypeBeforePromotion->getAs<EnumType>(); |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 785 | // If switch has default case, then ignore it. |
| 786 | if (!CaseListIsErroneous && !TheDefaultStmt && ET) { |
| 787 | const EnumDecl *ED = ET->getDecl(); |
| 788 | typedef llvm::SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> EnumValsTy; |
| 789 | EnumValsTy EnumVals; |
| 790 | |
| 791 | // Gather all enum values, set their type and sort them, allowing easier comparison |
| 792 | // with CaseVals. |
| 793 | for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin(); EDI != ED->enumerator_end(); EDI++) { |
| 794 | llvm::APSInt Val = (*EDI)->getInitVal(); |
| 795 | if(Val.getBitWidth() < CondWidth) |
| 796 | Val.extend(CondWidth); |
| 797 | Val.setIsSigned(CondIsSigned); |
| 798 | EnumVals.push_back(std::make_pair(Val, (*EDI))); |
| 799 | } |
| 800 | std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals); |
| 801 | EnumValsTy::iterator EIend = std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals); |
| 802 | // See which case values aren't in enum |
| 803 | EnumValsTy::const_iterator EI = EnumVals.begin(); |
| 804 | for (CaseValsTy::const_iterator CI = CaseVals.begin(); CI != CaseVals.end(); CI++) { |
| 805 | while (EI != EIend && EI->first < CI->first) |
| 806 | EI++; |
| 807 | if (EI == EIend || EI->first > CI->first) |
| 808 | Diag(CI->second->getLHS()->getExprLoc(), diag::not_in_enum) << ED->getDeclName(); |
| 809 | } |
| 810 | // See which of case ranges aren't in enum |
| 811 | EI = EnumVals.begin(); |
| 812 | for (CaseRangesTy::const_iterator RI = CaseRanges.begin(); RI != CaseRanges.end() && EI != EIend; RI++) { |
| 813 | while (EI != EIend && EI->first < RI->first) |
| 814 | EI++; |
| 815 | |
| 816 | if (EI == EIend || EI->first != RI->first) { |
| 817 | Diag(RI->second->getLHS()->getExprLoc(), diag::not_in_enum) << ED->getDeclName(); |
| 818 | } |
| 819 | |
| 820 | llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context); |
| 821 | while (EI != EIend && EI->first < Hi) |
| 822 | EI++; |
| 823 | if (EI == EIend || EI->first != Hi) |
| 824 | Diag(RI->second->getRHS()->getExprLoc(), diag::not_in_enum) << ED->getDeclName(); |
| 825 | } |
| 826 | //Check which enum vals aren't in switch |
| 827 | CaseValsTy::const_iterator CI = CaseVals.begin(); |
| 828 | CaseRangesTy::const_iterator RI = CaseRanges.begin(); |
| 829 | EI = EnumVals.begin(); |
| 830 | for (; EI != EIend; EI++) { |
| 831 | //Drop unneeded case values |
| 832 | llvm::APSInt CIVal; |
| 833 | while (CI != CaseVals.end() && CI->first < EI->first) |
| 834 | CI++; |
| 835 | |
| 836 | if (CI != CaseVals.end() && CI->first == EI->first) |
| 837 | continue; |
| 838 | |
| 839 | //Drop unneeded case ranges |
| 840 | for (; RI != CaseRanges.end(); RI++) { |
| 841 | llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context); |
| 842 | if (EI->first <= Hi) |
| 843 | break; |
| 844 | } |
| 845 | |
| 846 | if (RI == CaseRanges.end() || EI->first < RI->first) |
| 847 | Diag(CondExpr->getExprLoc(), diag::warn_missing_cases) << EI->second->getDeclName(); |
| 848 | } |
| 849 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 850 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 851 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 852 | // FIXME: If the case list was broken is some way, we don't have a good system |
| 853 | // to patch it up. Instead, just return the whole substmt as broken. |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 854 | if (CaseListIsErroneous) |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 855 | return StmtError(); |
| 856 | |
| 857 | Switch.release(); |
| 858 | return Owned(SS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 861 | Action::OwningStmtResult |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 862 | Sema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond, |
| 863 | DeclPtrTy CondVar, StmtArg Body) { |
| 864 | OwningExprResult CondResult(Cond.release()); |
| 865 | |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 866 | VarDecl *ConditionVar = 0; |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 867 | if (CondVar.get()) { |
| 868 | ConditionVar = CondVar.getAs<VarDecl>(); |
| 869 | CondResult = CheckConditionVariable(ConditionVar); |
| 870 | if (CondResult.isInvalid()) |
| 871 | return StmtError(); |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 872 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 873 | Expr *ConditionExpr = CondResult.takeAs<Expr>(); |
| 874 | if (!ConditionExpr) |
| 875 | return StmtError(); |
| 876 | |
| 877 | if (CheckBooleanCondition(ConditionExpr, WhileLoc)) { |
| 878 | CondResult = ConditionExpr; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 879 | return StmtError(); |
Douglas Gregor | 4a2e204 | 2009-05-15 21:45:53 +0000 | [diff] [blame] | 880 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 881 | |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 882 | Stmt *bodyStmt = Body.takeAs<Stmt>(); |
| 883 | DiagnoseUnusedExprResult(bodyStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 885 | CondResult.release(); |
| 886 | return Owned(new (Context) WhileStmt(ConditionVar, ConditionExpr, bodyStmt, |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 887 | WhileLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 890 | Action::OwningStmtResult |
| 891 | Sema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body, |
Chris Lattner | 9891359 | 2009-06-12 23:04:47 +0000 | [diff] [blame] | 892 | SourceLocation WhileLoc, SourceLocation CondLParen, |
| 893 | ExprArg Cond, SourceLocation CondRParen) { |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 894 | Expr *condExpr = Cond.takeAs<Expr>(); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 895 | assert(condExpr && "ActOnDoStmt(): missing expression"); |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 896 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 897 | if (CheckBooleanCondition(condExpr, DoLoc)) { |
Douglas Gregor | 9f3ca2a | 2009-05-15 21:56:04 +0000 | [diff] [blame] | 898 | Cond = condExpr; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 899 | return StmtError(); |
Douglas Gregor | 9f3ca2a | 2009-05-15 21:56:04 +0000 | [diff] [blame] | 900 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 901 | |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 902 | Stmt *bodyStmt = Body.takeAs<Stmt>(); |
| 903 | DiagnoseUnusedExprResult(bodyStmt); |
| 904 | |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 905 | Cond.release(); |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 906 | return Owned(new (Context) DoStmt(bodyStmt, condExpr, DoLoc, |
Chris Lattner | 9891359 | 2009-06-12 23:04:47 +0000 | [diff] [blame] | 907 | WhileLoc, CondRParen)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 910 | Action::OwningStmtResult |
| 911 | Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 912 | StmtArg first, FullExprArg second, DeclPtrTy secondVar, |
| 913 | FullExprArg third, |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 914 | SourceLocation RParenLoc, StmtArg body) { |
| 915 | Stmt *First = static_cast<Stmt*>(first.get()); |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 916 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 917 | if (!getLangOptions().CPlusPlus) { |
| 918 | if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 919 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only |
| 920 | // declare identifiers for objects having storage class 'auto' or |
| 921 | // 'register'. |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 922 | for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); |
| 923 | DI!=DE; ++DI) { |
| 924 | VarDecl *VD = dyn_cast<VarDecl>(*DI); |
| 925 | if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage()) |
| 926 | VD = 0; |
| 927 | if (VD == 0) |
| 928 | Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for); |
| 929 | // FIXME: mark decl erroneous! |
| 930 | } |
Chris Lattner | ae3b701 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 931 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 932 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 933 | |
| 934 | OwningExprResult SecondResult(second.release()); |
| 935 | VarDecl *ConditionVar = 0; |
| 936 | if (secondVar.get()) { |
| 937 | ConditionVar = secondVar.getAs<VarDecl>(); |
| 938 | SecondResult = CheckConditionVariable(ConditionVar); |
| 939 | if (SecondResult.isInvalid()) |
| 940 | return StmtError(); |
| 941 | } |
| 942 | |
| 943 | Expr *Second = SecondResult.takeAs<Expr>(); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 944 | if (Second && CheckBooleanCondition(Second, ForLoc)) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 945 | SecondResult = Second; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 946 | return StmtError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 947 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 949 | Expr *Third = third.release().takeAs<Expr>(); |
| 950 | Stmt *Body = static_cast<Stmt*>(body.get()); |
| 951 | |
Anders Carlsson | 3af708f | 2009-08-01 01:39:59 +0000 | [diff] [blame] | 952 | DiagnoseUnusedExprResult(First); |
| 953 | DiagnoseUnusedExprResult(Third); |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 954 | DiagnoseUnusedExprResult(Body); |
| 955 | |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 956 | first.release(); |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 957 | body.release(); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 958 | return Owned(new (Context) ForStmt(First, Second, ConditionVar, Third, Body, |
| 959 | ForLoc, LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 962 | Action::OwningStmtResult |
| 963 | Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, |
| 964 | SourceLocation LParenLoc, |
| 965 | StmtArg first, ExprArg second, |
| 966 | SourceLocation RParenLoc, StmtArg body) { |
| 967 | Stmt *First = static_cast<Stmt*>(first.get()); |
| 968 | Expr *Second = static_cast<Expr*>(second.get()); |
| 969 | Stmt *Body = static_cast<Stmt*>(body.get()); |
Fariborz Jahanian | 20552d2 | 2008-01-10 20:33:58 +0000 | [diff] [blame] | 970 | if (First) { |
| 971 | QualType FirstType; |
| 972 | if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) { |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 973 | if (!DS->isSingleDecl()) |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 974 | return StmtError(Diag((*DS->decl_begin())->getLocation(), |
| 975 | diag::err_toomany_element_decls)); |
| 976 | |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 977 | Decl *D = DS->getSingleDecl(); |
Ted Kremenek | f34afee | 2008-10-06 20:58:11 +0000 | [diff] [blame] | 978 | FirstType = cast<ValueDecl>(D)->getType(); |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 979 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only |
| 980 | // declare identifiers for objects having storage class 'auto' or |
| 981 | // 'register'. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 982 | VarDecl *VD = cast<VarDecl>(D); |
| 983 | if (VD->isBlockVarDecl() && !VD->hasLocalStorage()) |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 984 | return StmtError(Diag(VD->getLocation(), |
| 985 | diag::err_non_variable_decl_in_for)); |
Anders Carlsson | 1fe379f | 2008-08-25 18:16:36 +0000 | [diff] [blame] | 986 | } else { |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 987 | if (cast<Expr>(First)->isLvalue(Context) != Expr::LV_Valid) |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 988 | return StmtError(Diag(First->getLocStart(), |
| 989 | diag::err_selector_element_not_lvalue) |
| 990 | << First->getSourceRange()); |
| 991 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | FirstType = static_cast<Expr*>(First)->getType(); |
Anders Carlsson | 1fe379f | 2008-08-25 18:16:36 +0000 | [diff] [blame] | 993 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | if (!FirstType->isObjCObjectPointerType() && |
Fariborz Jahanian | a5e42a8 | 2009-08-14 21:53:27 +0000 | [diff] [blame] | 995 | !FirstType->isBlockPointerType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 996 | Diag(ForLoc, diag::err_selector_element_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 997 | << FirstType << First->getSourceRange(); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 998 | } |
| 999 | if (Second) { |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 1000 | DefaultFunctionArrayLvalueConversion(Second); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1001 | QualType SecondType = Second->getType(); |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 1002 | if (!SecondType->isObjCObjectPointerType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1003 | Diag(ForLoc, diag::err_collection_expr_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1004 | << SecondType << Second->getSourceRange(); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1005 | } |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1006 | first.release(); |
| 1007 | second.release(); |
| 1008 | body.release(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1009 | return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body, |
| 1010 | ForLoc, RParenLoc)); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1011 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1012 | |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1013 | Action::OwningStmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 1014 | Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1015 | IdentifierInfo *LabelII) { |
| 1016 | // Look up the record for this label identifier. |
Chris Lattner | ea29a3a | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 1017 | LabelStmt *&LabelDecl = getLabelMap()[LabelII]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1018 | |
Steve Naroff | caaacec | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 1019 | // If we haven't seen this label yet, create a forward reference. |
| 1020 | if (LabelDecl == 0) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1021 | LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0); |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1022 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1023 | return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1026 | Action::OwningStmtResult |
Chris Lattner | ad56d68 | 2009-04-19 01:04:21 +0000 | [diff] [blame] | 1027 | Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc, |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1028 | ExprArg DestExp) { |
Eli Friedman | bbf4623 | 2009-03-26 00:18:06 +0000 | [diff] [blame] | 1029 | // Convert operand to void* |
Eli Friedman | 3308382 | 2009-03-26 07:32:37 +0000 | [diff] [blame] | 1030 | Expr* E = DestExp.takeAs<Expr>(); |
Douglas Gregor | 5f1b9e6 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 1031 | if (!E->isTypeDependent()) { |
| 1032 | QualType ETy = E->getType(); |
Chandler Carruth | 2877998 | 2010-01-31 10:26:25 +0000 | [diff] [blame] | 1033 | QualType DestTy = Context.getPointerType(Context.VoidTy.withConst()); |
Douglas Gregor | 5f1b9e6 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 1034 | AssignConvertType ConvTy = |
Chandler Carruth | 2877998 | 2010-01-31 10:26:25 +0000 | [diff] [blame] | 1035 | CheckSingleAssignmentConstraints(DestTy, E); |
| 1036 | if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E, AA_Passing)) |
Douglas Gregor | 5f1b9e6 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 1037 | return StmtError(); |
| 1038 | } |
| 1039 | return Owned(new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1042 | Action::OwningStmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 1043 | Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1044 | Scope *S = CurScope->getContinueParent(); |
| 1045 | if (!S) { |
| 1046 | // C99 6.8.6.2p1: A break shall appear only in or as a loop body. |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1047 | return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1048 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1049 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1050 | return Owned(new (Context) ContinueStmt(ContinueLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1053 | Action::OwningStmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 1054 | Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1055 | Scope *S = CurScope->getBreakParent(); |
| 1056 | if (!S) { |
| 1057 | // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1058 | return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1059 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1060 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1061 | return Owned(new (Context) BreakStmt(BreakLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1064 | /// ActOnBlockReturnStmt - Utility routine to figure out block's return type. |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1065 | /// |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1066 | Action::OwningStmtResult |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1067 | Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1068 | // If this is the first return we've seen in the block, infer the type of |
| 1069 | // the block from it. |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1070 | BlockScopeInfo *CurBlock = getCurBlock(); |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1071 | if (CurBlock->ReturnType.isNull()) { |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 1072 | if (RetValExp) { |
Steve Naroff | 1656442 | 2008-09-24 22:26:48 +0000 | [diff] [blame] | 1073 | // Don't call UsualUnaryConversions(), since we don't want to do |
| 1074 | // integer promotions here. |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 1075 | DefaultFunctionArrayLvalueConversion(RetValExp); |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1076 | CurBlock->ReturnType = RetValExp->getType(); |
| 1077 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(RetValExp)) { |
| 1078 | // We have to remove a 'const' added to copied-in variable which was |
| 1079 | // part of the implementation spec. and not the actual qualifier for |
| 1080 | // the variable. |
| 1081 | if (CDRE->isConstQualAdded()) |
| 1082 | CurBlock->ReturnType.removeConst(); |
| 1083 | } |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 1084 | } else |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1085 | CurBlock->ReturnType = Context.VoidTy; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1086 | } |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1087 | QualType FnRetType = CurBlock->ReturnType; |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1088 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1089 | if (CurBlock->TheDecl->hasAttr<NoReturnAttr>()) { |
Mike Stump | 6c92fa7 | 2009-04-29 21:40:37 +0000 | [diff] [blame] | 1090 | Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr) |
| 1091 | << getCurFunctionOrMethodDecl()->getDeclName(); |
| 1092 | return StmtError(); |
| 1093 | } |
| 1094 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1095 | // Otherwise, verify that this result type matches the previous one. We are |
| 1096 | // pickier with blocks than for normal functions because we don't have GCC |
| 1097 | // compatibility to worry about here. |
| 1098 | if (CurBlock->ReturnType->isVoidType()) { |
| 1099 | if (RetValExp) { |
| 1100 | Diag(ReturnLoc, diag::err_return_block_has_expr); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1101 | RetValExp->Destroy(Context); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1102 | RetValExp = 0; |
| 1103 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1104 | return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1105 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1106 | |
| 1107 | if (!RetValExp) |
| 1108 | return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr)); |
| 1109 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1110 | if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) { |
| 1111 | // we have a non-void block with an expression, continue checking |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1112 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 1114 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1115 | // function return. |
| 1116 | |
| 1117 | // In C++ the return statement is handled via a copy initialization. |
| 1118 | // the C version of which boils down to CheckSingleAssignmentConstraints. |
Anders Carlsson | c6acbc5 | 2010-01-29 18:30:20 +0000 | [diff] [blame] | 1119 | OwningExprResult Res = PerformCopyInitialization( |
| 1120 | InitializedEntity::InitializeResult(ReturnLoc, |
| 1121 | FnRetType), |
| 1122 | SourceLocation(), |
| 1123 | Owned(RetValExp)); |
| 1124 | if (Res.isInvalid()) { |
| 1125 | // FIXME: Cleanup temporaries here, anyway? |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1126 | return StmtError(); |
Anders Carlsson | c6acbc5 | 2010-01-29 18:30:20 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | RetValExp = Res.takeAs<Expr>(); |
| 1130 | if (RetValExp) |
| 1131 | CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1132 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1133 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1134 | return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1135 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1136 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1137 | /// IsReturnCopyElidable - Whether returning @p RetExpr from a function that |
| 1138 | /// returns a @p RetType fulfills the criteria for copy elision (C++0x 12.8p15). |
| 1139 | static bool IsReturnCopyElidable(ASTContext &Ctx, QualType RetType, |
| 1140 | Expr *RetExpr) { |
| 1141 | QualType ExprType = RetExpr->getType(); |
| 1142 | // - in a return statement in a function with ... |
| 1143 | // ... a class return type ... |
| 1144 | if (!RetType->isRecordType()) |
| 1145 | return false; |
| 1146 | // ... the same cv-unqualified type as the function return type ... |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1147 | if (!Ctx.hasSameUnqualifiedType(RetType, ExprType)) |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1148 | return false; |
| 1149 | // ... the expression is the name of a non-volatile automatic object ... |
| 1150 | // We ignore parentheses here. |
| 1151 | // FIXME: Is this compliant? |
| 1152 | const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetExpr->IgnoreParens()); |
| 1153 | if (!DR) |
| 1154 | return false; |
| 1155 | const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 1156 | if (!VD) |
| 1157 | return false; |
| 1158 | return VD->hasLocalStorage() && !VD->getType()->isReferenceType() |
| 1159 | && !VD->getType().isVolatileQualified(); |
| 1160 | } |
| 1161 | |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1162 | Action::OwningStmtResult |
Anders Carlsson | f53b443 | 2009-08-18 16:11:00 +0000 | [diff] [blame] | 1163 | Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) { |
| 1164 | Expr *RetValExp = rex.takeAs<Expr>(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1165 | if (getCurBlock()) |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1166 | return ActOnBlockReturnStmt(ReturnLoc, RetValExp); |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1167 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1168 | QualType FnRetType; |
Mike Stump | f7c41da | 2009-04-29 00:43:21 +0000 | [diff] [blame] | 1169 | if (const FunctionDecl *FD = getCurFunctionDecl()) { |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1170 | FnRetType = FD->getResultType(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1171 | if (FD->hasAttr<NoReturnAttr>() || |
| 1172 | FD->getType()->getAs<FunctionType>()->getNoReturnAttr()) |
Chris Lattner | 8662587 | 2009-05-31 19:32:13 +0000 | [diff] [blame] | 1173 | Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr) |
Mike Stump | f7c41da | 2009-04-29 00:43:21 +0000 | [diff] [blame] | 1174 | << getCurFunctionOrMethodDecl()->getDeclName(); |
Mike Stump | f7c41da | 2009-04-29 00:43:21 +0000 | [diff] [blame] | 1175 | } else if (ObjCMethodDecl *MD = getCurMethodDecl()) |
Steve Naroff | c97fb9a | 2009-03-03 00:45:38 +0000 | [diff] [blame] | 1176 | FnRetType = MD->getResultType(); |
| 1177 | else // If we don't have a function/method context, bail. |
| 1178 | return StmtError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1180 | if (FnRetType->isVoidType()) { |
Douglas Gregor | 1be8aee | 2009-10-01 23:25:31 +0000 | [diff] [blame] | 1181 | if (RetValExp && !RetValExp->isTypeDependent()) { |
| 1182 | // C99 6.8.6.4p1 (ext_ since GCC warns) |
Chris Lattner | 65ce04b | 2008-12-18 02:01:17 +0000 | [diff] [blame] | 1183 | unsigned D = diag::ext_return_has_expr; |
| 1184 | if (RetValExp->getType()->isVoidType()) |
| 1185 | D = diag::ext_return_has_void_expr; |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1186 | |
Chris Lattner | e878eb0 | 2008-12-18 02:03:48 +0000 | [diff] [blame] | 1187 | // return (some void expression); is legal in C++. |
| 1188 | if (D != diag::ext_return_has_void_expr || |
| 1189 | !getLangOptions().CPlusPlus) { |
| 1190 | NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); |
| 1191 | Diag(ReturnLoc, D) |
| 1192 | << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl) |
| 1193 | << RetValExp->getSourceRange(); |
| 1194 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 1196 | RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1197 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1198 | return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1199 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1200 | |
Anders Carlsson | 03d7776 | 2009-05-15 00:48:27 +0000 | [diff] [blame] | 1201 | if (!RetValExp && !FnRetType->isDependentType()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1202 | unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4 |
| 1203 | // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 1204 | if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr; |
| 1205 | |
| 1206 | if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1207 | Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/; |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1208 | else |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1209 | Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1210 | return Owned(new (Context) ReturnStmt(ReturnLoc, (Expr*)0)); |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1211 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1212 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1213 | if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) { |
| 1214 | // we have a non-void function with an expression, continue checking |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1215 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 1217 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1218 | // function return. |
| 1219 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1220 | // C++0x 12.8p15: When certain criteria are met, an implementation is |
| 1221 | // allowed to omit the copy construction of a class object, [...] |
| 1222 | // - in a return statement in a function with a class return type, when |
| 1223 | // the expression is the name of a non-volatile automatic object with |
| 1224 | // the same cv-unqualified type as the function return type, the copy |
| 1225 | // operation can be omitted [...] |
| 1226 | // C++0x 12.8p16: When the criteria for elision of a copy operation are met |
| 1227 | // and the object to be copied is designated by an lvalue, overload |
| 1228 | // resolution to select the constructor for the copy is first performed |
| 1229 | // as if the object were designated by an rvalue. |
| 1230 | // Note that we only compute Elidable if we're in C++0x, since we don't |
| 1231 | // care otherwise. |
| 1232 | bool Elidable = getLangOptions().CPlusPlus0x ? |
| 1233 | IsReturnCopyElidable(Context, FnRetType, RetValExp) : |
| 1234 | false; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 1235 | // FIXME: Elidable |
| 1236 | (void)Elidable; |
| 1237 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1238 | // In C++ the return statement is handled via a copy initialization. |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1239 | // the C version of which boils down to CheckSingleAssignmentConstraints. |
Douglas Gregor | bd8f371 | 2009-12-18 17:31:58 +0000 | [diff] [blame] | 1240 | OwningExprResult Res = PerformCopyInitialization( |
| 1241 | InitializedEntity::InitializeResult(ReturnLoc, |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1242 | FnRetType), |
Douglas Gregor | bd8f371 | 2009-12-18 17:31:58 +0000 | [diff] [blame] | 1243 | SourceLocation(), |
| 1244 | Owned(RetValExp)); |
| 1245 | if (Res.isInvalid()) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 1246 | // FIXME: Cleanup temporaries here, anyway? |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1247 | return StmtError(); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1248 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 1249 | |
Douglas Gregor | bd8f371 | 2009-12-18 17:31:58 +0000 | [diff] [blame] | 1250 | RetValExp = Res.takeAs<Expr>(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 1251 | if (RetValExp) |
| 1252 | CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Anders Carlsson | f53b443 | 2009-08-18 16:11:00 +0000 | [diff] [blame] | 1255 | if (RetValExp) |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 1256 | RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1257 | return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 1260 | /// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently |
| 1261 | /// ignore "noop" casts in places where an lvalue is required by an inline asm. |
| 1262 | /// We emulate this behavior when -fheinous-gnu-extensions is specified, but |
| 1263 | /// provide a strong guidance to not use it. |
| 1264 | /// |
| 1265 | /// This method checks to see if the argument is an acceptable l-value and |
| 1266 | /// returns false if it is a case we can handle. |
| 1267 | static bool CheckAsmLValue(const Expr *E, Sema &S) { |
Anders Carlsson | 703e394 | 2010-01-24 05:50:09 +0000 | [diff] [blame] | 1268 | // Type dependent expressions will be checked during instantiation. |
| 1269 | if (E->isTypeDependent()) |
| 1270 | return false; |
| 1271 | |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 1272 | if (E->isLvalue(S.Context) == Expr::LV_Valid) |
| 1273 | return false; // Cool, this is an lvalue. |
| 1274 | |
| 1275 | // Okay, this is not an lvalue, but perhaps it is the result of a cast that we |
| 1276 | // are supposed to allow. |
| 1277 | const Expr *E2 = E->IgnoreParenNoopCasts(S.Context); |
| 1278 | if (E != E2 && E2->isLvalue(S.Context) == Expr::LV_Valid) { |
| 1279 | if (!S.getLangOptions().HeinousExtensions) |
| 1280 | S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue) |
| 1281 | << E->getSourceRange(); |
| 1282 | else |
| 1283 | S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue) |
| 1284 | << E->getSourceRange(); |
| 1285 | // Accept, even if we emitted an error diagnostic. |
| 1286 | return false; |
| 1287 | } |
| 1288 | |
| 1289 | // None of the above, just randomly invalid non-lvalue. |
| 1290 | return true; |
| 1291 | } |
| 1292 | |
| 1293 | |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1294 | Sema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, |
| 1295 | bool IsSimple, |
| 1296 | bool IsVolatile, |
| 1297 | unsigned NumOutputs, |
| 1298 | unsigned NumInputs, |
Anders Carlsson | ff93dbd | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 1299 | IdentifierInfo **Names, |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1300 | MultiExprArg constraints, |
| 1301 | MultiExprArg exprs, |
| 1302 | ExprArg asmString, |
| 1303 | MultiExprArg clobbers, |
Mike Stump | 3b11fd3 | 2010-01-04 22:37:17 +0000 | [diff] [blame] | 1304 | SourceLocation RParenLoc, |
| 1305 | bool MSAsm) { |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1306 | unsigned NumClobbers = clobbers.size(); |
| 1307 | StringLiteral **Constraints = |
| 1308 | reinterpret_cast<StringLiteral**>(constraints.get()); |
| 1309 | Expr **Exprs = reinterpret_cast<Expr **>(exprs.get()); |
| 1310 | StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString.get()); |
| 1311 | StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get()); |
| 1312 | |
Anders Carlsson | 03eb543 | 2009-01-27 20:38:24 +0000 | [diff] [blame] | 1313 | llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 1315 | // The parser verifies that there is a string literal here. |
Chris Lattner | 6bc5211 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 1316 | if (AsmString->isWide()) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1317 | return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character) |
| 1318 | << AsmString->getSourceRange()); |
| 1319 | |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 1320 | for (unsigned i = 0; i != NumOutputs; i++) { |
| 1321 | StringLiteral *Literal = Constraints[i]; |
Chris Lattner | 6bc5211 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 1322 | if (Literal->isWide()) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1323 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 1324 | << Literal->getSourceRange()); |
| 1325 | |
Anders Carlsson | ff93dbd | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 1326 | llvm::StringRef OutputName; |
| 1327 | if (Names[i]) |
| 1328 | OutputName = Names[i]->getName(); |
| 1329 | |
| 1330 | TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName); |
Chris Lattner | 432c869 | 2009-04-26 17:19:08 +0000 | [diff] [blame] | 1331 | if (!Context.Target.validateOutputConstraint(Info)) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1332 | return StmtError(Diag(Literal->getLocStart(), |
Chris Lattner | 432c869 | 2009-04-26 17:19:08 +0000 | [diff] [blame] | 1333 | diag::err_asm_invalid_output_constraint) |
| 1334 | << Info.getConstraintStr()); |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1335 | |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 1336 | // Check that the output exprs are valid lvalues. |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 1337 | Expr *OutputExpr = Exprs[i]; |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 1338 | if (CheckAsmLValue(OutputExpr, *this)) { |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 1339 | return StmtError(Diag(OutputExpr->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1340 | diag::err_asm_invalid_lvalue_in_output) |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 1341 | << OutputExpr->getSourceRange()); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 1342 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | |
Chris Lattner | 44def07 | 2009-04-26 07:16:29 +0000 | [diff] [blame] | 1344 | OutputConstraintInfos.push_back(Info); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 1345 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1346 | |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 1347 | llvm::SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; |
| 1348 | |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 1349 | for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 1350 | StringLiteral *Literal = Constraints[i]; |
Chris Lattner | 6bc5211 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 1351 | if (Literal->isWide()) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1352 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 1353 | << Literal->getSourceRange()); |
| 1354 | |
Anders Carlsson | ff93dbd | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 1355 | llvm::StringRef InputName; |
| 1356 | if (Names[i]) |
| 1357 | InputName = Names[i]->getName(); |
| 1358 | |
| 1359 | TargetInfo::ConstraintInfo Info(Literal->getString(), InputName); |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1360 | if (!Context.Target.validateInputConstraint(OutputConstraintInfos.data(), |
Chris Lattner | 2819fa8 | 2009-04-26 17:57:12 +0000 | [diff] [blame] | 1361 | NumOutputs, Info)) { |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1362 | return StmtError(Diag(Literal->getLocStart(), |
Chris Lattner | 432c869 | 2009-04-26 17:19:08 +0000 | [diff] [blame] | 1363 | diag::err_asm_invalid_input_constraint) |
| 1364 | << Info.getConstraintStr()); |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 1365 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1366 | |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 1367 | Expr *InputExpr = Exprs[i]; |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1368 | |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 1369 | // Only allow void types for memory constraints. |
Chris Lattner | 44def07 | 2009-04-26 07:16:29 +0000 | [diff] [blame] | 1370 | if (Info.allowsMemory() && !Info.allowsRegister()) { |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 1371 | if (CheckAsmLValue(InputExpr, *this)) |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 1372 | return StmtError(Diag(InputExpr->getLocStart(), |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 1373 | diag::err_asm_invalid_lvalue_in_input) |
Chris Lattner | 432c869 | 2009-04-26 17:19:08 +0000 | [diff] [blame] | 1374 | << Info.getConstraintStr() |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 1375 | << InputExpr->getSourceRange()); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 1376 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1377 | |
Chris Lattner | 44def07 | 2009-04-26 07:16:29 +0000 | [diff] [blame] | 1378 | if (Info.allowsRegister()) { |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 1379 | if (InputExpr->getType()->isVoidType()) { |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 1380 | return StmtError(Diag(InputExpr->getLocStart(), |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 1381 | diag::err_asm_invalid_type_in_input) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | << InputExpr->getType() << Info.getConstraintStr() |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 1383 | << InputExpr->getSourceRange()); |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 1384 | } |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 1385 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1386 | |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 1387 | DefaultFunctionArrayLvalueConversion(Exprs[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 1389 | InputConstraintInfos.push_back(Info); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 1390 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1391 | |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 1392 | // Check that the clobbers are valid. |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 1393 | for (unsigned i = 0; i != NumClobbers; i++) { |
| 1394 | StringLiteral *Literal = Clobbers[i]; |
Chris Lattner | 6bc5211 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 1395 | if (Literal->isWide()) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1396 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 1397 | << Literal->getSourceRange()); |
| 1398 | |
Anders Carlsson | fdba9c0 | 2010-01-30 19:34:25 +0000 | [diff] [blame] | 1399 | llvm::StringRef Clobber = Literal->getString(); |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1400 | |
Anders Carlsson | fdba9c0 | 2010-01-30 19:34:25 +0000 | [diff] [blame] | 1401 | if (!Context.Target.isValidGCCRegisterName(Clobber)) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1402 | return StmtError(Diag(Literal->getLocStart(), |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 1403 | diag::err_asm_unknown_register_name) << Clobber); |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 1404 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 1405 | |
| 1406 | constraints.release(); |
| 1407 | exprs.release(); |
| 1408 | asmString.release(); |
| 1409 | clobbers.release(); |
Chris Lattner | fb5058e | 2009-03-10 23:41:04 +0000 | [diff] [blame] | 1410 | AsmStmt *NS = |
Anders Carlsson | 966146e | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 1411 | new (Context) AsmStmt(Context, AsmLoc, IsSimple, IsVolatile, MSAsm, |
| 1412 | NumOutputs, NumInputs, Names, Constraints, Exprs, |
| 1413 | AsmString, NumClobbers, Clobbers, RParenLoc); |
Chris Lattner | fb5058e | 2009-03-10 23:41:04 +0000 | [diff] [blame] | 1414 | // Validate the asm string, ensuring it makes sense given the operands we |
| 1415 | // have. |
| 1416 | llvm::SmallVector<AsmStmt::AsmStringPiece, 8> Pieces; |
| 1417 | unsigned DiagOffs; |
| 1418 | if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { |
Chris Lattner | 2ff0f42 | 2009-03-10 23:57:07 +0000 | [diff] [blame] | 1419 | Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) |
| 1420 | << AsmString->getSourceRange(); |
Chris Lattner | fb5058e | 2009-03-10 23:41:04 +0000 | [diff] [blame] | 1421 | DeleteStmt(NS); |
| 1422 | return StmtError(); |
| 1423 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 1425 | // Validate tied input operands for type mismatches. |
| 1426 | for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) { |
| 1427 | TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 1429 | // If this is a tied constraint, verify that the output and input have |
| 1430 | // either exactly the same type, or that they are int/ptr operands with the |
| 1431 | // same size (int/long, int*/long, are ok etc). |
| 1432 | if (!Info.hasTiedOperand()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 1434 | unsigned TiedTo = Info.getTiedOperand(); |
Chris Lattner | f69fcae | 2009-05-03 07:04:21 +0000 | [diff] [blame] | 1435 | Expr *OutputExpr = Exprs[TiedTo]; |
Chris Lattner | c1f3b28 | 2009-05-03 06:50:40 +0000 | [diff] [blame] | 1436 | Expr *InputExpr = Exprs[i+NumOutputs]; |
Chris Lattner | 7adaa18 | 2009-05-03 05:59:17 +0000 | [diff] [blame] | 1437 | QualType InTy = InputExpr->getType(); |
| 1438 | QualType OutTy = OutputExpr->getType(); |
| 1439 | if (Context.hasSameType(InTy, OutTy)) |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 1440 | continue; // All types can be tied to themselves. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1441 | |
Chris Lattner | 7adaa18 | 2009-05-03 05:59:17 +0000 | [diff] [blame] | 1442 | // Int/ptr operands have some special cases that we allow. |
| 1443 | if ((OutTy->isIntegerType() || OutTy->isPointerType()) && |
| 1444 | (InTy->isIntegerType() || InTy->isPointerType())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | |
Chris Lattner | 7adaa18 | 2009-05-03 05:59:17 +0000 | [diff] [blame] | 1446 | // They are ok if they are the same size. Tying void* to int is ok if |
| 1447 | // they are the same size, for example. This also allows tying void* to |
| 1448 | // int*. |
Chris Lattner | 3351f11 | 2009-05-03 08:32:32 +0000 | [diff] [blame] | 1449 | uint64_t OutSize = Context.getTypeSize(OutTy); |
| 1450 | uint64_t InSize = Context.getTypeSize(InTy); |
| 1451 | if (OutSize == InSize) |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 1452 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | |
Chris Lattner | 3351f11 | 2009-05-03 08:32:32 +0000 | [diff] [blame] | 1454 | // If the smaller input/output operand is not mentioned in the asm string, |
| 1455 | // then we can promote it and the asm string won't notice. Check this |
Chris Lattner | f69fcae | 2009-05-03 07:04:21 +0000 | [diff] [blame] | 1456 | // case now. |
Chris Lattner | 3351f11 | 2009-05-03 08:32:32 +0000 | [diff] [blame] | 1457 | bool SmallerValueMentioned = false; |
Chris Lattner | 58bce89 | 2009-05-03 08:24:16 +0000 | [diff] [blame] | 1458 | for (unsigned p = 0, e = Pieces.size(); p != e; ++p) { |
| 1459 | AsmStmt::AsmStringPiece &Piece = Pieces[p]; |
| 1460 | if (!Piece.isOperand()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | |
Chris Lattner | 3351f11 | 2009-05-03 08:32:32 +0000 | [diff] [blame] | 1462 | // If this is a reference to the input and if the input was the smaller |
| 1463 | // one, then we have to reject this asm. |
| 1464 | if (Piece.getOperandNo() == i+NumOutputs) { |
| 1465 | if (InSize < OutSize) { |
| 1466 | SmallerValueMentioned = true; |
| 1467 | break; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | // If this is a reference to the input and if the input was the smaller |
| 1472 | // one, then we have to reject this asm. |
| 1473 | if (Piece.getOperandNo() == TiedTo) { |
| 1474 | if (InSize > OutSize) { |
| 1475 | SmallerValueMentioned = true; |
| 1476 | break; |
| 1477 | } |
| 1478 | } |
Chris Lattner | f69fcae | 2009-05-03 07:04:21 +0000 | [diff] [blame] | 1479 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1480 | |
Chris Lattner | 3351f11 | 2009-05-03 08:32:32 +0000 | [diff] [blame] | 1481 | // If the smaller value wasn't mentioned in the asm string, and if the |
| 1482 | // output was a register, just extend the shorter one to the size of the |
| 1483 | // larger one. |
| 1484 | if (!SmallerValueMentioned && |
Chris Lattner | f69fcae | 2009-05-03 07:04:21 +0000 | [diff] [blame] | 1485 | OutputConstraintInfos[TiedTo].allowsRegister()) |
| 1486 | continue; |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 1487 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | |
Chris Lattner | c1f3b28 | 2009-05-03 06:50:40 +0000 | [diff] [blame] | 1489 | Diag(InputExpr->getLocStart(), |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 1490 | diag::err_asm_tying_incompatible_types) |
Chris Lattner | 7adaa18 | 2009-05-03 05:59:17 +0000 | [diff] [blame] | 1491 | << InTy << OutTy << OutputExpr->getSourceRange() |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 1492 | << InputExpr->getSourceRange(); |
| 1493 | DeleteStmt(NS); |
| 1494 | return StmtError(); |
| 1495 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | |
Chris Lattner | fb5058e | 2009-03-10 23:41:04 +0000 | [diff] [blame] | 1497 | return Owned(NS); |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 1498 | } |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1499 | |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 1500 | Action::OwningStmtResult |
| 1501 | Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1502 | SourceLocation RParen, DeclPtrTy Parm, |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 1503 | StmtArg Body, StmtArg catchList) { |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 1504 | Stmt *CatchList = catchList.takeAs<Stmt>(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1505 | ParmVarDecl *PVD = cast_or_null<ParmVarDecl>(Parm.getAs<Decl>()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | |
Steve Naroff | f50cb36 | 2009-03-03 20:59:06 +0000 | [diff] [blame] | 1507 | // PVD == 0 implies @catch(...). |
Steve Naroff | 9d40ee5 | 2009-03-03 21:16:54 +0000 | [diff] [blame] | 1508 | if (PVD) { |
Chris Lattner | 93c4945 | 2009-04-12 23:26:56 +0000 | [diff] [blame] | 1509 | // If we already know the decl is invalid, reject it. |
| 1510 | if (PVD->isInvalidDecl()) |
| 1511 | return StmtError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1512 | |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 1513 | if (!PVD->getType()->isObjCObjectPointerType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | return StmtError(Diag(PVD->getLocation(), |
Steve Naroff | 9d40ee5 | 2009-03-03 21:16:54 +0000 | [diff] [blame] | 1515 | diag::err_catch_param_not_objc_type)); |
| 1516 | if (PVD->getType()->isObjCQualifiedIdType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1517 | return StmtError(Diag(PVD->getLocation(), |
Steve Naroff | d198aba | 2009-03-03 23:13:51 +0000 | [diff] [blame] | 1518 | diag::err_illegal_qualifiers_on_catch_parm)); |
Steve Naroff | 9d40ee5 | 2009-03-03 21:16:54 +0000 | [diff] [blame] | 1519 | } |
Chris Lattner | 93c4945 | 2009-04-12 23:26:56 +0000 | [diff] [blame] | 1520 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1521 | ObjCAtCatchStmt *CS = new (Context) ObjCAtCatchStmt(AtLoc, RParen, |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1522 | PVD, Body.takeAs<Stmt>(), CatchList); |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 1523 | return Owned(CatchList ? CatchList : CS); |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 1526 | Action::OwningStmtResult |
| 1527 | Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1528 | return Owned(new (Context) ObjCAtFinallyStmt(AtLoc, |
| 1529 | static_cast<Stmt*>(Body.release()))); |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1530 | } |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1531 | |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 1532 | Action::OwningStmtResult |
| 1533 | Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc, |
| 1534 | StmtArg Try, StmtArg Catch, StmtArg Finally) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1535 | FunctionNeedsScopeChecking() = true; |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1536 | return Owned(new (Context) ObjCAtTryStmt(AtLoc, Try.takeAs<Stmt>(), |
| 1537 | Catch.takeAs<Stmt>(), |
| 1538 | Finally.takeAs<Stmt>())); |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 1541 | Action::OwningStmtResult |
Steve Naroff | 3dcfe10 | 2009-02-12 15:54:59 +0000 | [diff] [blame] | 1542 | Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg expr,Scope *CurScope) { |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 1543 | Expr *ThrowExpr = expr.takeAs<Expr>(); |
Steve Naroff | 7151bbb | 2009-02-11 17:45:08 +0000 | [diff] [blame] | 1544 | if (!ThrowExpr) { |
Steve Naroff | e21dd6f | 2009-02-11 20:05:44 +0000 | [diff] [blame] | 1545 | // @throw without an expression designates a rethrow (which much occur |
| 1546 | // in the context of an @catch clause). |
| 1547 | Scope *AtCatchParent = CurScope; |
| 1548 | while (AtCatchParent && !AtCatchParent->isAtCatchScope()) |
| 1549 | AtCatchParent = AtCatchParent->getParent(); |
| 1550 | if (!AtCatchParent) |
Steve Naroff | 4ab2414 | 2009-02-12 18:09:32 +0000 | [diff] [blame] | 1551 | return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch)); |
Steve Naroff | 7151bbb | 2009-02-11 17:45:08 +0000 | [diff] [blame] | 1552 | } else { |
| 1553 | QualType ThrowType = ThrowExpr->getType(); |
| 1554 | // Make sure the expression type is an ObjC pointer or "void *". |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 1555 | if (!ThrowType->isObjCObjectPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1556 | const PointerType *PT = ThrowType->getAs<PointerType>(); |
Steve Naroff | 7151bbb | 2009-02-11 17:45:08 +0000 | [diff] [blame] | 1557 | if (!PT || !PT->getPointeeType()->isVoidType()) |
Steve Naroff | 4ab2414 | 2009-02-12 18:09:32 +0000 | [diff] [blame] | 1558 | return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object) |
| 1559 | << ThrowExpr->getType() << ThrowExpr->getSourceRange()); |
Steve Naroff | 7151bbb | 2009-02-11 17:45:08 +0000 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | return Owned(new (Context) ObjCAtThrowStmt(AtLoc, ThrowExpr)); |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 1563 | } |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1564 | |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 1565 | Action::OwningStmtResult |
| 1566 | Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr, |
| 1567 | StmtArg SynchBody) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1568 | FunctionNeedsScopeChecking() = true; |
Chris Lattner | 46c3c4b | 2009-04-21 06:01:00 +0000 | [diff] [blame] | 1569 | |
Chris Lattner | a868a20 | 2009-04-21 06:11:25 +0000 | [diff] [blame] | 1570 | // Make sure the expression type is an ObjC pointer or "void *". |
| 1571 | Expr *SyncExpr = static_cast<Expr*>(SynchExpr.get()); |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 1572 | if (!SyncExpr->getType()->isObjCObjectPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1573 | const PointerType *PT = SyncExpr->getType()->getAs<PointerType>(); |
Chris Lattner | a868a20 | 2009-04-21 06:11:25 +0000 | [diff] [blame] | 1574 | if (!PT || !PT->getPointeeType()->isVoidType()) |
| 1575 | return StmtError(Diag(AtLoc, diag::error_objc_synchronized_expects_object) |
| 1576 | << SyncExpr->getType() << SyncExpr->getSourceRange()); |
| 1577 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1578 | |
| 1579 | return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc, |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1580 | SynchExpr.takeAs<Stmt>(), |
| 1581 | SynchBody.takeAs<Stmt>())); |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1582 | } |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 1583 | |
| 1584 | /// ActOnCXXCatchBlock - Takes an exception declaration and a handler block |
| 1585 | /// and creates a proper catch handler from them. |
| 1586 | Action::OwningStmtResult |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1587 | Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclPtrTy ExDecl, |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 1588 | StmtArg HandlerBlock) { |
| 1589 | // There's nothing to test that ActOnExceptionDecl didn't already test. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1590 | return Owned(new (Context) CXXCatchStmt(CatchLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1591 | cast_or_null<VarDecl>(ExDecl.getAs<Decl>()), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1592 | HandlerBlock.takeAs<Stmt>())); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 1593 | } |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 1594 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1595 | class TypeWithHandler { |
| 1596 | QualType t; |
| 1597 | CXXCatchStmt *stmt; |
| 1598 | public: |
| 1599 | TypeWithHandler(const QualType &type, CXXCatchStmt *statement) |
| 1600 | : t(type), stmt(statement) {} |
| 1601 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1602 | // An arbitrary order is fine as long as it places identical |
| 1603 | // types next to each other. |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1604 | bool operator<(const TypeWithHandler &y) const { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1605 | if (t.getAsOpaquePtr() < y.t.getAsOpaquePtr()) |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1606 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1607 | if (t.getAsOpaquePtr() > y.t.getAsOpaquePtr()) |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1608 | return false; |
| 1609 | else |
| 1610 | return getTypeSpecStartLoc() < y.getTypeSpecStartLoc(); |
| 1611 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1612 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1613 | bool operator==(const TypeWithHandler& other) const { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1614 | return t == other.t; |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1615 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1616 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1617 | QualType getQualType() const { return t; } |
| 1618 | CXXCatchStmt *getCatchStmt() const { return stmt; } |
| 1619 | SourceLocation getTypeSpecStartLoc() const { |
| 1620 | return stmt->getExceptionDecl()->getTypeSpecStartLoc(); |
| 1621 | } |
| 1622 | }; |
| 1623 | |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 1624 | /// ActOnCXXTryBlock - Takes a try compound-statement and a number of |
| 1625 | /// handlers and creates a try statement from them. |
| 1626 | Action::OwningStmtResult |
| 1627 | Sema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock, |
| 1628 | MultiStmtArg RawHandlers) { |
| 1629 | unsigned NumHandlers = RawHandlers.size(); |
| 1630 | assert(NumHandlers > 0 && |
| 1631 | "The parser shouldn't call this if there are no handlers."); |
| 1632 | Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get()); |
| 1633 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1634 | llvm::SmallVector<TypeWithHandler, 8> TypesWithHandlers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | |
| 1636 | for (unsigned i = 0; i < NumHandlers; ++i) { |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 1637 | CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]); |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1638 | if (!Handler->getExceptionDecl()) { |
| 1639 | if (i < NumHandlers - 1) |
| 1640 | return StmtError(Diag(Handler->getLocStart(), |
| 1641 | diag::err_early_catch_all)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1643 | continue; |
| 1644 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1646 | const QualType CaughtType = Handler->getCaughtType(); |
| 1647 | const QualType CanonicalCaughtType = Context.getCanonicalType(CaughtType); |
| 1648 | TypesWithHandlers.push_back(TypeWithHandler(CanonicalCaughtType, Handler)); |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 1649 | } |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1650 | |
| 1651 | // Detect handlers for the same type as an earlier one. |
| 1652 | if (NumHandlers > 1) { |
| 1653 | llvm::array_pod_sort(TypesWithHandlers.begin(), TypesWithHandlers.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1655 | TypeWithHandler prev = TypesWithHandlers[0]; |
| 1656 | for (unsigned i = 1; i < TypesWithHandlers.size(); ++i) { |
| 1657 | TypeWithHandler curr = TypesWithHandlers[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1659 | if (curr == prev) { |
| 1660 | Diag(curr.getTypeSpecStartLoc(), |
| 1661 | diag::warn_exception_caught_by_earlier_handler) |
| 1662 | << curr.getCatchStmt()->getCaughtType().getAsString(); |
| 1663 | Diag(prev.getTypeSpecStartLoc(), |
| 1664 | diag::note_previous_exception_handler) |
| 1665 | << prev.getCatchStmt()->getCaughtType().getAsString(); |
| 1666 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1667 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 1668 | prev = curr; |
| 1669 | } |
| 1670 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 1672 | // FIXME: We should detect handlers that cannot catch anything because an |
| 1673 | // earlier handler catches a superclass. Need to find a method that is not |
| 1674 | // quadratic for this. |
| 1675 | // Neither of these are explicitly forbidden, but every compiler detects them |
| 1676 | // and warns. |
| 1677 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1678 | FunctionNeedsScopeChecking() = true; |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 1679 | RawHandlers.release(); |
Sam Weinig | a1a396d | 2010-02-03 03:56:39 +0000 | [diff] [blame] | 1680 | return Owned(CXXTryStmt::Create(Context, TryLoc, |
| 1681 | static_cast<Stmt*>(TryBlock.release()), |
| 1682 | Handlers, NumHandlers)); |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 1683 | } |