SemaStmt.cpp revision 2d88708cbe4e4ec5e04e4acb6bd7f5be68557379
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements semantic analysis for statements.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
142d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
155f1e0942a32657b625702aa52f82430d0120f424John McCall#include "clang/Sema/Scope.h"
16781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "clang/Sema/ScopeInfo.h"
17e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Initialization.h"
1851fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson#include "clang/AST/APValue.h"
19f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner#include "clang/AST/ASTContext.h"
20c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
2184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/AST/ExprCXX.h"
22419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner#include "clang/AST/ExprObjC.h"
2316f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h"
2416f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtCXX.h"
25209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall#include "clang/AST/TypeLoc.h"
2684fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/Lex/Preprocessor.h"
276fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson#include "clang/Basic/TargetInfo.h"
28c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/STLExtras.h"
29c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/SmallVector.h"
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
31781472fe99a120098c631b0cbe33c89f8cef5e70John McCallusing namespace sema;
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnExprStmt(FullExprArg expr) {
349ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Expr *E = expr.get();
351b273c403734d343d720acb28f04011807c8aa56Steve Naroff  assert(E && "ActOnExprStmt(): missing expression");
36834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // C99 6.8.3p2: The expression in an expression statement is evaluated as a
37834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // void expression for its side effects.  Conversion to void allows any
38834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // operand, even incomplete types.
39a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl
40834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // Same thing in for stmt first clause (when expr) and third clause.
41a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  return Owned(static_cast<Stmt*>(E));
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
468189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) NullStmt(SemiLoc));
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg,
50a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                           SourceLocation StartLoc,
51a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                           SourceLocation EndLoc) {
5220401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner  DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
54682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // If we have an invalid decl, just return an error.
5520401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner  if (DG.isNull()) return StmtError();
561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5724e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
60a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanianvoid Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) {
61a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
62a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian
63a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  // If we have an invalid decl, just return.
64a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  if (DG.isNull() || !DG.isSingleDecl()) return;
65a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  // suppress any potential 'unused variable' warning.
66a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  DG.getSingleDecl()->setUsed();
67a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian}
68a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian
69636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlssonvoid Sema::DiagnoseUnusedExprResult(const Stmt *S) {
70754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  const Expr *E = dyn_cast_or_null<Expr>(S);
71636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  if (!E)
72636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
73636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
74636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  SourceLocation Loc;
75636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  SourceRange R1, R2;
76df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump  if (!E->isUnusedResultAWarning(Loc, R1, R2, Context))
77636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
79419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // Okay, we have an unused result.  Depending on what the base expression is,
80419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // we might want to make a more specific diagnostic.  Check for one of these
81419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // cases now.
82419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  unsigned DiagID = diag::warn_unused_expr;
83419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  E = E->IgnoreParens();
8409105f52b1f28cbb1374c27c3c70f5517e2c465dFariborz Jahanian  if (isa<ObjCImplicitSetterGetterRefExpr>(E))
85419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner    DiagID = diag::warn_unused_property_expr;
86bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner
874dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor  if (const CXXExprWithTemporaries *Temps = dyn_cast<CXXExprWithTemporaries>(E))
884dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor    E = Temps->getSubExpr();
894dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor
90bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner  if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
910faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (E->getType()->isVoidType())
920faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return;
930faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall
94bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    // If the callee has attribute pure, const, or warn_unused_result, warn with
95bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    // a more specific message to make it clear what is happening.
96d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    if (const Decl *FD = CE->getCalleeDecl()) {
97bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<WarnUnusedResultAttr>()) {
98bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result";
99bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
100bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
101bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<PureAttr>()) {
102bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure";
103bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
104bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
105bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<ConstAttr>()) {
106bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const";
107bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
108bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
109bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    }
110bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner  }
111f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) {
112f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMethodDecl *MD = ME->getMethodDecl();
113f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
114f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result";
115f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      return;
116f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    }
117d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor  } else if (const CXXFunctionalCastExpr *FC
118d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor                                       = dyn_cast<CXXFunctionalCastExpr>(E)) {
119d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor    if (isa<CXXConstructExpr>(FC->getSubExpr()) ||
120d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor        isa<CXXTemporaryObjectExpr>(FC->getSubExpr()))
121d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor      return;
122f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  }
123209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall  // Diagnose "(void*) blah" as a typo for "(void) blah".
124209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall  else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) {
125209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    TypeSourceInfo *TI = CE->getTypeInfoAsWritten();
126209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    QualType T = TI->getType();
127209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall
128209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    // We really do want to use the non-canonical type here.
129209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    if (T == Context.VoidPtrTy) {
130209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall      PointerTypeLoc TL = cast<PointerTypeLoc>(TI->getTypeLoc());
131209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall
132209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall      Diag(Loc, diag::warn_unused_voidptr)
133209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall        << FixItHint::CreateRemoval(TL.getStarLoc());
134209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall      return;
135209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    }
136209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall  }
137209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall
13835e12c90c1b107a75c5615aa76fdbd403661aaa6Douglas Gregor  DiagRuntimeBehavior(Loc, PDiag(DiagID) << R1 << R2);
139636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson}
140636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
14160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1421b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
143a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                        MultiStmtArg elts, bool isStmtExpr) {
144a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  unsigned NumElts = elts.size();
145a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
146c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  // If we're in C89 mode, check that we don't have any decls after stmts.  If
147c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  // so, emit an extension diagnostic.
148c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
149c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // Note that __extension__ can be around a decl.
150c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    unsigned i = 0;
151c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // Skip over all declarations.
152c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
153c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      /*empty*/;
154c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner
155c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // We found the end of the list or a statement.  Scan for another declstmt.
156c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
157c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      /*empty*/;
1581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
159c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    if (i != NumElts) {
1604afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
161c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      Diag(D->getLocation(), diag::ext_mixed_decls_code);
162c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    }
163c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  }
16498414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  // Warn about unused expressions in statements.
16598414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  for (unsigned i = 0; i != NumElts; ++i) {
166636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    // Ignore statements that are last in a statement expression.
167636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    if (isStmtExpr && i == NumElts - 1)
16898414c1b7d1944a57156d52e29bd41c005de09acChris Lattner      continue;
1691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
170636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    DiagnoseUnusedExprResult(Elts[i]);
17198414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  }
172a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl
1738189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R));
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1779ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCaseStmt(SourceLocation CaseLoc, Expr *LHSVal,
1789ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                    SourceLocation DotDotDotLoc, Expr *RHSVal,
17924e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner                    SourceLocation ColonLoc) {
1809ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  assert((LHSVal != 0) && "missing expression in case statement");
181117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.4.2p3: The expression shall be an integer constant.
1831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // However, GCC allows any evaluatable integer expression.
1841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent() &&
185dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      VerifyIntegerConstantExpression(LHSVal))
18624e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner    return StmtError();
1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1886c36be5b383875b490684bcf439d6d427298c1afChris Lattner  // GCC extension: The expression shall be an integer constant.
189117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
190dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent() &&
191dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      VerifyIntegerConstantExpression(RHSVal)) {
192f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    RHSVal = 0;  // Recover by just forgetting about it.
193117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  }
194117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
195781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (getCurFunction()->SwitchStack.empty()) {
1968a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner    Diag(CaseLoc, diag::err_case_not_in_switch);
19724e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner    return StmtError();
1988a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner  }
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
200dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc,
201dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                        ColonLoc);
202781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->SwitchStack.back()->addSwitchCase(CS);
203117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  return Owned(CS);
2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20624e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner/// ActOnCaseStmtBody - This installs a statement as the body of a case.
2079ae2f076ca5ab1feb3ba95629099ec2319833701John McCallvoid Sema::ActOnCaseStmtBody(Stmt *caseStmt, Stmt *SubStmt) {
20824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  CaseStmt *CS = static_cast<CaseStmt*>(caseStmt);
20924e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  CS->setSubStmt(SubStmt);
21024e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner}
21124e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner
21260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
2131eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
2149ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                       Stmt *SubStmt, Scope *CurScope) {
215781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (getCurFunction()->SwitchStack.empty()) {
2160fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner    Diag(DefaultLoc, diag::err_default_not_in_switch);
217117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl    return Owned(SubStmt);
2180fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner  }
219117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
220dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt);
221781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->SwitchStack.back()->addSwitchCase(DS);
222117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  return Owned(DS);
2235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
2261b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
2279ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                     SourceLocation ColonLoc, Stmt *SubStmt) {
2285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Look up the record for this label identifier.
229781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  LabelStmt *&LabelDecl = getCurFunction()->LabelMap[II];
230de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If not forward referenced or defined already, just create a new LabelStmt.
232caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  if (LabelDecl == 0)
233caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff    return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt));
234de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(LabelDecl->getID() == II && "Label mismatch!");
236de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, this label was either forward reference or multiply defined.  If
2385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // multiply defined, reject it now.
2395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (LabelDecl->getSubStmt()) {
24008631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID();
2415f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner    Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition);
242de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl    return Owned(SubStmt);
2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
244de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, this label was forward declared, and we just found its real
2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // definition.  Fill in the forward definition and return it.
2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LabelDecl->setIdentLoc(IdentLoc);
2480fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner  LabelDecl->setSubStmt(SubStmt);
249de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(LabelDecl);
2505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
25260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
253d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, Decl *CondVar,
2549ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                  Stmt *thenStmt, SourceLocation ElseLoc,
2559ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                  Stmt *elseStmt) {
25660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult CondResult(CondVal.release());
2571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2588cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  VarDecl *ConditionVar = 0;
259d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (CondVar) {
260d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    ConditionVar = cast<VarDecl>(CondVar);
261586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    CondResult = CheckConditionVariable(ConditionVar, IfLoc, true);
26299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (CondResult.isInvalid())
26399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return StmtError();
2648cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  }
26599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  Expr *ConditionExpr = CondResult.takeAs<Expr>();
26699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!ConditionExpr)
26799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    return StmtError();
2688cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor
269754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(thenStmt);
2705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2712d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // Warn if the if block has a null body without an else value.
2722d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // this helps prevent bugs due to typos, such as
2732d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // if (condition);
2742d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  //   do_stuff();
2759ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!elseStmt) {
2762d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson    if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
2772d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson      Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
2782d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  }
2792d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson
280754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(elseStmt);
2811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28243dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  return Owned(new (Context) IfStmt(Context, IfLoc, ConditionVar, ConditionExpr,
28399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor                                    thenStmt, ElseLoc, elseStmt));
2845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
286f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
287f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified width and sign.  If an overflow occurs, detect it and emit
288f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified diagnostic.
289f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattnervoid Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
290f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                              unsigned NewWidth, bool NewSign,
2911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              SourceLocation Loc,
292f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                              unsigned DiagID) {
293f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Perform a conversion to the promoted condition type if needed.
294f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  if (NewWidth > Val.getBitWidth()) {
295f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If this is an extension, just do it.
296f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.extend(NewWidth);
297f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.setIsSigned(NewSign);
298f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor
299f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // If the input was signed and negative and the output is
300f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // unsigned, don't bother to warn: this is implementation-defined
301f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // behavior.
302f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // FIXME: Introduce a second, default-ignored warning for this case?
303f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  } else if (NewWidth < Val.getBitWidth()) {
304f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If this is a truncation, check for overflow.
305f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt ConvVal(Val);
306f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    ConvVal.trunc(NewWidth);
307b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    ConvVal.setIsSigned(NewSign);
308f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    ConvVal.extend(Val.getBitWidth());
309b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    ConvVal.setIsSigned(Val.isSigned());
310f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    if (ConvVal != Val)
311d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
3121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
313f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // Regardless of whether a diagnostic was emitted, really do the
314f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // truncation.
315f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.trunc(NewWidth);
316b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    Val.setIsSigned(NewSign);
317f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  } else if (NewSign != Val.isSigned()) {
318f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // Convert the sign to match the sign of the condition.  This can cause
319f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // overflow as well: unsigned(INTMIN)
3202853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor    // We don't diagnose this overflow, because it is implementation-defined
3212853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor    // behavior.
3222853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor    // FIXME: Introduce a second, default-ignored warning for this case?
323f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt OldVal(Val);
324f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.setIsSigned(NewSign);
325f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  }
326f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner}
327f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
3280471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattnernamespace {
3290471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  struct CaseCompareFunctor {
3300471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
3310471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner                    const llvm::APSInt &RHS) {
3320471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      return LHS.first < RHS;
3330471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    }
3340e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner    bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
3350e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner                    const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
3360e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner      return LHS.first < RHS.first;
3370e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner    }
3380471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    bool operator()(const llvm::APSInt &LHS,
3390471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner                    const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
3400471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      return LHS < RHS.first;
3410471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    }
3420471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  };
3430471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner}
3440471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner
345764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// CmpCaseVals - Comparison predicate for sorting case values.
346764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner///
347764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattnerstatic bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
348764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner                        const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
349764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  if (lhs.first < rhs.first)
350764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner    return true;
351764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner
352764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  if (lhs.first == rhs.first &&
353764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner      lhs.second->getCaseLoc().getRawEncoding()
354764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner       < rhs.second->getCaseLoc().getRawEncoding())
355764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner    return true;
356764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  return false;
357764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner}
358764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner
359ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// CmpEnumVals - Comparison predicate for sorting enumeration values.
360ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor///
361ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
362ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor                        const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
363ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{
364ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  return lhs.first < rhs.first;
365ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor}
366ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
367ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// EqEnumVals - Comparison preficate for uniqing enumeration values.
368ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor///
369ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
370ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor                       const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
371ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{
372ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  return lhs.first == rhs.first;
373ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor}
374ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
3755f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of
3765f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// potentially integral-promoted expression @p expr.
3775f04881eb025f61396d0555d8173730fe2759e0aChris Lattnerstatic QualType GetTypeBeforeIntegralPromotion(const Expr* expr) {
3786907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  if (const CastExpr *ImplicitCast = dyn_cast<ImplicitCastExpr>(expr)) {
3795f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    const Expr *ExprBeforePromotion = ImplicitCast->getSubExpr();
3805f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    QualType TypeBeforePromotion = ExprBeforePromotion->getType();
3812ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    if (TypeBeforePromotion->isIntegralOrEnumerationType()) {
3825f04881eb025f61396d0555d8173730fe2759e0aChris Lattner      return TypeBeforePromotion;
3835f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    }
3845f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  }
3855f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  return expr->getType();
3865f04881eb025f61396d0555d8173730fe2759e0aChris Lattner}
3875f04881eb025f61396d0555d8173730fe2759e0aChris Lattner
38860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
3899ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnStartOfSwitchStmt(SourceLocation SwitchLoc, Expr *Cond,
390d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                             Decl *CondVar) {
39160d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult CondResult;
3929ae2f076ca5ab1feb3ba95629099ec2319833701John McCall
393586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  VarDecl *ConditionVar = 0;
394d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (CondVar) {
395d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    ConditionVar = cast<VarDecl>(CondVar);
3969ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    CondResult = CheckConditionVariable(ConditionVar, SourceLocation(), false);
3979ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (CondResult.isInvalid())
398586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      return StmtError();
399eecf38f821fe8e113722096b77da7d68b26d28d1Douglas Gregor
4009ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    Cond = CondResult.release();
401586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  }
402586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
4039ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!Cond)
404586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    return StmtError();
405586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
4069ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  CondResult
4079ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    = ConvertToIntegralOrEnumerationType(SwitchLoc, Cond,
408c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                          PDiag(diag::err_typecheck_statement_requires_integer),
409c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                                   PDiag(diag::err_switch_incomplete_class_type)
4109ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     << Cond->getSourceRange(),
411c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                                   PDiag(diag::err_switch_explicit_conversion),
412c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                                         PDiag(diag::note_switch_conversion),
413c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                                   PDiag(diag::err_switch_multiple_conversions),
4146bc574daab3d3571d888cc4a21df67f2e2a14792Douglas Gregor                                         PDiag(diag::note_switch_conversion),
4156bc574daab3d3571d888cc4a21df67f2e2a14792Douglas Gregor                                         PDiag(0));
4169ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (CondResult.isInvalid()) return StmtError();
4179ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Cond = CondResult.take();
418c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor
419d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (!CondVar) {
4209ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    CondResult = MaybeCreateCXXExprWithTemporaries(Cond);
4219ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (CondResult.isInvalid())
422586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      return StmtError();
4239ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    Cond = CondResult.take();
424586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  }
425b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
426781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->setHasBranchIntoScope();
427586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
4289ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  SwitchStmt *SS = new (Context) SwitchStmt(Context, ConditionVar, Cond);
429781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->SwitchStack.push_back(SS);
430586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  return Owned(SS);
4317e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner}
4327e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner
43360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
4349ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
4359ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                            Stmt *BodyStmt) {
4369ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  SwitchStmt *SS = cast<SwitchStmt>(Switch);
437781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  assert(SS == getCurFunction()->SwitchStack.back() &&
438781472fe99a120098c631b0cbe33c89f8cef5e70John McCall         "switch stack missing push/pop!");
439de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
4409dcbfa450d751bd68fc4af8b75da381d4f6984b9Steve Naroff  SS->setBody(BodyStmt, SwitchLoc);
441781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->SwitchStack.pop_back();
442c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson
443ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (SS->getCond() == 0)
444be724bab2ba7ad47aebced25e7c8ec551eb72d28Douglas Gregor    return StmtError();
445be724bab2ba7ad47aebced25e7c8ec551eb72d28Douglas Gregor
446f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  Expr *CondExpr = SS->getCond();
4470fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall  Expr *CondExprBeforePromotion = CondExpr;
44884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  QualType CondTypeBeforePromotion =
44984fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor      GetTypeBeforeIntegralPromotion(CondExpr);
450de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
4510de55e7e6b8a53c5d1f2e9a811fd0a4ea13ed5b0Douglas Gregor  // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
4520de55e7e6b8a53c5d1f2e9a811fd0a4ea13ed5b0Douglas Gregor  UsualUnaryConversions(CondExpr);
453a0d3ca1ea5578bc736bb71bcec50ab41fefc87b9Douglas Gregor  QualType CondType = CondExpr->getType();
45484fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  SS->setCond(CondExpr);
45584fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor
4565f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // C++ 6.4.2.p2:
4575f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // Integral promotions are performed (on the switch condition).
4585f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  //
4595f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // A case value unrepresentable by the original switch condition
4605f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // type (before the promotion) doesn't make sense, even when it can
4615f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // be represented by the promoted type.  Therefore we need to find
4625f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // the pre-promotion type of the switch condition.
46312356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan  if (!CondExpr->isTypeDependent()) {
464acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor    // We have already converted the expression to an integral or enumeration
465acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor    // type, when we started the switch statement. If we don't have an
466acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor    // appropriate type now, just return an error.
467acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor    if (!CondType->isIntegralOrEnumerationType())
46812356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      return StmtError();
46912356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan
4702b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    if (CondExpr->isKnownToHaveBooleanValue()) {
47112356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      // switch(bool_expr) {...} is often a programmer error, e.g.
47212356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      //   switch(n && mask) { ... }  // Doh - should be "n & mask".
47312356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      // One can always use an if statement instead of switch(bool_expr).
47412356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      Diag(SwitchLoc, diag::warn_bool_switch_condition)
47512356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan          << CondExpr->getSourceRange();
47612356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan    }
477c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson  }
478de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
479f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Get the bitwidth of the switched-on value before promotions.  We must
480f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // convert the integer case values to this width before comparison.
4811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  bool HasDependentValue
482dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    = CondExpr->isTypeDependent() || CondExpr->isValueDependent();
4831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned CondWidth
484dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    = HasDependentValue? 0
4855f04881eb025f61396d0555d8173730fe2759e0aChris Lattner      : static_cast<unsigned>(Context.getTypeSize(CondTypeBeforePromotion));
4865f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  bool CondIsSigned = CondTypeBeforePromotion->isSignedIntegerType();
4871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
488f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Accumulate all of the case values in a vector so that we can sort them
489f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // and detect duplicates.  This vector contains the APInt for the case after
490f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // it has been converted to the condition type.
4910471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
4920471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  CaseValsTy CaseVals;
4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
494f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Keep track of any GNU case ranges we see.  The APSInt is the low value.
495ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy;
496ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  CaseRangesTy CaseRanges;
4971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
498f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  DefaultStmt *TheDefaultStmt = 0;
4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
500b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  bool CaseListIsErroneous = false;
5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
502dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue;
503c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson       SC = SC->getNextSwitchCase()) {
5041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
505c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson    if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
506f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      if (TheDefaultStmt) {
507f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
5085f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner        Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
509de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
510f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        // FIXME: Remove the default statement from the switch block so that
511390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // we'll return a valid AST.  This requires recursing down the AST and
512390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // finding it, not something we are set up to do right now.  For now,
513390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // just lop the entire switch stmt out of the AST.
514b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner        CaseListIsErroneous = true;
515c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson      }
516f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      TheDefaultStmt = DS;
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
518f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    } else {
519f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      CaseStmt *CS = cast<CaseStmt>(SC);
5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
521f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // We already verified that the expression has a i-c-e value (C99
522f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // 6.8.4.2p3) - get that value now.
5231e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      Expr *Lo = CS->getLHS();
524dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
525dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      if (Lo->isTypeDependent() || Lo->isValueDependent()) {
526dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        HasDependentValue = true;
527dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        break;
528dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      }
5291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53051fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson      llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
532f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // Convert the value to the same width/sign as the condition.
533f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
534f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                         CS->getLHS()->getLocStart(),
535f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                         diag::warn_case_value_overflow);
5366c36be5b383875b490684bcf439d6d427298c1afChris Lattner
5371e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      // If the LHS is not the same type as the condition, insert an implicit
5381e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      // cast.
5392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      ImpCastExprToType(Lo, CondType, CK_IntegralCast);
5401e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      CS->setLHS(Lo);
5411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
542b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner      // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
543dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      if (CS->getRHS()) {
5441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (CS->getRHS()->isTypeDependent() ||
545dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            CS->getRHS()->isValueDependent()) {
546dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          HasDependentValue = true;
547dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          break;
548dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
549f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        CaseRanges.push_back(std::make_pair(LoVal, CS));
5501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else
551b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner        CaseVals.push_back(std::make_pair(LoVal, CS));
552f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    }
553f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  }
554b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner
555dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (!HasDependentValue) {
5560fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // If we don't have a default statement, check whether the
5570fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // condition is constant.
5580fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    llvm::APSInt ConstantCondValue;
5590fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    bool HasConstantCond = false;
5600fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    bool ShouldCheckConstantCond = false;
5610fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    if (!HasDependentValue && !TheDefaultStmt) {
5620fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      Expr::EvalResult Result;
5630fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      HasConstantCond = CondExprBeforePromotion->Evaluate(Result, Context);
5640fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      if (HasConstantCond) {
5650fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        assert(Result.Val.isInt() && "switch condition evaluated to non-int");
5660fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        ConstantCondValue = Result.Val.getInt();
5670fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        ShouldCheckConstantCond = true;
5680fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
5690fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        assert(ConstantCondValue.getBitWidth() == CondWidth &&
5700fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall               ConstantCondValue.isSigned() == CondIsSigned);
5710fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      }
5720fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    }
5730fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
574dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // Sort all the scalar case values so we can easily detect duplicates.
575dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
576dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
577dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    if (!CaseVals.empty()) {
5780fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) {
5790fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        if (ShouldCheckConstantCond &&
5800fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            CaseVals[i].first == ConstantCondValue)
5810fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          ShouldCheckConstantCond = false;
5820fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
5830fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) {
584dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          // If we have a duplicate, report it.
5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          Diag(CaseVals[i].second->getLHS()->getLocStart(),
5860fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall               diag::err_duplicate_case) << CaseVals[i].first.toString(10);
5870fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          Diag(CaseVals[i-1].second->getLHS()->getLocStart(),
588dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::note_duplicate_case_prev);
589390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // FIXME: We really want to remove the bogus case stmt from the
590390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // substmt, but we have no way to do this right now.
591dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseListIsErroneous = true;
592dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
5936efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner      }
594b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner    }
5951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
596dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // Detect duplicate case ranges, which usually don't exist at all in
597dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // the first place.
598dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    if (!CaseRanges.empty()) {
599dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Sort all the case ranges by their low value so we can easily detect
600dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // overlaps between ranges.
601dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      std::stable_sort(CaseRanges.begin(), CaseRanges.end());
6021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
603dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Scan the ranges, computing the high values and removing empty ranges.
604dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      std::vector<llvm::APSInt> HiVals;
605dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
6060fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        llvm::APSInt &LoVal = CaseRanges[i].first;
607dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *CR = CaseRanges[i].second;
608dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        Expr *Hi = CR->getRHS();
609dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
6101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
611dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Convert the value to the same width/sign as the condition.
612dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
613dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                           CR->getRHS()->getLocStart(),
614dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                           diag::warn_case_value_overflow);
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
616dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // If the LHS is not the same type as the condition, insert an implicit
617dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // cast.
6182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        ImpCastExprToType(Hi, CondType, CK_IntegralCast);
619dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CR->setRHS(Hi);
6201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
621dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // If the low value is bigger than the high value, the case is empty.
6220fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        if (LoVal > HiVal) {
623dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
624dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            << SourceRange(CR->getLHS()->getLocStart(),
625dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                           CR->getRHS()->getLocEnd());
626dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseRanges.erase(CaseRanges.begin()+i);
627dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          --i, --e;
628dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          continue;
629dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6300fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
6310fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        if (ShouldCheckConstantCond &&
6320fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            LoVal <= ConstantCondValue &&
6330fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            ConstantCondValue <= HiVal)
6340fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          ShouldCheckConstantCond = false;
6350fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
636dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        HiVals.push_back(HiVal);
6370471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      }
6381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
639dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Rescan the ranges, looking for overlap with singleton values and other
640dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // ranges.  Since the range list is sorted, we only need to compare case
641dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // ranges with their neighbors.
642dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
643dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt &CRLo = CaseRanges[i].first;
644dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt &CRHi = HiVals[i];
645dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *CR = CaseRanges[i].second;
6461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
647dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Check to see whether the case range overlaps with any
648dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // singleton cases.
649dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *OverlapStmt = 0;
650dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt OverlapVal(32);
6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
652dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Find the smallest value >= the lower bound.  If I is in the
653dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // case range, then we have overlap.
654dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
655dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                                  CaseVals.end(), CRLo,
656dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                                  CaseCompareFunctor());
657dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (I != CaseVals.end() && I->first < CRHi) {
658dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = I->first;   // Found overlap with scalar.
659dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = I->second;
660dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
662dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Find the smallest value bigger than the upper bound.
663dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
664dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
665dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = (I-1)->first;      // Found overlap with scalar.
666dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = (I-1)->second;
667dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
669dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Check to see if this case stmt overlaps with the subsequent
670dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // case range.
671dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (i && CRLo <= HiVals[i-1]) {
672dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = HiVals[i-1];       // Found overlap with range.
673dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = CaseRanges[i-1].second;
674dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
676dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (OverlapStmt) {
677dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          // If we have a duplicate, report it.
678dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
679dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            << OverlapVal.toString(10);
6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          Diag(OverlapStmt->getLHS()->getLocStart(),
681dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::note_duplicate_case_prev);
682390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // FIXME: We really want to remove the bogus case stmt from the
683390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // substmt, but we have no way to do this right now.
684dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseListIsErroneous = true;
685dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6860471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      }
687b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner    }
688ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
6890fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // Complain if we have a constant condition and we didn't find a match.
6900fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    if (!CaseListIsErroneous && ShouldCheckConstantCond) {
6910fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      // TODO: it would be nice if we printed enums as enums, chars as
6920fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      // chars, etc.
6930fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      Diag(CondExpr->getExprLoc(), diag::warn_missing_case_for_condition)
6940fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        << ConstantCondValue.toString(10)
6950fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        << CondExpr->getSourceRange();
6960fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    }
6970fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
6980fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // Check to see if switch is over an Enum and handles all of its
6990fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // values.  We don't need to do this if there's a default
7000fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // statement or if we have a constant condition.
7010fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    //
7020fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // TODO: we might want to check whether case values are out of the
7030fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // enum even if we don't want to check whether all cases are handled.
70430ab37122300a5f6664b8ae2d0b43b4396eb6bcbDouglas Gregor    const EnumType* ET = CondTypeBeforePromotion->getAs<EnumType>();
705ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor    // If switch has default case, then ignore it.
7060fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    if (!CaseListIsErroneous && !TheDefaultStmt && !HasConstantCond && ET) {
707ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      const EnumDecl *ED = ET->getDecl();
708ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      typedef llvm::SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> EnumValsTy;
709ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EnumValsTy EnumVals;
710ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
7110fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      // Gather all enum values, set their type and sort them,
7120fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      // allowing easier comparison with CaseVals.
7130fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin();
7140fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall             EDI != ED->enumerator_end(); EDI++) {
715ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        llvm::APSInt Val = (*EDI)->getInitVal();
716ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if(Val.getBitWidth() < CondWidth)
717ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          Val.extend(CondWidth);
7186623584c0ec508110d75572eef092bf98fedf3f4Douglas Gregor        else if (Val.getBitWidth() > CondWidth)
7196623584c0ec508110d75572eef092bf98fedf3f4Douglas Gregor          Val.trunc(CondWidth);
720ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        Val.setIsSigned(CondIsSigned);
721ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        EnumVals.push_back(std::make_pair(Val, (*EDI)));
722ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
723ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals);
7240fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      EnumValsTy::iterator EIend =
7250fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals);
726ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      // See which case values aren't in enum
727ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EnumValsTy::const_iterator EI = EnumVals.begin();
7280fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      for (CaseValsTy::const_iterator CI = CaseVals.begin();
7290fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall             CI != CaseVals.end(); CI++) {
730ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        while (EI != EIend && EI->first < CI->first)
731ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          EI++;
732ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (EI == EIend || EI->first > CI->first)
7330fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            Diag(CI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum)
7340fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall              << ED->getDeclName();
735ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
736ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      // See which of case ranges aren't in enum
737ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EI = EnumVals.begin();
7380fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      for (CaseRangesTy::const_iterator RI = CaseRanges.begin();
7390fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall             RI != CaseRanges.end() && EI != EIend; RI++) {
740ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        while (EI != EIend && EI->first < RI->first)
741ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          EI++;
742ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
743ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (EI == EIend || EI->first != RI->first) {
7440fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          Diag(RI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum)
7450fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            << ED->getDeclName();
746ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        }
747ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
748ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context);
749ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        while (EI != EIend && EI->first < Hi)
750ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          EI++;
751ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (EI == EIend || EI->first != Hi)
7520fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          Diag(RI->second->getRHS()->getExprLoc(), diag::warn_not_in_enum)
7530fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            << ED->getDeclName();
754ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
755ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      //Check which enum vals aren't in switch
756ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      CaseValsTy::const_iterator CI = CaseVals.begin();
757ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      CaseRangesTy::const_iterator RI = CaseRanges.begin();
758ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EI = EnumVals.begin();
759ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      for (; EI != EIend; EI++) {
760ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        //Drop unneeded case values
761ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        llvm::APSInt CIVal;
762ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        while (CI != CaseVals.end() && CI->first < EI->first)
763ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          CI++;
764ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
765ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (CI != CaseVals.end() && CI->first == EI->first)
766ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          continue;
767ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
768ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        //Drop unneeded case ranges
769ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        for (; RI != CaseRanges.end(); RI++) {
770ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context);
771ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          if (EI->first <= Hi)
772ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor            break;
773ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        }
774ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
775ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (RI == CaseRanges.end() || EI->first < RI->first)
7760fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          Diag(CondExpr->getExprLoc(), diag::warn_missing_cases)
7770fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            << EI->second->getDeclName();
778ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
779ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor    }
780b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  }
781dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
782390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: If the case list was broken is some way, we don't have a good system
783390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // to patch it up.  Instead, just return the whole substmt as broken.
784b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  if (CaseListIsErroneous)
785de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl    return StmtError();
786de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
787de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(SS);
7885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
79060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
79199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas GregorSema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond,
7929ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                     Decl *CondVar, Stmt *Body) {
79360d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult CondResult(Cond.release());
79499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
7955656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  VarDecl *ConditionVar = 0;
796d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (CondVar) {
797d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    ConditionVar = cast<VarDecl>(CondVar);
798586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    CondResult = CheckConditionVariable(ConditionVar, WhileLoc, true);
79999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (CondResult.isInvalid())
80099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return StmtError();
8015656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  }
8029ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Expr *ConditionExpr = CondResult.take();
80399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!ConditionExpr)
80499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    return StmtError();
80599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
8069ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  DiagnoseUnusedExprResult(Body);
8071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80843dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  return Owned(new (Context) WhileStmt(Context, ConditionVar, ConditionExpr,
8099ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                       Body, WhileLoc));
8105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
81260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
8139ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnDoStmt(SourceLocation DoLoc, Stmt *Body,
814989135901c750af61ef012b6b0a0368be415bc46Chris Lattner                  SourceLocation WhileLoc, SourceLocation CondLParen,
8159ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                  Expr *Cond, SourceLocation CondRParen) {
8169ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  assert(Cond && "ActOnDoStmt(): missing expression");
817f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
8189ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (CheckBooleanCondition(Cond, DoLoc))
8195a881bb09928b7ade891efc680088aaad276f8d6John McCall    return StmtError();
8205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
82160d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult CondResult = MaybeCreateCXXExprWithTemporaries(Cond);
8229ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (CondResult.isInvalid())
823586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    return StmtError();
8249ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Cond = CondResult.take();
825586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
8269ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  DiagnoseUnusedExprResult(Body);
827754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
8289ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) DoStmt(Body, Cond, DoLoc, WhileLoc, CondRParen));
8295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
83160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
832f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
8339ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                   Stmt *First, FullExprArg second, Decl *secondVar,
83499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor                   FullExprArg third,
8359ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                   SourceLocation RParenLoc, Stmt *Body) {
8365921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  if (!getLangOptions().CPlusPlus) {
8375921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
838f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // C99 6.8.5p3: The declaration part of a 'for' statement shall only
839f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // declare identifiers for objects having storage class 'auto' or
840f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // 'register'.
8415921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis      for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
8425921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis           DI!=DE; ++DI) {
8435921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        VarDecl *VD = dyn_cast<VarDecl>(*DI);
8445921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
8455921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis          VD = 0;
8465921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        if (VD == 0)
8475921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis          Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
8485921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        // FIXME: mark decl erroneous!
8495921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis      }
850ae3b701f59e78e058b83344be17206af3bf5d277Chris Lattner    }
8515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
85299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
85360d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult SecondResult(second.release());
85499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  VarDecl *ConditionVar = 0;
855d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (secondVar) {
856d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    ConditionVar = cast<VarDecl>(secondVar);
857586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    SecondResult = CheckConditionVariable(ConditionVar, ForLoc, true);
85899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (SecondResult.isInvalid())
85999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return StmtError();
86099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
86199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
86299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  Expr *Third  = third.release().takeAs<Expr>();
86399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
8643af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson  DiagnoseUnusedExprResult(First);
8653af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson  DiagnoseUnusedExprResult(Third);
866754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(Body);
867754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
86843dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  return Owned(new (Context) ForStmt(Context, First,
8699ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     SecondResult.take(), ConditionVar,
87043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor                                     Third, Body, ForLoc, LParenLoc,
87143dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor                                     RParenLoc));
8725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
87460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
875f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
876f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                                 SourceLocation LParenLoc,
8779ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                 Stmt *First, Expr *Second,
8789ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                 SourceLocation RParenLoc, Stmt *Body) {
87920552d2842245692b649e0d25380670922f954a2Fariborz Jahanian  if (First) {
88020552d2842245692b649e0d25380670922f954a2Fariborz Jahanian    QualType FirstType;
88120552d2842245692b649e0d25380670922f954a2Fariborz Jahanian    if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
8827e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner      if (!DS->isSingleDecl())
883f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag((*DS->decl_begin())->getLocation(),
884f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                         diag::err_toomany_element_decls));
885f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
8867e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner      Decl *D = DS->getSingleDecl();
887f34afeed9a0112bf31fee185b6c80556111d3834Ted Kremenek      FirstType = cast<ValueDecl>(D)->getType();
888f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // C99 6.8.5p3: The declaration part of a 'for' statement shall only
889f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // declare identifiers for objects having storage class 'auto' or
890f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // 'register'.
891248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff      VarDecl *VD = cast<VarDecl>(D);
892248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff      if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
893f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag(VD->getLocation(),
894f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                              diag::err_non_variable_decl_in_for));
8951fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson    } else {
896c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor      Expr *FirstE = cast<Expr>(First);
897c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor      if (!FirstE->isTypeDependent() &&
898c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor          FirstE->isLvalue(Context) != Expr::LV_Valid)
899f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag(First->getLocStart(),
900f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                   diag::err_selector_element_not_lvalue)
901f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl          << First->getSourceRange());
9021fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson
9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FirstType = static_cast<Expr*>(First)->getType();
9041fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson    }
905c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor    if (!FirstType->isDependentType() &&
906c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor        !FirstType->isObjCObjectPointerType() &&
907a5e42a82ce055f29f3733f3a1f10da6cb9877deeFariborz Jahanian        !FirstType->isBlockPointerType())
908dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        Diag(ForLoc, diag::err_selector_element_type)
909d162584991885ab004a02573a73ce06422b921fcChris Lattner          << FirstType << First->getSourceRange();
9103ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  }
911c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor  if (Second && !Second->isTypeDependent()) {
912a873dfc9e7314681bb37efd9ab185045de121e43Douglas Gregor    DefaultFunctionArrayLvalueConversion(Second);
9133ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian    QualType SecondType = Second->getType();
914f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    if (!SecondType->isObjCObjectPointerType())
915dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      Diag(ForLoc, diag::err_collection_expr_type)
916d162584991885ab004a02573a73ce06422b921fcChris Lattner        << SecondType << Second->getSourceRange();
917ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian    else if (const ObjCObjectPointerType *OPT =
918ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian             SecondType->getAsObjCInterfacePointerType()) {
919ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      llvm::SmallVector<IdentifierInfo *, 4> KeyIdents;
920ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      IdentifierInfo* selIdent =
921ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian        &Context.Idents.get("countByEnumeratingWithState");
922ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      KeyIdents.push_back(selIdent);
923ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      selIdent = &Context.Idents.get("objects");
924ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      KeyIdents.push_back(selIdent);
925ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      selIdent = &Context.Idents.get("count");
926ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      KeyIdents.push_back(selIdent);
927ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      Selector CSelector = Context.Selectors.getSelector(3, &KeyIdents[0]);
928ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      if (ObjCInterfaceDecl *IDecl = OPT->getInterfaceDecl()) {
929ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian        if (!IDecl->isForwardDecl() &&
930ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian            !IDecl->lookupInstanceMethod(CSelector)) {
93180a785c2452c73b7c41d4a867edcf5a090c78c46Fariborz Jahanian          // Must further look into private implementation methods.
932ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian          if (!LookupPrivateInstanceMethod(CSelector, IDecl))
933ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian            Diag(ForLoc, diag::warn_collection_expr_type)
934ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian              << SecondType << CSelector << Second->getSourceRange();
935ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian        }
936ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      }
937ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian    }
9383ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  }
9398189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body,
9408189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                                                   ForLoc, RParenLoc));
9413ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian}
9425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
94360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
9441b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
9455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                    IdentifierInfo *LabelII) {
9465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Look up the record for this label identifier.
947781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  LabelStmt *&LabelDecl = getCurFunction()->LabelMap[LabelII];
9485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
949781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->setHasBranchIntoScope();
950b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
951caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  // If we haven't seen this label yet, create a forward reference.
952caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  if (LabelDecl == 0)
9538189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0);
9544cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
9558189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc));
9565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
95860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
959ad56d684259f706b7c0ae5ad9c23adb4f2926817Chris LattnerSema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc,
9609ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                            Expr *E) {
961bbf462314b1dc8e422b7c4dd4cac47e566aedf6dEli Friedman  // Convert operand to void*
9625f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  if (!E->isTypeDependent()) {
9635f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    QualType ETy = E->getType();
9642877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth    QualType DestTy = Context.getPointerType(Context.VoidTy.withConst());
9655f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    AssignConvertType ConvTy =
9662877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth      CheckSingleAssignmentConstraints(DestTy, E);
9672877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth    if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E, AA_Passing))
9685f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor      return StmtError();
9695f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  }
970b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
971781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->setHasIndirectGoto();
972b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
9735f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  return Owned(new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E));
9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
97660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
9771b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
9785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Scope *S = CurScope->getContinueParent();
9795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!S) {
9805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
9814cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9834cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
9848189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ContinueStmt(ContinueLoc));
9855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
98760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
9881b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
9895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Scope *S = CurScope->getBreakParent();
9905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!S) {
9915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
9924cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9944cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
9958189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) BreakStmt(BreakLoc));
9965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9985077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \brief Determine whether a return statement is a candidate for the named
9995077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// return value optimization (C++0x 12.8p34, bullet 1).
10005077c3876beeaed32280af88244e8050078619a8Douglas Gregor///
10015077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \param Ctx The context in which the return expression and type occur.
10025077c3876beeaed32280af88244e8050078619a8Douglas Gregor///
10035077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \param RetType The return type of the function or block.
10045077c3876beeaed32280af88244e8050078619a8Douglas Gregor///
10055077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \param RetExpr The expression being returned from the function or block.
10065077c3876beeaed32280af88244e8050078619a8Douglas Gregor///
10075077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \returns The NRVO candidate variable, if the return statement may use the
10085077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// NRVO, or NULL if there is no such candidate.
10095077c3876beeaed32280af88244e8050078619a8Douglas Gregorstatic const VarDecl *getNRVOCandidate(ASTContext &Ctx, QualType RetType,
10105077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                       Expr *RetExpr) {
10113c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  QualType ExprType = RetExpr->getType();
10123c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // - in a return statement in a function with ...
10133c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // ... a class return type ...
10143c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  if (!RetType->isRecordType())
10155077c3876beeaed32280af88244e8050078619a8Douglas Gregor    return 0;
10163c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // ... the same cv-unqualified type as the function return type ...
10173c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  if (!Ctx.hasSameUnqualifiedType(RetType, ExprType))
10185077c3876beeaed32280af88244e8050078619a8Douglas Gregor    return 0;
10193c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // ... the expression is the name of a non-volatile automatic object ...
10203c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // We ignore parentheses here.
10213c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // FIXME: Is this compliant? (Everyone else does it)
10223c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetExpr->IgnoreParens());
10233c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  if (!DR)
10245077c3876beeaed32280af88244e8050078619a8Douglas Gregor    return 0;
10253c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
10263c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  if (!VD)
10275077c3876beeaed32280af88244e8050078619a8Douglas Gregor    return 0;
10285077c3876beeaed32280af88244e8050078619a8Douglas Gregor
10295077c3876beeaed32280af88244e8050078619a8Douglas Gregor  if (VD->getKind() == Decl::Var && VD->hasLocalStorage() &&
1030d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor      !VD->getType()->isReferenceType() && !VD->hasAttr<BlocksAttr>() &&
1031d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor      !VD->getType().isVolatileQualified())
10325077c3876beeaed32280af88244e8050078619a8Douglas Gregor    return VD;
10335077c3876beeaed32280af88244e8050078619a8Douglas Gregor
10345077c3876beeaed32280af88244e8050078619a8Douglas Gregor  return 0;
10353c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor}
10363c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor
103727c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
10384eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
103960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
10404eb206bebcdab28ababe8df55c6185cec2cdc071Steve NaroffSema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
10414eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // If this is the first return we've seen in the block, infer the type of
10424eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // the block from it.
10439ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  BlockScopeInfo *CurBlock = getCurBlock();
10447d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian  if (CurBlock->ReturnType.isNull()) {
1045c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff    if (RetValExp) {
104616564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      // Don't call UsualUnaryConversions(), since we don't want to do
104716564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      // integer promotions here.
1048a873dfc9e7314681bb37efd9ab185045de121e43Douglas Gregor      DefaultFunctionArrayLvalueConversion(RetValExp);
10497d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      CurBlock->ReturnType = RetValExp->getType();
10507d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(RetValExp)) {
10517d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // We have to remove a 'const' added to copied-in variable which was
10527d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // part of the implementation spec. and not the actual qualifier for
10537d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // the variable.
10547d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        if (CDRE->isConstQualAdded())
10557d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian           CurBlock->ReturnType.removeConst();
10567d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      }
1057c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff    } else
10587d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      CurBlock->ReturnType = Context.VoidTy;
10594eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  }
10607d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian  QualType FnRetType = CurBlock->ReturnType;
10614cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
106240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (CurBlock->TheDecl->hasAttr<NoReturnAttr>()) {
10636c92fa75e62937f9738696840efcb258560f4568Mike Stump    Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr)
10646c92fa75e62937f9738696840efcb258560f4568Mike Stump      << getCurFunctionOrMethodDecl()->getDeclName();
10656c92fa75e62937f9738696840efcb258560f4568Mike Stump    return StmtError();
10666c92fa75e62937f9738696840efcb258560f4568Mike Stump  }
10676c92fa75e62937f9738696840efcb258560f4568Mike Stump
10684eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // Otherwise, verify that this result type matches the previous one.  We are
10694eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // pickier with blocks than for normal functions because we don't have GCC
10704eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // compatibility to worry about here.
10715077c3876beeaed32280af88244e8050078619a8Douglas Gregor  ReturnStmt *Result = 0;
10724eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  if (CurBlock->ReturnType->isVoidType()) {
10734eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    if (RetValExp) {
10744eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff      Diag(ReturnLoc, diag::err_return_block_has_expr);
10754eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff      RetValExp = 0;
10764eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    }
10775077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0);
10785077c3876beeaed32280af88244e8050078619a8Douglas Gregor  } else if (!RetValExp) {
10794cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr));
10805077c3876beeaed32280af88244e8050078619a8Douglas Gregor  } else {
10815077c3876beeaed32280af88244e8050078619a8Douglas Gregor    const VarDecl *NRVOCandidate = 0;
10825077c3876beeaed32280af88244e8050078619a8Douglas Gregor
10835077c3876beeaed32280af88244e8050078619a8Douglas Gregor    if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
10845077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // we have a non-void block with an expression, continue checking
10855077c3876beeaed32280af88244e8050078619a8Douglas Gregor
10865077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // C99 6.8.6.4p3(136): The return statement is not an assignment. The
10875077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // overlap restriction of subclause 6.5.16.1 does not apply to the case of
10885077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // function return.
10895077c3876beeaed32280af88244e8050078619a8Douglas Gregor
10905077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // In C++ the return statement is handled via a copy initialization.
10915077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // the C version of which boils down to CheckSingleAssignmentConstraints.
10925077c3876beeaed32280af88244e8050078619a8Douglas Gregor      NRVOCandidate = getNRVOCandidate(Context, FnRetType, RetValExp);
109360d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res = PerformCopyInitialization(
10945077c3876beeaed32280af88244e8050078619a8Douglas Gregor                               InitializedEntity::InitializeResult(ReturnLoc,
10955077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                                                   FnRetType,
10965077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                                            NRVOCandidate != 0),
10975077c3876beeaed32280af88244e8050078619a8Douglas Gregor                               SourceLocation(),
10985077c3876beeaed32280af88244e8050078619a8Douglas Gregor                               Owned(RetValExp));
10995077c3876beeaed32280af88244e8050078619a8Douglas Gregor      if (Res.isInvalid()) {
11005077c3876beeaed32280af88244e8050078619a8Douglas Gregor        // FIXME: Cleanup temporaries here, anyway?
11015077c3876beeaed32280af88244e8050078619a8Douglas Gregor        return StmtError();
11025077c3876beeaed32280af88244e8050078619a8Douglas Gregor      }
11035077c3876beeaed32280af88244e8050078619a8Douglas Gregor
11045077c3876beeaed32280af88244e8050078619a8Douglas Gregor      if (RetValExp)
11055077c3876beeaed32280af88244e8050078619a8Douglas Gregor        RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp);
11064cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
11075077c3876beeaed32280af88244e8050078619a8Douglas Gregor      RetValExp = Res.takeAs<Expr>();
11085077c3876beeaed32280af88244e8050078619a8Douglas Gregor      if (RetValExp)
11095077c3876beeaed32280af88244e8050078619a8Douglas Gregor        CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
1110c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson    }
1111c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson
11125077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate);
111398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  }
11144cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
11155077c3876beeaed32280af88244e8050078619a8Douglas Gregor  // If we need to check for the named return value optimization, save the
11165077c3876beeaed32280af88244e8050078619a8Douglas Gregor  // return statement in our scope for later processing.
11175077c3876beeaed32280af88244e8050078619a8Douglas Gregor  if (getLangOptions().CPlusPlus && FnRetType->isRecordType() &&
11185077c3876beeaed32280af88244e8050078619a8Douglas Gregor      !CurContext->isDependentContext())
11195077c3876beeaed32280af88244e8050078619a8Douglas Gregor    FunctionScopes.back()->Returns.push_back(Result);
11205077c3876beeaed32280af88244e8050078619a8Douglas Gregor
11215077c3876beeaed32280af88244e8050078619a8Douglas Gregor  return Owned(Result);
11224eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
11235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
112460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
11259ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
11269ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (getCurBlock())
11274eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
11284cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1129371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  QualType FnRetType;
1130f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump  if (const FunctionDecl *FD = getCurFunctionDecl()) {
1131371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner    FnRetType = FD->getResultType();
113204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FD->hasAttr<NoReturnAttr>() ||
113304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        FD->getType()->getAs<FunctionType>()->getNoReturnAttr())
11348662587fa75d3fb04f873e265841c9314c7f5523Chris Lattner      Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr)
1135f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump        << getCurFunctionOrMethodDecl()->getDeclName();
1136f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump  } else if (ObjCMethodDecl *MD = getCurMethodDecl())
1137c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff    FnRetType = MD->getResultType();
1138c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff  else // If we don't have a function/method context, bail.
1139c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff    return StmtError();
11401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11415077c3876beeaed32280af88244e8050078619a8Douglas Gregor  ReturnStmt *Result = 0;
11425cf216b7fa64b933b60743b0b26053e8e7aa87beChris Lattner  if (FnRetType->isVoidType()) {
11431be8aee8745e8b814ad2f151aa214b0ef07833dbDouglas Gregor    if (RetValExp && !RetValExp->isTypeDependent()) {
11441be8aee8745e8b814ad2f151aa214b0ef07833dbDouglas Gregor      // C99 6.8.6.4p1 (ext_ since GCC warns)
114565ce04bef06696379682410f399f37b43996d824Chris Lattner      unsigned D = diag::ext_return_has_expr;
114665ce04bef06696379682410f399f37b43996d824Chris Lattner      if (RetValExp->getType()->isVoidType())
114765ce04bef06696379682410f399f37b43996d824Chris Lattner        D = diag::ext_return_has_void_expr;
11484cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1149e878eb035b343d7d819c092102364ec9849716aeChris Lattner      // return (some void expression); is legal in C++.
1150e878eb035b343d7d819c092102364ec9849716aeChris Lattner      if (D != diag::ext_return_has_void_expr ||
1151e878eb035b343d7d819c092102364ec9849716aeChris Lattner          !getLangOptions().CPlusPlus) {
1152e878eb035b343d7d819c092102364ec9849716aeChris Lattner        NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
1153e878eb035b343d7d819c092102364ec9849716aeChris Lattner        Diag(ReturnLoc, D)
1154e878eb035b343d7d819c092102364ec9849716aeChris Lattner          << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
1155e878eb035b343d7d819c092102364ec9849716aeChris Lattner          << RetValExp->getSourceRange();
1156e878eb035b343d7d819c092102364ec9849716aeChris Lattner      }
11571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11580ece491d8f62ce67f047491a6703fac0d3bd4ff4Anders Carlsson      RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp);
11595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
11605077c3876beeaed32280af88244e8050078619a8Douglas Gregor
11615077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0);
11625077c3876beeaed32280af88244e8050078619a8Douglas Gregor  } else if (!RetValExp && !FnRetType->isDependentType()) {
11633c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    unsigned DiagID = diag::warn_return_missing_expr;  // C90 6.6.6.4p4
11643c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    // C99 6.8.6.4p1 (ext_ since GCC warns)
11653c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
11663c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner
11673c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    if (FunctionDecl *FD = getCurFunctionDecl())
116808631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
11693c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    else
117008631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
11715077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc);
11725077c3876beeaed32280af88244e8050078619a8Douglas Gregor  } else {
11735077c3876beeaed32280af88244e8050078619a8Douglas Gregor    const VarDecl *NRVOCandidate = 0;
11745077c3876beeaed32280af88244e8050078619a8Douglas Gregor    if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
11755077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // we have a non-void function with an expression, continue checking
11765077c3876beeaed32280af88244e8050078619a8Douglas Gregor
11775077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // C99 6.8.6.4p3(136): The return statement is not an assignment. The
11785077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // overlap restriction of subclause 6.5.16.1 does not apply to the case of
11795077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // function return.
11805077c3876beeaed32280af88244e8050078619a8Douglas Gregor
11815077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // In C++ the return statement is handled via a copy initialization.
11825077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // the C version of which boils down to CheckSingleAssignmentConstraints.
11835077c3876beeaed32280af88244e8050078619a8Douglas Gregor      NRVOCandidate = getNRVOCandidate(Context, FnRetType, RetValExp);
118460d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res = PerformCopyInitialization(
11855077c3876beeaed32280af88244e8050078619a8Douglas Gregor                               InitializedEntity::InitializeResult(ReturnLoc,
11865077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                                                   FnRetType,
11875077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                                            NRVOCandidate != 0),
11885077c3876beeaed32280af88244e8050078619a8Douglas Gregor                               SourceLocation(),
11895077c3876beeaed32280af88244e8050078619a8Douglas Gregor                               Owned(RetValExp));
11905077c3876beeaed32280af88244e8050078619a8Douglas Gregor      if (Res.isInvalid()) {
11915077c3876beeaed32280af88244e8050078619a8Douglas Gregor        // FIXME: Cleanup temporaries here, anyway?
11925077c3876beeaed32280af88244e8050078619a8Douglas Gregor        return StmtError();
11935077c3876beeaed32280af88244e8050078619a8Douglas Gregor      }
11944cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
11955077c3876beeaed32280af88244e8050078619a8Douglas Gregor      RetValExp = Res.takeAs<Expr>();
11965077c3876beeaed32280af88244e8050078619a8Douglas Gregor      if (RetValExp)
11975077c3876beeaed32280af88244e8050078619a8Douglas Gregor        CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
119866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    }
11995077c3876beeaed32280af88244e8050078619a8Douglas Gregor
12005077c3876beeaed32280af88244e8050078619a8Douglas Gregor    if (RetValExp)
12015077c3876beeaed32280af88244e8050078619a8Douglas Gregor      RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp);
12025077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate);
1203898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  }
12045077c3876beeaed32280af88244e8050078619a8Douglas Gregor
12055077c3876beeaed32280af88244e8050078619a8Douglas Gregor  // If we need to check for the named return value optimization, save the
12065077c3876beeaed32280af88244e8050078619a8Douglas Gregor  // return statement in our scope for later processing.
12075077c3876beeaed32280af88244e8050078619a8Douglas Gregor  if (getLangOptions().CPlusPlus && FnRetType->isRecordType() &&
12085077c3876beeaed32280af88244e8050078619a8Douglas Gregor      !CurContext->isDependentContext())
12095077c3876beeaed32280af88244e8050078619a8Douglas Gregor    FunctionScopes.back()->Returns.push_back(Result);
12105077c3876beeaed32280af88244e8050078619a8Douglas Gregor
12115077c3876beeaed32280af88244e8050078619a8Douglas Gregor  return Owned(Result);
12125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1214810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently
1215810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// ignore "noop" casts in places where an lvalue is required by an inline asm.
1216810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// We emulate this behavior when -fheinous-gnu-extensions is specified, but
1217810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// provide a strong guidance to not use it.
1218810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner///
1219810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// This method checks to see if the argument is an acceptable l-value and
1220810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// returns false if it is a case we can handle.
1221810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattnerstatic bool CheckAsmLValue(const Expr *E, Sema &S) {
1222703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson  // Type dependent expressions will be checked during instantiation.
1223703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson  if (E->isTypeDependent())
1224703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson    return false;
1225703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson
1226810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  if (E->isLvalue(S.Context) == Expr::LV_Valid)
1227810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    return false;  // Cool, this is an lvalue.
1228810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1229810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // Okay, this is not an lvalue, but perhaps it is the result of a cast that we
1230810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // are supposed to allow.
1231810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  const Expr *E2 = E->IgnoreParenNoopCasts(S.Context);
1232810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  if (E != E2 && E2->isLvalue(S.Context) == Expr::LV_Valid) {
1233810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    if (!S.getLangOptions().HeinousExtensions)
1234810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue)
1235810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner        << E->getSourceRange();
1236810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    else
1237810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue)
1238810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner        << E->getSourceRange();
1239810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    // Accept, even if we emitted an error diagnostic.
1240810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    return false;
1241810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  }
1242810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1243810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // None of the above, just randomly invalid non-lvalue.
1244810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  return true;
1245810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner}
1246810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1247810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
124860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
12493037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          bool IsSimple,
12503037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          bool IsVolatile,
12513037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          unsigned NumOutputs,
12523037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          unsigned NumInputs,
1253ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson                                          IdentifierInfo **Names,
12543037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg constraints,
12553037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg exprs,
12569ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                          Expr *asmString,
12573037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg clobbers,
12583b11fd3b52e7b88233c602407c151d07cb093e75Mike Stump                                          SourceLocation RParenLoc,
12593b11fd3b52e7b88233c602407c151d07cb093e75Mike Stump                                          bool MSAsm) {
12603037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  unsigned NumClobbers = clobbers.size();
12613037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral **Constraints =
12623037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl    reinterpret_cast<StringLiteral**>(constraints.get());
12639ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Expr **Exprs = exprs.get();
12649ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  StringLiteral *AsmString = cast<StringLiteral>(asmString);
12653037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get());
12663037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
126703eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson  llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
12681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12691708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  // The parser verifies that there is a string literal here.
12706bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner  if (AsmString->isWide())
12713037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl    return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
12723037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      << AsmString->getSourceRange());
12733037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
12741708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  for (unsigned i = 0; i != NumOutputs; i++) {
12751708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Constraints[i];
12766bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
12773037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
12783037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
12793037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1280ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    llvm::StringRef OutputName;
1281ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    if (Names[i])
1282ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson      OutputName = Names[i]->getName();
1283ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson
1284ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName);
1285432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner    if (!Context.Target.validateOutputConstraint(Info))
12863037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
1287432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                            diag::err_asm_invalid_output_constraint)
1288432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                       << Info.getConstraintStr());
12893037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1290d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson    // Check that the output exprs are valid lvalues.
129172056a237c536ee63285ab0850cb50f299281767Eli Friedman    Expr *OutputExpr = Exprs[i];
1292810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    if (CheckAsmLValue(OutputExpr, *this)) {
129372056a237c536ee63285ab0850cb50f299281767Eli Friedman      return StmtError(Diag(OutputExpr->getLocStart(),
1294dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                  diag::err_asm_invalid_lvalue_in_output)
129572056a237c536ee63285ab0850cb50f299281767Eli Friedman        << OutputExpr->getSourceRange());
129604728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson    }
12971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
129844def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    OutputConstraintInfos.push_back(Info);
129904728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  }
13003037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1301806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  llvm::SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
1302806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner
130304728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
13041708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Constraints[i];
13056bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
13063037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
13073037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
13083037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1309ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    llvm::StringRef InputName;
1310ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    if (Names[i])
1311ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson      InputName = Names[i]->getName();
1312ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson
1313ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    TargetInfo::ConstraintInfo Info(Literal->getString(), InputName);
1314beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    if (!Context.Target.validateInputConstraint(OutputConstraintInfos.data(),
13152819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                                NumOutputs, Info)) {
13163037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
1317432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                            diag::err_asm_invalid_input_constraint)
1318432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                       << Info.getConstraintStr());
1319d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson    }
13203037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
132172056a237c536ee63285ab0850cb50f299281767Eli Friedman    Expr *InputExpr = Exprs[i];
13223037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1323d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson    // Only allow void types for memory constraints.
132444def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.allowsMemory() && !Info.allowsRegister()) {
1325810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      if (CheckAsmLValue(InputExpr, *this))
132672056a237c536ee63285ab0850cb50f299281767Eli Friedman        return StmtError(Diag(InputExpr->getLocStart(),
1327d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson                              diag::err_asm_invalid_lvalue_in_input)
1328432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                         << Info.getConstraintStr()
132972056a237c536ee63285ab0850cb50f299281767Eli Friedman                         << InputExpr->getSourceRange());
133004728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson    }
13313037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
133244def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.allowsRegister()) {
1333d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson      if (InputExpr->getType()->isVoidType()) {
133472056a237c536ee63285ab0850cb50f299281767Eli Friedman        return StmtError(Diag(InputExpr->getLocStart(),
1335d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson                              diag::err_asm_invalid_type_in_input)
13361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          << InputExpr->getType() << Info.getConstraintStr()
133772056a237c536ee63285ab0850cb50f299281767Eli Friedman          << InputExpr->getSourceRange());
1338d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson      }
1339d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson    }
13401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1341a873dfc9e7314681bb37efd9ab185045de121e43Douglas Gregor    DefaultFunctionArrayLvalueConversion(Exprs[i]);
13421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1343806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    InputConstraintInfos.push_back(Info);
134404728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  }
13453037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
13466fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson  // Check that the clobbers are valid.
13471708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  for (unsigned i = 0; i != NumClobbers; i++) {
13481708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Clobbers[i];
13496bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
13503037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
13513037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
13523037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1353fdba9c069023f686e2608affde02c82131ee1cf8Anders Carlsson    llvm::StringRef Clobber = Literal->getString();
13543037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1355fdba9c069023f686e2608affde02c82131ee1cf8Anders Carlsson    if (!Context.Target.isValidGCCRegisterName(Clobber))
13563037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
13577765934ad7e157b5fcf925792a38e01b1edbcf8aDaniel Dunbar                  diag::err_asm_unknown_register_name) << Clobber);
13586fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson  }
13593037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1360fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  AsmStmt *NS =
1361966146e89141804ff6492739a2a6e6592ca671c7Anders Carlsson    new (Context) AsmStmt(Context, AsmLoc, IsSimple, IsVolatile, MSAsm,
1362966146e89141804ff6492739a2a6e6592ca671c7Anders Carlsson                          NumOutputs, NumInputs, Names, Constraints, Exprs,
1363966146e89141804ff6492739a2a6e6592ca671c7Anders Carlsson                          AsmString, NumClobbers, Clobbers, RParenLoc);
1364fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  // Validate the asm string, ensuring it makes sense given the operands we
1365fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  // have.
1366fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  llvm::SmallVector<AsmStmt::AsmStringPiece, 8> Pieces;
1367fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  unsigned DiagOffs;
1368fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
13692ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner    Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
13702ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner           << AsmString->getSourceRange();
1371fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner    DeleteStmt(NS);
1372fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner    return StmtError();
1373fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  }
13741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1375806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  // Validate tied input operands for type mismatches.
1376806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) {
1377806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
13781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1379806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // If this is a tied constraint, verify that the output and input have
1380806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // either exactly the same type, or that they are int/ptr operands with the
1381806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // same size (int/long, int*/long, are ok etc).
1382806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    if (!Info.hasTiedOperand()) continue;
13831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1384806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    unsigned TiedTo = Info.getTiedOperand();
1385f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner    Expr *OutputExpr = Exprs[TiedTo];
1386c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner    Expr *InputExpr = Exprs[i+NumOutputs];
13877adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    QualType InTy = InputExpr->getType();
13887adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    QualType OutTy = OutputExpr->getType();
13897adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    if (Context.hasSameType(InTy, OutTy))
1390806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner      continue;  // All types can be tied to themselves.
13911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1392aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // Decide if the input and output are in the same domain (integer/ptr or
1393aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // floating point.
1394aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    enum AsmDomain {
1395aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      AD_Int, AD_FP, AD_Other
1396aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    } InputDomain, OutputDomain;
1397aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner
1398aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    if (InTy->isIntegerType() || InTy->isPointerType())
1399aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      InputDomain = AD_Int;
14000c293ea13d452c1a47a05ada5a5ee9acc69c66ccDouglas Gregor    else if (InTy->isRealFloatingType())
1401aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      InputDomain = AD_FP;
1402aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    else
1403aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      InputDomain = AD_Other;
14043351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner
1405aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    if (OutTy->isIntegerType() || OutTy->isPointerType())
1406aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      OutputDomain = AD_Int;
14070c293ea13d452c1a47a05ada5a5ee9acc69c66ccDouglas Gregor    else if (OutTy->isRealFloatingType())
1408aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      OutputDomain = AD_FP;
1409aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    else
1410aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      OutputDomain = AD_Other;
1411aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner
1412aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // They are ok if they are the same size and in the same domain.  This
1413aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // allows tying things like:
1414aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    //   void* to int*
1415aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    //   void* to int            if they are the same size.
1416aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    //   double to long double   if they are the same size.
1417aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    //
1418aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    uint64_t OutSize = Context.getTypeSize(OutTy);
1419aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    uint64_t InSize = Context.getTypeSize(InTy);
1420aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    if (OutSize == InSize && InputDomain == OutputDomain &&
1421aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        InputDomain != AD_Other)
1422aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      continue;
1423aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner
1424aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // If the smaller input/output operand is not mentioned in the asm string,
1425aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // then we can promote it and the asm string won't notice.  Check this
1426aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // case now.
1427aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    bool SmallerValueMentioned = false;
1428aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    for (unsigned p = 0, e = Pieces.size(); p != e; ++p) {
1429aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      AsmStmt::AsmStringPiece &Piece = Pieces[p];
1430aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (!Piece.isOperand()) continue;
1431aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner
1432aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // If this is a reference to the input and if the input was the smaller
1433aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // one, then we have to reject this asm.
1434aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (Piece.getOperandNo() == i+NumOutputs) {
1435aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        if (InSize < OutSize) {
1436aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          SmallerValueMentioned = true;
1437aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          break;
14383351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        }
1439f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner      }
14401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1441aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // If this is a reference to the input and if the input was the smaller
1442aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // one, then we have to reject this asm.
1443aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (Piece.getOperandNo() == TiedTo) {
1444aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        if (InSize > OutSize) {
1445aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          SmallerValueMentioned = true;
1446aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          break;
1447aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        }
1448aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      }
1449806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    }
14501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1451aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // If the smaller value wasn't mentioned in the asm string, and if the
1452aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // output was a register, just extend the shorter one to the size of the
1453aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // larger one.
1454aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    if (!SmallerValueMentioned && InputDomain != AD_Other &&
1455aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        OutputConstraintInfos[TiedTo].allowsRegister())
1456aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      continue;
1457aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner
1458c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner    Diag(InputExpr->getLocStart(),
1459806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner         diag::err_asm_tying_incompatible_types)
14607adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      << InTy << OutTy << OutputExpr->getSourceRange()
1461806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner      << InputExpr->getSourceRange();
1462806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    DeleteStmt(NS);
1463806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    return StmtError();
1464806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  }
14651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1466fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  return Owned(NS);
1467fe795956194141c91ae555985c9b930595bff43fChris Lattner}
14683b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian
146960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1470431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
1471d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                           SourceLocation RParen, Decl *Parm,
14729ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                           Stmt *Body) {
1473d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  VarDecl *Var = cast_or_null<VarDecl>(Parm);
1474160b5630aa781ac348303e1ae088d27016637778Douglas Gregor  if (Var && Var->isInvalidDecl())
1475160b5630aa781ac348303e1ae088d27016637778Douglas Gregor    return StmtError();
1476160b5630aa781ac348303e1ae088d27016637778Douglas Gregor
14779ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) ObjCAtCatchStmt(AtLoc, RParen, Var, Body));
14783b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian}
14793b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian
148060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
14819ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, Stmt *Body) {
14829ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) ObjCAtFinallyStmt(AtLoc, Body));
1483161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian}
1484bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
148560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
14869ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtTryStmt(SourceLocation AtLoc, Stmt *Try,
14879ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                         MultiStmtArg CatchStmts, Stmt *Finally) {
1488781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->setHasBranchProtectedScope();
14898f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor  unsigned NumCatchStmts = CatchStmts.size();
14909ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(ObjCAtTryStmt::Create(Context, AtLoc, Try,
14919ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     CatchStmts.release(),
14928f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                     NumCatchStmts,
14939ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     Finally));
1494bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian}
1495bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
149660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::BuildObjCAtThrowStmt(SourceLocation AtLoc,
14979ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                  Expr *Throw) {
1498d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  if (Throw) {
1499d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    QualType ThrowType = Throw->getType();
1500d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    // Make sure the expression type is an ObjC pointer or "void *".
1501d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    if (!ThrowType->isDependentType() &&
1502d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor        !ThrowType->isObjCObjectPointerType()) {
1503d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor      const PointerType *PT = ThrowType->getAs<PointerType>();
1504d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor      if (!PT || !PT->getPointeeType()->isVoidType())
1505d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor        return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object)
1506d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor                         << Throw->getType() << Throw->getSourceRange());
1507d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    }
1508d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  }
1509d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor
15109ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) ObjCAtThrowStmt(AtLoc, Throw));
1511d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor}
1512d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor
151360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
15149ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw,
1515d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor                           Scope *CurScope) {
15169ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!Throw) {
1517e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    // @throw without an expression designates a rethrow (which much occur
1518e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    // in the context of an @catch clause).
1519e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    Scope *AtCatchParent = CurScope;
1520e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    while (AtCatchParent && !AtCatchParent->isAtCatchScope())
1521e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff      AtCatchParent = AtCatchParent->getParent();
1522e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    if (!AtCatchParent)
15234ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff      return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch));
1524d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  }
1525d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor
15269ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return BuildObjCAtThrowStmt(AtLoc, Throw);
152739f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian}
1528bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
152960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
15309ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, Expr *SyncExpr,
15319ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Stmt *SyncBody) {
1532781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->setHasBranchProtectedScope();
153346c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner
1534a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  // Make sure the expression type is an ObjC pointer or "void *".
15358fdc13a78a43f09ac396e682c35d57ca0b48216dDouglas Gregor  if (!SyncExpr->getType()->isDependentType() &&
15368fdc13a78a43f09ac396e682c35d57ca0b48216dDouglas Gregor      !SyncExpr->getType()->isObjCObjectPointerType()) {
15376217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    const PointerType *PT = SyncExpr->getType()->getAs<PointerType>();
1538a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner    if (!PT || !PT->getPointeeType()->isVoidType())
1539a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner      return StmtError(Diag(AtLoc, diag::error_objc_synchronized_expects_object)
1540a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner                       << SyncExpr->getType() << SyncExpr->getSourceRange());
1541a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  }
15421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15439ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc, SyncExpr, SyncBody));
1544fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian}
15454b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl
15464b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
15474b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// and creates a proper catch handler from them.
154860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1549d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnCXXCatchBlock(SourceLocation CatchLoc, Decl *ExDecl,
15509ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                         Stmt *HandlerBlock) {
15514b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl  // There's nothing to test that ActOnExceptionDecl didn't already test.
15528189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CXXCatchStmt(CatchLoc,
1553d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                                          cast_or_null<VarDecl>(ExDecl),
15549ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                          HandlerBlock));
15554b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl}
15568351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
15573c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace {
15583c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
1559c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlclass TypeWithHandler {
1560c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  QualType t;
1561c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  CXXCatchStmt *stmt;
1562c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlpublic:
1563c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  TypeWithHandler(const QualType &type, CXXCatchStmt *statement)
1564c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  : t(type), stmt(statement) {}
1565c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
15660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // An arbitrary order is fine as long as it places identical
15670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // types next to each other.
1568c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  bool operator<(const TypeWithHandler &y) const {
15690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (t.getAsOpaquePtr() < y.t.getAsOpaquePtr())
1570c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return true;
15710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (t.getAsOpaquePtr() > y.t.getAsOpaquePtr())
1572c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return false;
1573c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    else
1574c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return getTypeSpecStartLoc() < y.getTypeSpecStartLoc();
1575c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
15761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1577c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  bool operator==(const TypeWithHandler& other) const {
15780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return t == other.t;
1579c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
15801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1581c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  QualType getQualType() const { return t; }
1582c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  CXXCatchStmt *getCatchStmt() const { return stmt; }
1583c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  SourceLocation getTypeSpecStartLoc() const {
1584c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    return stmt->getExceptionDecl()->getTypeSpecStartLoc();
1585c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1586c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl};
1587c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
15883c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman}
15893c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
15908351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
15918351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// handlers and creates a try statement from them.
159260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
15939ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock,
15948351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl                       MultiStmtArg RawHandlers) {
15958351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  unsigned NumHandlers = RawHandlers.size();
15968351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  assert(NumHandlers > 0 &&
15978351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl         "The parser shouldn't call this if there are no handlers.");
15989ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Stmt **Handlers = RawHandlers.get();
15998351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
1600c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  llvm::SmallVector<TypeWithHandler, 8> TypesWithHandlers;
16011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (unsigned i = 0; i < NumHandlers; ++i) {
16038351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl    CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
1604c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    if (!Handler->getExceptionDecl()) {
1605c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      if (i < NumHandlers - 1)
1606c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        return StmtError(Diag(Handler->getLocStart(),
1607c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl                              diag::err_early_catch_all));
16081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1609c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      continue;
1610c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    }
16111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1612c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    const QualType CaughtType = Handler->getCaughtType();
1613c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    const QualType CanonicalCaughtType = Context.getCanonicalType(CaughtType);
1614c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    TypesWithHandlers.push_back(TypeWithHandler(CanonicalCaughtType, Handler));
1615c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1616c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1617c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  // Detect handlers for the same type as an earlier one.
1618c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  if (NumHandlers > 1) {
1619c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    llvm::array_pod_sort(TypesWithHandlers.begin(), TypesWithHandlers.end());
16201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1621c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    TypeWithHandler prev = TypesWithHandlers[0];
1622c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    for (unsigned i = 1; i < TypesWithHandlers.size(); ++i) {
1623c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      TypeWithHandler curr = TypesWithHandlers[i];
16241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1625c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      if (curr == prev) {
1626c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        Diag(curr.getTypeSpecStartLoc(),
1627c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl             diag::warn_exception_caught_by_earlier_handler)
1628c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl          << curr.getCatchStmt()->getCaughtType().getAsString();
1629c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        Diag(prev.getTypeSpecStartLoc(),
1630c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl             diag::note_previous_exception_handler)
1631c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl          << prev.getCatchStmt()->getCaughtType().getAsString();
1632c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      }
16331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1634c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      prev = curr;
1635c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    }
16368351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  }
16371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1638781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->setHasBranchProtectedScope();
1639b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
16408351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // FIXME: We should detect handlers that cannot catch anything because an
16418351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // earlier handler catches a superclass. Need to find a method that is not
16428351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // quadratic for this.
16438351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // Neither of these are explicitly forbidden, but every compiler detects them
16448351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // and warns.
16458351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
16469ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(CXXTryStmt::Create(Context, TryLoc, TryBlock,
1647a1a396df16c02b22983b5c9592022fd9237d4866Sam Weinig                                  Handlers, NumHandlers));
16488351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl}
1649