SemaStmt.cpp revision 60d7b3a319d84d688752be3870615ac0f111fb16
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
14e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Sema.h"
15e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Initialization.h"
1651fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson#include "clang/AST/APValue.h"
17f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner#include "clang/AST/ASTContext.h"
18c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
1984fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/AST/ExprCXX.h"
20419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner#include "clang/AST/ExprObjC.h"
2116f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h"
2216f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtCXX.h"
23209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall#include "clang/AST/TypeLoc.h"
2484fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/Lex/Preprocessor.h"
256fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson#include "clang/Basic/TargetInfo.h"
26c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/STLExtras.h"
27c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/SmallVector.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnExprStmt(FullExprArg expr) {
319ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Expr *E = expr.get();
321b273c403734d343d720acb28f04011807c8aa56Steve Naroff  assert(E && "ActOnExprStmt(): missing expression");
33834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // C99 6.8.3p2: The expression in an expression statement is evaluated as a
34834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // void expression for its side effects.  Conversion to void allows any
35834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // operand, even incomplete types.
36a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl
37834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // Same thing in for stmt first clause (when expr) and third clause.
38a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  return Owned(static_cast<Stmt*>(E));
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
438189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) NullStmt(SemiLoc));
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg,
47a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                           SourceLocation StartLoc,
48a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                           SourceLocation EndLoc) {
4920401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner  DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // If we have an invalid decl, just return an error.
5220401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner  if (DG.isNull()) return StmtError();
531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5424e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
57a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanianvoid Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) {
58a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
59a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian
60a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  // If we have an invalid decl, just return.
61a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  if (DG.isNull() || !DG.isSingleDecl()) return;
62a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  // suppress any potential 'unused variable' warning.
63a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  DG.getSingleDecl()->setUsed();
64a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian}
65a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian
66636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlssonvoid Sema::DiagnoseUnusedExprResult(const Stmt *S) {
67754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  const Expr *E = dyn_cast_or_null<Expr>(S);
68636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  if (!E)
69636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
70636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
71636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  SourceLocation Loc;
72636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  SourceRange R1, R2;
73df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump  if (!E->isUnusedResultAWarning(Loc, R1, R2, Context))
74636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // Okay, we have an unused result.  Depending on what the base expression is,
77419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // we might want to make a more specific diagnostic.  Check for one of these
78419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // cases now.
79419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  unsigned DiagID = diag::warn_unused_expr;
80419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  E = E->IgnoreParens();
8109105f52b1f28cbb1374c27c3c70f5517e2c465dFariborz Jahanian  if (isa<ObjCImplicitSetterGetterRefExpr>(E))
82419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner    DiagID = diag::warn_unused_property_expr;
83bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner
844dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor  if (const CXXExprWithTemporaries *Temps = dyn_cast<CXXExprWithTemporaries>(E))
854dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor    E = Temps->getSubExpr();
864dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor
87bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner  if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
880faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (E->getType()->isVoidType())
890faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return;
900faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall
91bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    // If the callee has attribute pure, const, or warn_unused_result, warn with
92bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    // a more specific message to make it clear what is happening.
93d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    if (const Decl *FD = CE->getCalleeDecl()) {
94bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<WarnUnusedResultAttr>()) {
95bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result";
96bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
97bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
98bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<PureAttr>()) {
99bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure";
100bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
101bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
102bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<ConstAttr>()) {
103bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const";
104bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
105bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
106bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    }
107bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner  }
108f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) {
109f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMethodDecl *MD = ME->getMethodDecl();
110f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
111f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result";
112f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      return;
113f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    }
114d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor  } else if (const CXXFunctionalCastExpr *FC
115d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor                                       = dyn_cast<CXXFunctionalCastExpr>(E)) {
116d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor    if (isa<CXXConstructExpr>(FC->getSubExpr()) ||
117d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor        isa<CXXTemporaryObjectExpr>(FC->getSubExpr()))
118d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor      return;
119f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  }
120209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall  // Diagnose "(void*) blah" as a typo for "(void) blah".
121209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall  else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) {
122209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    TypeSourceInfo *TI = CE->getTypeInfoAsWritten();
123209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    QualType T = TI->getType();
124209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall
125209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    // We really do want to use the non-canonical type here.
126209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    if (T == Context.VoidPtrTy) {
127209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall      PointerTypeLoc TL = cast<PointerTypeLoc>(TI->getTypeLoc());
128209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall
129209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall      Diag(Loc, diag::warn_unused_voidptr)
130209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall        << FixItHint::CreateRemoval(TL.getStarLoc());
131209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall      return;
132209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    }
133209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall  }
134209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall
13535e12c90c1b107a75c5615aa76fdbd403661aaa6Douglas Gregor  DiagRuntimeBehavior(Loc, PDiag(DiagID) << R1 << R2);
136636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson}
137636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
13860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1391b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
140a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                        MultiStmtArg elts, bool isStmtExpr) {
141a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  unsigned NumElts = elts.size();
142a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
143c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  // If we're in C89 mode, check that we don't have any decls after stmts.  If
144c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  // so, emit an extension diagnostic.
145c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
146c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // Note that __extension__ can be around a decl.
147c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    unsigned i = 0;
148c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // Skip over all declarations.
149c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
150c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      /*empty*/;
151c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner
152c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // We found the end of the list or a statement.  Scan for another declstmt.
153c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
154c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      /*empty*/;
1551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
156c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    if (i != NumElts) {
1574afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
158c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      Diag(D->getLocation(), diag::ext_mixed_decls_code);
159c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    }
160c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  }
16198414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  // Warn about unused expressions in statements.
16298414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  for (unsigned i = 0; i != NumElts; ++i) {
163636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    // Ignore statements that are last in a statement expression.
164636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    if (isStmtExpr && i == NumElts - 1)
16598414c1b7d1944a57156d52e29bd41c005de09acChris Lattner      continue;
1661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
167636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    DiagnoseUnusedExprResult(Elts[i]);
16898414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  }
169a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl
1708189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R));
1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1749ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCaseStmt(SourceLocation CaseLoc, Expr *LHSVal,
1759ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                    SourceLocation DotDotDotLoc, Expr *RHSVal,
17624e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner                    SourceLocation ColonLoc) {
1779ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  assert((LHSVal != 0) && "missing expression in case statement");
178117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.4.2p3: The expression shall be an integer constant.
1801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // However, GCC allows any evaluatable integer expression.
1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent() &&
182dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      VerifyIntegerConstantExpression(LHSVal))
18324e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner    return StmtError();
1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1856c36be5b383875b490684bcf439d6d427298c1afChris Lattner  // GCC extension: The expression shall be an integer constant.
186117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
187dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent() &&
188dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      VerifyIntegerConstantExpression(RHSVal)) {
189f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    RHSVal = 0;  // Recover by just forgetting about it.
190117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  }
191117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
192bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  if (getSwitchStack().empty()) {
1938a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner    Diag(CaseLoc, diag::err_case_not_in_switch);
19424e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner    return StmtError();
1958a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner  }
1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
197dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc,
198dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                        ColonLoc);
199bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  getSwitchStack().back()->addSwitchCase(CS);
200117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  return Owned(CS);
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20324e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner/// ActOnCaseStmtBody - This installs a statement as the body of a case.
2049ae2f076ca5ab1feb3ba95629099ec2319833701John McCallvoid Sema::ActOnCaseStmtBody(Stmt *caseStmt, Stmt *SubStmt) {
20524e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  CaseStmt *CS = static_cast<CaseStmt*>(caseStmt);
20624e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  CS->setSubStmt(SubStmt);
20724e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner}
20824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner
20960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
2101eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
2119ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                       Stmt *SubStmt, Scope *CurScope) {
212bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  if (getSwitchStack().empty()) {
2130fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner    Diag(DefaultLoc, diag::err_default_not_in_switch);
214117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl    return Owned(SubStmt);
2150fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner  }
216117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
217dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt);
218bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  getSwitchStack().back()->addSwitchCase(DS);
219117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  return Owned(DS);
2205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
2231b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
2249ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                     SourceLocation ColonLoc, Stmt *SubStmt) {
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Look up the record for this label identifier.
226ea29a3a0d6948c4a51a261d19ec1a585d2a9c779Chris Lattner  LabelStmt *&LabelDecl = getLabelMap()[II];
227de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If not forward referenced or defined already, just create a new LabelStmt.
229caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  if (LabelDecl == 0)
230caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff    return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt));
231de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(LabelDecl->getID() == II && "Label mismatch!");
233de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, this label was either forward reference or multiply defined.  If
2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // multiply defined, reject it now.
2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (LabelDecl->getSubStmt()) {
23708631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID();
2385f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner    Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition);
239de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl    return Owned(SubStmt);
2405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
241de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, this label was forward declared, and we just found its real
2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // definition.  Fill in the forward definition and return it.
2445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LabelDecl->setIdentLoc(IdentLoc);
2450fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner  LabelDecl->setSubStmt(SubStmt);
246de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(LabelDecl);
2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
250d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, Decl *CondVar,
2519ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                  Stmt *thenStmt, SourceLocation ElseLoc,
2529ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                  Stmt *elseStmt) {
25360d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult CondResult(CondVal.release());
2541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2558cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  VarDecl *ConditionVar = 0;
256d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (CondVar) {
257d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    ConditionVar = cast<VarDecl>(CondVar);
258586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    CondResult = CheckConditionVariable(ConditionVar, IfLoc, true);
25999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (CondResult.isInvalid())
26099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return StmtError();
2618cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  }
26299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  Expr *ConditionExpr = CondResult.takeAs<Expr>();
26399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!ConditionExpr)
26499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    return StmtError();
2658cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor
266754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(thenStmt);
2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2682d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // Warn if the if block has a null body without an else value.
2692d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // this helps prevent bugs due to typos, such as
2702d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // if (condition);
2712d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  //   do_stuff();
2729ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!elseStmt) {
2732d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson    if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
2742d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson      Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
2752d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  }
2762d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson
277754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(elseStmt);
2781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27943dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  return Owned(new (Context) IfStmt(Context, IfLoc, ConditionVar, ConditionExpr,
28099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor                                    thenStmt, ElseLoc, elseStmt));
2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
283f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
284f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified width and sign.  If an overflow occurs, detect it and emit
285f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified diagnostic.
286f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattnervoid Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
287f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                              unsigned NewWidth, bool NewSign,
2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              SourceLocation Loc,
289f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                              unsigned DiagID) {
290f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Perform a conversion to the promoted condition type if needed.
291f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  if (NewWidth > Val.getBitWidth()) {
292f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If this is an extension, just do it.
293f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.extend(NewWidth);
294f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.setIsSigned(NewSign);
295f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor
296f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // If the input was signed and negative and the output is
297f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // unsigned, don't bother to warn: this is implementation-defined
298f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // behavior.
299f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // FIXME: Introduce a second, default-ignored warning for this case?
300f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  } else if (NewWidth < Val.getBitWidth()) {
301f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If this is a truncation, check for overflow.
302f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt ConvVal(Val);
303f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    ConvVal.trunc(NewWidth);
304b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    ConvVal.setIsSigned(NewSign);
305f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    ConvVal.extend(Val.getBitWidth());
306b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    ConvVal.setIsSigned(Val.isSigned());
307f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    if (ConvVal != Val)
308d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
3091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
310f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // Regardless of whether a diagnostic was emitted, really do the
311f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // truncation.
312f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.trunc(NewWidth);
313b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    Val.setIsSigned(NewSign);
314f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  } else if (NewSign != Val.isSigned()) {
315f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // Convert the sign to match the sign of the condition.  This can cause
316f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // overflow as well: unsigned(INTMIN)
3172853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor    // We don't diagnose this overflow, because it is implementation-defined
3182853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor    // behavior.
3192853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor    // FIXME: Introduce a second, default-ignored warning for this case?
320f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt OldVal(Val);
321f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.setIsSigned(NewSign);
322f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  }
323f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner}
324f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
3250471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattnernamespace {
3260471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  struct CaseCompareFunctor {
3270471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
3280471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner                    const llvm::APSInt &RHS) {
3290471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      return LHS.first < RHS;
3300471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    }
3310e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner    bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
3320e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner                    const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
3330e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner      return LHS.first < RHS.first;
3340e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner    }
3350471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    bool operator()(const llvm::APSInt &LHS,
3360471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner                    const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
3370471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      return LHS < RHS.first;
3380471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    }
3390471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  };
3400471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner}
3410471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner
342764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// CmpCaseVals - Comparison predicate for sorting case values.
343764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner///
344764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattnerstatic bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
345764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner                        const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
346764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  if (lhs.first < rhs.first)
347764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner    return true;
348764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner
349764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  if (lhs.first == rhs.first &&
350764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner      lhs.second->getCaseLoc().getRawEncoding()
351764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner       < rhs.second->getCaseLoc().getRawEncoding())
352764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner    return true;
353764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  return false;
354764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner}
355764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner
356ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// CmpEnumVals - Comparison predicate for sorting enumeration values.
357ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor///
358ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
359ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor                        const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
360ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{
361ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  return lhs.first < rhs.first;
362ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor}
363ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
364ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// EqEnumVals - Comparison preficate for uniqing enumeration values.
365ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor///
366ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
367ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor                       const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
368ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{
369ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  return lhs.first == rhs.first;
370ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor}
371ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
3725f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of
3735f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// potentially integral-promoted expression @p expr.
3745f04881eb025f61396d0555d8173730fe2759e0aChris Lattnerstatic QualType GetTypeBeforeIntegralPromotion(const Expr* expr) {
3756907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  if (const CastExpr *ImplicitCast = dyn_cast<ImplicitCastExpr>(expr)) {
3765f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    const Expr *ExprBeforePromotion = ImplicitCast->getSubExpr();
3775f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    QualType TypeBeforePromotion = ExprBeforePromotion->getType();
3782ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    if (TypeBeforePromotion->isIntegralOrEnumerationType()) {
3795f04881eb025f61396d0555d8173730fe2759e0aChris Lattner      return TypeBeforePromotion;
3805f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    }
3815f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  }
3825f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  return expr->getType();
3835f04881eb025f61396d0555d8173730fe2759e0aChris Lattner}
3845f04881eb025f61396d0555d8173730fe2759e0aChris Lattner
38560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
3869ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnStartOfSwitchStmt(SourceLocation SwitchLoc, Expr *Cond,
387d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                             Decl *CondVar) {
38860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult CondResult;
3899ae2f076ca5ab1feb3ba95629099ec2319833701John McCall
390586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  VarDecl *ConditionVar = 0;
391d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (CondVar) {
392d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    ConditionVar = cast<VarDecl>(CondVar);
3939ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    CondResult = CheckConditionVariable(ConditionVar, SourceLocation(), false);
3949ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (CondResult.isInvalid())
395586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      return StmtError();
396eecf38f821fe8e113722096b77da7d68b26d28d1Douglas Gregor
3979ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    Cond = CondResult.release();
398586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  }
399586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
4009ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!Cond)
401586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    return StmtError();
402586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
4039ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  CondResult
4049ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    = ConvertToIntegralOrEnumerationType(SwitchLoc, Cond,
405c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                          PDiag(diag::err_typecheck_statement_requires_integer),
406c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                                   PDiag(diag::err_switch_incomplete_class_type)
4079ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     << Cond->getSourceRange(),
408c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                                   PDiag(diag::err_switch_explicit_conversion),
409c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                                         PDiag(diag::note_switch_conversion),
410c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                                   PDiag(diag::err_switch_multiple_conversions),
4116bc574daab3d3571d888cc4a21df67f2e2a14792Douglas Gregor                                         PDiag(diag::note_switch_conversion),
4126bc574daab3d3571d888cc4a21df67f2e2a14792Douglas Gregor                                         PDiag(0));
4139ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (CondResult.isInvalid()) return StmtError();
4149ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Cond = CondResult.take();
415c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor
416d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (!CondVar) {
4179ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    CondResult = MaybeCreateCXXExprWithTemporaries(Cond);
4189ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (CondResult.isInvalid())
419586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      return StmtError();
4209ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    Cond = CondResult.take();
421586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  }
422b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
423b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  setFunctionHasBranchIntoScope();
424586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
4259ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  SwitchStmt *SS = new (Context) SwitchStmt(Context, ConditionVar, Cond);
426586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  getSwitchStack().push_back(SS);
427586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  return Owned(SS);
4287e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner}
4297e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner
43060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
4319ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
4329ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                            Stmt *BodyStmt) {
4339ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  SwitchStmt *SS = cast<SwitchStmt>(Switch);
4349ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  assert(SS == getSwitchStack().back() && "switch stack missing push/pop!");
435de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
4369dcbfa450d751bd68fc4af8b75da381d4f6984b9Steve Naroff  SS->setBody(BodyStmt, SwitchLoc);
4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getSwitchStack().pop_back();
438c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson
439ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (SS->getCond() == 0)
440be724bab2ba7ad47aebced25e7c8ec551eb72d28Douglas Gregor    return StmtError();
441be724bab2ba7ad47aebced25e7c8ec551eb72d28Douglas Gregor
442f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  Expr *CondExpr = SS->getCond();
4430fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall  Expr *CondExprBeforePromotion = CondExpr;
44484fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  QualType CondTypeBeforePromotion =
44584fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor      GetTypeBeforeIntegralPromotion(CondExpr);
446de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
4470de55e7e6b8a53c5d1f2e9a811fd0a4ea13ed5b0Douglas Gregor  // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
4480de55e7e6b8a53c5d1f2e9a811fd0a4ea13ed5b0Douglas Gregor  UsualUnaryConversions(CondExpr);
449a0d3ca1ea5578bc736bb71bcec50ab41fefc87b9Douglas Gregor  QualType CondType = CondExpr->getType();
45084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  SS->setCond(CondExpr);
45184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor
4525f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // C++ 6.4.2.p2:
4535f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // Integral promotions are performed (on the switch condition).
4545f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  //
4555f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // A case value unrepresentable by the original switch condition
4565f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // type (before the promotion) doesn't make sense, even when it can
4575f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // be represented by the promoted type.  Therefore we need to find
4585f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // the pre-promotion type of the switch condition.
45912356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan  if (!CondExpr->isTypeDependent()) {
460acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor    // We have already converted the expression to an integral or enumeration
461acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor    // type, when we started the switch statement. If we don't have an
462acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor    // appropriate type now, just return an error.
463acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor    if (!CondType->isIntegralOrEnumerationType())
46412356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      return StmtError();
46512356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan
4662b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    if (CondExpr->isKnownToHaveBooleanValue()) {
46712356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      // switch(bool_expr) {...} is often a programmer error, e.g.
46812356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      //   switch(n && mask) { ... }  // Doh - should be "n & mask".
46912356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      // One can always use an if statement instead of switch(bool_expr).
47012356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      Diag(SwitchLoc, diag::warn_bool_switch_condition)
47112356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan          << CondExpr->getSourceRange();
47212356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan    }
473c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson  }
474de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
475f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Get the bitwidth of the switched-on value before promotions.  We must
476f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // convert the integer case values to this width before comparison.
4771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  bool HasDependentValue
478dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    = CondExpr->isTypeDependent() || CondExpr->isValueDependent();
4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned CondWidth
480dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    = HasDependentValue? 0
4815f04881eb025f61396d0555d8173730fe2759e0aChris Lattner      : static_cast<unsigned>(Context.getTypeSize(CondTypeBeforePromotion));
4825f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  bool CondIsSigned = CondTypeBeforePromotion->isSignedIntegerType();
4831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
484f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Accumulate all of the case values in a vector so that we can sort them
485f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // and detect duplicates.  This vector contains the APInt for the case after
486f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // it has been converted to the condition type.
4870471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
4880471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  CaseValsTy CaseVals;
4891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
490f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Keep track of any GNU case ranges we see.  The APSInt is the low value.
491ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy;
492ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  CaseRangesTy CaseRanges;
4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
494f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  DefaultStmt *TheDefaultStmt = 0;
4951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
496b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  bool CaseListIsErroneous = false;
4971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
498dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue;
499c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson       SC = SC->getNextSwitchCase()) {
5001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
501c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson    if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
502f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      if (TheDefaultStmt) {
503f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
5045f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner        Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
505de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
506f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        // FIXME: Remove the default statement from the switch block so that
507390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // we'll return a valid AST.  This requires recursing down the AST and
508390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // finding it, not something we are set up to do right now.  For now,
509390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // just lop the entire switch stmt out of the AST.
510b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner        CaseListIsErroneous = true;
511c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson      }
512f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      TheDefaultStmt = DS;
5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
514f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    } else {
515f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      CaseStmt *CS = cast<CaseStmt>(SC);
5161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
517f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // We already verified that the expression has a i-c-e value (C99
518f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // 6.8.4.2p3) - get that value now.
5191e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      Expr *Lo = CS->getLHS();
520dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
521dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      if (Lo->isTypeDependent() || Lo->isValueDependent()) {
522dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        HasDependentValue = true;
523dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        break;
524dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      }
5251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52651fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson      llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
5271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
528f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // Convert the value to the same width/sign as the condition.
529f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
530f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                         CS->getLHS()->getLocStart(),
531f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                         diag::warn_case_value_overflow);
5326c36be5b383875b490684bcf439d6d427298c1afChris Lattner
5331e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      // If the LHS is not the same type as the condition, insert an implicit
5341e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      // cast.
53573c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman      ImpCastExprToType(Lo, CondType, CastExpr::CK_IntegralCast);
5361e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      CS->setLHS(Lo);
5371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
538b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner      // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
539dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      if (CS->getRHS()) {
5401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (CS->getRHS()->isTypeDependent() ||
541dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            CS->getRHS()->isValueDependent()) {
542dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          HasDependentValue = true;
543dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          break;
544dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
545f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        CaseRanges.push_back(std::make_pair(LoVal, CS));
5461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else
547b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner        CaseVals.push_back(std::make_pair(LoVal, CS));
548f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    }
549f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  }
550b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner
551dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (!HasDependentValue) {
5520fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // If we don't have a default statement, check whether the
5530fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // condition is constant.
5540fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    llvm::APSInt ConstantCondValue;
5550fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    bool HasConstantCond = false;
5560fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    bool ShouldCheckConstantCond = false;
5570fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    if (!HasDependentValue && !TheDefaultStmt) {
5580fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      Expr::EvalResult Result;
5590fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      HasConstantCond = CondExprBeforePromotion->Evaluate(Result, Context);
5600fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      if (HasConstantCond) {
5610fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        assert(Result.Val.isInt() && "switch condition evaluated to non-int");
5620fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        ConstantCondValue = Result.Val.getInt();
5630fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        ShouldCheckConstantCond = true;
5640fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
5650fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        assert(ConstantCondValue.getBitWidth() == CondWidth &&
5660fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall               ConstantCondValue.isSigned() == CondIsSigned);
5670fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      }
5680fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    }
5690fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
570dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // Sort all the scalar case values so we can easily detect duplicates.
571dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
572dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
573dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    if (!CaseVals.empty()) {
5740fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) {
5750fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        if (ShouldCheckConstantCond &&
5760fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            CaseVals[i].first == ConstantCondValue)
5770fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          ShouldCheckConstantCond = false;
5780fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
5790fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) {
580dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          // If we have a duplicate, report it.
5811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          Diag(CaseVals[i].second->getLHS()->getLocStart(),
5820fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall               diag::err_duplicate_case) << CaseVals[i].first.toString(10);
5830fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          Diag(CaseVals[i-1].second->getLHS()->getLocStart(),
584dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::note_duplicate_case_prev);
585390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // FIXME: We really want to remove the bogus case stmt from the
586390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // substmt, but we have no way to do this right now.
587dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseListIsErroneous = true;
588dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
5896efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner      }
590b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner    }
5911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
592dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // Detect duplicate case ranges, which usually don't exist at all in
593dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // the first place.
594dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    if (!CaseRanges.empty()) {
595dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Sort all the case ranges by their low value so we can easily detect
596dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // overlaps between ranges.
597dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      std::stable_sort(CaseRanges.begin(), CaseRanges.end());
5981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
599dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Scan the ranges, computing the high values and removing empty ranges.
600dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      std::vector<llvm::APSInt> HiVals;
601dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
6020fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        llvm::APSInt &LoVal = CaseRanges[i].first;
603dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *CR = CaseRanges[i].second;
604dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        Expr *Hi = CR->getRHS();
605dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
6061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
607dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Convert the value to the same width/sign as the condition.
608dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
609dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                           CR->getRHS()->getLocStart(),
610dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                           diag::warn_case_value_overflow);
6111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
612dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // If the LHS is not the same type as the condition, insert an implicit
613dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // cast.
61473c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman        ImpCastExprToType(Hi, CondType, CastExpr::CK_IntegralCast);
615dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CR->setRHS(Hi);
6161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
617dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // If the low value is bigger than the high value, the case is empty.
6180fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        if (LoVal > HiVal) {
619dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
620dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            << SourceRange(CR->getLHS()->getLocStart(),
621dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                           CR->getRHS()->getLocEnd());
622dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseRanges.erase(CaseRanges.begin()+i);
623dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          --i, --e;
624dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          continue;
625dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6260fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
6270fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        if (ShouldCheckConstantCond &&
6280fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            LoVal <= ConstantCondValue &&
6290fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            ConstantCondValue <= HiVal)
6300fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          ShouldCheckConstantCond = false;
6310fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
632dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        HiVals.push_back(HiVal);
6330471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      }
6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
635dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Rescan the ranges, looking for overlap with singleton values and other
636dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // ranges.  Since the range list is sorted, we only need to compare case
637dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // ranges with their neighbors.
638dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
639dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt &CRLo = CaseRanges[i].first;
640dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt &CRHi = HiVals[i];
641dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *CR = CaseRanges[i].second;
6421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
643dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Check to see whether the case range overlaps with any
644dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // singleton cases.
645dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *OverlapStmt = 0;
646dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt OverlapVal(32);
6471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
648dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Find the smallest value >= the lower bound.  If I is in the
649dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // case range, then we have overlap.
650dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
651dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                                  CaseVals.end(), CRLo,
652dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                                  CaseCompareFunctor());
653dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (I != CaseVals.end() && I->first < CRHi) {
654dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = I->first;   // Found overlap with scalar.
655dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = I->second;
656dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
658dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Find the smallest value bigger than the upper bound.
659dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
660dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
661dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = (I-1)->first;      // Found overlap with scalar.
662dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = (I-1)->second;
663dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
665dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Check to see if this case stmt overlaps with the subsequent
666dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // case range.
667dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (i && CRLo <= HiVals[i-1]) {
668dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = HiVals[i-1];       // Found overlap with range.
669dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = CaseRanges[i-1].second;
670dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
672dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (OverlapStmt) {
673dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          // If we have a duplicate, report it.
674dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
675dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            << OverlapVal.toString(10);
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          Diag(OverlapStmt->getLHS()->getLocStart(),
677dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::note_duplicate_case_prev);
678390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // FIXME: We really want to remove the bogus case stmt from the
679390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // substmt, but we have no way to do this right now.
680dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseListIsErroneous = true;
681dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6820471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      }
683b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner    }
684ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
6850fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // Complain if we have a constant condition and we didn't find a match.
6860fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    if (!CaseListIsErroneous && ShouldCheckConstantCond) {
6870fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      // TODO: it would be nice if we printed enums as enums, chars as
6880fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      // chars, etc.
6890fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      Diag(CondExpr->getExprLoc(), diag::warn_missing_case_for_condition)
6900fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        << ConstantCondValue.toString(10)
6910fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        << CondExpr->getSourceRange();
6920fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    }
6930fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
6940fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // Check to see if switch is over an Enum and handles all of its
6950fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // values.  We don't need to do this if there's a default
6960fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // statement or if we have a constant condition.
6970fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    //
6980fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // TODO: we might want to check whether case values are out of the
6990fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // enum even if we don't want to check whether all cases are handled.
70030ab37122300a5f6664b8ae2d0b43b4396eb6bcbDouglas Gregor    const EnumType* ET = CondTypeBeforePromotion->getAs<EnumType>();
701ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor    // If switch has default case, then ignore it.
7020fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    if (!CaseListIsErroneous && !TheDefaultStmt && !HasConstantCond && ET) {
703ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      const EnumDecl *ED = ET->getDecl();
704ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      typedef llvm::SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> EnumValsTy;
705ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EnumValsTy EnumVals;
706ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
7070fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      // Gather all enum values, set their type and sort them,
7080fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      // allowing easier comparison with CaseVals.
7090fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin();
7100fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall             EDI != ED->enumerator_end(); EDI++) {
711ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        llvm::APSInt Val = (*EDI)->getInitVal();
712ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if(Val.getBitWidth() < CondWidth)
713ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          Val.extend(CondWidth);
7146623584c0ec508110d75572eef092bf98fedf3f4Douglas Gregor        else if (Val.getBitWidth() > CondWidth)
7156623584c0ec508110d75572eef092bf98fedf3f4Douglas Gregor          Val.trunc(CondWidth);
716ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        Val.setIsSigned(CondIsSigned);
717ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        EnumVals.push_back(std::make_pair(Val, (*EDI)));
718ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
719ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals);
7200fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      EnumValsTy::iterator EIend =
7210fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals);
722ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      // See which case values aren't in enum
723ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EnumValsTy::const_iterator EI = EnumVals.begin();
7240fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      for (CaseValsTy::const_iterator CI = CaseVals.begin();
7250fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall             CI != CaseVals.end(); CI++) {
726ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        while (EI != EIend && EI->first < CI->first)
727ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          EI++;
728ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (EI == EIend || EI->first > CI->first)
7290fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            Diag(CI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum)
7300fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall              << ED->getDeclName();
731ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
732ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      // See which of case ranges aren't in enum
733ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EI = EnumVals.begin();
7340fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      for (CaseRangesTy::const_iterator RI = CaseRanges.begin();
7350fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall             RI != CaseRanges.end() && EI != EIend; RI++) {
736ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        while (EI != EIend && EI->first < RI->first)
737ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          EI++;
738ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
739ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (EI == EIend || EI->first != RI->first) {
7400fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          Diag(RI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum)
7410fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            << ED->getDeclName();
742ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        }
743ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
744ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context);
745ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        while (EI != EIend && EI->first < Hi)
746ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          EI++;
747ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (EI == EIend || EI->first != Hi)
7480fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          Diag(RI->second->getRHS()->getExprLoc(), diag::warn_not_in_enum)
7490fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            << ED->getDeclName();
750ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
751ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      //Check which enum vals aren't in switch
752ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      CaseValsTy::const_iterator CI = CaseVals.begin();
753ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      CaseRangesTy::const_iterator RI = CaseRanges.begin();
754ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EI = EnumVals.begin();
755ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      for (; EI != EIend; EI++) {
756ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        //Drop unneeded case values
757ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        llvm::APSInt CIVal;
758ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        while (CI != CaseVals.end() && CI->first < EI->first)
759ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          CI++;
760ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
761ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (CI != CaseVals.end() && CI->first == EI->first)
762ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          continue;
763ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
764ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        //Drop unneeded case ranges
765ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        for (; RI != CaseRanges.end(); RI++) {
766ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context);
767ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          if (EI->first <= Hi)
768ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor            break;
769ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        }
770ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
771ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (RI == CaseRanges.end() || EI->first < RI->first)
7720fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          Diag(CondExpr->getExprLoc(), diag::warn_missing_cases)
7730fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            << EI->second->getDeclName();
774ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
775ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor    }
776b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  }
777dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
778390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: If the case list was broken is some way, we don't have a good system
779390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // to patch it up.  Instead, just return the whole substmt as broken.
780b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  if (CaseListIsErroneous)
781de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl    return StmtError();
782de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
783de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(SS);
7845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
78660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
78799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas GregorSema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond,
7889ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                     Decl *CondVar, Stmt *Body) {
78960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult CondResult(Cond.release());
79099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
7915656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  VarDecl *ConditionVar = 0;
792d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (CondVar) {
793d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    ConditionVar = cast<VarDecl>(CondVar);
794586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    CondResult = CheckConditionVariable(ConditionVar, WhileLoc, true);
79599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (CondResult.isInvalid())
79699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return StmtError();
7975656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  }
7989ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Expr *ConditionExpr = CondResult.take();
79999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!ConditionExpr)
80099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    return StmtError();
80199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
8029ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  DiagnoseUnusedExprResult(Body);
8031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80443dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  return Owned(new (Context) WhileStmt(Context, ConditionVar, ConditionExpr,
8059ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                       Body, WhileLoc));
8065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
80860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
8099ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnDoStmt(SourceLocation DoLoc, Stmt *Body,
810989135901c750af61ef012b6b0a0368be415bc46Chris Lattner                  SourceLocation WhileLoc, SourceLocation CondLParen,
8119ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                  Expr *Cond, SourceLocation CondRParen) {
8129ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  assert(Cond && "ActOnDoStmt(): missing expression");
813f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
8149ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (CheckBooleanCondition(Cond, DoLoc))
8155a881bb09928b7ade891efc680088aaad276f8d6John McCall    return StmtError();
8165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
81760d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult CondResult = MaybeCreateCXXExprWithTemporaries(Cond);
8189ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (CondResult.isInvalid())
819586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    return StmtError();
8209ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Cond = CondResult.take();
821586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor
8229ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  DiagnoseUnusedExprResult(Body);
823754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
8249ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) DoStmt(Body, Cond, DoLoc, WhileLoc, CondRParen));
8255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
82760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
828f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
8299ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                   Stmt *First, FullExprArg second, Decl *secondVar,
83099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor                   FullExprArg third,
8319ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                   SourceLocation RParenLoc, Stmt *Body) {
8325921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  if (!getLangOptions().CPlusPlus) {
8335921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
834f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // C99 6.8.5p3: The declaration part of a 'for' statement shall only
835f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // declare identifiers for objects having storage class 'auto' or
836f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // 'register'.
8375921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis      for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
8385921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis           DI!=DE; ++DI) {
8395921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        VarDecl *VD = dyn_cast<VarDecl>(*DI);
8405921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
8415921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis          VD = 0;
8425921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        if (VD == 0)
8435921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis          Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
8445921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        // FIXME: mark decl erroneous!
8455921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis      }
846ae3b701f59e78e058b83344be17206af3bf5d277Chris Lattner    }
8475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
84899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
84960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult SecondResult(second.release());
85099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  VarDecl *ConditionVar = 0;
851d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (secondVar) {
852d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    ConditionVar = cast<VarDecl>(secondVar);
853586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    SecondResult = CheckConditionVariable(ConditionVar, ForLoc, true);
85499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (SecondResult.isInvalid())
85599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return StmtError();
85699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
85799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
85899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  Expr *Third  = third.release().takeAs<Expr>();
85999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
8603af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson  DiagnoseUnusedExprResult(First);
8613af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson  DiagnoseUnusedExprResult(Third);
862754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(Body);
863754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
86443dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  return Owned(new (Context) ForStmt(Context, First,
8659ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     SecondResult.take(), ConditionVar,
86643dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor                                     Third, Body, ForLoc, LParenLoc,
86743dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor                                     RParenLoc));
8685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
87060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
871f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
872f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                                 SourceLocation LParenLoc,
8739ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                 Stmt *First, Expr *Second,
8749ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                 SourceLocation RParenLoc, Stmt *Body) {
87520552d2842245692b649e0d25380670922f954a2Fariborz Jahanian  if (First) {
87620552d2842245692b649e0d25380670922f954a2Fariborz Jahanian    QualType FirstType;
87720552d2842245692b649e0d25380670922f954a2Fariborz Jahanian    if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
8787e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner      if (!DS->isSingleDecl())
879f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag((*DS->decl_begin())->getLocation(),
880f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                         diag::err_toomany_element_decls));
881f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
8827e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner      Decl *D = DS->getSingleDecl();
883f34afeed9a0112bf31fee185b6c80556111d3834Ted Kremenek      FirstType = cast<ValueDecl>(D)->getType();
884f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // C99 6.8.5p3: The declaration part of a 'for' statement shall only
885f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // declare identifiers for objects having storage class 'auto' or
886f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // 'register'.
887248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff      VarDecl *VD = cast<VarDecl>(D);
888248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff      if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
889f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag(VD->getLocation(),
890f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                              diag::err_non_variable_decl_in_for));
8911fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson    } else {
892c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor      Expr *FirstE = cast<Expr>(First);
893c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor      if (!FirstE->isTypeDependent() &&
894c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor          FirstE->isLvalue(Context) != Expr::LV_Valid)
895f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag(First->getLocStart(),
896f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                   diag::err_selector_element_not_lvalue)
897f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl          << First->getSourceRange());
8981fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson
8991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FirstType = static_cast<Expr*>(First)->getType();
9001fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson    }
901c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor    if (!FirstType->isDependentType() &&
902c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor        !FirstType->isObjCObjectPointerType() &&
903a5e42a82ce055f29f3733f3a1f10da6cb9877deeFariborz Jahanian        !FirstType->isBlockPointerType())
904dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        Diag(ForLoc, diag::err_selector_element_type)
905d162584991885ab004a02573a73ce06422b921fcChris Lattner          << FirstType << First->getSourceRange();
9063ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  }
907c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor  if (Second && !Second->isTypeDependent()) {
908a873dfc9e7314681bb37efd9ab185045de121e43Douglas Gregor    DefaultFunctionArrayLvalueConversion(Second);
9093ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian    QualType SecondType = Second->getType();
910f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    if (!SecondType->isObjCObjectPointerType())
911dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      Diag(ForLoc, diag::err_collection_expr_type)
912d162584991885ab004a02573a73ce06422b921fcChris Lattner        << SecondType << Second->getSourceRange();
913ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian    else if (const ObjCObjectPointerType *OPT =
914ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian             SecondType->getAsObjCInterfacePointerType()) {
915ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      llvm::SmallVector<IdentifierInfo *, 4> KeyIdents;
916ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      IdentifierInfo* selIdent =
917ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian        &Context.Idents.get("countByEnumeratingWithState");
918ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      KeyIdents.push_back(selIdent);
919ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      selIdent = &Context.Idents.get("objects");
920ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      KeyIdents.push_back(selIdent);
921ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      selIdent = &Context.Idents.get("count");
922ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      KeyIdents.push_back(selIdent);
923ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      Selector CSelector = Context.Selectors.getSelector(3, &KeyIdents[0]);
924ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      if (ObjCInterfaceDecl *IDecl = OPT->getInterfaceDecl()) {
925ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian        if (!IDecl->isForwardDecl() &&
926ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian            !IDecl->lookupInstanceMethod(CSelector)) {
92780a785c2452c73b7c41d4a867edcf5a090c78c46Fariborz Jahanian          // Must further look into private implementation methods.
928ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian          if (!LookupPrivateInstanceMethod(CSelector, IDecl))
929ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian            Diag(ForLoc, diag::warn_collection_expr_type)
930ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian              << SecondType << CSelector << Second->getSourceRange();
931ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian        }
932ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      }
933ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian    }
9343ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  }
9358189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body,
9368189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                                                   ForLoc, RParenLoc));
9373ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian}
9385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
93960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
9401b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
9415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                    IdentifierInfo *LabelII) {
9425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Look up the record for this label identifier.
943ea29a3a0d6948c4a51a261d19ec1a585d2a9c779Chris Lattner  LabelStmt *&LabelDecl = getLabelMap()[LabelII];
9445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
945b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  setFunctionHasBranchIntoScope();
946b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
947caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  // If we haven't seen this label yet, create a forward reference.
948caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  if (LabelDecl == 0)
9498189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0);
9504cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
9518189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc));
9525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
95460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
955ad56d684259f706b7c0ae5ad9c23adb4f2926817Chris LattnerSema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc,
9569ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                            Expr *E) {
957bbf462314b1dc8e422b7c4dd4cac47e566aedf6dEli Friedman  // Convert operand to void*
9585f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  if (!E->isTypeDependent()) {
9595f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    QualType ETy = E->getType();
9602877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth    QualType DestTy = Context.getPointerType(Context.VoidTy.withConst());
9615f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    AssignConvertType ConvTy =
9622877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth      CheckSingleAssignmentConstraints(DestTy, E);
9632877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth    if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E, AA_Passing))
9645f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor      return StmtError();
9655f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  }
966b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
967b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  setFunctionHasIndirectGoto();
968b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
9695f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  return Owned(new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E));
9705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
97260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
9731b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Scope *S = CurScope->getContinueParent();
9755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!S) {
9765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
9774cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
9785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9794cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
9808189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ContinueStmt(ContinueLoc));
9815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
98360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
9841b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
9855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Scope *S = CurScope->getBreakParent();
9865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!S) {
9875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
9884cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
9895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9904cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
9918189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) BreakStmt(BreakLoc));
9925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9945077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \brief Determine whether a return statement is a candidate for the named
9955077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// return value optimization (C++0x 12.8p34, bullet 1).
9965077c3876beeaed32280af88244e8050078619a8Douglas Gregor///
9975077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \param Ctx The context in which the return expression and type occur.
9985077c3876beeaed32280af88244e8050078619a8Douglas Gregor///
9995077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \param RetType The return type of the function or block.
10005077c3876beeaed32280af88244e8050078619a8Douglas Gregor///
10015077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \param RetExpr The expression being returned from the function or block.
10025077c3876beeaed32280af88244e8050078619a8Douglas Gregor///
10035077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \returns The NRVO candidate variable, if the return statement may use the
10045077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// NRVO, or NULL if there is no such candidate.
10055077c3876beeaed32280af88244e8050078619a8Douglas Gregorstatic const VarDecl *getNRVOCandidate(ASTContext &Ctx, QualType RetType,
10065077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                       Expr *RetExpr) {
10073c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  QualType ExprType = RetExpr->getType();
10083c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // - in a return statement in a function with ...
10093c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // ... a class return type ...
10103c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  if (!RetType->isRecordType())
10115077c3876beeaed32280af88244e8050078619a8Douglas Gregor    return 0;
10123c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // ... the same cv-unqualified type as the function return type ...
10133c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  if (!Ctx.hasSameUnqualifiedType(RetType, ExprType))
10145077c3876beeaed32280af88244e8050078619a8Douglas Gregor    return 0;
10153c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // ... the expression is the name of a non-volatile automatic object ...
10163c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // We ignore parentheses here.
10173c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // FIXME: Is this compliant? (Everyone else does it)
10183c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetExpr->IgnoreParens());
10193c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  if (!DR)
10205077c3876beeaed32280af88244e8050078619a8Douglas Gregor    return 0;
10213c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
10223c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  if (!VD)
10235077c3876beeaed32280af88244e8050078619a8Douglas Gregor    return 0;
10245077c3876beeaed32280af88244e8050078619a8Douglas Gregor
10255077c3876beeaed32280af88244e8050078619a8Douglas Gregor  if (VD->getKind() == Decl::Var && VD->hasLocalStorage() &&
1026d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor      !VD->getType()->isReferenceType() && !VD->hasAttr<BlocksAttr>() &&
1027d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor      !VD->getType().isVolatileQualified())
10285077c3876beeaed32280af88244e8050078619a8Douglas Gregor    return VD;
10295077c3876beeaed32280af88244e8050078619a8Douglas Gregor
10305077c3876beeaed32280af88244e8050078619a8Douglas Gregor  return 0;
10313c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor}
10323c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor
103327c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
10344eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
103560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
10364eb206bebcdab28ababe8df55c6185cec2cdc071Steve NaroffSema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
10374eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // If this is the first return we've seen in the block, infer the type of
10384eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // the block from it.
10399ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  BlockScopeInfo *CurBlock = getCurBlock();
10407d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian  if (CurBlock->ReturnType.isNull()) {
1041c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff    if (RetValExp) {
104216564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      // Don't call UsualUnaryConversions(), since we don't want to do
104316564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      // integer promotions here.
1044a873dfc9e7314681bb37efd9ab185045de121e43Douglas Gregor      DefaultFunctionArrayLvalueConversion(RetValExp);
10457d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      CurBlock->ReturnType = RetValExp->getType();
10467d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(RetValExp)) {
10477d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // We have to remove a 'const' added to copied-in variable which was
10487d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // part of the implementation spec. and not the actual qualifier for
10497d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // the variable.
10507d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        if (CDRE->isConstQualAdded())
10517d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian           CurBlock->ReturnType.removeConst();
10527d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      }
1053c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff    } else
10547d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      CurBlock->ReturnType = Context.VoidTy;
10554eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  }
10567d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian  QualType FnRetType = CurBlock->ReturnType;
10574cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
105840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (CurBlock->TheDecl->hasAttr<NoReturnAttr>()) {
10596c92fa75e62937f9738696840efcb258560f4568Mike Stump    Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr)
10606c92fa75e62937f9738696840efcb258560f4568Mike Stump      << getCurFunctionOrMethodDecl()->getDeclName();
10616c92fa75e62937f9738696840efcb258560f4568Mike Stump    return StmtError();
10626c92fa75e62937f9738696840efcb258560f4568Mike Stump  }
10636c92fa75e62937f9738696840efcb258560f4568Mike Stump
10644eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // Otherwise, verify that this result type matches the previous one.  We are
10654eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // pickier with blocks than for normal functions because we don't have GCC
10664eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // compatibility to worry about here.
10675077c3876beeaed32280af88244e8050078619a8Douglas Gregor  ReturnStmt *Result = 0;
10684eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  if (CurBlock->ReturnType->isVoidType()) {
10694eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    if (RetValExp) {
10704eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff      Diag(ReturnLoc, diag::err_return_block_has_expr);
10714eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff      RetValExp = 0;
10724eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    }
10735077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0);
10745077c3876beeaed32280af88244e8050078619a8Douglas Gregor  } else if (!RetValExp) {
10754cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr));
10765077c3876beeaed32280af88244e8050078619a8Douglas Gregor  } else {
10775077c3876beeaed32280af88244e8050078619a8Douglas Gregor    const VarDecl *NRVOCandidate = 0;
10785077c3876beeaed32280af88244e8050078619a8Douglas Gregor
10795077c3876beeaed32280af88244e8050078619a8Douglas Gregor    if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
10805077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // we have a non-void block with an expression, continue checking
10815077c3876beeaed32280af88244e8050078619a8Douglas Gregor
10825077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // C99 6.8.6.4p3(136): The return statement is not an assignment. The
10835077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // overlap restriction of subclause 6.5.16.1 does not apply to the case of
10845077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // function return.
10855077c3876beeaed32280af88244e8050078619a8Douglas Gregor
10865077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // In C++ the return statement is handled via a copy initialization.
10875077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // the C version of which boils down to CheckSingleAssignmentConstraints.
10885077c3876beeaed32280af88244e8050078619a8Douglas Gregor      NRVOCandidate = getNRVOCandidate(Context, FnRetType, RetValExp);
108960d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res = PerformCopyInitialization(
10905077c3876beeaed32280af88244e8050078619a8Douglas Gregor                               InitializedEntity::InitializeResult(ReturnLoc,
10915077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                                                   FnRetType,
10925077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                                            NRVOCandidate != 0),
10935077c3876beeaed32280af88244e8050078619a8Douglas Gregor                               SourceLocation(),
10945077c3876beeaed32280af88244e8050078619a8Douglas Gregor                               Owned(RetValExp));
10955077c3876beeaed32280af88244e8050078619a8Douglas Gregor      if (Res.isInvalid()) {
10965077c3876beeaed32280af88244e8050078619a8Douglas Gregor        // FIXME: Cleanup temporaries here, anyway?
10975077c3876beeaed32280af88244e8050078619a8Douglas Gregor        return StmtError();
10985077c3876beeaed32280af88244e8050078619a8Douglas Gregor      }
10995077c3876beeaed32280af88244e8050078619a8Douglas Gregor
11005077c3876beeaed32280af88244e8050078619a8Douglas Gregor      if (RetValExp)
11015077c3876beeaed32280af88244e8050078619a8Douglas Gregor        RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp);
11024cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
11035077c3876beeaed32280af88244e8050078619a8Douglas Gregor      RetValExp = Res.takeAs<Expr>();
11045077c3876beeaed32280af88244e8050078619a8Douglas Gregor      if (RetValExp)
11055077c3876beeaed32280af88244e8050078619a8Douglas Gregor        CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
1106c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson    }
1107c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson
11085077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate);
110998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  }
11104cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
11115077c3876beeaed32280af88244e8050078619a8Douglas Gregor  // If we need to check for the named return value optimization, save the
11125077c3876beeaed32280af88244e8050078619a8Douglas Gregor  // return statement in our scope for later processing.
11135077c3876beeaed32280af88244e8050078619a8Douglas Gregor  if (getLangOptions().CPlusPlus && FnRetType->isRecordType() &&
11145077c3876beeaed32280af88244e8050078619a8Douglas Gregor      !CurContext->isDependentContext())
11155077c3876beeaed32280af88244e8050078619a8Douglas Gregor    FunctionScopes.back()->Returns.push_back(Result);
11165077c3876beeaed32280af88244e8050078619a8Douglas Gregor
11175077c3876beeaed32280af88244e8050078619a8Douglas Gregor  return Owned(Result);
11184eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
112060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
11219ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
11229ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (getCurBlock())
11234eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
11244cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1125371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  QualType FnRetType;
1126f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump  if (const FunctionDecl *FD = getCurFunctionDecl()) {
1127371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner    FnRetType = FD->getResultType();
112804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FD->hasAttr<NoReturnAttr>() ||
112904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        FD->getType()->getAs<FunctionType>()->getNoReturnAttr())
11308662587fa75d3fb04f873e265841c9314c7f5523Chris Lattner      Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr)
1131f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump        << getCurFunctionOrMethodDecl()->getDeclName();
1132f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump  } else if (ObjCMethodDecl *MD = getCurMethodDecl())
1133c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff    FnRetType = MD->getResultType();
1134c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff  else // If we don't have a function/method context, bail.
1135c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff    return StmtError();
11361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11375077c3876beeaed32280af88244e8050078619a8Douglas Gregor  ReturnStmt *Result = 0;
11385cf216b7fa64b933b60743b0b26053e8e7aa87beChris Lattner  if (FnRetType->isVoidType()) {
11391be8aee8745e8b814ad2f151aa214b0ef07833dbDouglas Gregor    if (RetValExp && !RetValExp->isTypeDependent()) {
11401be8aee8745e8b814ad2f151aa214b0ef07833dbDouglas Gregor      // C99 6.8.6.4p1 (ext_ since GCC warns)
114165ce04bef06696379682410f399f37b43996d824Chris Lattner      unsigned D = diag::ext_return_has_expr;
114265ce04bef06696379682410f399f37b43996d824Chris Lattner      if (RetValExp->getType()->isVoidType())
114365ce04bef06696379682410f399f37b43996d824Chris Lattner        D = diag::ext_return_has_void_expr;
11444cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1145e878eb035b343d7d819c092102364ec9849716aeChris Lattner      // return (some void expression); is legal in C++.
1146e878eb035b343d7d819c092102364ec9849716aeChris Lattner      if (D != diag::ext_return_has_void_expr ||
1147e878eb035b343d7d819c092102364ec9849716aeChris Lattner          !getLangOptions().CPlusPlus) {
1148e878eb035b343d7d819c092102364ec9849716aeChris Lattner        NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
1149e878eb035b343d7d819c092102364ec9849716aeChris Lattner        Diag(ReturnLoc, D)
1150e878eb035b343d7d819c092102364ec9849716aeChris Lattner          << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
1151e878eb035b343d7d819c092102364ec9849716aeChris Lattner          << RetValExp->getSourceRange();
1152e878eb035b343d7d819c092102364ec9849716aeChris Lattner      }
11531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11540ece491d8f62ce67f047491a6703fac0d3bd4ff4Anders Carlsson      RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp);
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
11565077c3876beeaed32280af88244e8050078619a8Douglas Gregor
11575077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0);
11585077c3876beeaed32280af88244e8050078619a8Douglas Gregor  } else if (!RetValExp && !FnRetType->isDependentType()) {
11593c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    unsigned DiagID = diag::warn_return_missing_expr;  // C90 6.6.6.4p4
11603c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    // C99 6.8.6.4p1 (ext_ since GCC warns)
11613c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
11623c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner
11633c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    if (FunctionDecl *FD = getCurFunctionDecl())
116408631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
11653c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    else
116608631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
11675077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc);
11685077c3876beeaed32280af88244e8050078619a8Douglas Gregor  } else {
11695077c3876beeaed32280af88244e8050078619a8Douglas Gregor    const VarDecl *NRVOCandidate = 0;
11705077c3876beeaed32280af88244e8050078619a8Douglas Gregor    if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
11715077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // we have a non-void function with an expression, continue checking
11725077c3876beeaed32280af88244e8050078619a8Douglas Gregor
11735077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // C99 6.8.6.4p3(136): The return statement is not an assignment. The
11745077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // overlap restriction of subclause 6.5.16.1 does not apply to the case of
11755077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // function return.
11765077c3876beeaed32280af88244e8050078619a8Douglas Gregor
11775077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // In C++ the return statement is handled via a copy initialization.
11785077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // the C version of which boils down to CheckSingleAssignmentConstraints.
11795077c3876beeaed32280af88244e8050078619a8Douglas Gregor      NRVOCandidate = getNRVOCandidate(Context, FnRetType, RetValExp);
118060d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res = PerformCopyInitialization(
11815077c3876beeaed32280af88244e8050078619a8Douglas Gregor                               InitializedEntity::InitializeResult(ReturnLoc,
11825077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                                                   FnRetType,
11835077c3876beeaed32280af88244e8050078619a8Douglas Gregor                                                            NRVOCandidate != 0),
11845077c3876beeaed32280af88244e8050078619a8Douglas Gregor                               SourceLocation(),
11855077c3876beeaed32280af88244e8050078619a8Douglas Gregor                               Owned(RetValExp));
11865077c3876beeaed32280af88244e8050078619a8Douglas Gregor      if (Res.isInvalid()) {
11875077c3876beeaed32280af88244e8050078619a8Douglas Gregor        // FIXME: Cleanup temporaries here, anyway?
11885077c3876beeaed32280af88244e8050078619a8Douglas Gregor        return StmtError();
11895077c3876beeaed32280af88244e8050078619a8Douglas Gregor      }
11904cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
11915077c3876beeaed32280af88244e8050078619a8Douglas Gregor      RetValExp = Res.takeAs<Expr>();
11925077c3876beeaed32280af88244e8050078619a8Douglas Gregor      if (RetValExp)
11935077c3876beeaed32280af88244e8050078619a8Douglas Gregor        CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
119466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    }
11955077c3876beeaed32280af88244e8050078619a8Douglas Gregor
11965077c3876beeaed32280af88244e8050078619a8Douglas Gregor    if (RetValExp)
11975077c3876beeaed32280af88244e8050078619a8Douglas Gregor      RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp);
11985077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate);
1199898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  }
12005077c3876beeaed32280af88244e8050078619a8Douglas Gregor
12015077c3876beeaed32280af88244e8050078619a8Douglas Gregor  // If we need to check for the named return value optimization, save the
12025077c3876beeaed32280af88244e8050078619a8Douglas Gregor  // return statement in our scope for later processing.
12035077c3876beeaed32280af88244e8050078619a8Douglas Gregor  if (getLangOptions().CPlusPlus && FnRetType->isRecordType() &&
12045077c3876beeaed32280af88244e8050078619a8Douglas Gregor      !CurContext->isDependentContext())
12055077c3876beeaed32280af88244e8050078619a8Douglas Gregor    FunctionScopes.back()->Returns.push_back(Result);
12065077c3876beeaed32280af88244e8050078619a8Douglas Gregor
12075077c3876beeaed32280af88244e8050078619a8Douglas Gregor  return Owned(Result);
12085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1210810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently
1211810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// ignore "noop" casts in places where an lvalue is required by an inline asm.
1212810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// We emulate this behavior when -fheinous-gnu-extensions is specified, but
1213810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// provide a strong guidance to not use it.
1214810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner///
1215810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// This method checks to see if the argument is an acceptable l-value and
1216810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// returns false if it is a case we can handle.
1217810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattnerstatic bool CheckAsmLValue(const Expr *E, Sema &S) {
1218703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson  // Type dependent expressions will be checked during instantiation.
1219703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson  if (E->isTypeDependent())
1220703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson    return false;
1221703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson
1222810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  if (E->isLvalue(S.Context) == Expr::LV_Valid)
1223810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    return false;  // Cool, this is an lvalue.
1224810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1225810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // Okay, this is not an lvalue, but perhaps it is the result of a cast that we
1226810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // are supposed to allow.
1227810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  const Expr *E2 = E->IgnoreParenNoopCasts(S.Context);
1228810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  if (E != E2 && E2->isLvalue(S.Context) == Expr::LV_Valid) {
1229810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    if (!S.getLangOptions().HeinousExtensions)
1230810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue)
1231810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner        << E->getSourceRange();
1232810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    else
1233810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue)
1234810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner        << E->getSourceRange();
1235810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    // Accept, even if we emitted an error diagnostic.
1236810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    return false;
1237810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  }
1238810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1239810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // None of the above, just randomly invalid non-lvalue.
1240810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  return true;
1241810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner}
1242810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1243810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
124460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
12453037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          bool IsSimple,
12463037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          bool IsVolatile,
12473037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          unsigned NumOutputs,
12483037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          unsigned NumInputs,
1249ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson                                          IdentifierInfo **Names,
12503037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg constraints,
12513037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg exprs,
12529ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                          Expr *asmString,
12533037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg clobbers,
12543b11fd3b52e7b88233c602407c151d07cb093e75Mike Stump                                          SourceLocation RParenLoc,
12553b11fd3b52e7b88233c602407c151d07cb093e75Mike Stump                                          bool MSAsm) {
12563037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  unsigned NumClobbers = clobbers.size();
12573037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral **Constraints =
12583037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl    reinterpret_cast<StringLiteral**>(constraints.get());
12599ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Expr **Exprs = exprs.get();
12609ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  StringLiteral *AsmString = cast<StringLiteral>(asmString);
12613037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get());
12623037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
126303eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson  llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
12641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12651708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  // The parser verifies that there is a string literal here.
12666bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner  if (AsmString->isWide())
12673037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl    return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
12683037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      << AsmString->getSourceRange());
12693037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
12701708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  for (unsigned i = 0; i != NumOutputs; i++) {
12711708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Constraints[i];
12726bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
12733037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
12743037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
12753037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1276ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    llvm::StringRef OutputName;
1277ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    if (Names[i])
1278ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson      OutputName = Names[i]->getName();
1279ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson
1280ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName);
1281432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner    if (!Context.Target.validateOutputConstraint(Info))
12823037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
1283432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                            diag::err_asm_invalid_output_constraint)
1284432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                       << Info.getConstraintStr());
12853037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1286d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson    // Check that the output exprs are valid lvalues.
128772056a237c536ee63285ab0850cb50f299281767Eli Friedman    Expr *OutputExpr = Exprs[i];
1288810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    if (CheckAsmLValue(OutputExpr, *this)) {
128972056a237c536ee63285ab0850cb50f299281767Eli Friedman      return StmtError(Diag(OutputExpr->getLocStart(),
1290dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                  diag::err_asm_invalid_lvalue_in_output)
129172056a237c536ee63285ab0850cb50f299281767Eli Friedman        << OutputExpr->getSourceRange());
129204728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson    }
12931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
129444def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    OutputConstraintInfos.push_back(Info);
129504728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  }
12963037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1297806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  llvm::SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
1298806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner
129904728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
13001708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Constraints[i];
13016bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
13023037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
13033037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
13043037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1305ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    llvm::StringRef InputName;
1306ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    if (Names[i])
1307ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson      InputName = Names[i]->getName();
1308ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson
1309ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    TargetInfo::ConstraintInfo Info(Literal->getString(), InputName);
1310beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    if (!Context.Target.validateInputConstraint(OutputConstraintInfos.data(),
13112819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                                NumOutputs, Info)) {
13123037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
1313432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                            diag::err_asm_invalid_input_constraint)
1314432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                       << Info.getConstraintStr());
1315d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson    }
13163037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
131772056a237c536ee63285ab0850cb50f299281767Eli Friedman    Expr *InputExpr = Exprs[i];
13183037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1319d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson    // Only allow void types for memory constraints.
132044def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.allowsMemory() && !Info.allowsRegister()) {
1321810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      if (CheckAsmLValue(InputExpr, *this))
132272056a237c536ee63285ab0850cb50f299281767Eli Friedman        return StmtError(Diag(InputExpr->getLocStart(),
1323d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson                              diag::err_asm_invalid_lvalue_in_input)
1324432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                         << Info.getConstraintStr()
132572056a237c536ee63285ab0850cb50f299281767Eli Friedman                         << InputExpr->getSourceRange());
132604728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson    }
13273037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
132844def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.allowsRegister()) {
1329d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson      if (InputExpr->getType()->isVoidType()) {
133072056a237c536ee63285ab0850cb50f299281767Eli Friedman        return StmtError(Diag(InputExpr->getLocStart(),
1331d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson                              diag::err_asm_invalid_type_in_input)
13321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          << InputExpr->getType() << Info.getConstraintStr()
133372056a237c536ee63285ab0850cb50f299281767Eli Friedman          << InputExpr->getSourceRange());
1334d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson      }
1335d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson    }
13361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1337a873dfc9e7314681bb37efd9ab185045de121e43Douglas Gregor    DefaultFunctionArrayLvalueConversion(Exprs[i]);
13381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1339806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    InputConstraintInfos.push_back(Info);
134004728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  }
13413037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
13426fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson  // Check that the clobbers are valid.
13431708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  for (unsigned i = 0; i != NumClobbers; i++) {
13441708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Clobbers[i];
13456bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
13463037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
13473037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
13483037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1349fdba9c069023f686e2608affde02c82131ee1cf8Anders Carlsson    llvm::StringRef Clobber = Literal->getString();
13503037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1351fdba9c069023f686e2608affde02c82131ee1cf8Anders Carlsson    if (!Context.Target.isValidGCCRegisterName(Clobber))
13523037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
13537765934ad7e157b5fcf925792a38e01b1edbcf8aDaniel Dunbar                  diag::err_asm_unknown_register_name) << Clobber);
13546fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson  }
13553037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1356fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  AsmStmt *NS =
1357966146e89141804ff6492739a2a6e6592ca671c7Anders Carlsson    new (Context) AsmStmt(Context, AsmLoc, IsSimple, IsVolatile, MSAsm,
1358966146e89141804ff6492739a2a6e6592ca671c7Anders Carlsson                          NumOutputs, NumInputs, Names, Constraints, Exprs,
1359966146e89141804ff6492739a2a6e6592ca671c7Anders Carlsson                          AsmString, NumClobbers, Clobbers, RParenLoc);
1360fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  // Validate the asm string, ensuring it makes sense given the operands we
1361fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  // have.
1362fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  llvm::SmallVector<AsmStmt::AsmStringPiece, 8> Pieces;
1363fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  unsigned DiagOffs;
1364fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
13652ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner    Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
13662ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner           << AsmString->getSourceRange();
1367fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner    DeleteStmt(NS);
1368fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner    return StmtError();
1369fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  }
13701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1371806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  // Validate tied input operands for type mismatches.
1372806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) {
1373806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
13741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1375806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // If this is a tied constraint, verify that the output and input have
1376806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // either exactly the same type, or that they are int/ptr operands with the
1377806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // same size (int/long, int*/long, are ok etc).
1378806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    if (!Info.hasTiedOperand()) continue;
13791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1380806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    unsigned TiedTo = Info.getTiedOperand();
1381f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner    Expr *OutputExpr = Exprs[TiedTo];
1382c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner    Expr *InputExpr = Exprs[i+NumOutputs];
13837adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    QualType InTy = InputExpr->getType();
13847adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    QualType OutTy = OutputExpr->getType();
13857adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    if (Context.hasSameType(InTy, OutTy))
1386806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner      continue;  // All types can be tied to themselves.
13871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1388aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // Decide if the input and output are in the same domain (integer/ptr or
1389aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // floating point.
1390aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    enum AsmDomain {
1391aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      AD_Int, AD_FP, AD_Other
1392aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    } InputDomain, OutputDomain;
1393aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner
1394aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    if (InTy->isIntegerType() || InTy->isPointerType())
1395aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      InputDomain = AD_Int;
13960c293ea13d452c1a47a05ada5a5ee9acc69c66ccDouglas Gregor    else if (InTy->isRealFloatingType())
1397aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      InputDomain = AD_FP;
1398aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    else
1399aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      InputDomain = AD_Other;
14003351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner
1401aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    if (OutTy->isIntegerType() || OutTy->isPointerType())
1402aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      OutputDomain = AD_Int;
14030c293ea13d452c1a47a05ada5a5ee9acc69c66ccDouglas Gregor    else if (OutTy->isRealFloatingType())
1404aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      OutputDomain = AD_FP;
1405aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    else
1406aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      OutputDomain = AD_Other;
1407aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner
1408aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // They are ok if they are the same size and in the same domain.  This
1409aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // allows tying things like:
1410aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    //   void* to int*
1411aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    //   void* to int            if they are the same size.
1412aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    //   double to long double   if they are the same size.
1413aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    //
1414aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    uint64_t OutSize = Context.getTypeSize(OutTy);
1415aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    uint64_t InSize = Context.getTypeSize(InTy);
1416aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    if (OutSize == InSize && InputDomain == OutputDomain &&
1417aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        InputDomain != AD_Other)
1418aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      continue;
1419aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner
1420aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // If the smaller input/output operand is not mentioned in the asm string,
1421aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // then we can promote it and the asm string won't notice.  Check this
1422aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // case now.
1423aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    bool SmallerValueMentioned = false;
1424aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    for (unsigned p = 0, e = Pieces.size(); p != e; ++p) {
1425aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      AsmStmt::AsmStringPiece &Piece = Pieces[p];
1426aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (!Piece.isOperand()) continue;
1427aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner
1428aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // If this is a reference to the input and if the input was the smaller
1429aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // one, then we have to reject this asm.
1430aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (Piece.getOperandNo() == i+NumOutputs) {
1431aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        if (InSize < OutSize) {
1432aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          SmallerValueMentioned = true;
1433aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          break;
14343351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        }
1435f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner      }
14361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1437aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // If this is a reference to the input and if the input was the smaller
1438aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // one, then we have to reject this asm.
1439aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (Piece.getOperandNo() == TiedTo) {
1440aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        if (InSize > OutSize) {
1441aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          SmallerValueMentioned = true;
1442aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          break;
1443aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        }
1444aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      }
1445806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    }
14461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1447aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // If the smaller value wasn't mentioned in the asm string, and if the
1448aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // output was a register, just extend the shorter one to the size of the
1449aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // larger one.
1450aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    if (!SmallerValueMentioned && InputDomain != AD_Other &&
1451aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        OutputConstraintInfos[TiedTo].allowsRegister())
1452aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      continue;
1453aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner
1454c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner    Diag(InputExpr->getLocStart(),
1455806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner         diag::err_asm_tying_incompatible_types)
14567adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      << InTy << OutTy << OutputExpr->getSourceRange()
1457806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner      << InputExpr->getSourceRange();
1458806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    DeleteStmt(NS);
1459806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    return StmtError();
1460806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  }
14611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1462fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  return Owned(NS);
1463fe795956194141c91ae555985c9b930595bff43fChris Lattner}
14643b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian
146560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1466431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
1467d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                           SourceLocation RParen, Decl *Parm,
14689ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                           Stmt *Body) {
1469d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  VarDecl *Var = cast_or_null<VarDecl>(Parm);
1470160b5630aa781ac348303e1ae088d27016637778Douglas Gregor  if (Var && Var->isInvalidDecl())
1471160b5630aa781ac348303e1ae088d27016637778Douglas Gregor    return StmtError();
1472160b5630aa781ac348303e1ae088d27016637778Douglas Gregor
14739ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) ObjCAtCatchStmt(AtLoc, RParen, Var, Body));
14743b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian}
14753b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian
147660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
14779ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, Stmt *Body) {
14789ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) ObjCAtFinallyStmt(AtLoc, Body));
1479161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian}
1480bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
148160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
14829ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtTryStmt(SourceLocation AtLoc, Stmt *Try,
14839ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                         MultiStmtArg CatchStmts, Stmt *Finally) {
1484b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  setFunctionHasBranchProtectedScope();
14858f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor  unsigned NumCatchStmts = CatchStmts.size();
14869ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(ObjCAtTryStmt::Create(Context, AtLoc, Try,
14879ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     CatchStmts.release(),
14888f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                     NumCatchStmts,
14899ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     Finally));
1490bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian}
1491bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
149260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::BuildObjCAtThrowStmt(SourceLocation AtLoc,
14939ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                  Expr *Throw) {
1494d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  if (Throw) {
1495d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    QualType ThrowType = Throw->getType();
1496d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    // Make sure the expression type is an ObjC pointer or "void *".
1497d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    if (!ThrowType->isDependentType() &&
1498d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor        !ThrowType->isObjCObjectPointerType()) {
1499d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor      const PointerType *PT = ThrowType->getAs<PointerType>();
1500d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor      if (!PT || !PT->getPointeeType()->isVoidType())
1501d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor        return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object)
1502d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor                         << Throw->getType() << Throw->getSourceRange());
1503d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    }
1504d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  }
1505d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor
15069ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) ObjCAtThrowStmt(AtLoc, Throw));
1507d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor}
1508d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor
150960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
15109ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw,
1511d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor                           Scope *CurScope) {
15129ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!Throw) {
1513e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    // @throw without an expression designates a rethrow (which much occur
1514e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    // in the context of an @catch clause).
1515e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    Scope *AtCatchParent = CurScope;
1516e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    while (AtCatchParent && !AtCatchParent->isAtCatchScope())
1517e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff      AtCatchParent = AtCatchParent->getParent();
1518e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    if (!AtCatchParent)
15194ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff      return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch));
1520d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  }
1521d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor
15229ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return BuildObjCAtThrowStmt(AtLoc, Throw);
152339f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian}
1524bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
152560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
15269ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, Expr *SyncExpr,
15279ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Stmt *SyncBody) {
1528b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  setFunctionHasBranchProtectedScope();
152946c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner
1530a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  // Make sure the expression type is an ObjC pointer or "void *".
15318fdc13a78a43f09ac396e682c35d57ca0b48216dDouglas Gregor  if (!SyncExpr->getType()->isDependentType() &&
15328fdc13a78a43f09ac396e682c35d57ca0b48216dDouglas Gregor      !SyncExpr->getType()->isObjCObjectPointerType()) {
15336217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    const PointerType *PT = SyncExpr->getType()->getAs<PointerType>();
1534a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner    if (!PT || !PT->getPointeeType()->isVoidType())
1535a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner      return StmtError(Diag(AtLoc, diag::error_objc_synchronized_expects_object)
1536a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner                       << SyncExpr->getType() << SyncExpr->getSourceRange());
1537a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  }
15381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15399ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc, SyncExpr, SyncBody));
1540fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian}
15414b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl
15424b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
15434b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// and creates a proper catch handler from them.
154460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1545d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnCXXCatchBlock(SourceLocation CatchLoc, Decl *ExDecl,
15469ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                         Stmt *HandlerBlock) {
15474b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl  // There's nothing to test that ActOnExceptionDecl didn't already test.
15488189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CXXCatchStmt(CatchLoc,
1549d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                                          cast_or_null<VarDecl>(ExDecl),
15509ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                          HandlerBlock));
15514b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl}
15528351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
15533c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace {
15543c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
1555c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlclass TypeWithHandler {
1556c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  QualType t;
1557c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  CXXCatchStmt *stmt;
1558c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlpublic:
1559c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  TypeWithHandler(const QualType &type, CXXCatchStmt *statement)
1560c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  : t(type), stmt(statement) {}
1561c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
15620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // An arbitrary order is fine as long as it places identical
15630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // types next to each other.
1564c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  bool operator<(const TypeWithHandler &y) const {
15650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (t.getAsOpaquePtr() < y.t.getAsOpaquePtr())
1566c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return true;
15670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (t.getAsOpaquePtr() > y.t.getAsOpaquePtr())
1568c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return false;
1569c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    else
1570c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return getTypeSpecStartLoc() < y.getTypeSpecStartLoc();
1571c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
15721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1573c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  bool operator==(const TypeWithHandler& other) const {
15740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return t == other.t;
1575c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
15761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1577c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  QualType getQualType() const { return t; }
1578c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  CXXCatchStmt *getCatchStmt() const { return stmt; }
1579c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  SourceLocation getTypeSpecStartLoc() const {
1580c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    return stmt->getExceptionDecl()->getTypeSpecStartLoc();
1581c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1582c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl};
1583c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
15843c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman}
15853c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
15868351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
15878351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// handlers and creates a try statement from them.
158860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
15899ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock,
15908351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl                       MultiStmtArg RawHandlers) {
15918351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  unsigned NumHandlers = RawHandlers.size();
15928351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  assert(NumHandlers > 0 &&
15938351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl         "The parser shouldn't call this if there are no handlers.");
15949ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Stmt **Handlers = RawHandlers.get();
15958351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
1596c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  llvm::SmallVector<TypeWithHandler, 8> TypesWithHandlers;
15971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (unsigned i = 0; i < NumHandlers; ++i) {
15998351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl    CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
1600c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    if (!Handler->getExceptionDecl()) {
1601c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      if (i < NumHandlers - 1)
1602c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        return StmtError(Diag(Handler->getLocStart(),
1603c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl                              diag::err_early_catch_all));
16041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1605c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      continue;
1606c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    }
16071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1608c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    const QualType CaughtType = Handler->getCaughtType();
1609c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    const QualType CanonicalCaughtType = Context.getCanonicalType(CaughtType);
1610c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    TypesWithHandlers.push_back(TypeWithHandler(CanonicalCaughtType, Handler));
1611c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1612c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1613c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  // Detect handlers for the same type as an earlier one.
1614c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  if (NumHandlers > 1) {
1615c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    llvm::array_pod_sort(TypesWithHandlers.begin(), TypesWithHandlers.end());
16161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1617c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    TypeWithHandler prev = TypesWithHandlers[0];
1618c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    for (unsigned i = 1; i < TypesWithHandlers.size(); ++i) {
1619c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      TypeWithHandler curr = TypesWithHandlers[i];
16201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1621c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      if (curr == prev) {
1622c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        Diag(curr.getTypeSpecStartLoc(),
1623c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl             diag::warn_exception_caught_by_earlier_handler)
1624c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl          << curr.getCatchStmt()->getCaughtType().getAsString();
1625c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        Diag(prev.getTypeSpecStartLoc(),
1626c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl             diag::note_previous_exception_handler)
1627c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl          << prev.getCatchStmt()->getCaughtType().getAsString();
1628c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      }
16291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1630c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      prev = curr;
1631c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    }
16328351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  }
16331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1634b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall  setFunctionHasBranchProtectedScope();
1635b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
16368351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // FIXME: We should detect handlers that cannot catch anything because an
16378351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // earlier handler catches a superclass. Need to find a method that is not
16388351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // quadratic for this.
16398351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // Neither of these are explicitly forbidden, but every compiler detects them
16408351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // and warns.
16418351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
16429ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(CXXTryStmt::Create(Context, TryLoc, TryBlock,
1643a1a396df16c02b22983b5c9592022fd9237d4866Sam Weinig                                  Handlers, NumHandlers));
16448351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl}
1645