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