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 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 16 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Initialization.h" |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Lookup.h" |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
John McCall | 1cd76e8 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 20 | #include "clang/AST/CharUnits.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Richard Trieu | 694e796 | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 22 | #include "clang/AST/EvaluatedExprVisitor.h" |
Douglas Gregor | 84fb9c0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 419cfb3 | 2009-08-16 16:57:27 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprObjC.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtObjC.h" |
| 26 | #include "clang/AST/StmtCXX.h" |
John McCall | 209acbd | 2010-04-06 22:24:14 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 84fb9c0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | ca57b4b | 2011-02-21 21:40:33 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/ArrayRef.h" |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Richard Trieu | 694e796 | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 50de5e3 | 2012-05-16 16:11:17 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallString.h" |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 36 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 38 | StmtResult Sema::ActOnExprStmt(FullExprArg expr) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 39 | Expr *E = expr.get(); |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 40 | if (!E) // FIXME: FullExprArg has no error state? |
| 41 | return StmtError(); |
| 42 | |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 43 | // C99 6.8.3p2: The expression in an expression statement is evaluated as a |
| 44 | // void expression for its side effects. Conversion to void allows any |
| 45 | // operand, even incomplete types. |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 47 | // Same thing in for stmt first clause (when expr) and third clause. |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 48 | return Owned(static_cast<Stmt*>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | |
Argyrios Kyrtzidis | b7d98d3 | 2011-04-27 05:04:02 +0000 | [diff] [blame] | 52 | StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc, |
Argyrios Kyrtzidis | e2ca828 | 2011-09-01 21:53:45 +0000 | [diff] [blame] | 53 | bool HasLeadingEmptyMacro) { |
| 54 | return Owned(new (Context) NullStmt(SemiLoc, HasLeadingEmptyMacro)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 57 | StmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg, SourceLocation StartLoc, |
| 58 | SourceLocation EndLoc) { |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 59 | DeclGroupRef DG = dg.getAsVal<DeclGroupRef>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 2040169 | 2009-04-12 20:13:14 +0000 | [diff] [blame] | 61 | // If we have an invalid decl, just return an error. |
| 62 | if (DG.isNull()) return StmtError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 64 | return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Fariborz Jahanian | a7cf23a | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 67 | void Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) { |
| 68 | DeclGroupRef DG = dg.getAsVal<DeclGroupRef>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 69 | |
Fariborz Jahanian | a7cf23a | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 70 | // If we have an invalid decl, just return. |
| 71 | if (DG.isNull() || !DG.isSingleDecl()) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 72 | VarDecl *var = cast<VarDecl>(DG.getSingleDecl()); |
| 73 | |
Fariborz Jahanian | a7cf23a | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 74 | // suppress any potential 'unused variable' warning. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 75 | var->setUsed(); |
| 76 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 77 | // foreach variables are never actually initialized in the way that |
| 78 | // the parser came up with. |
| 79 | var->setInit(0); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 80 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 81 | // In ARC, we don't need to retain the iteration variable of a fast |
| 82 | // enumeration loop. Rather than actually trying to catch that |
| 83 | // during declaration processing, we remove the consequences here. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 84 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 85 | QualType type = var->getType(); |
| 86 | |
| 87 | // Only do this if we inferred the lifetime. Inferred lifetime |
| 88 | // will show up as a local qualifier because explicit lifetime |
| 89 | // should have shown up as an AttributedType instead. |
| 90 | if (type.getLocalQualifiers().getObjCLifetime() == Qualifiers::OCL_Strong) { |
| 91 | // Add 'const' and mark the variable as pseudo-strong. |
| 92 | var->setType(type.withConst()); |
| 93 | var->setARCPseudoStrong(true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
Fariborz Jahanian | a7cf23a | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Chandler Carruth | ec8058f | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 98 | /// \brief Diagnose unused '==' and '!=' as likely typos for '=' or '|='. |
Chandler Carruth | 9d8eb3b | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 99 | /// |
| 100 | /// Adding a cast to void (or other expression wrappers) will prevent the |
| 101 | /// warning from firing. |
Chandler Carruth | ec8058f | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 102 | static bool DiagnoseUnusedComparison(Sema &S, const Expr *E) { |
Chandler Carruth | 9d8eb3b | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 103 | SourceLocation Loc; |
Chandler Carruth | 50bf68f | 2011-08-17 08:38:11 +0000 | [diff] [blame] | 104 | bool IsNotEqual, CanAssign; |
Chandler Carruth | 9d8eb3b | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 105 | |
| 106 | if (const BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { |
| 107 | if (Op->getOpcode() != BO_EQ && Op->getOpcode() != BO_NE) |
Chandler Carruth | ec8058f | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 108 | return false; |
Chandler Carruth | 9d8eb3b | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 109 | |
Chandler Carruth | 9d8eb3b | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 110 | Loc = Op->getOperatorLoc(); |
Chandler Carruth | 50bf68f | 2011-08-17 08:38:11 +0000 | [diff] [blame] | 111 | IsNotEqual = Op->getOpcode() == BO_NE; |
| 112 | CanAssign = Op->getLHS()->IgnoreParenImpCasts()->isLValue(); |
Chandler Carruth | 9d8eb3b | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 113 | } else if (const CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 114 | if (Op->getOperator() != OO_EqualEqual && |
| 115 | Op->getOperator() != OO_ExclaimEqual) |
Chandler Carruth | ec8058f | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 116 | return false; |
Chandler Carruth | 9d8eb3b | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 117 | |
Chandler Carruth | 9d8eb3b | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 118 | Loc = Op->getOperatorLoc(); |
Chandler Carruth | 50bf68f | 2011-08-17 08:38:11 +0000 | [diff] [blame] | 119 | IsNotEqual = Op->getOperator() == OO_ExclaimEqual; |
| 120 | CanAssign = Op->getArg(0)->IgnoreParenImpCasts()->isLValue(); |
Chandler Carruth | 9d8eb3b | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 121 | } else { |
| 122 | // Not a typo-prone comparison. |
Chandler Carruth | ec8058f | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 123 | return false; |
Chandler Carruth | 9d8eb3b | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Suppress warnings when the operator, suspicious as it may be, comes from |
| 127 | // a macro expansion. |
| 128 | if (Loc.isMacroID()) |
Chandler Carruth | ec8058f | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 129 | return false; |
Chandler Carruth | 9d8eb3b | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 130 | |
Chandler Carruth | ec8058f | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 131 | S.Diag(Loc, diag::warn_unused_comparison) |
Chandler Carruth | 9d8eb3b | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 132 | << (unsigned)IsNotEqual << E->getSourceRange(); |
| 133 | |
Chandler Carruth | 50bf68f | 2011-08-17 08:38:11 +0000 | [diff] [blame] | 134 | // If the LHS is a plausible entity to assign to, provide a fixit hint to |
| 135 | // correct common typos. |
| 136 | if (CanAssign) { |
| 137 | if (IsNotEqual) |
| 138 | S.Diag(Loc, diag::note_inequality_comparison_to_or_assign) |
| 139 | << FixItHint::CreateReplacement(Loc, "|="); |
| 140 | else |
| 141 | S.Diag(Loc, diag::note_equality_comparison_to_assign) |
| 142 | << FixItHint::CreateReplacement(Loc, "="); |
| 143 | } |
Chandler Carruth | ec8058f | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 144 | |
| 145 | return true; |
Chandler Carruth | 9d8eb3b | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 148 | void Sema::DiagnoseUnusedExprResult(const Stmt *S) { |
Argyrios Kyrtzidis | d2827af | 2010-09-19 21:21:10 +0000 | [diff] [blame] | 149 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
| 150 | return DiagnoseUnusedExprResult(Label->getSubStmt()); |
| 151 | |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 152 | const Expr *E = dyn_cast_or_null<Expr>(S); |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 153 | if (!E) |
| 154 | return; |
| 155 | |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 156 | const Expr *WarnExpr; |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 157 | SourceLocation Loc; |
| 158 | SourceRange R1, R2; |
Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 159 | if (SourceMgr.isInSystemMacro(E->getExprLoc()) || |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 160 | !E->isUnusedResultAWarning(WarnExpr, Loc, R1, R2, Context)) |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 161 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 419cfb3 | 2009-08-16 16:57:27 +0000 | [diff] [blame] | 163 | // Okay, we have an unused result. Depending on what the base expression is, |
| 164 | // we might want to make a more specific diagnostic. Check for one of these |
| 165 | // cases now. |
| 166 | unsigned DiagID = diag::warn_unused_expr; |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 167 | if (const ExprWithCleanups *Temps = dyn_cast<ExprWithCleanups>(E)) |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 168 | E = Temps->getSubExpr(); |
Chandler Carruth | 34d4947 | 2011-02-21 00:56:56 +0000 | [diff] [blame] | 169 | if (const CXXBindTemporaryExpr *TempExpr = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 170 | E = TempExpr->getSubExpr(); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 171 | |
Chandler Carruth | ec8058f | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 172 | if (DiagnoseUnusedComparison(*this, E)) |
| 173 | return; |
| 174 | |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 175 | E = WarnExpr; |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 176 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { |
John McCall | 0faede6 | 2010-03-12 07:11:26 +0000 | [diff] [blame] | 177 | if (E->getType()->isVoidType()) |
| 178 | return; |
| 179 | |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 180 | // If the callee has attribute pure, const, or warn_unused_result, warn with |
| 181 | // a more specific message to make it clear what is happening. |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 182 | if (const Decl *FD = CE->getCalleeDecl()) { |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 183 | if (FD->getAttr<WarnUnusedResultAttr>()) { |
Matt Beaumont-Gay | 42d7b2d | 2011-08-04 23:11:04 +0000 | [diff] [blame] | 184 | Diag(Loc, diag::warn_unused_result) << R1 << R2; |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | if (FD->getAttr<PureAttr>()) { |
| 188 | Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure"; |
| 189 | return; |
| 190 | } |
| 191 | if (FD->getAttr<ConstAttr>()) { |
| 192 | Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const"; |
| 193 | return; |
| 194 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 195 | } |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 196 | } else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 197 | if (getLangOpts().ObjCAutoRefCount && ME->isDelegateInitCall()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 198 | Diag(Loc, diag::err_arc_unused_init_message) << R1; |
| 199 | return; |
| 200 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 201 | const ObjCMethodDecl *MD = ME->getMethodDecl(); |
| 202 | if (MD && MD->getAttr<WarnUnusedResultAttr>()) { |
Matt Beaumont-Gay | 42d7b2d | 2011-08-04 23:11:04 +0000 | [diff] [blame] | 203 | Diag(Loc, diag::warn_unused_result) << R1 << R2; |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 204 | return; |
| 205 | } |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 206 | } else if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { |
| 207 | const Expr *Source = POE->getSyntacticForm(); |
| 208 | if (isa<ObjCSubscriptRefExpr>(Source)) |
| 209 | DiagID = diag::warn_unused_container_subscript_expr; |
| 210 | else |
| 211 | DiagID = diag::warn_unused_property_expr; |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 212 | } else if (const CXXFunctionalCastExpr *FC |
| 213 | = dyn_cast<CXXFunctionalCastExpr>(E)) { |
| 214 | if (isa<CXXConstructExpr>(FC->getSubExpr()) || |
| 215 | isa<CXXTemporaryObjectExpr>(FC->getSubExpr())) |
| 216 | return; |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 217 | } |
John McCall | 209acbd | 2010-04-06 22:24:14 +0000 | [diff] [blame] | 218 | // Diagnose "(void*) blah" as a typo for "(void) blah". |
| 219 | else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) { |
| 220 | TypeSourceInfo *TI = CE->getTypeInfoAsWritten(); |
| 221 | QualType T = TI->getType(); |
| 222 | |
| 223 | // We really do want to use the non-canonical type here. |
| 224 | if (T == Context.VoidPtrTy) { |
| 225 | PointerTypeLoc TL = cast<PointerTypeLoc>(TI->getTypeLoc()); |
| 226 | |
| 227 | Diag(Loc, diag::warn_unused_voidptr) |
| 228 | << FixItHint::CreateRemoval(TL.getStarLoc()); |
| 229 | return; |
| 230 | } |
| 231 | } |
| 232 | |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 233 | if (E->isGLValue() && E->getType().isVolatileQualified()) { |
| 234 | Diag(Loc, diag::warn_unused_volatile) << R1 << R2; |
| 235 | return; |
| 236 | } |
| 237 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 238 | DiagRuntimeBehavior(Loc, 0, PDiag(DiagID) << R1 << R2); |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 241 | void Sema::ActOnStartOfCompoundStmt() { |
| 242 | PushCompoundScope(); |
| 243 | } |
| 244 | |
| 245 | void Sema::ActOnFinishOfCompoundStmt() { |
| 246 | PopCompoundScope(); |
| 247 | } |
| 248 | |
| 249 | sema::CompoundScopeInfo &Sema::getCurCompoundScope() const { |
| 250 | return getCurFunction()->CompoundScopes.back(); |
| 251 | } |
| 252 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 253 | StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 254 | Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 255 | MultiStmtArg elts, bool isStmtExpr) { |
| 256 | unsigned NumElts = elts.size(); |
| 257 | Stmt **Elts = reinterpret_cast<Stmt**>(elts.release()); |
Chris Lattner | c30ebfb | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 258 | // If we're in C89 mode, check that we don't have any decls after stmts. If |
| 259 | // so, emit an extension diagnostic. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 260 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus) { |
Chris Lattner | c30ebfb | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 261 | // Note that __extension__ can be around a decl. |
| 262 | unsigned i = 0; |
| 263 | // Skip over all declarations. |
| 264 | for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) |
| 265 | /*empty*/; |
| 266 | |
| 267 | // We found the end of the list or a statement. Scan for another declstmt. |
| 268 | for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) |
| 269 | /*empty*/; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | |
Chris Lattner | c30ebfb | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 271 | if (i != NumElts) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 272 | Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin(); |
Chris Lattner | c30ebfb | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 273 | Diag(D->getLocation(), diag::ext_mixed_decls_code); |
| 274 | } |
| 275 | } |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 276 | // Warn about unused expressions in statements. |
| 277 | for (unsigned i = 0; i != NumElts; ++i) { |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 278 | // Ignore statements that are last in a statement expression. |
| 279 | if (isStmtExpr && i == NumElts - 1) |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 280 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Anders Carlsson | 636463e | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 282 | DiagnoseUnusedExprResult(Elts[i]); |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 283 | } |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 284 | |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 285 | // Check for suspicious empty body (null statement) in `for' and `while' |
| 286 | // statements. Don't do anything for template instantiations, this just adds |
| 287 | // noise. |
| 288 | if (NumElts != 0 && !CurrentInstantiationScope && |
| 289 | getCurCompoundScope().HasEmptyLoopBodies) { |
| 290 | for (unsigned i = 0; i != NumElts - 1; ++i) |
| 291 | DiagnoseEmptyLoopBody(Elts[i], Elts[i + 1]); |
| 292 | } |
| 293 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 294 | return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | } |
| 296 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 297 | StmtResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 298 | Sema::ActOnCaseStmt(SourceLocation CaseLoc, Expr *LHSVal, |
| 299 | SourceLocation DotDotDotLoc, Expr *RHSVal, |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 300 | SourceLocation ColonLoc) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 301 | assert((LHSVal != 0) && "missing expression in case statement"); |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 302 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 303 | if (getCurFunction()->SwitchStack.empty()) { |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 304 | Diag(CaseLoc, diag::err_case_not_in_switch); |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 305 | return StmtError(); |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 306 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 308 | if (!getLangOpts().CPlusPlus0x) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 309 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
| 310 | // However, GCC allows any evaluatable integer expression. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 311 | if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent()) { |
| 312 | LHSVal = VerifyIntegerConstantExpression(LHSVal).take(); |
| 313 | if (!LHSVal) |
| 314 | return StmtError(); |
| 315 | } |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 316 | |
| 317 | // GCC extension: The expression shall be an integer constant. |
| 318 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 319 | if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent()) { |
| 320 | RHSVal = VerifyIntegerConstantExpression(RHSVal).take(); |
| 321 | // Recover from an error by just forgetting about it. |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 325 | CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc, |
| 326 | ColonLoc); |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 327 | getCurFunction()->SwitchStack.back()->addSwitchCase(CS); |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 328 | return Owned(CS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 331 | /// ActOnCaseStmtBody - This installs a statement as the body of a case. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 332 | void Sema::ActOnCaseStmtBody(Stmt *caseStmt, Stmt *SubStmt) { |
Chandler Carruth | 5440bfa | 2011-08-18 02:04:29 +0000 | [diff] [blame] | 333 | DiagnoseUnusedExprResult(SubStmt); |
| 334 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 335 | CaseStmt *CS = static_cast<CaseStmt*>(caseStmt); |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 336 | CS->setSubStmt(SubStmt); |
| 337 | } |
| 338 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 339 | StmtResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 341 | Stmt *SubStmt, Scope *CurScope) { |
Chandler Carruth | 5440bfa | 2011-08-18 02:04:29 +0000 | [diff] [blame] | 342 | DiagnoseUnusedExprResult(SubStmt); |
| 343 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 344 | if (getCurFunction()->SwitchStack.empty()) { |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 345 | Diag(DefaultLoc, diag::err_default_not_in_switch); |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 346 | return Owned(SubStmt); |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 347 | } |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 348 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 349 | DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt); |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 350 | getCurFunction()->SwitchStack.back()->addSwitchCase(DS); |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 351 | return Owned(DS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 352 | } |
| 353 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 354 | StmtResult |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 355 | Sema::ActOnLabelStmt(SourceLocation IdentLoc, LabelDecl *TheDecl, |
| 356 | SourceLocation ColonLoc, Stmt *SubStmt) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 357 | // If the label was multiply defined, reject it now. |
| 358 | if (TheDecl->getStmt()) { |
| 359 | Diag(IdentLoc, diag::err_redefinition_of_label) << TheDecl->getDeclName(); |
| 360 | Diag(TheDecl->getLocation(), diag::note_previous_definition); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 361 | return Owned(SubStmt); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | } |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 363 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 364 | // Otherwise, things are good. Fill in the declaration and return it. |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 365 | LabelStmt *LS = new (Context) LabelStmt(IdentLoc, TheDecl, SubStmt); |
| 366 | TheDecl->setStmt(LS); |
Abramo Bagnara | 203548b | 2011-03-03 18:24:14 +0000 | [diff] [blame] | 367 | if (!TheDecl->isGnuLocal()) |
| 368 | TheDecl->setLocation(IdentLoc); |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 369 | return Owned(LS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 372 | StmtResult Sema::ActOnAttributedStmt(SourceLocation AttrLoc, |
| 373 | const AttrVec &Attrs, |
| 374 | Stmt *SubStmt) { |
| 375 | // Fill in the declaration and return it. Variable length will require to |
| 376 | // change this to AttributedStmt::Create(Context, ....); |
| 377 | // and probably using ArrayRef |
| 378 | AttributedStmt *LS = new (Context) AttributedStmt(AttrLoc, Attrs, SubStmt); |
| 379 | return Owned(LS); |
| 380 | } |
| 381 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 382 | StmtResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 383 | Sema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, Decl *CondVar, |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 384 | Stmt *thenStmt, SourceLocation ElseLoc, |
| 385 | Stmt *elseStmt) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 386 | ExprResult CondResult(CondVal.release()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 388 | VarDecl *ConditionVar = 0; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 389 | if (CondVar) { |
| 390 | ConditionVar = cast<VarDecl>(CondVar); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 391 | CondResult = CheckConditionVariable(ConditionVar, IfLoc, true); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 392 | if (CondResult.isInvalid()) |
| 393 | return StmtError(); |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 394 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 395 | Expr *ConditionExpr = CondResult.takeAs<Expr>(); |
| 396 | if (!ConditionExpr) |
| 397 | return StmtError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 398 | |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 399 | DiagnoseUnusedExprResult(thenStmt); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 400 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 401 | if (!elseStmt) { |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 402 | DiagnoseEmptyStmtBody(ConditionExpr->getLocEnd(), thenStmt, |
| 403 | diag::warn_empty_if_body); |
Anders Carlsson | 2d85f8b | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 406 | DiagnoseUnusedExprResult(elseStmt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 408 | return Owned(new (Context) IfStmt(Context, IfLoc, ConditionVar, ConditionExpr, |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 409 | thenStmt, ElseLoc, elseStmt)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 412 | /// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have |
| 413 | /// the specified width and sign. If an overflow occurs, detect it and emit |
| 414 | /// the specified diagnostic. |
| 415 | void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, |
| 416 | unsigned NewWidth, bool NewSign, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | SourceLocation Loc, |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 418 | unsigned DiagID) { |
| 419 | // Perform a conversion to the promoted condition type if needed. |
| 420 | if (NewWidth > Val.getBitWidth()) { |
| 421 | // If this is an extension, just do it. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 422 | Val = Val.extend(NewWidth); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 423 | Val.setIsSigned(NewSign); |
Douglas Gregor | f9f627d | 2010-03-01 01:04:55 +0000 | [diff] [blame] | 424 | |
| 425 | // If the input was signed and negative and the output is |
| 426 | // unsigned, don't bother to warn: this is implementation-defined |
| 427 | // behavior. |
| 428 | // FIXME: Introduce a second, default-ignored warning for this case? |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 429 | } else if (NewWidth < Val.getBitWidth()) { |
| 430 | // If this is a truncation, check for overflow. |
| 431 | llvm::APSInt ConvVal(Val); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 432 | ConvVal = ConvVal.trunc(NewWidth); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 433 | ConvVal.setIsSigned(NewSign); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 434 | ConvVal = ConvVal.extend(Val.getBitWidth()); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 435 | ConvVal.setIsSigned(Val.isSigned()); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 436 | if (ConvVal != Val) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 437 | Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 439 | // Regardless of whether a diagnostic was emitted, really do the |
| 440 | // truncation. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 441 | Val = Val.trunc(NewWidth); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 442 | Val.setIsSigned(NewSign); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 443 | } else if (NewSign != Val.isSigned()) { |
| 444 | // Convert the sign to match the sign of the condition. This can cause |
| 445 | // overflow as well: unsigned(INTMIN) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 446 | // We don't diagnose this overflow, because it is implementation-defined |
Douglas Gregor | 2853eac | 2010-02-18 00:56:01 +0000 | [diff] [blame] | 447 | // behavior. |
| 448 | // FIXME: Introduce a second, default-ignored warning for this case? |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 449 | llvm::APSInt OldVal(Val); |
| 450 | Val.setIsSigned(NewSign); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 454 | namespace { |
| 455 | struct CaseCompareFunctor { |
| 456 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 457 | const llvm::APSInt &RHS) { |
| 458 | return LHS.first < RHS; |
| 459 | } |
Chris Lattner | 0e85a27 | 2007-09-03 18:31:57 +0000 | [diff] [blame] | 460 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 461 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 462 | return LHS.first < RHS.first; |
| 463 | } |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 464 | bool operator()(const llvm::APSInt &LHS, |
| 465 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 466 | return LHS < RHS.first; |
| 467 | } |
| 468 | }; |
| 469 | } |
| 470 | |
Chris Lattner | 764a7ce | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 471 | /// CmpCaseVals - Comparison predicate for sorting case values. |
| 472 | /// |
| 473 | static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, |
| 474 | const std::pair<llvm::APSInt, CaseStmt*>& rhs) { |
| 475 | if (lhs.first < rhs.first) |
| 476 | return true; |
| 477 | |
| 478 | if (lhs.first == rhs.first && |
| 479 | lhs.second->getCaseLoc().getRawEncoding() |
| 480 | < rhs.second->getCaseLoc().getRawEncoding()) |
| 481 | return true; |
| 482 | return false; |
| 483 | } |
| 484 | |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 485 | /// CmpEnumVals - Comparison predicate for sorting enumeration values. |
| 486 | /// |
| 487 | static bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, |
| 488 | const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) |
| 489 | { |
| 490 | return lhs.first < rhs.first; |
| 491 | } |
| 492 | |
| 493 | /// EqEnumVals - Comparison preficate for uniqing enumeration values. |
| 494 | /// |
| 495 | static bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, |
| 496 | const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) |
| 497 | { |
| 498 | return lhs.first == rhs.first; |
| 499 | } |
| 500 | |
Chris Lattner | 5f04881 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 501 | /// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of |
| 502 | /// potentially integral-promoted expression @p expr. |
John McCall | a8e0cd8 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 503 | static QualType GetTypeBeforeIntegralPromotion(Expr *&expr) { |
| 504 | if (ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(expr)) |
| 505 | expr = cleanups->getSubExpr(); |
| 506 | while (ImplicitCastExpr *impcast = dyn_cast<ImplicitCastExpr>(expr)) { |
| 507 | if (impcast->getCastKind() != CK_IntegralCast) break; |
| 508 | expr = impcast->getSubExpr(); |
Chris Lattner | 5f04881 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 509 | } |
| 510 | return expr->getType(); |
| 511 | } |
| 512 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 513 | StmtResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 514 | Sema::ActOnStartOfSwitchStmt(SourceLocation SwitchLoc, Expr *Cond, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 515 | Decl *CondVar) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 516 | ExprResult CondResult; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 517 | |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 518 | VarDecl *ConditionVar = 0; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 519 | if (CondVar) { |
| 520 | ConditionVar = cast<VarDecl>(CondVar); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 521 | CondResult = CheckConditionVariable(ConditionVar, SourceLocation(), false); |
| 522 | if (CondResult.isInvalid()) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 523 | return StmtError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 524 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 525 | Cond = CondResult.release(); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 526 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 527 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 528 | if (!Cond) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 529 | return StmtError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 530 | |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 531 | class SwitchConvertDiagnoser : public ICEConvertDiagnoser { |
| 532 | Expr *Cond; |
| 533 | |
| 534 | public: |
| 535 | SwitchConvertDiagnoser(Expr *Cond) |
| 536 | : ICEConvertDiagnoser(false, true), Cond(Cond) { } |
| 537 | |
| 538 | virtual DiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 539 | QualType T) { |
| 540 | return S.Diag(Loc, diag::err_typecheck_statement_requires_integer) << T; |
| 541 | } |
| 542 | |
| 543 | virtual DiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, |
| 544 | QualType T) { |
| 545 | return S.Diag(Loc, diag::err_switch_incomplete_class_type) |
| 546 | << T << Cond->getSourceRange(); |
| 547 | } |
| 548 | |
| 549 | virtual DiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, |
| 550 | QualType T, |
| 551 | QualType ConvTy) { |
| 552 | return S.Diag(Loc, diag::err_switch_explicit_conversion) << T << ConvTy; |
| 553 | } |
| 554 | |
| 555 | virtual DiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, |
| 556 | QualType ConvTy) { |
| 557 | return S.Diag(Conv->getLocation(), diag::note_switch_conversion) |
| 558 | << ConvTy->isEnumeralType() << ConvTy; |
| 559 | } |
| 560 | |
| 561 | virtual DiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 562 | QualType T) { |
| 563 | return S.Diag(Loc, diag::err_switch_multiple_conversions) << T; |
| 564 | } |
| 565 | |
| 566 | virtual DiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, |
| 567 | QualType ConvTy) { |
| 568 | return S.Diag(Conv->getLocation(), diag::note_switch_conversion) |
| 569 | << ConvTy->isEnumeralType() << ConvTy; |
| 570 | } |
| 571 | |
| 572 | virtual DiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, |
| 573 | QualType T, |
| 574 | QualType ConvTy) { |
| 575 | return DiagnosticBuilder::getEmpty(); |
| 576 | } |
| 577 | } SwitchDiagnoser(Cond); |
| 578 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 579 | CondResult |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 580 | = ConvertToIntegralOrEnumerationType(SwitchLoc, Cond, SwitchDiagnoser, |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 581 | /*AllowScopedEnumerations*/ true); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 582 | if (CondResult.isInvalid()) return StmtError(); |
| 583 | Cond = CondResult.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 584 | |
John McCall | a8e0cd8 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 585 | // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. |
| 586 | CondResult = UsualUnaryConversions(Cond); |
| 587 | if (CondResult.isInvalid()) return StmtError(); |
| 588 | Cond = CondResult.take(); |
| 589 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 590 | if (!CondVar) { |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 591 | CheckImplicitConversions(Cond, SwitchLoc); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 592 | CondResult = MaybeCreateExprWithCleanups(Cond); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 593 | if (CondResult.isInvalid()) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 594 | return StmtError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 595 | Cond = CondResult.take(); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 596 | } |
John McCall | b60a77e | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 597 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 598 | getCurFunction()->setHasBranchIntoScope(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 599 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 600 | SwitchStmt *SS = new (Context) SwitchStmt(Context, ConditionVar, Cond); |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 601 | getCurFunction()->SwitchStack.push_back(SS); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 602 | return Owned(SS); |
Chris Lattner | 7e52de4 | 2010-01-24 01:50:29 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Gabor Greif | 28164ab | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 605 | static void AdjustAPSInt(llvm::APSInt &Val, unsigned BitWidth, bool IsSigned) { |
| 606 | if (Val.getBitWidth() < BitWidth) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 607 | Val = Val.extend(BitWidth); |
Gabor Greif | 28164ab | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 608 | else if (Val.getBitWidth() > BitWidth) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 609 | Val = Val.trunc(BitWidth); |
Gabor Greif | 28164ab | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 610 | Val.setIsSigned(IsSigned); |
| 611 | } |
| 612 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 613 | StmtResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 614 | Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch, |
| 615 | Stmt *BodyStmt) { |
| 616 | SwitchStmt *SS = cast<SwitchStmt>(Switch); |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 617 | assert(SS == getCurFunction()->SwitchStack.back() && |
| 618 | "switch stack missing push/pop!"); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 619 | |
Steve Naroff | 9dcbfa4 | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 620 | SS->setBody(BodyStmt, SwitchLoc); |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 621 | getCurFunction()->SwitchStack.pop_back(); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 622 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 623 | Expr *CondExpr = SS->getCond(); |
John McCall | a8e0cd8 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 624 | if (!CondExpr) return StmtError(); |
| 625 | |
| 626 | QualType CondType = CondExpr->getType(); |
| 627 | |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 628 | Expr *CondExprBeforePromotion = CondExpr; |
Douglas Gregor | 84fb9c0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 629 | QualType CondTypeBeforePromotion = |
John McCall | a8e0cd8 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 630 | GetTypeBeforeIntegralPromotion(CondExprBeforePromotion); |
Douglas Gregor | 84fb9c0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 631 | |
Chris Lattner | 5f04881 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 632 | // C++ 6.4.2.p2: |
| 633 | // Integral promotions are performed (on the switch condition). |
| 634 | // |
| 635 | // A case value unrepresentable by the original switch condition |
| 636 | // type (before the promotion) doesn't make sense, even when it can |
| 637 | // be represented by the promoted type. Therefore we need to find |
| 638 | // the pre-promotion type of the switch condition. |
Edward O'Callaghan | 12356b1 | 2009-10-17 19:32:54 +0000 | [diff] [blame] | 639 | if (!CondExpr->isTypeDependent()) { |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 640 | // We have already converted the expression to an integral or enumeration |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 641 | // type, when we started the switch statement. If we don't have an |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 642 | // appropriate type now, just return an error. |
| 643 | if (!CondType->isIntegralOrEnumerationType()) |
Edward O'Callaghan | 12356b1 | 2009-10-17 19:32:54 +0000 | [diff] [blame] | 644 | return StmtError(); |
Edward O'Callaghan | 12356b1 | 2009-10-17 19:32:54 +0000 | [diff] [blame] | 645 | |
Chris Lattner | 2b334bb | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 646 | if (CondExpr->isKnownToHaveBooleanValue()) { |
Edward O'Callaghan | 12356b1 | 2009-10-17 19:32:54 +0000 | [diff] [blame] | 647 | // switch(bool_expr) {...} is often a programmer error, e.g. |
| 648 | // switch(n && mask) { ... } // Doh - should be "n & mask". |
| 649 | // One can always use an if statement instead of switch(bool_expr). |
| 650 | Diag(SwitchLoc, diag::warn_bool_switch_condition) |
| 651 | << CondExpr->getSourceRange(); |
| 652 | } |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 653 | } |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 654 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 655 | // Get the bitwidth of the switched-on value before promotions. We must |
| 656 | // convert the integer case values to this width before comparison. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | bool HasDependentValue |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 658 | = CondExpr->isTypeDependent() || CondExpr->isValueDependent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | unsigned CondWidth |
Chris Lattner | 1d6ab7a | 2011-02-24 07:31:28 +0000 | [diff] [blame] | 660 | = HasDependentValue ? 0 : Context.getIntWidth(CondTypeBeforePromotion); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 661 | bool CondIsSigned |
| 662 | = CondTypeBeforePromotion->isSignedIntegerOrEnumerationType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 663 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 664 | // Accumulate all of the case values in a vector so that we can sort them |
| 665 | // and detect duplicates. This vector contains the APInt for the case after |
| 666 | // it has been converted to the condition type. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 667 | typedef SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 668 | CaseValsTy CaseVals; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 669 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 670 | // 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] | 671 | typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy; |
| 672 | CaseRangesTy CaseRanges; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 674 | DefaultStmt *TheDefaultStmt = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 676 | bool CaseListIsErroneous = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 677 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 678 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 679 | SC = SC->getNextSwitchCase()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 681 | if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 682 | if (TheDefaultStmt) { |
| 683 | Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 684 | Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 685 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 686 | // FIXME: Remove the default statement from the switch block so that |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 687 | // we'll return a valid AST. This requires recursing down the AST and |
| 688 | // finding it, not something we are set up to do right now. For now, |
| 689 | // just lop the entire switch stmt out of the AST. |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 690 | CaseListIsErroneous = true; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 691 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 692 | TheDefaultStmt = DS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 694 | } else { |
| 695 | CaseStmt *CS = cast<CaseStmt>(SC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 697 | Expr *Lo = CS->getLHS(); |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 698 | |
| 699 | if (Lo->isTypeDependent() || Lo->isValueDependent()) { |
| 700 | HasDependentValue = true; |
| 701 | break; |
| 702 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 704 | llvm::APSInt LoVal; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 705 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 706 | if (getLangOpts().CPlusPlus0x) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 707 | // C++11 [stmt.switch]p2: the constant-expression shall be a converted |
| 708 | // constant expression of the promoted type of the switch condition. |
| 709 | ExprResult ConvLo = |
| 710 | CheckConvertedConstantExpression(Lo, CondType, LoVal, CCEK_CaseValue); |
| 711 | if (ConvLo.isInvalid()) { |
| 712 | CaseListIsErroneous = true; |
| 713 | continue; |
| 714 | } |
| 715 | Lo = ConvLo.take(); |
| 716 | } else { |
| 717 | // We already verified that the expression has a i-c-e value (C99 |
| 718 | // 6.8.4.2p3) - get that value now. |
| 719 | LoVal = Lo->EvaluateKnownConstInt(Context); |
| 720 | |
| 721 | // If the LHS is not the same type as the condition, insert an implicit |
| 722 | // cast. |
| 723 | Lo = DefaultLvalueConversion(Lo).take(); |
| 724 | Lo = ImpCastExprToType(Lo, CondType, CK_IntegralCast).take(); |
| 725 | } |
| 726 | |
| 727 | // Convert the value to the same width/sign as the condition had prior to |
| 728 | // integral promotions. |
| 729 | // |
| 730 | // FIXME: This causes us to reject valid code: |
| 731 | // switch ((char)c) { case 256: case 0: return 0; } |
| 732 | // Here we claim there is a duplicated condition value, but there is not. |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 733 | ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, |
Gabor Greif | 28164ab | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 734 | Lo->getLocStart(), |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 735 | diag::warn_case_value_overflow); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 736 | |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 737 | CS->setLHS(Lo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 738 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 739 | // If this is a case range, remember it in CaseRanges, otherwise CaseVals. |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 740 | if (CS->getRHS()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | if (CS->getRHS()->isTypeDependent() || |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 742 | CS->getRHS()->isValueDependent()) { |
| 743 | HasDependentValue = true; |
| 744 | break; |
| 745 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 746 | CaseRanges.push_back(std::make_pair(LoVal, CS)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | } else |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 748 | CaseVals.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 751 | |
| 752 | if (!HasDependentValue) { |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 753 | // If we don't have a default statement, check whether the |
| 754 | // condition is constant. |
| 755 | llvm::APSInt ConstantCondValue; |
| 756 | bool HasConstantCond = false; |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 757 | if (!HasDependentValue && !TheDefaultStmt) { |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 758 | HasConstantCond |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 759 | = CondExprBeforePromotion->EvaluateAsInt(ConstantCondValue, Context, |
| 760 | Expr::SE_AllowSideEffects); |
| 761 | assert(!HasConstantCond || |
| 762 | (ConstantCondValue.getBitWidth() == CondWidth && |
| 763 | ConstantCondValue.isSigned() == CondIsSigned)); |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 764 | } |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 765 | bool ShouldCheckConstantCond = HasConstantCond; |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 766 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 767 | // Sort all the scalar case values so we can easily detect duplicates. |
| 768 | std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); |
| 769 | |
| 770 | if (!CaseVals.empty()) { |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 771 | for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) { |
| 772 | if (ShouldCheckConstantCond && |
| 773 | CaseVals[i].first == ConstantCondValue) |
| 774 | ShouldCheckConstantCond = false; |
| 775 | |
| 776 | if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) { |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 777 | // If we have a duplicate, report it. |
Douglas Gregor | 3940ce8 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 778 | // First, determine if either case value has a name |
| 779 | StringRef PrevString, CurrString; |
| 780 | Expr *PrevCase = CaseVals[i-1].second->getLHS()->IgnoreParenCasts(); |
| 781 | Expr *CurrCase = CaseVals[i].second->getLHS()->IgnoreParenCasts(); |
| 782 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(PrevCase)) { |
| 783 | PrevString = DeclRef->getDecl()->getName(); |
| 784 | } |
| 785 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(CurrCase)) { |
| 786 | CurrString = DeclRef->getDecl()->getName(); |
| 787 | } |
Douglas Gregor | 50de5e3 | 2012-05-16 16:11:17 +0000 | [diff] [blame] | 788 | llvm::SmallString<16> CaseValStr; |
| 789 | CaseVals[i-1].first.toString(CaseValStr); |
Douglas Gregor | 3940ce8 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 790 | |
| 791 | if (PrevString == CurrString) |
| 792 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
| 793 | diag::err_duplicate_case) << |
Douglas Gregor | 50de5e3 | 2012-05-16 16:11:17 +0000 | [diff] [blame] | 794 | (PrevString.empty() ? CaseValStr.str() : PrevString); |
Douglas Gregor | 3940ce8 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 795 | else |
| 796 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
| 797 | diag::err_duplicate_case_differing_expr) << |
Douglas Gregor | 50de5e3 | 2012-05-16 16:11:17 +0000 | [diff] [blame] | 798 | (PrevString.empty() ? CaseValStr.str() : PrevString) << |
| 799 | (CurrString.empty() ? CaseValStr.str() : CurrString) << |
Douglas Gregor | 3940ce8 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 800 | CaseValStr; |
| 801 | |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 802 | Diag(CaseVals[i-1].second->getLHS()->getLocStart(), |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 803 | diag::note_duplicate_case_prev); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 804 | // FIXME: We really want to remove the bogus case stmt from the |
| 805 | // substmt, but we have no way to do this right now. |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 806 | CaseListIsErroneous = true; |
| 807 | } |
| 808 | } |
| 809 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 811 | // Detect duplicate case ranges, which usually don't exist at all in |
| 812 | // the first place. |
| 813 | if (!CaseRanges.empty()) { |
| 814 | // Sort all the case ranges by their low value so we can easily detect |
| 815 | // overlaps between ranges. |
| 816 | std::stable_sort(CaseRanges.begin(), CaseRanges.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 818 | // Scan the ranges, computing the high values and removing empty ranges. |
| 819 | std::vector<llvm::APSInt> HiVals; |
| 820 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 821 | llvm::APSInt &LoVal = CaseRanges[i].first; |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 822 | CaseStmt *CR = CaseRanges[i].second; |
| 823 | Expr *Hi = CR->getRHS(); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 824 | llvm::APSInt HiVal; |
| 825 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 826 | if (getLangOpts().CPlusPlus0x) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 827 | // C++11 [stmt.switch]p2: the constant-expression shall be a converted |
| 828 | // constant expression of the promoted type of the switch condition. |
| 829 | ExprResult ConvHi = |
| 830 | CheckConvertedConstantExpression(Hi, CondType, HiVal, |
| 831 | CCEK_CaseValue); |
| 832 | if (ConvHi.isInvalid()) { |
| 833 | CaseListIsErroneous = true; |
| 834 | continue; |
| 835 | } |
| 836 | Hi = ConvHi.take(); |
| 837 | } else { |
| 838 | HiVal = Hi->EvaluateKnownConstInt(Context); |
| 839 | |
| 840 | // If the RHS is not the same type as the condition, insert an |
| 841 | // implicit cast. |
| 842 | Hi = DefaultLvalueConversion(Hi).take(); |
| 843 | Hi = ImpCastExprToType(Hi, CondType, CK_IntegralCast).take(); |
| 844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 846 | // Convert the value to the same width/sign as the condition. |
| 847 | ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, |
Gabor Greif | 28164ab | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 848 | Hi->getLocStart(), |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 849 | diag::warn_case_value_overflow); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 851 | CR->setRHS(Hi); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 853 | // If the low value is bigger than the high value, the case is empty. |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 854 | if (LoVal > HiVal) { |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 855 | Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range) |
| 856 | << SourceRange(CR->getLHS()->getLocStart(), |
Gabor Greif | 28164ab | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 857 | Hi->getLocEnd()); |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 858 | CaseRanges.erase(CaseRanges.begin()+i); |
| 859 | --i, --e; |
| 860 | continue; |
| 861 | } |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 862 | |
| 863 | if (ShouldCheckConstantCond && |
| 864 | LoVal <= ConstantCondValue && |
| 865 | ConstantCondValue <= HiVal) |
| 866 | ShouldCheckConstantCond = false; |
| 867 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 868 | HiVals.push_back(HiVal); |
| 869 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 870 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 871 | // Rescan the ranges, looking for overlap with singleton values and other |
| 872 | // ranges. Since the range list is sorted, we only need to compare case |
| 873 | // ranges with their neighbors. |
| 874 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
| 875 | llvm::APSInt &CRLo = CaseRanges[i].first; |
| 876 | llvm::APSInt &CRHi = HiVals[i]; |
| 877 | CaseStmt *CR = CaseRanges[i].second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 879 | // Check to see whether the case range overlaps with any |
| 880 | // singleton cases. |
| 881 | CaseStmt *OverlapStmt = 0; |
| 882 | llvm::APSInt OverlapVal(32); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 884 | // Find the smallest value >= the lower bound. If I is in the |
| 885 | // case range, then we have overlap. |
| 886 | CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), |
| 887 | CaseVals.end(), CRLo, |
| 888 | CaseCompareFunctor()); |
| 889 | if (I != CaseVals.end() && I->first < CRHi) { |
| 890 | OverlapVal = I->first; // Found overlap with scalar. |
| 891 | OverlapStmt = I->second; |
| 892 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 894 | // Find the smallest value bigger than the upper bound. |
| 895 | I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); |
| 896 | if (I != CaseVals.begin() && (I-1)->first >= CRLo) { |
| 897 | OverlapVal = (I-1)->first; // Found overlap with scalar. |
| 898 | OverlapStmt = (I-1)->second; |
| 899 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 901 | // Check to see if this case stmt overlaps with the subsequent |
| 902 | // case range. |
| 903 | if (i && CRLo <= HiVals[i-1]) { |
| 904 | OverlapVal = HiVals[i-1]; // Found overlap with range. |
| 905 | OverlapStmt = CaseRanges[i-1].second; |
| 906 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 907 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 908 | if (OverlapStmt) { |
| 909 | // If we have a duplicate, report it. |
| 910 | Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case) |
| 911 | << OverlapVal.toString(10); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 912 | Diag(OverlapStmt->getLHS()->getLocStart(), |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 913 | diag::note_duplicate_case_prev); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 914 | // FIXME: We really want to remove the bogus case stmt from the |
| 915 | // substmt, but we have no way to do this right now. |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 916 | CaseListIsErroneous = true; |
| 917 | } |
Chris Lattner | f334850 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 918 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 919 | } |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 920 | |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 921 | // Complain if we have a constant condition and we didn't find a match. |
| 922 | if (!CaseListIsErroneous && ShouldCheckConstantCond) { |
| 923 | // TODO: it would be nice if we printed enums as enums, chars as |
| 924 | // chars, etc. |
| 925 | Diag(CondExpr->getExprLoc(), diag::warn_missing_case_for_condition) |
| 926 | << ConstantCondValue.toString(10) |
| 927 | << CondExpr->getSourceRange(); |
| 928 | } |
| 929 | |
| 930 | // Check to see if switch is over an Enum and handles all of its |
Ted Kremenek | 559fb55 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 931 | // values. We only issue a warning if there is not 'default:', but |
| 932 | // we still do the analysis to preserve this information in the AST |
| 933 | // (which can be used by flow-based analyes). |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 934 | // |
Chris Lattner | ce78461 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 935 | const EnumType *ET = CondTypeBeforePromotion->getAs<EnumType>(); |
Ted Kremenek | 559fb55 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 936 | |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 937 | // If switch has default case, then ignore it. |
Ted Kremenek | 559fb55 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 938 | if (!CaseListIsErroneous && !HasConstantCond && ET) { |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 939 | const EnumDecl *ED = ET->getDecl(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 940 | typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> |
Francois Pichet | 58f14c0 | 2011-06-02 00:47:27 +0000 | [diff] [blame] | 941 | EnumValsTy; |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 942 | EnumValsTy EnumVals; |
| 943 | |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 944 | // Gather all enum values, set their type and sort them, |
| 945 | // allowing easier comparison with CaseVals. |
| 946 | for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin(); |
Gabor Greif | 28164ab | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 947 | EDI != ED->enumerator_end(); ++EDI) { |
| 948 | llvm::APSInt Val = EDI->getInitVal(); |
| 949 | AdjustAPSInt(Val, CondWidth, CondIsSigned); |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 950 | EnumVals.push_back(std::make_pair(Val, &*EDI)); |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 951 | } |
| 952 | std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals); |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 953 | EnumValsTy::iterator EIend = |
| 954 | std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals); |
Ted Kremenek | 559fb55 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 955 | |
| 956 | // See which case values aren't in enum. |
David Blaikie | 9366750 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 957 | EnumValsTy::const_iterator EI = EnumVals.begin(); |
| 958 | for (CaseValsTy::const_iterator CI = CaseVals.begin(); |
| 959 | CI != CaseVals.end(); CI++) { |
| 960 | while (EI != EIend && EI->first < CI->first) |
| 961 | EI++; |
| 962 | if (EI == EIend || EI->first > CI->first) |
| 963 | Diag(CI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum) |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 964 | << CondTypeBeforePromotion; |
David Blaikie | 9366750 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 965 | } |
| 966 | // See which of case ranges aren't in enum |
| 967 | EI = EnumVals.begin(); |
| 968 | for (CaseRangesTy::const_iterator RI = CaseRanges.begin(); |
| 969 | RI != CaseRanges.end() && EI != EIend; RI++) { |
| 970 | while (EI != EIend && EI->first < RI->first) |
| 971 | EI++; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 972 | |
David Blaikie | 9366750 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 973 | if (EI == EIend || EI->first != RI->first) { |
| 974 | Diag(RI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum) |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 975 | << CondTypeBeforePromotion; |
Ted Kremenek | 47bb27f | 2010-09-09 06:53:59 +0000 | [diff] [blame] | 976 | } |
David Blaikie | 9366750 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 977 | |
| 978 | llvm::APSInt Hi = |
| 979 | RI->second->getRHS()->EvaluateKnownConstInt(Context); |
| 980 | AdjustAPSInt(Hi, CondWidth, CondIsSigned); |
| 981 | while (EI != EIend && EI->first < Hi) |
| 982 | EI++; |
| 983 | if (EI == EIend || EI->first != Hi) |
| 984 | Diag(RI->second->getRHS()->getExprLoc(), diag::warn_not_in_enum) |
Fariborz Jahanian | 54faba4 | 2012-03-21 20:56:29 +0000 | [diff] [blame] | 985 | << CondTypeBeforePromotion; |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 986 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 987 | |
Ted Kremenek | 559fb55 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 988 | // Check which enum vals aren't in switch |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 989 | CaseValsTy::const_iterator CI = CaseVals.begin(); |
| 990 | CaseRangesTy::const_iterator RI = CaseRanges.begin(); |
Ted Kremenek | 559fb55 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 991 | bool hasCasesNotInSwitch = false; |
| 992 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 993 | SmallVector<DeclarationName,8> UnhandledNames; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 994 | |
David Blaikie | 9366750 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 995 | for (EI = EnumVals.begin(); EI != EIend; EI++){ |
Chris Lattner | ce78461 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 996 | // Drop unneeded case values |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 997 | llvm::APSInt CIVal; |
| 998 | while (CI != CaseVals.end() && CI->first < EI->first) |
| 999 | CI++; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1000 | |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1001 | if (CI != CaseVals.end() && CI->first == EI->first) |
| 1002 | continue; |
| 1003 | |
Ted Kremenek | 559fb55 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1004 | // Drop unneeded case ranges |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1005 | for (; RI != CaseRanges.end(); RI++) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1006 | llvm::APSInt Hi = |
| 1007 | RI->second->getRHS()->EvaluateKnownConstInt(Context); |
Gabor Greif | 28164ab | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 1008 | AdjustAPSInt(Hi, CondWidth, CondIsSigned); |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1009 | if (EI->first <= Hi) |
| 1010 | break; |
| 1011 | } |
| 1012 | |
Ted Kremenek | 559fb55 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1013 | if (RI == CaseRanges.end() || EI->first < RI->first) { |
Ted Kremenek | 47bb27f | 2010-09-09 06:53:59 +0000 | [diff] [blame] | 1014 | hasCasesNotInSwitch = true; |
David Blaikie | 31ceb61 | 2012-01-21 18:12:07 +0000 | [diff] [blame] | 1015 | UnhandledNames.push_back(EI->second->getDeclName()); |
Ted Kremenek | 47bb27f | 2010-09-09 06:53:59 +0000 | [diff] [blame] | 1016 | } |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1017 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1018 | |
David Blaikie | 585d779 | 2012-01-23 04:46:12 +0000 | [diff] [blame] | 1019 | if (TheDefaultStmt && UnhandledNames.empty()) |
| 1020 | Diag(TheDefaultStmt->getDefaultLoc(), diag::warn_unreachable_default); |
David Blaikie | 31ceb61 | 2012-01-21 18:12:07 +0000 | [diff] [blame] | 1021 | |
Chris Lattner | ce78461 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 1022 | // Produce a nice diagnostic if multiple values aren't handled. |
| 1023 | switch (UnhandledNames.size()) { |
| 1024 | case 0: break; |
| 1025 | case 1: |
David Blaikie | 585d779 | 2012-01-23 04:46:12 +0000 | [diff] [blame] | 1026 | Diag(CondExpr->getExprLoc(), TheDefaultStmt |
| 1027 | ? diag::warn_def_missing_case1 : diag::warn_missing_case1) |
Chris Lattner | ce78461 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 1028 | << UnhandledNames[0]; |
| 1029 | break; |
| 1030 | case 2: |
David Blaikie | 585d779 | 2012-01-23 04:46:12 +0000 | [diff] [blame] | 1031 | Diag(CondExpr->getExprLoc(), TheDefaultStmt |
| 1032 | ? diag::warn_def_missing_case2 : diag::warn_missing_case2) |
Chris Lattner | ce78461 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 1033 | << UnhandledNames[0] << UnhandledNames[1]; |
| 1034 | break; |
| 1035 | case 3: |
David Blaikie | 585d779 | 2012-01-23 04:46:12 +0000 | [diff] [blame] | 1036 | Diag(CondExpr->getExprLoc(), TheDefaultStmt |
| 1037 | ? diag::warn_def_missing_case3 : diag::warn_missing_case3) |
Chris Lattner | ce78461 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 1038 | << UnhandledNames[0] << UnhandledNames[1] << UnhandledNames[2]; |
| 1039 | break; |
| 1040 | default: |
David Blaikie | 585d779 | 2012-01-23 04:46:12 +0000 | [diff] [blame] | 1041 | Diag(CondExpr->getExprLoc(), TheDefaultStmt |
| 1042 | ? diag::warn_def_missing_cases : diag::warn_missing_cases) |
Chris Lattner | ce78461 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 1043 | << (unsigned)UnhandledNames.size() |
| 1044 | << UnhandledNames[0] << UnhandledNames[1] << UnhandledNames[2]; |
| 1045 | break; |
| 1046 | } |
Ted Kremenek | 559fb55 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (!hasCasesNotInSwitch) |
Ted Kremenek | 47bb27f | 2010-09-09 06:53:59 +0000 | [diff] [blame] | 1049 | SS->setAllEnumCasesCovered(); |
Douglas Gregor | ba915af | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1050 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 1051 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 1052 | |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1053 | DiagnoseEmptyStmtBody(CondExpr->getLocEnd(), BodyStmt, |
| 1054 | diag::warn_empty_switch_body); |
| 1055 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1056 | // FIXME: If the case list was broken is some way, we don't have a good system |
| 1057 | // to patch it up. Instead, just return the whole substmt as broken. |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 1058 | if (CaseListIsErroneous) |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 1059 | return StmtError(); |
| 1060 | |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 1061 | return Owned(SS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1064 | StmtResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1065 | Sema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1066 | Decl *CondVar, Stmt *Body) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1067 | ExprResult CondResult(Cond.release()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1068 | |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 1069 | VarDecl *ConditionVar = 0; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1070 | if (CondVar) { |
| 1071 | ConditionVar = cast<VarDecl>(CondVar); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1072 | CondResult = CheckConditionVariable(ConditionVar, WhileLoc, true); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1073 | if (CondResult.isInvalid()) |
| 1074 | return StmtError(); |
Douglas Gregor | 5656e14 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 1075 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1076 | Expr *ConditionExpr = CondResult.take(); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1077 | if (!ConditionExpr) |
| 1078 | return StmtError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1079 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1080 | DiagnoseUnusedExprResult(Body); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1082 | if (isa<NullStmt>(Body)) |
| 1083 | getCurCompoundScope().setHasEmptyLoopBodies(); |
| 1084 | |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 1085 | return Owned(new (Context) WhileStmt(Context, ConditionVar, ConditionExpr, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1086 | Body, WhileLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1089 | StmtResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1090 | Sema::ActOnDoStmt(SourceLocation DoLoc, Stmt *Body, |
Chris Lattner | 9891359 | 2009-06-12 23:04:47 +0000 | [diff] [blame] | 1091 | SourceLocation WhileLoc, SourceLocation CondLParen, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1092 | Expr *Cond, SourceLocation CondRParen) { |
| 1093 | assert(Cond && "ActOnDoStmt(): missing expression"); |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1094 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1095 | ExprResult CondResult = CheckBooleanCondition(Cond, DoLoc); |
| 1096 | if (CondResult.isInvalid() || CondResult.isInvalid()) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 1097 | return StmtError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1098 | Cond = CondResult.take(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1099 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1100 | CheckImplicitConversions(Cond, DoLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1101 | CondResult = MaybeCreateExprWithCleanups(Cond); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1102 | if (CondResult.isInvalid()) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1103 | return StmtError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1104 | Cond = CondResult.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1105 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1106 | DiagnoseUnusedExprResult(Body); |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 1107 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1108 | return Owned(new (Context) DoStmt(Body, Cond, DoLoc, WhileLoc, CondRParen)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Richard Trieu | 694e796 | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1111 | namespace { |
| 1112 | // This visitor will traverse a conditional statement and store all |
| 1113 | // the evaluated decls into a vector. Simple is set to true if none |
| 1114 | // of the excluded constructs are used. |
| 1115 | class DeclExtractor : public EvaluatedExprVisitor<DeclExtractor> { |
| 1116 | llvm::SmallPtrSet<VarDecl*, 8> &Decls; |
| 1117 | llvm::SmallVector<SourceRange, 10> &Ranges; |
| 1118 | bool Simple; |
| 1119 | PartialDiagnostic &PDiag; |
| 1120 | public: |
| 1121 | typedef EvaluatedExprVisitor<DeclExtractor> Inherited; |
| 1122 | |
| 1123 | DeclExtractor(Sema &S, llvm::SmallPtrSet<VarDecl*, 8> &Decls, |
| 1124 | llvm::SmallVector<SourceRange, 10> &Ranges, |
| 1125 | PartialDiagnostic &PDiag) : |
| 1126 | Inherited(S.Context), |
| 1127 | Decls(Decls), |
| 1128 | Ranges(Ranges), |
| 1129 | Simple(true), |
| 1130 | PDiag(PDiag) {} |
| 1131 | |
| 1132 | bool isSimple() { return Simple; } |
| 1133 | |
| 1134 | // Replaces the method in EvaluatedExprVisitor. |
| 1135 | void VisitMemberExpr(MemberExpr* E) { |
| 1136 | Simple = false; |
| 1137 | } |
| 1138 | |
| 1139 | // Any Stmt not whitelisted will cause the condition to be marked complex. |
| 1140 | void VisitStmt(Stmt *S) { |
| 1141 | Simple = false; |
| 1142 | } |
| 1143 | |
| 1144 | void VisitBinaryOperator(BinaryOperator *E) { |
| 1145 | Visit(E->getLHS()); |
| 1146 | Visit(E->getRHS()); |
| 1147 | } |
| 1148 | |
| 1149 | void VisitCastExpr(CastExpr *E) { |
| 1150 | Visit(E->getSubExpr()); |
| 1151 | } |
| 1152 | |
| 1153 | void VisitUnaryOperator(UnaryOperator *E) { |
| 1154 | // Skip checking conditionals with derefernces. |
| 1155 | if (E->getOpcode() == UO_Deref) |
| 1156 | Simple = false; |
| 1157 | else |
| 1158 | Visit(E->getSubExpr()); |
| 1159 | } |
| 1160 | |
| 1161 | void VisitConditionalOperator(ConditionalOperator *E) { |
| 1162 | Visit(E->getCond()); |
| 1163 | Visit(E->getTrueExpr()); |
| 1164 | Visit(E->getFalseExpr()); |
| 1165 | } |
| 1166 | |
| 1167 | void VisitParenExpr(ParenExpr *E) { |
| 1168 | Visit(E->getSubExpr()); |
| 1169 | } |
| 1170 | |
| 1171 | void VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { |
| 1172 | Visit(E->getOpaqueValue()->getSourceExpr()); |
| 1173 | Visit(E->getFalseExpr()); |
| 1174 | } |
| 1175 | |
| 1176 | void VisitIntegerLiteral(IntegerLiteral *E) { } |
| 1177 | void VisitFloatingLiteral(FloatingLiteral *E) { } |
| 1178 | void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { } |
| 1179 | void VisitCharacterLiteral(CharacterLiteral *E) { } |
| 1180 | void VisitGNUNullExpr(GNUNullExpr *E) { } |
| 1181 | void VisitImaginaryLiteral(ImaginaryLiteral *E) { } |
| 1182 | |
| 1183 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 1184 | VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()); |
| 1185 | if (!VD) return; |
| 1186 | |
| 1187 | Ranges.push_back(E->getSourceRange()); |
| 1188 | |
| 1189 | Decls.insert(VD); |
| 1190 | } |
| 1191 | |
| 1192 | }; // end class DeclExtractor |
| 1193 | |
| 1194 | // DeclMatcher checks to see if the decls are used in a non-evauluated |
| 1195 | // context. |
| 1196 | class DeclMatcher : public EvaluatedExprVisitor<DeclMatcher> { |
| 1197 | llvm::SmallPtrSet<VarDecl*, 8> &Decls; |
| 1198 | bool FoundDecl; |
| 1199 | //bool EvalDecl; |
| 1200 | |
| 1201 | public: |
| 1202 | typedef EvaluatedExprVisitor<DeclMatcher> Inherited; |
| 1203 | |
| 1204 | DeclMatcher(Sema &S, llvm::SmallPtrSet<VarDecl*, 8> &Decls, Stmt *Statement) : |
| 1205 | Inherited(S.Context), Decls(Decls), FoundDecl(false) { |
| 1206 | if (!Statement) return; |
| 1207 | |
| 1208 | Visit(Statement); |
| 1209 | } |
| 1210 | |
| 1211 | void VisitReturnStmt(ReturnStmt *S) { |
| 1212 | FoundDecl = true; |
| 1213 | } |
| 1214 | |
| 1215 | void VisitBreakStmt(BreakStmt *S) { |
| 1216 | FoundDecl = true; |
| 1217 | } |
| 1218 | |
| 1219 | void VisitGotoStmt(GotoStmt *S) { |
| 1220 | FoundDecl = true; |
| 1221 | } |
| 1222 | |
| 1223 | void VisitCastExpr(CastExpr *E) { |
| 1224 | if (E->getCastKind() == CK_LValueToRValue) |
| 1225 | CheckLValueToRValueCast(E->getSubExpr()); |
| 1226 | else |
| 1227 | Visit(E->getSubExpr()); |
| 1228 | } |
| 1229 | |
| 1230 | void CheckLValueToRValueCast(Expr *E) { |
| 1231 | E = E->IgnoreParenImpCasts(); |
| 1232 | |
| 1233 | if (isa<DeclRefExpr>(E)) { |
| 1234 | return; |
| 1235 | } |
| 1236 | |
| 1237 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 1238 | Visit(CO->getCond()); |
| 1239 | CheckLValueToRValueCast(CO->getTrueExpr()); |
| 1240 | CheckLValueToRValueCast(CO->getFalseExpr()); |
| 1241 | return; |
| 1242 | } |
| 1243 | |
| 1244 | if (BinaryConditionalOperator *BCO = |
| 1245 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 1246 | CheckLValueToRValueCast(BCO->getOpaqueValue()->getSourceExpr()); |
| 1247 | CheckLValueToRValueCast(BCO->getFalseExpr()); |
| 1248 | return; |
| 1249 | } |
| 1250 | |
| 1251 | Visit(E); |
| 1252 | } |
| 1253 | |
| 1254 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 1255 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
| 1256 | if (Decls.count(VD)) |
| 1257 | FoundDecl = true; |
| 1258 | } |
| 1259 | |
| 1260 | bool FoundDeclInUse() { return FoundDecl; } |
| 1261 | |
| 1262 | }; // end class DeclMatcher |
| 1263 | |
| 1264 | void CheckForLoopConditionalStatement(Sema &S, Expr *Second, |
| 1265 | Expr *Third, Stmt *Body) { |
| 1266 | // Condition is empty |
| 1267 | if (!Second) return; |
| 1268 | |
| 1269 | if (S.Diags.getDiagnosticLevel(diag::warn_variables_not_in_loop_body, |
| 1270 | Second->getLocStart()) |
| 1271 | == DiagnosticsEngine::Ignored) |
| 1272 | return; |
| 1273 | |
| 1274 | PartialDiagnostic PDiag = S.PDiag(diag::warn_variables_not_in_loop_body); |
| 1275 | llvm::SmallPtrSet<VarDecl*, 8> Decls; |
| 1276 | llvm::SmallVector<SourceRange, 10> Ranges; |
| 1277 | DeclExtractor DE(S, Decls, Ranges, PDiag); |
| 1278 | DE.Visit(Second); |
| 1279 | |
| 1280 | // Don't analyze complex conditionals. |
| 1281 | if (!DE.isSimple()) return; |
| 1282 | |
| 1283 | // No decls found. |
| 1284 | if (Decls.size() == 0) return; |
| 1285 | |
Richard Trieu | 9087599 | 2012-05-04 03:01:54 +0000 | [diff] [blame] | 1286 | // Don't warn on volatile, static, or global variables. |
Richard Trieu | 694e796 | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1287 | for (llvm::SmallPtrSet<VarDecl*, 8>::iterator I = Decls.begin(), |
| 1288 | E = Decls.end(); |
| 1289 | I != E; ++I) |
Richard Trieu | 9087599 | 2012-05-04 03:01:54 +0000 | [diff] [blame] | 1290 | if ((*I)->getType().isVolatileQualified() || |
| 1291 | (*I)->hasGlobalStorage()) return; |
Richard Trieu | 694e796 | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1292 | |
| 1293 | if (DeclMatcher(S, Decls, Second).FoundDeclInUse() || |
| 1294 | DeclMatcher(S, Decls, Third).FoundDeclInUse() || |
| 1295 | DeclMatcher(S, Decls, Body).FoundDeclInUse()) |
| 1296 | return; |
| 1297 | |
| 1298 | // Load decl names into diagnostic. |
| 1299 | if (Decls.size() > 4) |
| 1300 | PDiag << 0; |
| 1301 | else { |
| 1302 | PDiag << Decls.size(); |
| 1303 | for (llvm::SmallPtrSet<VarDecl*, 8>::iterator I = Decls.begin(), |
| 1304 | E = Decls.end(); |
| 1305 | I != E; ++I) |
| 1306 | PDiag << (*I)->getDeclName(); |
| 1307 | } |
| 1308 | |
| 1309 | // Load SourceRanges into diagnostic if there is room. |
| 1310 | // Otherwise, load the SourceRange of the conditional expression. |
| 1311 | if (Ranges.size() <= PartialDiagnostic::MaxArguments) |
| 1312 | for (llvm::SmallVector<SourceRange, 10>::iterator I = Ranges.begin(), |
| 1313 | E = Ranges.end(); |
| 1314 | I != E; ++I) |
| 1315 | PDiag << *I; |
| 1316 | else |
| 1317 | PDiag << Second->getSourceRange(); |
| 1318 | |
| 1319 | S.Diag(Ranges.begin()->getBegin(), PDiag); |
| 1320 | } |
| 1321 | |
| 1322 | } // end namespace |
| 1323 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1324 | StmtResult |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1325 | Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1326 | Stmt *First, FullExprArg second, Decl *secondVar, |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1327 | FullExprArg third, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1328 | SourceLocation RParenLoc, Stmt *Body) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1329 | if (!getLangOpts().CPlusPlus) { |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1330 | if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 1331 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only |
| 1332 | // declare identifiers for objects having storage class 'auto' or |
| 1333 | // 'register'. |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1334 | for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); |
| 1335 | DI!=DE; ++DI) { |
| 1336 | VarDecl *VD = dyn_cast<VarDecl>(*DI); |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1337 | if (VD && VD->isLocalVarDecl() && !VD->hasLocalStorage()) |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1338 | VD = 0; |
| 1339 | if (VD == 0) |
| 1340 | Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for); |
| 1341 | // FIXME: mark decl erroneous! |
| 1342 | } |
Chris Lattner | ae3b701 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 1343 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1344 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1345 | |
Richard Trieu | 694e796 | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1346 | CheckForLoopConditionalStatement(*this, second.get(), third.get(), Body); |
| 1347 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1348 | ExprResult SecondResult(second.release()); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1349 | VarDecl *ConditionVar = 0; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1350 | if (secondVar) { |
| 1351 | ConditionVar = cast<VarDecl>(secondVar); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1352 | SecondResult = CheckConditionVariable(ConditionVar, ForLoc, true); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1353 | if (SecondResult.isInvalid()) |
| 1354 | return StmtError(); |
| 1355 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1356 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1357 | Expr *Third = third.release().takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1358 | |
Anders Carlsson | 3af708f | 2009-08-01 01:39:59 +0000 | [diff] [blame] | 1359 | DiagnoseUnusedExprResult(First); |
| 1360 | DiagnoseUnusedExprResult(Third); |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 1361 | DiagnoseUnusedExprResult(Body); |
| 1362 | |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1363 | if (isa<NullStmt>(Body)) |
| 1364 | getCurCompoundScope().setHasEmptyLoopBodies(); |
| 1365 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1366 | return Owned(new (Context) ForStmt(Context, First, |
| 1367 | SecondResult.take(), ConditionVar, |
| 1368 | Third, Body, ForLoc, LParenLoc, |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 1369 | RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1372 | /// In an Objective C collection iteration statement: |
| 1373 | /// for (x in y) |
| 1374 | /// x can be an arbitrary l-value expression. Bind it up as a |
| 1375 | /// full-expression. |
| 1376 | StmtResult Sema::ActOnForEachLValueExpr(Expr *E) { |
John McCall | 29bbd1a | 2012-03-30 05:43:39 +0000 | [diff] [blame] | 1377 | // Reduce placeholder expressions here. Note that this rejects the |
| 1378 | // use of pseudo-object l-values in this position. |
| 1379 | ExprResult result = CheckPlaceholderExpr(E); |
| 1380 | if (result.isInvalid()) return StmtError(); |
| 1381 | E = result.take(); |
| 1382 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1383 | CheckImplicitConversions(E); |
John McCall | 29bbd1a | 2012-03-30 05:43:39 +0000 | [diff] [blame] | 1384 | |
| 1385 | result = MaybeCreateExprWithCleanups(E); |
| 1386 | if (result.isInvalid()) return StmtError(); |
| 1387 | |
| 1388 | return Owned(static_cast<Stmt*>(result.take())); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1391 | ExprResult |
| 1392 | Sema::ActOnObjCForCollectionOperand(SourceLocation forLoc, Expr *collection) { |
| 1393 | assert(collection); |
| 1394 | |
| 1395 | // Bail out early if we've got a type-dependent expression. |
| 1396 | if (collection->isTypeDependent()) return Owned(collection); |
| 1397 | |
| 1398 | // Perform normal l-value conversion. |
| 1399 | ExprResult result = DefaultFunctionArrayLvalueConversion(collection); |
| 1400 | if (result.isInvalid()) |
| 1401 | return ExprError(); |
| 1402 | collection = result.take(); |
| 1403 | |
| 1404 | // The operand needs to have object-pointer type. |
| 1405 | // TODO: should we do a contextual conversion? |
| 1406 | const ObjCObjectPointerType *pointerType = |
| 1407 | collection->getType()->getAs<ObjCObjectPointerType>(); |
| 1408 | if (!pointerType) |
| 1409 | return Diag(forLoc, diag::err_collection_expr_type) |
| 1410 | << collection->getType() << collection->getSourceRange(); |
| 1411 | |
| 1412 | // Check that the operand provides |
| 1413 | // - countByEnumeratingWithState:objects:count: |
| 1414 | const ObjCObjectType *objectType = pointerType->getObjectType(); |
| 1415 | ObjCInterfaceDecl *iface = objectType->getInterface(); |
| 1416 | |
| 1417 | // If we have a forward-declared type, we can't do this check. |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1418 | // Under ARC, it is an error not to have a forward-declared class. |
| 1419 | if (iface && |
| 1420 | RequireCompleteType(forLoc, QualType(objectType, 0), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1421 | getLangOpts().ObjCAutoRefCount |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1422 | ? diag::err_arc_collection_forward |
| 1423 | : 0, |
| 1424 | collection)) { |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1425 | // Otherwise, if we have any useful type information, check that |
| 1426 | // the type declares the appropriate method. |
| 1427 | } else if (iface || !objectType->qual_empty()) { |
| 1428 | IdentifierInfo *selectorIdents[] = { |
| 1429 | &Context.Idents.get("countByEnumeratingWithState"), |
| 1430 | &Context.Idents.get("objects"), |
| 1431 | &Context.Idents.get("count") |
| 1432 | }; |
| 1433 | Selector selector = Context.Selectors.getSelector(3, &selectorIdents[0]); |
| 1434 | |
| 1435 | ObjCMethodDecl *method = 0; |
| 1436 | |
| 1437 | // If there's an interface, look in both the public and private APIs. |
| 1438 | if (iface) { |
| 1439 | method = iface->lookupInstanceMethod(selector); |
| 1440 | if (!method) method = LookupPrivateInstanceMethod(selector, iface); |
| 1441 | } |
| 1442 | |
| 1443 | // Also check protocol qualifiers. |
| 1444 | if (!method) |
| 1445 | method = LookupMethodInQualifiedType(selector, pointerType, |
| 1446 | /*instance*/ true); |
| 1447 | |
| 1448 | // If we didn't find it anywhere, give up. |
| 1449 | if (!method) { |
| 1450 | Diag(forLoc, diag::warn_collection_expr_type) |
| 1451 | << collection->getType() << selector << collection->getSourceRange(); |
| 1452 | } |
| 1453 | |
| 1454 | // TODO: check for an incompatible signature? |
| 1455 | } |
| 1456 | |
| 1457 | // Wrap up any cleanups in the expression. |
| 1458 | return Owned(MaybeCreateExprWithCleanups(collection)); |
| 1459 | } |
| 1460 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1461 | StmtResult |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1462 | Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, |
| 1463 | SourceLocation LParenLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1464 | Stmt *First, Expr *Second, |
| 1465 | SourceLocation RParenLoc, Stmt *Body) { |
Fariborz Jahanian | 20552d2 | 2008-01-10 20:33:58 +0000 | [diff] [blame] | 1466 | if (First) { |
| 1467 | QualType FirstType; |
| 1468 | if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) { |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1469 | if (!DS->isSingleDecl()) |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1470 | return StmtError(Diag((*DS->decl_begin())->getLocation(), |
| 1471 | diag::err_toomany_element_decls)); |
| 1472 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1473 | VarDecl *D = cast<VarDecl>(DS->getSingleDecl()); |
| 1474 | FirstType = D->getType(); |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 1475 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only |
| 1476 | // declare identifiers for objects having storage class 'auto' or |
| 1477 | // 'register'. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1478 | if (!D->hasLocalStorage()) |
| 1479 | return StmtError(Diag(D->getLocation(), |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1480 | diag::err_non_variable_decl_in_for)); |
Anders Carlsson | 1fe379f | 2008-08-25 18:16:36 +0000 | [diff] [blame] | 1481 | } else { |
Douglas Gregor | c3203e7 | 2010-04-22 23:10:45 +0000 | [diff] [blame] | 1482 | Expr *FirstE = cast<Expr>(First); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1483 | if (!FirstE->isTypeDependent() && !FirstE->isLValue()) |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1484 | return StmtError(Diag(First->getLocStart(), |
| 1485 | diag::err_selector_element_not_lvalue) |
| 1486 | << First->getSourceRange()); |
| 1487 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | FirstType = static_cast<Expr*>(First)->getType(); |
Anders Carlsson | 1fe379f | 2008-08-25 18:16:36 +0000 | [diff] [blame] | 1489 | } |
Douglas Gregor | c3203e7 | 2010-04-22 23:10:45 +0000 | [diff] [blame] | 1490 | if (!FirstType->isDependentType() && |
| 1491 | !FirstType->isObjCObjectPointerType() && |
Fariborz Jahanian | a5e42a8 | 2009-08-14 21:53:27 +0000 | [diff] [blame] | 1492 | !FirstType->isBlockPointerType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1493 | Diag(ForLoc, diag::err_selector_element_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1494 | << FirstType << First->getSourceRange(); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1495 | } |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1496 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1497 | return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body, |
| 1498 | ForLoc, RParenLoc)); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1499 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1500 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1501 | namespace { |
| 1502 | |
| 1503 | enum BeginEndFunction { |
| 1504 | BEF_begin, |
| 1505 | BEF_end |
| 1506 | }; |
| 1507 | |
| 1508 | /// Build a variable declaration for a for-range statement. |
| 1509 | static VarDecl *BuildForRangeVarDecl(Sema &SemaRef, SourceLocation Loc, |
| 1510 | QualType Type, const char *Name) { |
| 1511 | DeclContext *DC = SemaRef.CurContext; |
| 1512 | IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); |
| 1513 | TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); |
| 1514 | VarDecl *Decl = VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, |
| 1515 | TInfo, SC_Auto, SC_None); |
Richard Smith | b403d6d | 2011-04-18 15:49:25 +0000 | [diff] [blame] | 1516 | Decl->setImplicit(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1517 | return Decl; |
| 1518 | } |
| 1519 | |
| 1520 | /// Finish building a variable declaration for a for-range statement. |
| 1521 | /// \return true if an error occurs. |
| 1522 | static bool FinishForRangeVarDecl(Sema &SemaRef, VarDecl *Decl, Expr *Init, |
| 1523 | SourceLocation Loc, int diag) { |
| 1524 | // Deduce the type for the iterator variable now rather than leaving it to |
| 1525 | // AddInitializerToDecl, so we can produce a more suitable diagnostic. |
| 1526 | TypeSourceInfo *InitTSI = 0; |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 1527 | if ((!isa<InitListExpr>(Init) && Init->getType()->isVoidType()) || |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 1528 | SemaRef.DeduceAutoType(Decl->getTypeSourceInfo(), Init, InitTSI) == |
| 1529 | Sema::DAR_Failed) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1530 | SemaRef.Diag(Loc, diag) << Init->getType(); |
| 1531 | if (!InitTSI) { |
| 1532 | Decl->setInvalidDecl(); |
| 1533 | return true; |
| 1534 | } |
| 1535 | Decl->setTypeSourceInfo(InitTSI); |
| 1536 | Decl->setType(InitTSI->getType()); |
| 1537 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1538 | // In ARC, infer lifetime. |
| 1539 | // FIXME: ARC may want to turn this into 'const __unsafe_unretained' if |
| 1540 | // we're doing the equivalent of fast iteration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1541 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1542 | SemaRef.inferObjCARCLifetime(Decl)) |
| 1543 | Decl->setInvalidDecl(); |
| 1544 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1545 | SemaRef.AddInitializerToDecl(Decl, Init, /*DirectInit=*/false, |
| 1546 | /*TypeMayContainAuto=*/false); |
| 1547 | SemaRef.FinalizeDeclaration(Decl); |
Richard Smith | b403d6d | 2011-04-18 15:49:25 +0000 | [diff] [blame] | 1548 | SemaRef.CurContext->addHiddenDecl(Decl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1549 | return false; |
| 1550 | } |
| 1551 | |
| 1552 | /// Produce a note indicating which begin/end function was implicitly called |
| 1553 | /// by a C++0x for-range statement. This is often not obvious from the code, |
| 1554 | /// nor from the diagnostics produced when analysing the implicit expressions |
| 1555 | /// required in a for-range statement. |
| 1556 | void NoteForRangeBeginEndFunction(Sema &SemaRef, Expr *E, |
| 1557 | BeginEndFunction BEF) { |
| 1558 | CallExpr *CE = dyn_cast<CallExpr>(E); |
| 1559 | if (!CE) |
| 1560 | return; |
| 1561 | FunctionDecl *D = dyn_cast<FunctionDecl>(CE->getCalleeDecl()); |
| 1562 | if (!D) |
| 1563 | return; |
| 1564 | SourceLocation Loc = D->getLocation(); |
| 1565 | |
| 1566 | std::string Description; |
| 1567 | bool IsTemplate = false; |
| 1568 | if (FunctionTemplateDecl *FunTmpl = D->getPrimaryTemplate()) { |
| 1569 | Description = SemaRef.getTemplateArgumentBindingsText( |
| 1570 | FunTmpl->getTemplateParameters(), *D->getTemplateSpecializationArgs()); |
| 1571 | IsTemplate = true; |
| 1572 | } |
| 1573 | |
| 1574 | SemaRef.Diag(Loc, diag::note_for_range_begin_end) |
| 1575 | << BEF << IsTemplate << Description << E->getType(); |
| 1576 | } |
| 1577 | |
| 1578 | /// Build a call to 'begin' or 'end' for a C++0x for-range statement. If the |
| 1579 | /// given LookupResult is non-empty, it is assumed to describe a member which |
| 1580 | /// will be invoked. Otherwise, the function will be found via argument |
| 1581 | /// dependent lookup. |
| 1582 | static ExprResult BuildForRangeBeginEndCall(Sema &SemaRef, Scope *S, |
| 1583 | SourceLocation Loc, |
| 1584 | VarDecl *Decl, |
| 1585 | BeginEndFunction BEF, |
| 1586 | const DeclarationNameInfo &NameInfo, |
| 1587 | LookupResult &MemberLookup, |
| 1588 | Expr *Range) { |
| 1589 | ExprResult CallExpr; |
| 1590 | if (!MemberLookup.empty()) { |
| 1591 | ExprResult MemberRef = |
| 1592 | SemaRef.BuildMemberReferenceExpr(Range, Range->getType(), Loc, |
| 1593 | /*IsPtr=*/false, CXXScopeSpec(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1594 | /*TemplateKWLoc=*/SourceLocation(), |
| 1595 | /*FirstQualifierInScope=*/0, |
| 1596 | MemberLookup, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1597 | /*TemplateArgs=*/0); |
| 1598 | if (MemberRef.isInvalid()) |
| 1599 | return ExprError(); |
| 1600 | CallExpr = SemaRef.ActOnCallExpr(S, MemberRef.get(), Loc, MultiExprArg(), |
| 1601 | Loc, 0); |
| 1602 | if (CallExpr.isInvalid()) |
| 1603 | return ExprError(); |
| 1604 | } else { |
| 1605 | UnresolvedSet<0> FoundNames; |
| 1606 | // C++0x [stmt.ranged]p1: For the purposes of this name lookup, namespace |
| 1607 | // std is an associated namespace. |
| 1608 | UnresolvedLookupExpr *Fn = |
| 1609 | UnresolvedLookupExpr::Create(SemaRef.Context, /*NamingClass=*/0, |
| 1610 | NestedNameSpecifierLoc(), NameInfo, |
| 1611 | /*NeedsADL=*/true, /*Overloaded=*/false, |
| 1612 | FoundNames.begin(), FoundNames.end(), |
| 1613 | /*LookInStdNamespace=*/true); |
| 1614 | CallExpr = SemaRef.BuildOverloadedCallExpr(S, Fn, Fn, Loc, &Range, 1, Loc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 1615 | 0, /*AllowTypoCorrection=*/false); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1616 | if (CallExpr.isInvalid()) { |
| 1617 | SemaRef.Diag(Range->getLocStart(), diag::note_for_range_type) |
| 1618 | << Range->getType(); |
| 1619 | return ExprError(); |
| 1620 | } |
| 1621 | } |
| 1622 | if (FinishForRangeVarDecl(SemaRef, Decl, CallExpr.get(), Loc, |
| 1623 | diag::err_for_range_iter_deduction_failure)) { |
| 1624 | NoteForRangeBeginEndFunction(SemaRef, CallExpr.get(), BEF); |
| 1625 | return ExprError(); |
| 1626 | } |
| 1627 | return CallExpr; |
| 1628 | } |
| 1629 | |
| 1630 | } |
| 1631 | |
| 1632 | /// ActOnCXXForRangeStmt - Check and build a C++0x for-range statement. |
| 1633 | /// |
| 1634 | /// C++0x [stmt.ranged]: |
| 1635 | /// A range-based for statement is equivalent to |
| 1636 | /// |
| 1637 | /// { |
| 1638 | /// auto && __range = range-init; |
| 1639 | /// for ( auto __begin = begin-expr, |
| 1640 | /// __end = end-expr; |
| 1641 | /// __begin != __end; |
| 1642 | /// ++__begin ) { |
| 1643 | /// for-range-declaration = *__begin; |
| 1644 | /// statement |
| 1645 | /// } |
| 1646 | /// } |
| 1647 | /// |
| 1648 | /// The body of the loop is not available yet, since it cannot be analysed until |
| 1649 | /// we have determined the type of the for-range-declaration. |
| 1650 | StmtResult |
| 1651 | Sema::ActOnCXXForRangeStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
| 1652 | Stmt *First, SourceLocation ColonLoc, Expr *Range, |
| 1653 | SourceLocation RParenLoc) { |
| 1654 | if (!First || !Range) |
| 1655 | return StmtError(); |
| 1656 | |
| 1657 | DeclStmt *DS = dyn_cast<DeclStmt>(First); |
| 1658 | assert(DS && "first part of for range not a decl stmt"); |
| 1659 | |
| 1660 | if (!DS->isSingleDecl()) { |
| 1661 | Diag(DS->getStartLoc(), diag::err_type_defined_in_for_range); |
| 1662 | return StmtError(); |
| 1663 | } |
| 1664 | if (DS->getSingleDecl()->isInvalidDecl()) |
| 1665 | return StmtError(); |
| 1666 | |
| 1667 | if (DiagnoseUnexpandedParameterPack(Range, UPPC_Expression)) |
| 1668 | return StmtError(); |
| 1669 | |
| 1670 | // Build auto && __range = range-init |
| 1671 | SourceLocation RangeLoc = Range->getLocStart(); |
| 1672 | VarDecl *RangeVar = BuildForRangeVarDecl(*this, RangeLoc, |
| 1673 | Context.getAutoRRefDeductType(), |
| 1674 | "__range"); |
| 1675 | if (FinishForRangeVarDecl(*this, RangeVar, Range, RangeLoc, |
| 1676 | diag::err_for_range_deduction_failure)) |
| 1677 | return StmtError(); |
| 1678 | |
| 1679 | // Claim the type doesn't contain auto: we've already done the checking. |
| 1680 | DeclGroupPtrTy RangeGroup = |
| 1681 | BuildDeclaratorGroup((Decl**)&RangeVar, 1, /*TypeMayContainAuto=*/false); |
| 1682 | StmtResult RangeDecl = ActOnDeclStmt(RangeGroup, RangeLoc, RangeLoc); |
| 1683 | if (RangeDecl.isInvalid()) |
| 1684 | return StmtError(); |
| 1685 | |
| 1686 | return BuildCXXForRangeStmt(ForLoc, ColonLoc, RangeDecl.get(), |
| 1687 | /*BeginEndDecl=*/0, /*Cond=*/0, /*Inc=*/0, DS, |
| 1688 | RParenLoc); |
| 1689 | } |
| 1690 | |
| 1691 | /// BuildCXXForRangeStmt - Build or instantiate a C++0x for-range statement. |
| 1692 | StmtResult |
| 1693 | Sema::BuildCXXForRangeStmt(SourceLocation ForLoc, SourceLocation ColonLoc, |
| 1694 | Stmt *RangeDecl, Stmt *BeginEnd, Expr *Cond, |
| 1695 | Expr *Inc, Stmt *LoopVarDecl, |
| 1696 | SourceLocation RParenLoc) { |
| 1697 | Scope *S = getCurScope(); |
| 1698 | |
| 1699 | DeclStmt *RangeDS = cast<DeclStmt>(RangeDecl); |
| 1700 | VarDecl *RangeVar = cast<VarDecl>(RangeDS->getSingleDecl()); |
| 1701 | QualType RangeVarType = RangeVar->getType(); |
| 1702 | |
| 1703 | DeclStmt *LoopVarDS = cast<DeclStmt>(LoopVarDecl); |
| 1704 | VarDecl *LoopVar = cast<VarDecl>(LoopVarDS->getSingleDecl()); |
| 1705 | |
| 1706 | StmtResult BeginEndDecl = BeginEnd; |
| 1707 | ExprResult NotEqExpr = Cond, IncrExpr = Inc; |
| 1708 | |
| 1709 | if (!BeginEndDecl.get() && !RangeVarType->isDependentType()) { |
| 1710 | SourceLocation RangeLoc = RangeVar->getLocation(); |
| 1711 | |
Ted Kremenek | e50b015 | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 1712 | const QualType RangeVarNonRefType = RangeVarType.getNonReferenceType(); |
| 1713 | |
| 1714 | ExprResult BeginRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType, |
| 1715 | VK_LValue, ColonLoc); |
| 1716 | if (BeginRangeRef.isInvalid()) |
| 1717 | return StmtError(); |
| 1718 | |
| 1719 | ExprResult EndRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType, |
| 1720 | VK_LValue, ColonLoc); |
| 1721 | if (EndRangeRef.isInvalid()) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1722 | return StmtError(); |
| 1723 | |
| 1724 | QualType AutoType = Context.getAutoDeductType(); |
| 1725 | Expr *Range = RangeVar->getInit(); |
| 1726 | if (!Range) |
| 1727 | return StmtError(); |
| 1728 | QualType RangeType = Range->getType(); |
| 1729 | |
| 1730 | if (RequireCompleteType(RangeLoc, RangeType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1731 | diag::err_for_range_incomplete_type)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1732 | return StmtError(); |
| 1733 | |
| 1734 | // Build auto __begin = begin-expr, __end = end-expr. |
| 1735 | VarDecl *BeginVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, |
| 1736 | "__begin"); |
| 1737 | VarDecl *EndVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, |
| 1738 | "__end"); |
| 1739 | |
| 1740 | // Build begin-expr and end-expr and attach to __begin and __end variables. |
| 1741 | ExprResult BeginExpr, EndExpr; |
| 1742 | if (const ArrayType *UnqAT = RangeType->getAsArrayTypeUnsafe()) { |
| 1743 | // - if _RangeT is an array type, begin-expr and end-expr are __range and |
| 1744 | // __range + __bound, respectively, where __bound is the array bound. If |
| 1745 | // _RangeT is an array of unknown size or an array of incomplete type, |
| 1746 | // the program is ill-formed; |
| 1747 | |
| 1748 | // begin-expr is __range. |
Ted Kremenek | e50b015 | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 1749 | BeginExpr = BeginRangeRef; |
| 1750 | if (FinishForRangeVarDecl(*this, BeginVar, BeginRangeRef.get(), ColonLoc, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1751 | diag::err_for_range_iter_deduction_failure)) { |
| 1752 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 1753 | return StmtError(); |
| 1754 | } |
| 1755 | |
| 1756 | // Find the array bound. |
| 1757 | ExprResult BoundExpr; |
| 1758 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(UnqAT)) |
| 1759 | BoundExpr = Owned(IntegerLiteral::Create(Context, CAT->getSize(), |
Richard Trieu | 1dd986d | 2011-05-02 23:00:27 +0000 | [diff] [blame] | 1760 | Context.getPointerDiffType(), |
| 1761 | RangeLoc)); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1762 | else if (const VariableArrayType *VAT = |
| 1763 | dyn_cast<VariableArrayType>(UnqAT)) |
| 1764 | BoundExpr = VAT->getSizeExpr(); |
| 1765 | else { |
| 1766 | // Can't be a DependentSizedArrayType or an IncompleteArrayType since |
| 1767 | // UnqAT is not incomplete and Range is not type-dependent. |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1768 | llvm_unreachable("Unexpected array type in for-range"); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | // end-expr is __range + __bound. |
Ted Kremenek | e50b015 | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 1772 | EndExpr = ActOnBinOp(S, ColonLoc, tok::plus, EndRangeRef.get(), |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1773 | BoundExpr.get()); |
| 1774 | if (EndExpr.isInvalid()) |
| 1775 | return StmtError(); |
| 1776 | if (FinishForRangeVarDecl(*this, EndVar, EndExpr.get(), ColonLoc, |
| 1777 | diag::err_for_range_iter_deduction_failure)) { |
| 1778 | NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); |
| 1779 | return StmtError(); |
| 1780 | } |
| 1781 | } else { |
| 1782 | DeclarationNameInfo BeginNameInfo(&PP.getIdentifierTable().get("begin"), |
| 1783 | ColonLoc); |
| 1784 | DeclarationNameInfo EndNameInfo(&PP.getIdentifierTable().get("end"), |
| 1785 | ColonLoc); |
| 1786 | |
| 1787 | LookupResult BeginMemberLookup(*this, BeginNameInfo, LookupMemberName); |
| 1788 | LookupResult EndMemberLookup(*this, EndNameInfo, LookupMemberName); |
| 1789 | |
| 1790 | if (CXXRecordDecl *D = RangeType->getAsCXXRecordDecl()) { |
| 1791 | // - if _RangeT is a class type, the unqualified-ids begin and end are |
| 1792 | // looked up in the scope of class _RangeT as if by class member access |
| 1793 | // lookup (3.4.5), and if either (or both) finds at least one |
| 1794 | // declaration, begin-expr and end-expr are __range.begin() and |
| 1795 | // __range.end(), respectively; |
| 1796 | LookupQualifiedName(BeginMemberLookup, D); |
| 1797 | LookupQualifiedName(EndMemberLookup, D); |
| 1798 | |
| 1799 | if (BeginMemberLookup.empty() != EndMemberLookup.empty()) { |
| 1800 | Diag(ColonLoc, diag::err_for_range_member_begin_end_mismatch) |
| 1801 | << RangeType << BeginMemberLookup.empty(); |
| 1802 | return StmtError(); |
| 1803 | } |
| 1804 | } else { |
| 1805 | // - otherwise, begin-expr and end-expr are begin(__range) and |
| 1806 | // end(__range), respectively, where begin and end are looked up with |
| 1807 | // argument-dependent lookup (3.4.2). For the purposes of this name |
| 1808 | // lookup, namespace std is an associated namespace. |
| 1809 | } |
| 1810 | |
| 1811 | BeginExpr = BuildForRangeBeginEndCall(*this, S, ColonLoc, BeginVar, |
| 1812 | BEF_begin, BeginNameInfo, |
Ted Kremenek | e50b015 | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 1813 | BeginMemberLookup, |
| 1814 | BeginRangeRef.get()); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1815 | if (BeginExpr.isInvalid()) |
| 1816 | return StmtError(); |
| 1817 | |
| 1818 | EndExpr = BuildForRangeBeginEndCall(*this, S, ColonLoc, EndVar, |
| 1819 | BEF_end, EndNameInfo, |
Ted Kremenek | e50b015 | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 1820 | EndMemberLookup, EndRangeRef.get()); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1821 | if (EndExpr.isInvalid()) |
| 1822 | return StmtError(); |
| 1823 | } |
| 1824 | |
| 1825 | // C++0x [decl.spec.auto]p6: BeginType and EndType must be the same. |
| 1826 | QualType BeginType = BeginVar->getType(), EndType = EndVar->getType(); |
| 1827 | if (!Context.hasSameType(BeginType, EndType)) { |
| 1828 | Diag(RangeLoc, diag::err_for_range_begin_end_types_differ) |
| 1829 | << BeginType << EndType; |
| 1830 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 1831 | NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); |
| 1832 | } |
| 1833 | |
| 1834 | Decl *BeginEndDecls[] = { BeginVar, EndVar }; |
| 1835 | // Claim the type doesn't contain auto: we've already done the checking. |
| 1836 | DeclGroupPtrTy BeginEndGroup = |
| 1837 | BuildDeclaratorGroup(BeginEndDecls, 2, /*TypeMayContainAuto=*/false); |
| 1838 | BeginEndDecl = ActOnDeclStmt(BeginEndGroup, ColonLoc, ColonLoc); |
| 1839 | |
Ted Kremenek | e50b015 | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 1840 | const QualType BeginRefNonRefType = BeginType.getNonReferenceType(); |
| 1841 | ExprResult BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1842 | VK_LValue, ColonLoc); |
Ted Kremenek | e50b015 | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 1843 | if (BeginRef.isInvalid()) |
| 1844 | return StmtError(); |
| 1845 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1846 | ExprResult EndRef = BuildDeclRefExpr(EndVar, EndType.getNonReferenceType(), |
| 1847 | VK_LValue, ColonLoc); |
Ted Kremenek | e50b015 | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 1848 | if (EndRef.isInvalid()) |
| 1849 | return StmtError(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1850 | |
| 1851 | // Build and check __begin != __end expression. |
| 1852 | NotEqExpr = ActOnBinOp(S, ColonLoc, tok::exclaimequal, |
| 1853 | BeginRef.get(), EndRef.get()); |
| 1854 | NotEqExpr = ActOnBooleanCondition(S, ColonLoc, NotEqExpr.get()); |
| 1855 | NotEqExpr = ActOnFinishFullExpr(NotEqExpr.get()); |
| 1856 | if (NotEqExpr.isInvalid()) { |
| 1857 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 1858 | if (!Context.hasSameType(BeginType, EndType)) |
| 1859 | NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); |
| 1860 | return StmtError(); |
| 1861 | } |
| 1862 | |
| 1863 | // Build and check ++__begin expression. |
Ted Kremenek | e50b015 | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 1864 | BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, |
| 1865 | VK_LValue, ColonLoc); |
| 1866 | if (BeginRef.isInvalid()) |
| 1867 | return StmtError(); |
| 1868 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1869 | IncrExpr = ActOnUnaryOp(S, ColonLoc, tok::plusplus, BeginRef.get()); |
| 1870 | IncrExpr = ActOnFinishFullExpr(IncrExpr.get()); |
| 1871 | if (IncrExpr.isInvalid()) { |
| 1872 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 1873 | return StmtError(); |
| 1874 | } |
| 1875 | |
| 1876 | // Build and check *__begin expression. |
Ted Kremenek | e50b015 | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 1877 | BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, |
| 1878 | VK_LValue, ColonLoc); |
| 1879 | if (BeginRef.isInvalid()) |
| 1880 | return StmtError(); |
| 1881 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1882 | ExprResult DerefExpr = ActOnUnaryOp(S, ColonLoc, tok::star, BeginRef.get()); |
| 1883 | if (DerefExpr.isInvalid()) { |
| 1884 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 1885 | return StmtError(); |
| 1886 | } |
| 1887 | |
| 1888 | // Attach *__begin as initializer for VD. |
| 1889 | if (!LoopVar->isInvalidDecl()) { |
| 1890 | AddInitializerToDecl(LoopVar, DerefExpr.get(), /*DirectInit=*/false, |
| 1891 | /*TypeMayContainAuto=*/true); |
| 1892 | if (LoopVar->isInvalidDecl()) |
| 1893 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 1894 | } |
Richard Smith | cd6f366 | 2011-06-21 23:07:19 +0000 | [diff] [blame] | 1895 | } else { |
| 1896 | // The range is implicitly used as a placeholder when it is dependent. |
| 1897 | RangeVar->setUsed(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | return Owned(new (Context) CXXForRangeStmt(RangeDS, |
| 1901 | cast_or_null<DeclStmt>(BeginEndDecl.get()), |
| 1902 | NotEqExpr.take(), IncrExpr.take(), |
| 1903 | LoopVarDS, /*Body=*/0, ForLoc, |
| 1904 | ColonLoc, RParenLoc)); |
| 1905 | } |
| 1906 | |
| 1907 | /// FinishCXXForRangeStmt - Attach the body to a C++0x for-range statement. |
| 1908 | /// This is a separate step from ActOnCXXForRangeStmt because analysis of the |
| 1909 | /// body cannot be performed until after the type of the range variable is |
| 1910 | /// determined. |
| 1911 | StmtResult Sema::FinishCXXForRangeStmt(Stmt *S, Stmt *B) { |
| 1912 | if (!S || !B) |
| 1913 | return StmtError(); |
| 1914 | |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1915 | CXXForRangeStmt *ForStmt = cast<CXXForRangeStmt>(S); |
| 1916 | ForStmt->setBody(B); |
| 1917 | |
| 1918 | DiagnoseEmptyStmtBody(ForStmt->getRParenLoc(), B, |
| 1919 | diag::warn_empty_range_based_for_body); |
| 1920 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1921 | return S; |
| 1922 | } |
| 1923 | |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 1924 | StmtResult Sema::ActOnGotoStmt(SourceLocation GotoLoc, |
| 1925 | SourceLocation LabelLoc, |
| 1926 | LabelDecl *TheDecl) { |
| 1927 | getCurFunction()->setHasBranchIntoScope(); |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1928 | TheDecl->setUsed(); |
| 1929 | return Owned(new (Context) GotoStmt(TheDecl, GotoLoc, LabelLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1932 | StmtResult |
Chris Lattner | ad56d68 | 2009-04-19 01:04:21 +0000 | [diff] [blame] | 1933 | Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1934 | Expr *E) { |
Eli Friedman | bbf4623 | 2009-03-26 00:18:06 +0000 | [diff] [blame] | 1935 | // Convert operand to void* |
Douglas Gregor | 5f1b9e6 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 1936 | if (!E->isTypeDependent()) { |
| 1937 | QualType ETy = E->getType(); |
Chandler Carruth | 2877998 | 2010-01-31 10:26:25 +0000 | [diff] [blame] | 1938 | QualType DestTy = Context.getPointerType(Context.VoidTy.withConst()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1939 | ExprResult ExprRes = Owned(E); |
Douglas Gregor | 5f1b9e6 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 1940 | AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1941 | CheckSingleAssignmentConstraints(DestTy, ExprRes); |
| 1942 | if (ExprRes.isInvalid()) |
| 1943 | return StmtError(); |
| 1944 | E = ExprRes.take(); |
Chandler Carruth | 2877998 | 2010-01-31 10:26:25 +0000 | [diff] [blame] | 1945 | if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E, AA_Passing)) |
Douglas Gregor | 5f1b9e6 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 1946 | return StmtError(); |
Eli Friedman | d29975f | 2012-01-31 22:47:07 +0000 | [diff] [blame] | 1947 | E = MaybeCreateExprWithCleanups(E); |
Douglas Gregor | 5f1b9e6 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 1948 | } |
John McCall | b60a77e | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 1949 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1950 | getCurFunction()->setHasIndirectGoto(); |
John McCall | b60a77e | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 1951 | |
Douglas Gregor | 5f1b9e6 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 1952 | return Owned(new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1955 | StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 1956 | Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1957 | Scope *S = CurScope->getContinueParent(); |
| 1958 | if (!S) { |
| 1959 | // 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] | 1960 | return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1961 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1962 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1963 | return Owned(new (Context) ContinueStmt(ContinueLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1966 | StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 1967 | Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1968 | Scope *S = CurScope->getBreakParent(); |
| 1969 | if (!S) { |
| 1970 | // 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] | 1971 | return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1972 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 1973 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1974 | return Owned(new (Context) BreakStmt(BreakLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1977 | /// \brief Determine whether the given expression is a candidate for |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 1978 | /// copy elision in either a return statement or a throw expression. |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 1979 | /// |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 1980 | /// \param ReturnType If we're determining the copy elision candidate for |
| 1981 | /// a return statement, this is the return type of the function. If we're |
| 1982 | /// determining the copy elision candidate for a throw expression, this will |
| 1983 | /// be a NULL type. |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 1984 | /// |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 1985 | /// \param E The expression being returned from the function or block, or |
| 1986 | /// being thrown. |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 1987 | /// |
Douglas Gregor | 4926d83 | 2011-05-20 15:00:53 +0000 | [diff] [blame] | 1988 | /// \param AllowFunctionParameter Whether we allow function parameters to |
| 1989 | /// be considered NRVO candidates. C++ prohibits this for NRVO itself, but |
| 1990 | /// we re-use this logic to determine whether we should try to move as part of |
| 1991 | /// a return or throw (which does allow function parameters). |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 1992 | /// |
| 1993 | /// \returns The NRVO candidate variable, if the return statement may use the |
| 1994 | /// NRVO, or NULL if there is no such candidate. |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 1995 | const VarDecl *Sema::getCopyElisionCandidate(QualType ReturnType, |
| 1996 | Expr *E, |
| 1997 | bool AllowFunctionParameter) { |
| 1998 | QualType ExprType = E->getType(); |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 1999 | // - in a return statement in a function with ... |
| 2000 | // ... a class return type ... |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2001 | if (!ReturnType.isNull()) { |
| 2002 | if (!ReturnType->isRecordType()) |
| 2003 | return 0; |
| 2004 | // ... the same cv-unqualified type as the function return type ... |
| 2005 | if (!Context.hasSameUnqualifiedType(ReturnType, ExprType)) |
| 2006 | return 0; |
| 2007 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2008 | |
| 2009 | // ... the expression is the name of a non-volatile automatic object |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2010 | // (other than a function or catch-clause parameter)) ... |
| 2011 | const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()); |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2012 | if (!DR) |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2013 | return 0; |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2014 | const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 2015 | if (!VD) |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2016 | return 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2017 | |
John McCall | 1cd76e8 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2018 | // ...object (other than a function or catch-clause parameter)... |
| 2019 | if (VD->getKind() != Decl::Var && |
| 2020 | !(AllowFunctionParameter && VD->getKind() == Decl::ParmVar)) |
| 2021 | return 0; |
| 2022 | if (VD->isExceptionVariable()) return 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2023 | |
John McCall | 1cd76e8 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2024 | // ...automatic... |
| 2025 | if (!VD->hasLocalStorage()) return 0; |
| 2026 | |
| 2027 | // ...non-volatile... |
| 2028 | if (VD->getType().isVolatileQualified()) return 0; |
| 2029 | if (VD->getType()->isReferenceType()) return 0; |
| 2030 | |
| 2031 | // __block variables can't be allocated in a way that permits NRVO. |
| 2032 | if (VD->hasAttr<BlocksAttr>()) return 0; |
| 2033 | |
| 2034 | // Variables with higher required alignment than their type's ABI |
| 2035 | // alignment cannot use NRVO. |
| 2036 | if (VD->hasAttr<AlignedAttr>() && |
| 2037 | Context.getDeclAlign(VD) > Context.getTypeAlignInChars(VD->getType())) |
| 2038 | return 0; |
| 2039 | |
| 2040 | return VD; |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Douglas Gregor | 07f402c | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2043 | /// \brief Perform the initialization of a potentially-movable value, which |
| 2044 | /// is the result of return value. |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2045 | /// |
| 2046 | /// This routine implements C++0x [class.copy]p33, which attempts to treat |
| 2047 | /// returned lvalues as rvalues in certain cases (to prefer move construction), |
| 2048 | /// then falls back to treating them as lvalues if that failed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2049 | ExprResult |
Douglas Gregor | 07f402c | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2050 | Sema::PerformMoveOrCopyInitialization(const InitializedEntity &Entity, |
| 2051 | const VarDecl *NRVOCandidate, |
| 2052 | QualType ResultType, |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 2053 | Expr *Value, |
| 2054 | bool AllowNRVO) { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2055 | // C++0x [class.copy]p33: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2056 | // When the criteria for elision of a copy operation are met or would |
| 2057 | // be met save for the fact that the source object is a function |
| 2058 | // parameter, and the object to be copied is designated by an lvalue, |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2059 | // overload resolution to select the constructor for the copy is first |
| 2060 | // performed as if the object were designated by an rvalue. |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2061 | ExprResult Res = ExprError(); |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 2062 | if (AllowNRVO && |
| 2063 | (NRVOCandidate || getCopyElisionCandidate(ResultType, Value, true))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2064 | ImplicitCastExpr AsRvalue(ImplicitCastExpr::OnStack, |
Richard Smith | dbbeccc | 2012-05-15 05:04:02 +0000 | [diff] [blame] | 2065 | Value->getType(), CK_NoOp, Value, VK_XValue); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2066 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2067 | Expr *InitExpr = &AsRvalue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2068 | InitializationKind Kind |
Douglas Gregor | 07f402c | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2069 | = InitializationKind::CreateCopy(Value->getLocStart(), |
| 2070 | Value->getLocStart()); |
| 2071 | InitializationSequence Seq(*this, Entity, Kind, &InitExpr, 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2072 | |
| 2073 | // [...] If overload resolution fails, or if the type of the first |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2074 | // parameter of the selected constructor is not an rvalue reference |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2075 | // to the object's type (possibly cv-qualified), overload resolution |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2076 | // is performed again, considering the object as an lvalue. |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 2077 | if (Seq) { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2078 | for (InitializationSequence::step_iterator Step = Seq.step_begin(), |
| 2079 | StepEnd = Seq.step_end(); |
| 2080 | Step != StepEnd; ++Step) { |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 2081 | if (Step->Kind != InitializationSequence::SK_ConstructorInitialization) |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2082 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2083 | |
| 2084 | CXXConstructorDecl *Constructor |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2085 | = cast<CXXConstructorDecl>(Step->Function.Function); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2086 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2087 | const RValueReferenceType *RRefType |
Douglas Gregor | 07f402c | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2088 | = Constructor->getParamDecl(0)->getType() |
| 2089 | ->getAs<RValueReferenceType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2091 | // If we don't meet the criteria, break out now. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2092 | if (!RRefType || |
Douglas Gregor | 07f402c | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2093 | !Context.hasSameUnqualifiedType(RRefType->getPointeeType(), |
| 2094 | Context.getTypeDeclType(Constructor->getParent()))) |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2095 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2096 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2097 | // Promote "AsRvalue" to the heap, since we now need this |
| 2098 | // expression node to persist. |
Douglas Gregor | 07f402c | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2099 | Value = ImplicitCastExpr::Create(Context, Value->getType(), |
Richard Smith | dbbeccc | 2012-05-15 05:04:02 +0000 | [diff] [blame] | 2100 | CK_NoOp, Value, 0, VK_XValue); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2101 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2102 | // Complete type-checking the initialization of the return type |
| 2103 | // using the constructor we found. |
Douglas Gregor | 07f402c | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2104 | Res = Seq.Perform(*this, Entity, Kind, MultiExprArg(&Value, 1)); |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2105 | } |
| 2106 | } |
| 2107 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2108 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2109 | // Either we didn't meet the criteria for treating an lvalue as an rvalue, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2110 | // above, or overload resolution failed. Either way, we need to try |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2111 | // (again) now with the return value expression as written. |
| 2112 | if (Res.isInvalid()) |
Douglas Gregor | 07f402c | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2113 | Res = PerformCopyInitialization(Entity, SourceLocation(), Value); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2114 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2115 | return Res; |
| 2116 | } |
| 2117 | |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2118 | /// ActOnCapScopeReturnStmt - Utility routine to type-check return statements |
| 2119 | /// for capturing scopes. |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2120 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2121 | StmtResult |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2122 | Sema::ActOnCapScopeReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { |
| 2123 | // If this is the first return we've seen, infer the return type. |
| 2124 | // [expr.prim.lambda]p4 in C++11; block literals follow a superset of those |
| 2125 | // rules which allows multiple return statements. |
| 2126 | CapturingScopeInfo *CurCap = cast<CapturingScopeInfo>(getCurFunction()); |
| 2127 | if (CurCap->HasImplicitReturnType) { |
| 2128 | QualType ReturnT; |
Douglas Gregor | a0c2b21 | 2012-02-09 18:40:39 +0000 | [diff] [blame] | 2129 | if (RetValExp && !isa<InitListExpr>(RetValExp)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2130 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RetValExp); |
| 2131 | if (Result.isInvalid()) |
| 2132 | return StmtError(); |
| 2133 | RetValExp = Result.take(); |
Douglas Gregor | 6a576ab | 2011-06-05 05:04:23 +0000 | [diff] [blame] | 2134 | |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2135 | if (!RetValExp->isTypeDependent()) |
| 2136 | ReturnT = RetValExp->getType(); |
| 2137 | else |
| 2138 | ReturnT = Context.DependentTy; |
Douglas Gregor | a0c2b21 | 2012-02-09 18:40:39 +0000 | [diff] [blame] | 2139 | } else { |
| 2140 | if (RetValExp) { |
| 2141 | // C++11 [expr.lambda.prim]p4 bans inferring the result from an |
| 2142 | // initializer list, because it is not an expression (even |
| 2143 | // though we represent it as one). We still deduce 'void'. |
| 2144 | Diag(ReturnLoc, diag::err_lambda_return_init_list) |
| 2145 | << RetValExp->getSourceRange(); |
| 2146 | } |
| 2147 | |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2148 | ReturnT = Context.VoidTy; |
Fariborz Jahanian | 649657e | 2011-12-03 23:53:56 +0000 | [diff] [blame] | 2149 | } |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2150 | // We require the return types to strictly match here. |
| 2151 | if (!CurCap->ReturnType.isNull() && |
| 2152 | !CurCap->ReturnType->isDependentType() && |
| 2153 | !ReturnT->isDependentType() && |
| 2154 | !Context.hasSameType(ReturnT, CurCap->ReturnType)) { |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2155 | Diag(ReturnLoc, diag::err_typecheck_missing_return_type_incompatible) |
Douglas Gregor | 4e88df7 | 2012-02-15 15:57:22 +0000 | [diff] [blame] | 2156 | << ReturnT << CurCap->ReturnType |
Douglas Gregor | 0bcc3d8 | 2012-02-15 15:59:09 +0000 | [diff] [blame] | 2157 | << (getCurLambda() != 0); |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2158 | return StmtError(); |
| 2159 | } |
| 2160 | CurCap->ReturnType = ReturnT; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2161 | } |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2162 | QualType FnRetType = CurCap->ReturnType; |
| 2163 | assert(!FnRetType.isNull()); |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2164 | |
Douglas Gregor | 793cd1c | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 2165 | if (BlockScopeInfo *CurBlock = dyn_cast<BlockScopeInfo>(CurCap)) { |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2166 | if (CurBlock->FunctionType->getAs<FunctionType>()->getNoReturnAttr()) { |
| 2167 | Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr); |
| 2168 | return StmtError(); |
| 2169 | } |
Douglas Gregor | 793cd1c | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 2170 | } else { |
| 2171 | LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(CurCap); |
| 2172 | if (LSI->CallOperator->getType()->getAs<FunctionType>()->getNoReturnAttr()){ |
| 2173 | Diag(ReturnLoc, diag::err_noreturn_lambda_has_return_expr); |
| 2174 | return StmtError(); |
| 2175 | } |
| 2176 | } |
Mike Stump | 6c92fa7 | 2009-04-29 21:40:37 +0000 | [diff] [blame] | 2177 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2178 | // Otherwise, verify that this result type matches the previous one. We are |
| 2179 | // pickier with blocks than for normal functions because we don't have GCC |
| 2180 | // compatibility to worry about here. |
John McCall | d963c37 | 2011-08-17 21:34:14 +0000 | [diff] [blame] | 2181 | const VarDecl *NRVOCandidate = 0; |
John McCall | 0a7efe1 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2182 | if (FnRetType->isDependentType()) { |
| 2183 | // Delay processing for now. TODO: there are lots of dependent |
| 2184 | // types we can conclusively prove aren't void. |
| 2185 | } else if (FnRetType->isVoidType()) { |
Sebastian Redl | 5b38a0f | 2012-02-22 17:38:04 +0000 | [diff] [blame] | 2186 | if (RetValExp && !isa<InitListExpr>(RetValExp) && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2187 | !(getLangOpts().CPlusPlus && |
John McCall | 0a7efe1 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2188 | (RetValExp->isTypeDependent() || |
| 2189 | RetValExp->getType()->isVoidType()))) { |
Fariborz Jahanian | 4e648e4 | 2012-03-21 16:45:13 +0000 | [diff] [blame] | 2190 | if (!getLangOpts().CPlusPlus && |
| 2191 | RetValExp->getType()->isVoidType()) |
Fariborz Jahanian | 9354f6a | 2012-03-21 20:28:39 +0000 | [diff] [blame] | 2192 | Diag(ReturnLoc, diag::ext_return_has_void_expr) << "literal" << 2; |
Fariborz Jahanian | 4e648e4 | 2012-03-21 16:45:13 +0000 | [diff] [blame] | 2193 | else { |
| 2194 | Diag(ReturnLoc, diag::err_return_block_has_expr); |
| 2195 | RetValExp = 0; |
| 2196 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2197 | } |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2198 | } else if (!RetValExp) { |
John McCall | 0a7efe1 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2199 | return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr)); |
| 2200 | } else if (!RetValExp->isTypeDependent()) { |
| 2201 | // we have a non-void block with an expression, continue checking |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2202 | |
John McCall | 0a7efe1 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2203 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 2204 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 2205 | // function return. |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2206 | |
John McCall | 0a7efe1 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2207 | // In C++ the return statement is handled via a copy initialization. |
| 2208 | // the C version of which boils down to CheckSingleAssignmentConstraints. |
| 2209 | NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); |
| 2210 | InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, |
| 2211 | FnRetType, |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 2212 | NRVOCandidate != 0); |
John McCall | 0a7efe1 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2213 | ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, |
| 2214 | FnRetType, RetValExp); |
| 2215 | if (Res.isInvalid()) { |
| 2216 | // FIXME: Cleanup temporaries here, anyway? |
| 2217 | return StmtError(); |
Anders Carlsson | c6acbc5 | 2010-01-29 18:30:20 +0000 | [diff] [blame] | 2218 | } |
John McCall | 0a7efe1 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2219 | RetValExp = Res.take(); |
| 2220 | CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2221 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2222 | |
John McCall | d963c37 | 2011-08-17 21:34:14 +0000 | [diff] [blame] | 2223 | if (RetValExp) { |
| 2224 | CheckImplicitConversions(RetValExp, ReturnLoc); |
| 2225 | RetValExp = MaybeCreateExprWithCleanups(RetValExp); |
| 2226 | } |
John McCall | 0a7efe1 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2227 | ReturnStmt *Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, |
| 2228 | NRVOCandidate); |
John McCall | d963c37 | 2011-08-17 21:34:14 +0000 | [diff] [blame] | 2229 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2230 | // If we need to check for the named return value optimization, save the |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2231 | // return statement in our scope for later processing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2232 | if (getLangOpts().CPlusPlus && FnRetType->isRecordType() && |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2233 | !CurContext->isDependentContext()) |
| 2234 | FunctionScopes.back()->Returns.push_back(Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2235 | |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2236 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2237 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2238 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2239 | StmtResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2240 | Sema::ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { |
Douglas Gregor | fc92137 | 2011-05-20 15:32:55 +0000 | [diff] [blame] | 2241 | // Check for unexpanded parameter packs. |
| 2242 | if (RetValExp && DiagnoseUnexpandedParameterPack(RetValExp)) |
| 2243 | return StmtError(); |
| 2244 | |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2245 | if (isa<CapturingScopeInfo>(getCurFunction())) |
| 2246 | return ActOnCapScopeReturnStmt(ReturnLoc, RetValExp); |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2247 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 2248 | QualType FnRetType; |
Eli Friedman | 38ac243 | 2012-03-30 01:13:43 +0000 | [diff] [blame] | 2249 | QualType RelatedRetType; |
Mike Stump | f7c41da | 2009-04-29 00:43:21 +0000 | [diff] [blame] | 2250 | if (const FunctionDecl *FD = getCurFunctionDecl()) { |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 2251 | FnRetType = FD->getResultType(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2252 | if (FD->hasAttr<NoReturnAttr>() || |
| 2253 | FD->getType()->getAs<FunctionType>()->getNoReturnAttr()) |
Chris Lattner | 8662587 | 2009-05-31 19:32:13 +0000 | [diff] [blame] | 2254 | Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr) |
Eli Friedman | 79430e9 | 2012-01-05 00:49:17 +0000 | [diff] [blame] | 2255 | << FD->getDeclName(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2256 | } else if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
Eli Friedman | 38ac243 | 2012-03-30 01:13:43 +0000 | [diff] [blame] | 2257 | FnRetType = MD->getResultType(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2258 | if (MD->hasRelatedResultType() && MD->getClassInterface()) { |
| 2259 | // In the implementation of a method with a related return type, the |
| 2260 | // type used to type-check the validity of return statements within the |
| 2261 | // method body is a pointer to the type of the class being implemented. |
Eli Friedman | 38ac243 | 2012-03-30 01:13:43 +0000 | [diff] [blame] | 2262 | RelatedRetType = Context.getObjCInterfaceType(MD->getClassInterface()); |
| 2263 | RelatedRetType = Context.getObjCObjectPointerType(RelatedRetType); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2264 | } |
| 2265 | } else // If we don't have a function/method context, bail. |
Steve Naroff | c97fb9a | 2009-03-03 00:45:38 +0000 | [diff] [blame] | 2266 | return StmtError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2267 | |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2268 | ReturnStmt *Result = 0; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2269 | if (FnRetType->isVoidType()) { |
Nick Lewycky | 8d79461 | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 2270 | if (RetValExp) { |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2271 | if (isa<InitListExpr>(RetValExp)) { |
| 2272 | // We simply never allow init lists as the return value of void |
| 2273 | // functions. This is compatible because this was never allowed before, |
| 2274 | // so there's no legacy code to deal with. |
| 2275 | NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); |
| 2276 | int FunctionKind = 0; |
| 2277 | if (isa<ObjCMethodDecl>(CurDecl)) |
| 2278 | FunctionKind = 1; |
| 2279 | else if (isa<CXXConstructorDecl>(CurDecl)) |
| 2280 | FunctionKind = 2; |
| 2281 | else if (isa<CXXDestructorDecl>(CurDecl)) |
| 2282 | FunctionKind = 3; |
| 2283 | |
| 2284 | Diag(ReturnLoc, diag::err_return_init_list) |
| 2285 | << CurDecl->getDeclName() << FunctionKind |
| 2286 | << RetValExp->getSourceRange(); |
| 2287 | |
| 2288 | // Drop the expression. |
| 2289 | RetValExp = 0; |
| 2290 | } else if (!RetValExp->isTypeDependent()) { |
Nick Lewycky | 8d79461 | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 2291 | // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 2292 | unsigned D = diag::ext_return_has_expr; |
| 2293 | if (RetValExp->getType()->isVoidType()) |
| 2294 | D = diag::ext_return_has_void_expr; |
| 2295 | else { |
| 2296 | ExprResult Result = Owned(RetValExp); |
| 2297 | Result = IgnoredValueConversions(Result.take()); |
| 2298 | if (Result.isInvalid()) |
| 2299 | return StmtError(); |
| 2300 | RetValExp = Result.take(); |
| 2301 | RetValExp = ImpCastExprToType(RetValExp, |
| 2302 | Context.VoidTy, CK_ToVoid).take(); |
| 2303 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2304 | |
Nick Lewycky | 8d79461 | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 2305 | // return (some void expression); is legal in C++. |
| 2306 | if (D != diag::ext_return_has_void_expr || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2307 | !getLangOpts().CPlusPlus) { |
Nick Lewycky | 8d79461 | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 2308 | NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); |
Chandler Carruth | ca0d0d4 | 2011-06-30 08:56:22 +0000 | [diff] [blame] | 2309 | |
| 2310 | int FunctionKind = 0; |
| 2311 | if (isa<ObjCMethodDecl>(CurDecl)) |
| 2312 | FunctionKind = 1; |
| 2313 | else if (isa<CXXConstructorDecl>(CurDecl)) |
| 2314 | FunctionKind = 2; |
| 2315 | else if (isa<CXXDestructorDecl>(CurDecl)) |
| 2316 | FunctionKind = 3; |
| 2317 | |
Nick Lewycky | 8d79461 | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 2318 | Diag(ReturnLoc, D) |
Chandler Carruth | ca0d0d4 | 2011-06-30 08:56:22 +0000 | [diff] [blame] | 2319 | << CurDecl->getDeclName() << FunctionKind |
Nick Lewycky | 8d79461 | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 2320 | << RetValExp->getSourceRange(); |
| 2321 | } |
Chris Lattner | e878eb0 | 2008-12-18 02:03:48 +0000 | [diff] [blame] | 2322 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2323 | |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2324 | if (RetValExp) { |
| 2325 | CheckImplicitConversions(RetValExp, ReturnLoc); |
| 2326 | RetValExp = MaybeCreateExprWithCleanups(RetValExp); |
| 2327 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2328 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2329 | |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2330 | Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0); |
| 2331 | } else if (!RetValExp && !FnRetType->isDependentType()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2332 | unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4 |
| 2333 | // C99 6.8.6.4p1 (ext_ since GCC warns) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2334 | if (getLangOpts().C99) DiagID = diag::ext_return_missing_expr; |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2335 | |
| 2336 | if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2337 | Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/; |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2338 | else |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2339 | Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/; |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2340 | Result = new (Context) ReturnStmt(ReturnLoc); |
| 2341 | } else { |
| 2342 | const VarDecl *NRVOCandidate = 0; |
| 2343 | if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) { |
| 2344 | // we have a non-void function with an expression, continue checking |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2345 | |
Eli Friedman | 38ac243 | 2012-03-30 01:13:43 +0000 | [diff] [blame] | 2346 | if (!RelatedRetType.isNull()) { |
| 2347 | // If we have a related result type, perform an extra conversion here. |
| 2348 | // FIXME: The diagnostics here don't really describe what is happening. |
| 2349 | InitializedEntity Entity = |
| 2350 | InitializedEntity::InitializeTemporary(RelatedRetType); |
| 2351 | |
| 2352 | ExprResult Res = PerformCopyInitialization(Entity, SourceLocation(), |
| 2353 | RetValExp); |
| 2354 | if (Res.isInvalid()) { |
| 2355 | // FIXME: Cleanup temporaries here, anyway? |
| 2356 | return StmtError(); |
| 2357 | } |
| 2358 | RetValExp = Res.takeAs<Expr>(); |
| 2359 | } |
| 2360 | |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2361 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 2362 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 2363 | // function return. |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2364 | |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 2365 | // In C++ the return statement is handled via a copy initialization, |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2366 | // the C version of which boils down to CheckSingleAssignmentConstraints. |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2367 | NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2368 | InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, |
Douglas Gregor | 07f402c | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2369 | FnRetType, |
Francois Pichet | 58f14c0 | 2011-06-02 00:47:27 +0000 | [diff] [blame] | 2370 | NRVOCandidate != 0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2371 | ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, |
Douglas Gregor | 07f402c | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2372 | FnRetType, RetValExp); |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2373 | if (Res.isInvalid()) { |
| 2374 | // FIXME: Cleanup temporaries here, anyway? |
| 2375 | return StmtError(); |
| 2376 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2377 | |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2378 | RetValExp = Res.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2379 | if (RetValExp) |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2380 | CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2381 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2382 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2383 | if (RetValExp) { |
| 2384 | CheckImplicitConversions(RetValExp, ReturnLoc); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 2385 | RetValExp = MaybeCreateExprWithCleanups(RetValExp); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2386 | } |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2387 | Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2388 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2389 | |
| 2390 | // If we need to check for the named return value optimization, save the |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2391 | // return statement in our scope for later processing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2392 | if (getLangOpts().CPlusPlus && FnRetType->isRecordType() && |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2393 | !CurContext->isDependentContext()) |
| 2394 | FunctionScopes.back()->Returns.push_back(Result); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2395 | |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2396 | return Owned(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 2399 | /// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently |
| 2400 | /// ignore "noop" casts in places where an lvalue is required by an inline asm. |
| 2401 | /// We emulate this behavior when -fheinous-gnu-extensions is specified, but |
| 2402 | /// provide a strong guidance to not use it. |
| 2403 | /// |
| 2404 | /// This method checks to see if the argument is an acceptable l-value and |
| 2405 | /// returns false if it is a case we can handle. |
| 2406 | static bool CheckAsmLValue(const Expr *E, Sema &S) { |
Anders Carlsson | 703e394 | 2010-01-24 05:50:09 +0000 | [diff] [blame] | 2407 | // Type dependent expressions will be checked during instantiation. |
| 2408 | if (E->isTypeDependent()) |
| 2409 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2410 | |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2411 | if (E->isLValue()) |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 2412 | return false; // Cool, this is an lvalue. |
| 2413 | |
| 2414 | // Okay, this is not an lvalue, but perhaps it is the result of a cast that we |
| 2415 | // are supposed to allow. |
| 2416 | const Expr *E2 = E->IgnoreParenNoopCasts(S.Context); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2417 | if (E != E2 && E2->isLValue()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2418 | if (!S.getLangOpts().HeinousExtensions) |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 2419 | S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue) |
| 2420 | << E->getSourceRange(); |
| 2421 | else |
| 2422 | S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue) |
| 2423 | << E->getSourceRange(); |
| 2424 | // Accept, even if we emitted an error diagnostic. |
| 2425 | return false; |
| 2426 | } |
| 2427 | |
| 2428 | // None of the above, just randomly invalid non-lvalue. |
| 2429 | return true; |
| 2430 | } |
| 2431 | |
Chris Lattner | ca57b4b | 2011-02-21 21:40:33 +0000 | [diff] [blame] | 2432 | /// isOperandMentioned - Return true if the specified operand # is mentioned |
| 2433 | /// anywhere in the decomposed asm string. |
| 2434 | static bool isOperandMentioned(unsigned OpNo, |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 2435 | ArrayRef<AsmStmt::AsmStringPiece> AsmStrPieces) { |
Chris Lattner | ca57b4b | 2011-02-21 21:40:33 +0000 | [diff] [blame] | 2436 | for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) { |
| 2437 | const AsmStmt::AsmStringPiece &Piece = AsmStrPieces[p]; |
| 2438 | if (!Piece.isOperand()) continue; |
| 2439 | |
| 2440 | // If this is a reference to the input and if the input was the smaller |
| 2441 | // one, then we have to reject this asm. |
| 2442 | if (Piece.getOperandNo() == OpNo) |
| 2443 | return true; |
| 2444 | } |
| 2445 | |
| 2446 | return false; |
| 2447 | } |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 2448 | |
Chris Lattner | ca57b4b | 2011-02-21 21:40:33 +0000 | [diff] [blame] | 2449 | StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, bool IsSimple, |
| 2450 | bool IsVolatile, unsigned NumOutputs, |
| 2451 | unsigned NumInputs, IdentifierInfo **Names, |
| 2452 | MultiExprArg constraints, MultiExprArg exprs, |
| 2453 | Expr *asmString, MultiExprArg clobbers, |
| 2454 | SourceLocation RParenLoc, bool MSAsm) { |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2455 | unsigned NumClobbers = clobbers.size(); |
| 2456 | StringLiteral **Constraints = |
| 2457 | reinterpret_cast<StringLiteral**>(constraints.get()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2458 | Expr **Exprs = exprs.get(); |
| 2459 | StringLiteral *AsmString = cast<StringLiteral>(asmString); |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2460 | StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get()); |
| 2461 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2462 | SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2463 | |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 2464 | // The parser verifies that there is a string literal here. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2465 | if (!AsmString->isAscii()) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2466 | return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character) |
| 2467 | << AsmString->getSourceRange()); |
| 2468 | |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 2469 | for (unsigned i = 0; i != NumOutputs; i++) { |
| 2470 | StringLiteral *Literal = Constraints[i]; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2471 | if (!Literal->isAscii()) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2472 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 2473 | << Literal->getSourceRange()); |
| 2474 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2475 | StringRef OutputName; |
Anders Carlsson | ff93dbd | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 2476 | if (Names[i]) |
| 2477 | OutputName = Names[i]->getName(); |
| 2478 | |
| 2479 | TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2480 | if (!Context.getTargetInfo().validateOutputConstraint(Info)) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2481 | return StmtError(Diag(Literal->getLocStart(), |
Chris Lattner | 432c869 | 2009-04-26 17:19:08 +0000 | [diff] [blame] | 2482 | diag::err_asm_invalid_output_constraint) |
| 2483 | << Info.getConstraintStr()); |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2484 | |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 2485 | // Check that the output exprs are valid lvalues. |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 2486 | Expr *OutputExpr = Exprs[i]; |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 2487 | if (CheckAsmLValue(OutputExpr, *this)) { |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 2488 | return StmtError(Diag(OutputExpr->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 2489 | diag::err_asm_invalid_lvalue_in_output) |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 2490 | << OutputExpr->getSourceRange()); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 2491 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2492 | |
Chris Lattner | 44def07 | 2009-04-26 07:16:29 +0000 | [diff] [blame] | 2493 | OutputConstraintInfos.push_back(Info); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 2494 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2495 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2496 | SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 2497 | |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 2498 | for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 2499 | StringLiteral *Literal = Constraints[i]; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2500 | if (!Literal->isAscii()) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2501 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 2502 | << Literal->getSourceRange()); |
| 2503 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2504 | StringRef InputName; |
Anders Carlsson | ff93dbd | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 2505 | if (Names[i]) |
| 2506 | InputName = Names[i]->getName(); |
| 2507 | |
| 2508 | TargetInfo::ConstraintInfo Info(Literal->getString(), InputName); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2509 | if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos.data(), |
Chris Lattner | 2819fa8 | 2009-04-26 17:57:12 +0000 | [diff] [blame] | 2510 | NumOutputs, Info)) { |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2511 | return StmtError(Diag(Literal->getLocStart(), |
Chris Lattner | 432c869 | 2009-04-26 17:19:08 +0000 | [diff] [blame] | 2512 | diag::err_asm_invalid_input_constraint) |
| 2513 | << Info.getConstraintStr()); |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 2514 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2515 | |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 2516 | Expr *InputExpr = Exprs[i]; |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2517 | |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 2518 | // Only allow void types for memory constraints. |
Chris Lattner | 44def07 | 2009-04-26 07:16:29 +0000 | [diff] [blame] | 2519 | if (Info.allowsMemory() && !Info.allowsRegister()) { |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 2520 | if (CheckAsmLValue(InputExpr, *this)) |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 2521 | return StmtError(Diag(InputExpr->getLocStart(), |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 2522 | diag::err_asm_invalid_lvalue_in_input) |
Chris Lattner | 432c869 | 2009-04-26 17:19:08 +0000 | [diff] [blame] | 2523 | << Info.getConstraintStr() |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 2524 | << InputExpr->getSourceRange()); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 2525 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2526 | |
Chris Lattner | 44def07 | 2009-04-26 07:16:29 +0000 | [diff] [blame] | 2527 | if (Info.allowsRegister()) { |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 2528 | if (InputExpr->getType()->isVoidType()) { |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 2529 | return StmtError(Diag(InputExpr->getLocStart(), |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 2530 | diag::err_asm_invalid_type_in_input) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2531 | << InputExpr->getType() << Info.getConstraintStr() |
Eli Friedman | 72056a2 | 2009-05-03 07:49:42 +0000 | [diff] [blame] | 2532 | << InputExpr->getSourceRange()); |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 2533 | } |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 2534 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2535 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2536 | ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]); |
| 2537 | if (Result.isInvalid()) |
| 2538 | return StmtError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2539 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2540 | Exprs[i] = Result.take(); |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 2541 | InputConstraintInfos.push_back(Info); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 2542 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2543 | |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 2544 | // Check that the clobbers are valid. |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 2545 | for (unsigned i = 0; i != NumClobbers; i++) { |
| 2546 | StringLiteral *Literal = Clobbers[i]; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2547 | if (!Literal->isAscii()) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2548 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 2549 | << Literal->getSourceRange()); |
| 2550 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2551 | StringRef Clobber = Literal->getString(); |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2552 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2553 | if (!Context.getTargetInfo().isValidClobber(Clobber)) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2554 | return StmtError(Diag(Literal->getLocStart(), |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 2555 | diag::err_asm_unknown_register_name) << Clobber); |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 2556 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 2557 | |
Chris Lattner | fb5058e | 2009-03-10 23:41:04 +0000 | [diff] [blame] | 2558 | AsmStmt *NS = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2559 | new (Context) AsmStmt(Context, AsmLoc, IsSimple, IsVolatile, MSAsm, |
| 2560 | NumOutputs, NumInputs, Names, Constraints, Exprs, |
Anders Carlsson | 966146e | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 2561 | AsmString, NumClobbers, Clobbers, RParenLoc); |
Chris Lattner | fb5058e | 2009-03-10 23:41:04 +0000 | [diff] [blame] | 2562 | // Validate the asm string, ensuring it makes sense given the operands we |
| 2563 | // have. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2564 | SmallVector<AsmStmt::AsmStringPiece, 8> Pieces; |
Chris Lattner | fb5058e | 2009-03-10 23:41:04 +0000 | [diff] [blame] | 2565 | unsigned DiagOffs; |
| 2566 | if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { |
Chris Lattner | 2ff0f42 | 2009-03-10 23:57:07 +0000 | [diff] [blame] | 2567 | Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) |
| 2568 | << AsmString->getSourceRange(); |
Chris Lattner | fb5058e | 2009-03-10 23:41:04 +0000 | [diff] [blame] | 2569 | return StmtError(); |
| 2570 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2571 | |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 2572 | // Validate tied input operands for type mismatches. |
| 2573 | for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) { |
| 2574 | TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 2576 | // If this is a tied constraint, verify that the output and input have |
| 2577 | // either exactly the same type, or that they are int/ptr operands with the |
| 2578 | // same size (int/long, int*/long, are ok etc). |
| 2579 | if (!Info.hasTiedOperand()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 2581 | unsigned TiedTo = Info.getTiedOperand(); |
Chris Lattner | 935f0f0 | 2011-02-21 22:09:29 +0000 | [diff] [blame] | 2582 | unsigned InputOpNo = i+NumOutputs; |
Chris Lattner | f69fcae | 2009-05-03 07:04:21 +0000 | [diff] [blame] | 2583 | Expr *OutputExpr = Exprs[TiedTo]; |
Chris Lattner | 935f0f0 | 2011-02-21 22:09:29 +0000 | [diff] [blame] | 2584 | Expr *InputExpr = Exprs[InputOpNo]; |
Eli Friedman | f45b357 | 2011-09-14 19:20:00 +0000 | [diff] [blame] | 2585 | |
| 2586 | if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent()) |
| 2587 | continue; |
| 2588 | |
Chris Lattner | 7adaa18 | 2009-05-03 05:59:17 +0000 | [diff] [blame] | 2589 | QualType InTy = InputExpr->getType(); |
| 2590 | QualType OutTy = OutputExpr->getType(); |
| 2591 | if (Context.hasSameType(InTy, OutTy)) |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 2592 | continue; // All types can be tied to themselves. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2593 | |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 2594 | // Decide if the input and output are in the same domain (integer/ptr or |
| 2595 | // floating point. |
| 2596 | enum AsmDomain { |
| 2597 | AD_Int, AD_FP, AD_Other |
| 2598 | } InputDomain, OutputDomain; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2599 | |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 2600 | if (InTy->isIntegerType() || InTy->isPointerType()) |
| 2601 | InputDomain = AD_Int; |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 2602 | else if (InTy->isRealFloatingType()) |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 2603 | InputDomain = AD_FP; |
| 2604 | else |
| 2605 | InputDomain = AD_Other; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2606 | |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 2607 | if (OutTy->isIntegerType() || OutTy->isPointerType()) |
| 2608 | OutputDomain = AD_Int; |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 2609 | else if (OutTy->isRealFloatingType()) |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 2610 | OutputDomain = AD_FP; |
| 2611 | else |
| 2612 | OutputDomain = AD_Other; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2613 | |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 2614 | // They are ok if they are the same size and in the same domain. This |
| 2615 | // allows tying things like: |
| 2616 | // void* to int* |
| 2617 | // void* to int if they are the same size. |
| 2618 | // double to long double if they are the same size. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2619 | // |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 2620 | uint64_t OutSize = Context.getTypeSize(OutTy); |
| 2621 | uint64_t InSize = Context.getTypeSize(InTy); |
| 2622 | if (OutSize == InSize && InputDomain == OutputDomain && |
| 2623 | InputDomain != AD_Other) |
| 2624 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2625 | |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 2626 | // If the smaller input/output operand is not mentioned in the asm string, |
Chris Lattner | f0c4d28 | 2011-02-21 21:50:25 +0000 | [diff] [blame] | 2627 | // then we can promote the smaller one to a larger input and the asm string |
| 2628 | // won't notice. |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 2629 | bool SmallerValueMentioned = false; |
Chris Lattner | ca57b4b | 2011-02-21 21:40:33 +0000 | [diff] [blame] | 2630 | |
| 2631 | // If this is a reference to the input and if the input was the smaller |
| 2632 | // one, then we have to reject this asm. |
Chris Lattner | 935f0f0 | 2011-02-21 22:09:29 +0000 | [diff] [blame] | 2633 | if (isOperandMentioned(InputOpNo, Pieces)) { |
Chris Lattner | ca57b4b | 2011-02-21 21:40:33 +0000 | [diff] [blame] | 2634 | // This is a use in the asm string of the smaller operand. Since we |
| 2635 | // codegen this by promoting to a wider value, the asm will get printed |
| 2636 | // "wrong". |
Chris Lattner | f0c4d28 | 2011-02-21 21:50:25 +0000 | [diff] [blame] | 2637 | SmallerValueMentioned |= InSize < OutSize; |
Chris Lattner | ca57b4b | 2011-02-21 21:40:33 +0000 | [diff] [blame] | 2638 | } |
Chris Lattner | f0c4d28 | 2011-02-21 21:50:25 +0000 | [diff] [blame] | 2639 | if (isOperandMentioned(TiedTo, Pieces)) { |
Chris Lattner | ca57b4b | 2011-02-21 21:40:33 +0000 | [diff] [blame] | 2640 | // If this is a reference to the output, and if the output is the larger |
| 2641 | // value, then it's ok because we'll promote the input to the larger type. |
Chris Lattner | f0c4d28 | 2011-02-21 21:50:25 +0000 | [diff] [blame] | 2642 | SmallerValueMentioned |= OutSize < InSize; |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 2643 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2644 | |
Chris Lattner | aab64d0 | 2010-04-23 17:27:29 +0000 | [diff] [blame] | 2645 | // If the smaller value wasn't mentioned in the asm string, and if the |
| 2646 | // output was a register, just extend the shorter one to the size of the |
| 2647 | // larger one. |
| 2648 | if (!SmallerValueMentioned && InputDomain != AD_Other && |
| 2649 | OutputConstraintInfos[TiedTo].allowsRegister()) |
| 2650 | continue; |
Chris Lattner | f0c4d28 | 2011-02-21 21:50:25 +0000 | [diff] [blame] | 2651 | |
Chris Lattner | 935f0f0 | 2011-02-21 22:09:29 +0000 | [diff] [blame] | 2652 | // Either both of the operands were mentioned or the smaller one was |
| 2653 | // mentioned. One more special case that we'll allow: if the tied input is |
| 2654 | // integer, unmentioned, and is a constant, then we'll allow truncating it |
| 2655 | // down to the size of the destination. |
| 2656 | if (InputDomain == AD_Int && OutputDomain == AD_Int && |
| 2657 | !isOperandMentioned(InputOpNo, Pieces) && |
| 2658 | InputExpr->isEvaluatable(Context)) { |
John McCall | 4da89c8 | 2011-05-10 23:39:47 +0000 | [diff] [blame] | 2659 | CastKind castKind = |
| 2660 | (OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast); |
| 2661 | InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).take(); |
Chris Lattner | 935f0f0 | 2011-02-21 22:09:29 +0000 | [diff] [blame] | 2662 | Exprs[InputOpNo] = InputExpr; |
| 2663 | NS->setInputExpr(i, InputExpr); |
| 2664 | continue; |
| 2665 | } |
| 2666 | |
Chris Lattner | c1f3b28 | 2009-05-03 06:50:40 +0000 | [diff] [blame] | 2667 | Diag(InputExpr->getLocStart(), |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 2668 | diag::err_asm_tying_incompatible_types) |
Chris Lattner | 7adaa18 | 2009-05-03 05:59:17 +0000 | [diff] [blame] | 2669 | << InTy << OutTy << OutputExpr->getSourceRange() |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 2670 | << InputExpr->getSourceRange(); |
Chris Lattner | 806503f | 2009-05-03 05:55:43 +0000 | [diff] [blame] | 2671 | return StmtError(); |
| 2672 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2673 | |
Chris Lattner | fb5058e | 2009-03-10 23:41:04 +0000 | [diff] [blame] | 2674 | return Owned(NS); |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 2675 | } |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 2676 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2677 | StmtResult |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 2678 | Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2679 | SourceLocation RParen, Decl *Parm, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2680 | Stmt *Body) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2681 | VarDecl *Var = cast_or_null<VarDecl>(Parm); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 2682 | if (Var && Var->isInvalidDecl()) |
| 2683 | return StmtError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2684 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2685 | return Owned(new (Context) ObjCAtCatchStmt(AtLoc, RParen, Var, Body)); |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2688 | StmtResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2689 | Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, Stmt *Body) { |
| 2690 | return Owned(new (Context) ObjCAtFinallyStmt(AtLoc, Body)); |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 2691 | } |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 2692 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2693 | StmtResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2694 | Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc, Stmt *Try, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2695 | MultiStmtArg CatchStmts, Stmt *Finally) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2696 | if (!getLangOpts().ObjCExceptions) |
Anders Carlsson | da4b7cf | 2011-02-19 23:53:54 +0000 | [diff] [blame] | 2697 | Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@try"; |
| 2698 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 2699 | getCurFunction()->setHasBranchProtectedScope(); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2700 | unsigned NumCatchStmts = CatchStmts.size(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2701 | return Owned(ObjCAtTryStmt::Create(Context, AtLoc, Try, |
| 2702 | CatchStmts.release(), |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2703 | NumCatchStmts, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2704 | Finally)); |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
John McCall | d1376ee | 2012-05-08 21:41:25 +0000 | [diff] [blame] | 2707 | StmtResult Sema::BuildObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw) { |
Douglas Gregor | d1377b2 | 2010-04-22 21:44:01 +0000 | [diff] [blame] | 2708 | if (Throw) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2709 | ExprResult Result = DefaultLvalueConversion(Throw); |
| 2710 | if (Result.isInvalid()) |
| 2711 | return StmtError(); |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 2712 | |
John McCall | d1376ee | 2012-05-08 21:41:25 +0000 | [diff] [blame] | 2713 | Throw = MaybeCreateExprWithCleanups(Result.take()); |
Douglas Gregor | d1377b2 | 2010-04-22 21:44:01 +0000 | [diff] [blame] | 2714 | QualType ThrowType = Throw->getType(); |
| 2715 | // Make sure the expression type is an ObjC pointer or "void *". |
| 2716 | if (!ThrowType->isDependentType() && |
| 2717 | !ThrowType->isObjCObjectPointerType()) { |
| 2718 | const PointerType *PT = ThrowType->getAs<PointerType>(); |
| 2719 | if (!PT || !PT->getPointeeType()->isVoidType()) |
| 2720 | return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object) |
| 2721 | << Throw->getType() << Throw->getSourceRange()); |
| 2722 | } |
| 2723 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2724 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2725 | return Owned(new (Context) ObjCAtThrowStmt(AtLoc, Throw)); |
Douglas Gregor | d1377b2 | 2010-04-22 21:44:01 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2728 | StmtResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2729 | Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw, |
Douglas Gregor | d1377b2 | 2010-04-22 21:44:01 +0000 | [diff] [blame] | 2730 | Scope *CurScope) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2731 | if (!getLangOpts().ObjCExceptions) |
Anders Carlsson | da4b7cf | 2011-02-19 23:53:54 +0000 | [diff] [blame] | 2732 | Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@throw"; |
| 2733 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2734 | if (!Throw) { |
Steve Naroff | e21dd6f | 2009-02-11 20:05:44 +0000 | [diff] [blame] | 2735 | // @throw without an expression designates a rethrow (which much occur |
| 2736 | // in the context of an @catch clause). |
| 2737 | Scope *AtCatchParent = CurScope; |
| 2738 | while (AtCatchParent && !AtCatchParent->isAtCatchScope()) |
| 2739 | AtCatchParent = AtCatchParent->getParent(); |
| 2740 | if (!AtCatchParent) |
Steve Naroff | 4ab2414 | 2009-02-12 18:09:32 +0000 | [diff] [blame] | 2741 | return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2742 | } |
Fariborz Jahanian | f2dd68f | 2011-07-20 23:39:56 +0000 | [diff] [blame] | 2743 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2744 | return BuildObjCAtThrowStmt(AtLoc, Throw); |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 2745 | } |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 2746 | |
John McCall | 0752403 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 2747 | ExprResult |
| 2748 | Sema::ActOnObjCAtSynchronizedOperand(SourceLocation atLoc, Expr *operand) { |
| 2749 | ExprResult result = DefaultLvalueConversion(operand); |
| 2750 | if (result.isInvalid()) |
| 2751 | return ExprError(); |
| 2752 | operand = result.take(); |
| 2753 | |
| 2754 | // Make sure the expression type is an ObjC pointer or "void *". |
| 2755 | QualType type = operand->getType(); |
| 2756 | if (!type->isDependentType() && |
| 2757 | !type->isObjCObjectPointerType()) { |
| 2758 | const PointerType *pointerType = type->getAs<PointerType>(); |
| 2759 | if (!pointerType || !pointerType->getPointeeType()->isVoidType()) |
| 2760 | return Diag(atLoc, diag::error_objc_synchronized_expects_object) |
| 2761 | << type << operand->getSourceRange(); |
| 2762 | } |
| 2763 | |
| 2764 | // The operand to @synchronized is a full-expression. |
| 2765 | return MaybeCreateExprWithCleanups(operand); |
| 2766 | } |
| 2767 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2768 | StmtResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2769 | Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, Expr *SyncExpr, |
| 2770 | Stmt *SyncBody) { |
John McCall | 0752403 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 2771 | // We can't jump into or indirect-jump out of a @synchronized block. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 2772 | getCurFunction()->setHasBranchProtectedScope(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2773 | return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc, SyncExpr, SyncBody)); |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 2774 | } |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2775 | |
| 2776 | /// ActOnCXXCatchBlock - Takes an exception declaration and a handler block |
| 2777 | /// and creates a proper catch handler from them. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2778 | StmtResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2779 | Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, Decl *ExDecl, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2780 | Stmt *HandlerBlock) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2781 | // There's nothing to test that ActOnExceptionDecl didn't already test. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2782 | return Owned(new (Context) CXXCatchStmt(CatchLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2783 | cast_or_null<VarDecl>(ExDecl), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2784 | HandlerBlock)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 2785 | } |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 2786 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2787 | StmtResult |
| 2788 | Sema::ActOnObjCAutoreleasePoolStmt(SourceLocation AtLoc, Stmt *Body) { |
| 2789 | getCurFunction()->setHasBranchProtectedScope(); |
| 2790 | return Owned(new (Context) ObjCAutoreleasePoolStmt(AtLoc, Body)); |
| 2791 | } |
| 2792 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2793 | namespace { |
| 2794 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2795 | class TypeWithHandler { |
| 2796 | QualType t; |
| 2797 | CXXCatchStmt *stmt; |
| 2798 | public: |
| 2799 | TypeWithHandler(const QualType &type, CXXCatchStmt *statement) |
| 2800 | : t(type), stmt(statement) {} |
| 2801 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2802 | // An arbitrary order is fine as long as it places identical |
| 2803 | // types next to each other. |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2804 | bool operator<(const TypeWithHandler &y) const { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2805 | if (t.getAsOpaquePtr() < y.t.getAsOpaquePtr()) |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2806 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2807 | if (t.getAsOpaquePtr() > y.t.getAsOpaquePtr()) |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2808 | return false; |
| 2809 | else |
| 2810 | return getTypeSpecStartLoc() < y.getTypeSpecStartLoc(); |
| 2811 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2812 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2813 | bool operator==(const TypeWithHandler& other) const { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2814 | return t == other.t; |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2815 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2816 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2817 | CXXCatchStmt *getCatchStmt() const { return stmt; } |
| 2818 | SourceLocation getTypeSpecStartLoc() const { |
| 2819 | return stmt->getExceptionDecl()->getTypeSpecStartLoc(); |
| 2820 | } |
| 2821 | }; |
| 2822 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 2825 | /// ActOnCXXTryBlock - Takes a try compound-statement and a number of |
| 2826 | /// handlers and creates a try statement from them. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2827 | StmtResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2828 | Sema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock, |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 2829 | MultiStmtArg RawHandlers) { |
Anders Carlsson | 729b853 | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 2830 | // Don't report an error if 'try' is used in system headers. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2831 | if (!getLangOpts().CXXExceptions && |
Anders Carlsson | 729b853 | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 2832 | !getSourceManager().isInSystemHeader(TryLoc)) |
| 2833 | Diag(TryLoc, diag::err_exceptions_disabled) << "try"; |
Anders Carlsson | 7f11d9c | 2011-02-19 19:26:44 +0000 | [diff] [blame] | 2834 | |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 2835 | unsigned NumHandlers = RawHandlers.size(); |
| 2836 | assert(NumHandlers > 0 && |
| 2837 | "The parser shouldn't call this if there are no handlers."); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2838 | Stmt **Handlers = RawHandlers.get(); |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 2839 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2840 | SmallVector<TypeWithHandler, 8> TypesWithHandlers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2841 | |
| 2842 | for (unsigned i = 0; i < NumHandlers; ++i) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2843 | CXXCatchStmt *Handler = cast<CXXCatchStmt>(Handlers[i]); |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2844 | if (!Handler->getExceptionDecl()) { |
| 2845 | if (i < NumHandlers - 1) |
| 2846 | return StmtError(Diag(Handler->getLocStart(), |
| 2847 | diag::err_early_catch_all)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2849 | continue; |
| 2850 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2852 | const QualType CaughtType = Handler->getCaughtType(); |
| 2853 | const QualType CanonicalCaughtType = Context.getCanonicalType(CaughtType); |
| 2854 | TypesWithHandlers.push_back(TypeWithHandler(CanonicalCaughtType, Handler)); |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 2855 | } |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2856 | |
| 2857 | // Detect handlers for the same type as an earlier one. |
| 2858 | if (NumHandlers > 1) { |
| 2859 | llvm::array_pod_sort(TypesWithHandlers.begin(), TypesWithHandlers.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2860 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2861 | TypeWithHandler prev = TypesWithHandlers[0]; |
| 2862 | for (unsigned i = 1; i < TypesWithHandlers.size(); ++i) { |
| 2863 | TypeWithHandler curr = TypesWithHandlers[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2864 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2865 | if (curr == prev) { |
| 2866 | Diag(curr.getTypeSpecStartLoc(), |
| 2867 | diag::warn_exception_caught_by_earlier_handler) |
| 2868 | << curr.getCatchStmt()->getCaughtType().getAsString(); |
| 2869 | Diag(prev.getTypeSpecStartLoc(), |
| 2870 | diag::note_previous_exception_handler) |
| 2871 | << prev.getCatchStmt()->getCaughtType().getAsString(); |
| 2872 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2873 | |
Sebastian Redl | c447aba | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 2874 | prev = curr; |
| 2875 | } |
| 2876 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2877 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 2878 | getCurFunction()->setHasBranchProtectedScope(); |
John McCall | b60a77e | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 2879 | |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 2880 | // FIXME: We should detect handlers that cannot catch anything because an |
| 2881 | // earlier handler catches a superclass. Need to find a method that is not |
| 2882 | // quadratic for this. |
| 2883 | // Neither of these are explicitly forbidden, but every compiler detects them |
| 2884 | // and warns. |
| 2885 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2886 | return Owned(CXXTryStmt::Create(Context, TryLoc, TryBlock, |
Sam Weinig | a1a396d | 2010-02-03 03:56:39 +0000 | [diff] [blame] | 2887 | Handlers, NumHandlers)); |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 2888 | } |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2889 | |
| 2890 | StmtResult |
| 2891 | Sema::ActOnSEHTryBlock(bool IsCXXTry, |
| 2892 | SourceLocation TryLoc, |
| 2893 | Stmt *TryBlock, |
| 2894 | Stmt *Handler) { |
| 2895 | assert(TryBlock && Handler); |
| 2896 | |
| 2897 | getCurFunction()->setHasBranchProtectedScope(); |
| 2898 | |
| 2899 | return Owned(SEHTryStmt::Create(Context,IsCXXTry,TryLoc,TryBlock,Handler)); |
| 2900 | } |
| 2901 | |
| 2902 | StmtResult |
| 2903 | Sema::ActOnSEHExceptBlock(SourceLocation Loc, |
| 2904 | Expr *FilterExpr, |
| 2905 | Stmt *Block) { |
| 2906 | assert(FilterExpr && Block); |
| 2907 | |
| 2908 | if(!FilterExpr->getType()->isIntegerType()) { |
Francois Pichet | 58f14c0 | 2011-06-02 00:47:27 +0000 | [diff] [blame] | 2909 | return StmtError(Diag(FilterExpr->getExprLoc(), |
| 2910 | diag::err_filter_expression_integral) |
| 2911 | << FilterExpr->getType()); |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
| 2914 | return Owned(SEHExceptStmt::Create(Context,Loc,FilterExpr,Block)); |
| 2915 | } |
| 2916 | |
| 2917 | StmtResult |
| 2918 | Sema::ActOnSEHFinallyBlock(SourceLocation Loc, |
| 2919 | Stmt *Block) { |
| 2920 | assert(Block); |
| 2921 | return Owned(SEHFinallyStmt::Create(Context,Loc,Block)); |
| 2922 | } |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 2923 | |
| 2924 | StmtResult Sema::BuildMSDependentExistsStmt(SourceLocation KeywordLoc, |
| 2925 | bool IsIfExists, |
| 2926 | NestedNameSpecifierLoc QualifierLoc, |
| 2927 | DeclarationNameInfo NameInfo, |
| 2928 | Stmt *Nested) |
| 2929 | { |
| 2930 | return new (Context) MSDependentExistsStmt(KeywordLoc, IsIfExists, |
| 2931 | QualifierLoc, NameInfo, |
| 2932 | cast<CompoundStmt>(Nested)); |
| 2933 | } |
| 2934 | |
| 2935 | |
| 2936 | StmtResult Sema::ActOnMSDependentExistsStmt(SourceLocation KeywordLoc, |
| 2937 | bool IsIfExists, |
| 2938 | CXXScopeSpec &SS, |
| 2939 | UnqualifiedId &Name, |
| 2940 | Stmt *Nested) { |
| 2941 | return BuildMSDependentExistsStmt(KeywordLoc, IsIfExists, |
| 2942 | SS.getWithLocInContext(Context), |
| 2943 | GetNameFromUnqualifiedId(Name), |
| 2944 | Nested); |
| 2945 | } |