SemaStmt.cpp revision d1377b25a36adfe6604f78cbd3a23a07cf0f29e6
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
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "Sema.h"
1518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor#include "SemaInit.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
306b1d283fe879fb11d7ce7a69feecf66e77b0eaf3Anders CarlssonSema::OwningStmtResult Sema::ActOnExprStmt(FullExprArg expr) {
316b1d283fe879fb11d7ce7a69feecf66e77b0eaf3Anders Carlsson  Expr *E = expr->takeAs<Expr>();
321b273c403734d343d720acb28f04011807c8aa56Steve Naroff  assert(E && "ActOnExprStmt(): missing expression");
3316b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian  if (E->getType()->isObjCInterfaceType()) {
3416b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian    if (LangOpts.ObjCNonFragileABI)
3516b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian      Diag(E->getLocEnd(), diag::err_indirection_requires_nonfragile_object)
3616b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian             << E->getType();
3716b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian    else
3816b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian      Diag(E->getLocEnd(), diag::err_direct_interface_unsupported)
3916b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian             << E->getType();
4016b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian    return StmtError();
4116b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian  }
42834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // C99 6.8.3p2: The expression in an expression statement is evaluated as a
43834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // void expression for its side effects.  Conversion to void allows any
44834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // operand, even incomplete types.
45a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl
46834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // Same thing in for stmt first clause (when expr) and third clause.
47a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  return Owned(static_cast<Stmt*>(E));
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
51a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian RedlSema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
528189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) NullStmt(SemiLoc));
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
55682bf92db408a6cbc3d37b5496a99b6ef85041ecChris LattnerSema::OwningStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg,
56a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                           SourceLocation StartLoc,
57a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                           SourceLocation EndLoc) {
5820401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner  DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // If we have an invalid decl, just return an error.
6120401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner  if (DG.isNull()) return StmtError();
621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6324e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
66a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanianvoid Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) {
67a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
68a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian
69a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  // If we have an invalid decl, just return.
70a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  if (DG.isNull() || !DG.isSingleDecl()) return;
71a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  // suppress any potential 'unused variable' warning.
72a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  DG.getSingleDecl()->setUsed();
73a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian}
74a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian
75636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlssonvoid Sema::DiagnoseUnusedExprResult(const Stmt *S) {
76754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  const Expr *E = dyn_cast_or_null<Expr>(S);
77636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  if (!E)
78636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
79636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
80636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  SourceLocation Loc;
81636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  SourceRange R1, R2;
82df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump  if (!E->isUnusedResultAWarning(Loc, R1, R2, Context))
83636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // Okay, we have an unused result.  Depending on what the base expression is,
86419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // we might want to make a more specific diagnostic.  Check for one of these
87419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // cases now.
88419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  unsigned DiagID = diag::warn_unused_expr;
89419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  E = E->IgnoreParens();
9009105f52b1f28cbb1374c27c3c70f5517e2c465dFariborz Jahanian  if (isa<ObjCImplicitSetterGetterRefExpr>(E))
91419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner    DiagID = diag::warn_unused_property_expr;
92bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner
934dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor  if (const CXXExprWithTemporaries *Temps = dyn_cast<CXXExprWithTemporaries>(E))
944dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor    E = Temps->getSubExpr();
954dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor  if (const CXXZeroInitValueExpr *Zero = dyn_cast<CXXZeroInitValueExpr>(E)) {
964dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor    if (const RecordType *RecordT = Zero->getType()->getAs<RecordType>())
974dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor      if (CXXRecordDecl *RecordD = dyn_cast<CXXRecordDecl>(RecordT->getDecl()))
984dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor        if (!RecordD->hasTrivialDestructor())
994dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor          return;
1004dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor  }
1014dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor
102bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner  if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1030faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (E->getType()->isVoidType())
1040faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return;
1050faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall
106bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    // If the callee has attribute pure, const, or warn_unused_result, warn with
107bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    // a more specific message to make it clear what is happening.
108d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    if (const Decl *FD = CE->getCalleeDecl()) {
109bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<WarnUnusedResultAttr>()) {
110bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result";
111bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
112bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
113bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<PureAttr>()) {
114bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure";
115bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
116bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
117bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<ConstAttr>()) {
118bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const";
119bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
120bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
121bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    }
122bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner  }
123f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) {
124f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMethodDecl *MD = ME->getMethodDecl();
125f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
126f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result";
127f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      return;
128f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    }
129d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor  } else if (const CXXFunctionalCastExpr *FC
130d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor                                       = dyn_cast<CXXFunctionalCastExpr>(E)) {
131d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor    if (isa<CXXConstructExpr>(FC->getSubExpr()) ||
132d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor        isa<CXXTemporaryObjectExpr>(FC->getSubExpr()))
133d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor      return;
134f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  }
135209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall  // Diagnose "(void*) blah" as a typo for "(void) blah".
136209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall  else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) {
137209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    TypeSourceInfo *TI = CE->getTypeInfoAsWritten();
138209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    QualType T = TI->getType();
139209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall
140209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    // We really do want to use the non-canonical type here.
141209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    if (T == Context.VoidPtrTy) {
142209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall      PointerTypeLoc TL = cast<PointerTypeLoc>(TI->getTypeLoc());
143209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall
144209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall      Diag(Loc, diag::warn_unused_voidptr)
145209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall        << FixItHint::CreateRemoval(TL.getStarLoc());
146209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall      return;
147209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    }
148209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall  }
149209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall
150419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  Diag(Loc, DiagID) << R1 << R2;
151636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson}
152636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
153a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian RedlAction::OwningStmtResult
1541b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
155a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                        MultiStmtArg elts, bool isStmtExpr) {
156a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  unsigned NumElts = elts.size();
157a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
158c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  // If we're in C89 mode, check that we don't have any decls after stmts.  If
159c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  // so, emit an extension diagnostic.
160c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
161c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // Note that __extension__ can be around a decl.
162c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    unsigned i = 0;
163c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // Skip over all declarations.
164c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
165c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      /*empty*/;
166c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner
167c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // We found the end of the list or a statement.  Scan for another declstmt.
168c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
169c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      /*empty*/;
1701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
171c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    if (i != NumElts) {
1724afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
173c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      Diag(D->getLocation(), diag::ext_mixed_decls_code);
174c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    }
175c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  }
17698414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  // Warn about unused expressions in statements.
17798414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  for (unsigned i = 0; i != NumElts; ++i) {
178636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    // Ignore statements that are last in a statement expression.
179636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    if (isStmtExpr && i == NumElts - 1)
18098414c1b7d1944a57156d52e29bd41c005de09acChris Lattner      continue;
1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
182636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    DiagnoseUnusedExprResult(Elts[i]);
18398414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  }
184a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl
1858189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R));
1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
188117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian RedlAction::OwningStmtResult
189117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian RedlSema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval,
190117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl                    SourceLocation DotDotDotLoc, ExprArg rhsval,
19124e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner                    SourceLocation ColonLoc) {
192117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  assert((lhsval.get() != 0) && "missing expression in case statement");
193117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.4.2p3: The expression shall be an integer constant.
1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // However, GCC allows any evaluatable integer expression.
196117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  Expr *LHSVal = static_cast<Expr*>(lhsval.get());
1971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent() &&
198dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      VerifyIntegerConstantExpression(LHSVal))
19924e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner    return StmtError();
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2016c36be5b383875b490684bcf439d6d427298c1afChris Lattner  // GCC extension: The expression shall be an integer constant.
202117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
203117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  Expr *RHSVal = static_cast<Expr*>(rhsval.get());
204dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent() &&
205dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      VerifyIntegerConstantExpression(RHSVal)) {
206f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    RHSVal = 0;  // Recover by just forgetting about it.
207117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl    rhsval = 0;
208117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  }
209117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
210bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  if (getSwitchStack().empty()) {
2118a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner    Diag(CaseLoc, diag::err_case_not_in_switch);
21224e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner    return StmtError();
2138a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner  }
2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
215117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  // Only now release the smart pointers.
216117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  lhsval.release();
217117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  rhsval.release();
218dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc,
219dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                        ColonLoc);
220bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  getSwitchStack().back()->addSwitchCase(CS);
221117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  return Owned(CS);
2225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22424e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner/// ActOnCaseStmtBody - This installs a statement as the body of a case.
22524e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattnervoid Sema::ActOnCaseStmtBody(StmtTy *caseStmt, StmtArg subStmt) {
22624e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  CaseStmt *CS = static_cast<CaseStmt*>(caseStmt);
227f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Stmt *SubStmt = subStmt.takeAs<Stmt>();
22824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  CS->setSubStmt(SubStmt);
22924e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner}
23024e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner
231117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian RedlAction::OwningStmtResult
2321eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
233117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl                       StmtArg subStmt, Scope *CurScope) {
234f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Stmt *SubStmt = subStmt.takeAs<Stmt>();
235117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
236bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  if (getSwitchStack().empty()) {
2370fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner    Diag(DefaultLoc, diag::err_default_not_in_switch);
238117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl    return Owned(SubStmt);
2390fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner  }
240117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
241dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt);
242bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  getSwitchStack().back()->addSwitchCase(DS);
243117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  return Owned(DS);
2445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
246de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult
2471b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
248de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl                     SourceLocation ColonLoc, StmtArg subStmt) {
249f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Stmt *SubStmt = subStmt.takeAs<Stmt>();
2505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Look up the record for this label identifier.
251ea29a3a0d6948c4a51a261d19ec1a585d2a9c779Chris Lattner  LabelStmt *&LabelDecl = getLabelMap()[II];
252de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If not forward referenced or defined already, just create a new LabelStmt.
254caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  if (LabelDecl == 0)
255caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff    return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt));
256de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(LabelDecl->getID() == II && "Label mismatch!");
258de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, this label was either forward reference or multiply defined.  If
2605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // multiply defined, reject it now.
2615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (LabelDecl->getSubStmt()) {
26208631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID();
2635f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner    Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition);
264de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl    return Owned(SubStmt);
2655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
266de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, this label was forward declared, and we just found its real
2685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // definition.  Fill in the forward definition and return it.
2695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LabelDecl->setIdentLoc(IdentLoc);
2700fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner  LabelDecl->setSubStmt(SubStmt);
271de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(LabelDecl);
2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
274de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult
27599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas GregorSema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, DeclPtrTy CondVar,
276de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl                  StmtArg ThenVal, SourceLocation ElseLoc,
277de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl                  StmtArg ElseVal) {
278a99fad8ff134273fe85f2970c7d89133d1218900Anders Carlsson  OwningExprResult CondResult(CondVal.release());
2791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2808cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  VarDecl *ConditionVar = 0;
28199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (CondVar.get()) {
28299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    ConditionVar = CondVar.getAs<VarDecl>();
28399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    CondResult = CheckConditionVariable(ConditionVar);
28499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (CondResult.isInvalid())
28599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return StmtError();
2868cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  }
28799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  Expr *ConditionExpr = CondResult.takeAs<Expr>();
28899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!ConditionExpr)
28999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    return StmtError();
2908cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor
29199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (CheckBooleanCondition(ConditionExpr, IfLoc)) {
29299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    CondResult = ConditionExpr;
2935a881bb09928b7ade891efc680088aaad276f8d6John McCall    return StmtError();
294d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor  }
295de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
296e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson  Stmt *thenStmt = ThenVal.takeAs<Stmt>();
297754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(thenStmt);
2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2992d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // Warn if the if block has a null body without an else value.
3002d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // this helps prevent bugs due to typos, such as
3012d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // if (condition);
3022d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  //   do_stuff();
3031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ElseVal.get()) {
3042d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson    if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
3052d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson      Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
3062d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  }
3072d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson
308754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  Stmt *elseStmt = ElseVal.takeAs<Stmt>();
309754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(elseStmt);
3101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
311a99fad8ff134273fe85f2970c7d89133d1218900Anders Carlsson  CondResult.release();
31299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  return Owned(new (Context) IfStmt(IfLoc, ConditionVar, ConditionExpr,
31399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor                                    thenStmt, ElseLoc, elseStmt));
3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
316de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult
31799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas GregorSema::ActOnStartOfSwitchStmt(FullExprArg cond, DeclPtrTy CondVar) {
31899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  OwningExprResult CondResult(cond.release());
31999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
320d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor  VarDecl *ConditionVar = 0;
32199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (CondVar.get()) {
32299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    ConditionVar = CondVar.getAs<VarDecl>();
32399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    CondResult = CheckConditionVariable(ConditionVar);
32499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (CondResult.isInvalid())
32599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return StmtError();
326d3d5301c44138b92bf01286183f5bf310cdd37cfDouglas Gregor  }
327be724bab2ba7ad47aebced25e7c8ec551eb72d28Douglas Gregor  SwitchStmt *SS = new (Context) SwitchStmt(ConditionVar,
328be724bab2ba7ad47aebced25e7c8ec551eb72d28Douglas Gregor                                            CondResult.takeAs<Expr>());
329bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  getSwitchStack().push_back(SS);
330de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(SS);
331c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson}
3326c36be5b383875b490684bcf439d6d427298c1afChris Lattner
333f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
334f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified width and sign.  If an overflow occurs, detect it and emit
335f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified diagnostic.
336f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattnervoid Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
337f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                              unsigned NewWidth, bool NewSign,
3381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              SourceLocation Loc,
339f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                              unsigned DiagID) {
340f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Perform a conversion to the promoted condition type if needed.
341f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  if (NewWidth > Val.getBitWidth()) {
342f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If this is an extension, just do it.
343f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.extend(NewWidth);
344f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.setIsSigned(NewSign);
345f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor
346f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // If the input was signed and negative and the output is
347f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // unsigned, don't bother to warn: this is implementation-defined
348f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // behavior.
349f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // FIXME: Introduce a second, default-ignored warning for this case?
350f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  } else if (NewWidth < Val.getBitWidth()) {
351f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If this is a truncation, check for overflow.
352f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt ConvVal(Val);
353f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    ConvVal.trunc(NewWidth);
354b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    ConvVal.setIsSigned(NewSign);
355f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    ConvVal.extend(Val.getBitWidth());
356b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    ConvVal.setIsSigned(Val.isSigned());
357f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    if (ConvVal != Val)
358d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
3591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
360f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // Regardless of whether a diagnostic was emitted, really do the
361f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // truncation.
362f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.trunc(NewWidth);
363b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    Val.setIsSigned(NewSign);
364f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  } else if (NewSign != Val.isSigned()) {
365f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // Convert the sign to match the sign of the condition.  This can cause
366f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // overflow as well: unsigned(INTMIN)
3672853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor    // We don't diagnose this overflow, because it is implementation-defined
3682853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor    // behavior.
3692853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor    // FIXME: Introduce a second, default-ignored warning for this case?
370f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt OldVal(Val);
371f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.setIsSigned(NewSign);
372f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  }
373f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner}
374f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
3750471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattnernamespace {
3760471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  struct CaseCompareFunctor {
3770471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
3780471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner                    const llvm::APSInt &RHS) {
3790471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      return LHS.first < RHS;
3800471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    }
3810e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner    bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
3820e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner                    const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
3830e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner      return LHS.first < RHS.first;
3840e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner    }
3850471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    bool operator()(const llvm::APSInt &LHS,
3860471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner                    const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
3870471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      return LHS < RHS.first;
3880471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    }
3890471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  };
3900471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner}
3910471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner
392764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// CmpCaseVals - Comparison predicate for sorting case values.
393764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner///
394764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattnerstatic bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
395764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner                        const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
396764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  if (lhs.first < rhs.first)
397764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner    return true;
398764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner
399764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  if (lhs.first == rhs.first &&
400764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner      lhs.second->getCaseLoc().getRawEncoding()
401764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner       < rhs.second->getCaseLoc().getRawEncoding())
402764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner    return true;
403764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  return false;
404764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner}
405764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner
406ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// CmpEnumVals - Comparison predicate for sorting enumeration values.
407ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor///
408ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
409ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor                        const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
410ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{
411ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  return lhs.first < rhs.first;
412ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor}
413ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
414ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// EqEnumVals - Comparison preficate for uniqing enumeration values.
415ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor///
416ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
417ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor                       const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
418ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{
419ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  return lhs.first == rhs.first;
420ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor}
421ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
4225f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of
4235f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// potentially integral-promoted expression @p expr.
4245f04881eb025f61396d0555d8173730fe2759e0aChris Lattnerstatic QualType GetTypeBeforeIntegralPromotion(const Expr* expr) {
4255f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  const ImplicitCastExpr *ImplicitCast =
4265f04881eb025f61396d0555d8173730fe2759e0aChris Lattner      dyn_cast_or_null<ImplicitCastExpr>(expr);
4275f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  if (ImplicitCast != NULL) {
4285f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    const Expr *ExprBeforePromotion = ImplicitCast->getSubExpr();
4295f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    QualType TypeBeforePromotion = ExprBeforePromotion->getType();
4305f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    if (TypeBeforePromotion->isIntegralType()) {
4315f04881eb025f61396d0555d8173730fe2759e0aChris Lattner      return TypeBeforePromotion;
4325f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    }
4335f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  }
4345f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  return expr->getType();
4355f04881eb025f61396d0555d8173730fe2759e0aChris Lattner}
4365f04881eb025f61396d0555d8173730fe2759e0aChris Lattner
4370be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor/// \brief Check (and possibly convert) the condition in a switch
4380be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor/// statement in C++.
4390be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregorstatic bool CheckCXXSwitchCondition(Sema &S, SourceLocation SwitchLoc,
4400be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor                                    Expr *&CondExpr) {
4410be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  if (CondExpr->isTypeDependent())
4420be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor    return false;
4430be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor
4440be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  QualType CondType = CondExpr->getType();
4450be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor
4460be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  // C++ 6.4.2.p2:
4470be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  // The condition shall be of integral type, enumeration type, or of a class
4480be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  // type for which a single conversion function to integral or enumeration
4490be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  // type exists (12.3). If the condition is of class type, the condition is
4500be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  // converted by calling that conversion function, and the result of the
4510be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  // conversion is used in place of the original condition for the remainder
4520be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  // of this section. Integral promotions are performed.
4530be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor
4540be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  // Make sure that the condition expression has a complete type,
4550be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  // otherwise we'll never find any conversions.
4560be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  if (S.RequireCompleteType(SwitchLoc, CondType,
457fe6b2d481d91140923f4541f273b253291884214Douglas Gregor                            S.PDiag(diag::err_switch_incomplete_class_type)
4580be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor                              << CondExpr->getSourceRange()))
4590be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor    return true;
4600be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor
4616bb8017bb9e828d118e15e59d71c66bba323c364John McCall  UnresolvedSet<4> ViableConversions;
4626bb8017bb9e828d118e15e59d71c66bba323c364John McCall  UnresolvedSet<4> ExplicitConversions;
4630be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  if (const RecordType *RecordTy = CondType->getAs<RecordType>()) {
464eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall    const UnresolvedSetImpl *Conversions
4650be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor      = cast<CXXRecordDecl>(RecordTy->getDecl())
4660be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor                                             ->getVisibleConversionFunctions();
467eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall    for (UnresolvedSetImpl::iterator I = Conversions->begin(),
4680be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor           E = Conversions->end(); I != E; ++I) {
4696bb8017bb9e828d118e15e59d71c66bba323c364John McCall      if (CXXConversionDecl *Conversion
4706bb8017bb9e828d118e15e59d71c66bba323c364John McCall            = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl()))
4710be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor        if (Conversion->getConversionType().getNonReferenceType()
4720be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor              ->isIntegralType()) {
4730be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor          if (Conversion->isExplicit())
4746bb8017bb9e828d118e15e59d71c66bba323c364John McCall            ExplicitConversions.addDecl(I.getDecl(), I.getAccess());
4750be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor          else
4766bb8017bb9e828d118e15e59d71c66bba323c364John McCall            ViableConversions.addDecl(I.getDecl(), I.getAccess());
4770be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor        }
4780be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor    }
4790be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor
4800be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor    switch (ViableConversions.size()) {
4810be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor    case 0:
4820be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor      if (ExplicitConversions.size() == 1) {
4836bb8017bb9e828d118e15e59d71c66bba323c364John McCall        DeclAccessPair Found = ExplicitConversions[0];
4846bb8017bb9e828d118e15e59d71c66bba323c364John McCall        CXXConversionDecl *Conversion =
4856bb8017bb9e828d118e15e59d71c66bba323c364John McCall          cast<CXXConversionDecl>(Found->getUnderlyingDecl());
4860be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor        // The user probably meant to invoke the given explicit
4870be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor        // conversion; use it.
4880be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor        QualType ConvTy
4896bb8017bb9e828d118e15e59d71c66bba323c364John McCall          = Conversion->getConversionType().getNonReferenceType();
4900be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor        std::string TypeStr;
4910be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor        ConvTy.getAsStringInternal(TypeStr, S.Context.PrintingPolicy);
4920be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor
4930be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor        S.Diag(SwitchLoc, diag::err_switch_explicit_conversion)
4940be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor          << CondType << ConvTy << CondExpr->getSourceRange()
495849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateInsertion(CondExpr->getLocStart(),
496849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor                                        "static_cast<" + TypeStr + ">(")
497849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor          << FixItHint::CreateInsertion(
4980be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor                            S.PP.getLocForEndOfToken(CondExpr->getLocEnd()),
4990be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor                               ")");
5006bb8017bb9e828d118e15e59d71c66bba323c364John McCall        S.Diag(Conversion->getLocation(), diag::note_switch_conversion)
5010be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor          << ConvTy->isEnumeralType() << ConvTy;
5020be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor
5030be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor        // If we aren't in a SFINAE context, build a call to the
5040be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor        // explicit conversion function.
5050be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor        if (S.isSFINAEContext())
5060be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor          return true;
5070be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor
5086bb8017bb9e828d118e15e59d71c66bba323c364John McCall        S.CheckMemberOperatorAccess(CondExpr->getExprLoc(),
5096bb8017bb9e828d118e15e59d71c66bba323c364John McCall                                    CondExpr, 0, Found);
5106bb8017bb9e828d118e15e59d71c66bba323c364John McCall        CondExpr = S.BuildCXXMemberCallExpr(CondExpr, Found, Conversion);
5110be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor      }
5120be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor
5130be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor      // We'll complain below about a non-integral condition type.
5140be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor      break;
5150be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor
5166bb8017bb9e828d118e15e59d71c66bba323c364John McCall    case 1: {
5170be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor      // Apply this conversion.
5186bb8017bb9e828d118e15e59d71c66bba323c364John McCall      DeclAccessPair Found = ViableConversions[0];
5196bb8017bb9e828d118e15e59d71c66bba323c364John McCall      S.CheckMemberOperatorAccess(CondExpr->getExprLoc(),
5206bb8017bb9e828d118e15e59d71c66bba323c364John McCall                                  CondExpr, 0, Found);
5216bb8017bb9e828d118e15e59d71c66bba323c364John McCall      CondExpr = S.BuildCXXMemberCallExpr(CondExpr, Found,
5226bb8017bb9e828d118e15e59d71c66bba323c364John McCall                        cast<CXXConversionDecl>(Found->getUnderlyingDecl()));
5230be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor      break;
5246bb8017bb9e828d118e15e59d71c66bba323c364John McCall    }
5250be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor
5260be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor    default:
5270be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor      S.Diag(SwitchLoc, diag::err_switch_multiple_conversions)
5280be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor        << CondType << CondExpr->getSourceRange();
5290be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor      for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) {
5306bb8017bb9e828d118e15e59d71c66bba323c364John McCall        CXXConversionDecl *Conv
5316bb8017bb9e828d118e15e59d71c66bba323c364John McCall          = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl());
5326bb8017bb9e828d118e15e59d71c66bba323c364John McCall        QualType ConvTy = Conv->getConversionType().getNonReferenceType();
5336bb8017bb9e828d118e15e59d71c66bba323c364John McCall        S.Diag(Conv->getLocation(), diag::note_switch_conversion)
5340be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor          << ConvTy->isEnumeralType() << ConvTy;
5350be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor      }
5360be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor      return true;
5370be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor    }
5380be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  }
5390be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor
5400be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor  return false;
5410be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor}
5420be3193b3bc695eb4b0debc7f85bc832026ce862Douglas Gregor
5437e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner/// ActOnSwitchBodyError - This is called if there is an error parsing the
5447e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner/// body of the switch stmt instead of ActOnFinishSwitchStmt.
5457e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattnervoid Sema::ActOnSwitchBodyError(SourceLocation SwitchLoc, StmtArg Switch,
5467e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner                                StmtArg Body) {
5477e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner  // Keep the switch stack balanced.
5487e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner  assert(getSwitchStack().back() == (SwitchStmt*)Switch.get() &&
5497e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner         "switch stack missing push/pop!");
5507e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner  getSwitchStack().pop_back();
5517e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner}
5527e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner
553de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult
554de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlSema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch,
555de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl                            StmtArg Body) {
556e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson  Stmt *BodyStmt = Body.takeAs<Stmt>();
557de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
558bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  SwitchStmt *SS = getSwitchStack().back();
559de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  assert(SS == (SwitchStmt*)Switch.get() && "switch stack missing push/pop!");
560de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
5619dcbfa450d751bd68fc4af8b75da381d4f6984b9Steve Naroff  SS->setBody(BodyStmt, SwitchLoc);
5621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getSwitchStack().pop_back();
563c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson
564be724bab2ba7ad47aebced25e7c8ec551eb72d28Douglas Gregor  if (SS->getCond() == 0) {
565be724bab2ba7ad47aebced25e7c8ec551eb72d28Douglas Gregor    SS->Destroy(Context);
566be724bab2ba7ad47aebced25e7c8ec551eb72d28Douglas Gregor    return StmtError();
567be724bab2ba7ad47aebced25e7c8ec551eb72d28Douglas Gregor  }
568be724bab2ba7ad47aebced25e7c8ec551eb72d28Douglas Gregor
569f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  Expr *CondExpr = SS->getCond();
57084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  QualType CondTypeBeforePromotion =
57184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor      GetTypeBeforeIntegralPromotion(CondExpr);
572de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
5730de55e7e6b8a53c5d1f2e9a811fd0a4ea13ed5b0Douglas Gregor  if (getLangOptions().CPlusPlus &&
574a0d3ca1ea5578bc736bb71bcec50ab41fefc87b9Douglas Gregor      CheckCXXSwitchCondition(*this, SwitchLoc, CondExpr))
5750de55e7e6b8a53c5d1f2e9a811fd0a4ea13ed5b0Douglas Gregor    return StmtError();
576a0d3ca1ea5578bc736bb71bcec50ab41fefc87b9Douglas Gregor
5770de55e7e6b8a53c5d1f2e9a811fd0a4ea13ed5b0Douglas Gregor  // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
5780de55e7e6b8a53c5d1f2e9a811fd0a4ea13ed5b0Douglas Gregor  UsualUnaryConversions(CondExpr);
579a0d3ca1ea5578bc736bb71bcec50ab41fefc87b9Douglas Gregor  QualType CondType = CondExpr->getType();
58084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  SS->setCond(CondExpr);
58184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor
5825f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // C++ 6.4.2.p2:
5835f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // Integral promotions are performed (on the switch condition).
5845f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  //
5855f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // A case value unrepresentable by the original switch condition
5865f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // type (before the promotion) doesn't make sense, even when it can
5875f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // be represented by the promoted type.  Therefore we need to find
5885f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // the pre-promotion type of the switch condition.
58912356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan  if (!CondExpr->isTypeDependent()) {
59012356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan    if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
59112356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
59212356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan          << CondType << CondExpr->getSourceRange();
59312356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      return StmtError();
59412356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan    }
59512356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan
5962b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    if (CondExpr->isKnownToHaveBooleanValue()) {
59712356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      // switch(bool_expr) {...} is often a programmer error, e.g.
59812356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      //   switch(n && mask) { ... }  // Doh - should be "n & mask".
59912356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      // One can always use an if statement instead of switch(bool_expr).
60012356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      Diag(SwitchLoc, diag::warn_bool_switch_condition)
60112356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan          << CondExpr->getSourceRange();
60212356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan    }
603c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson  }
604de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
605f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Get the bitwidth of the switched-on value before promotions.  We must
606f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // convert the integer case values to this width before comparison.
6071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  bool HasDependentValue
608dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    = CondExpr->isTypeDependent() || CondExpr->isValueDependent();
6091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned CondWidth
610dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    = HasDependentValue? 0
6115f04881eb025f61396d0555d8173730fe2759e0aChris Lattner      : static_cast<unsigned>(Context.getTypeSize(CondTypeBeforePromotion));
6125f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  bool CondIsSigned = CondTypeBeforePromotion->isSignedIntegerType();
6131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
614f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Accumulate all of the case values in a vector so that we can sort them
615f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // and detect duplicates.  This vector contains the APInt for the case after
616f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // it has been converted to the condition type.
6170471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
6180471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  CaseValsTy CaseVals;
6191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
620f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Keep track of any GNU case ranges we see.  The APSInt is the low value.
621ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy;
622ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  CaseRangesTy CaseRanges;
6231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
624f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  DefaultStmt *TheDefaultStmt = 0;
6251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
626b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  bool CaseListIsErroneous = false;
6271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
628dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue;
629c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson       SC = SC->getNextSwitchCase()) {
6301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
631c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson    if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
632f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      if (TheDefaultStmt) {
633f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
6345f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner        Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
635de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
636f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        // FIXME: Remove the default statement from the switch block so that
637390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // we'll return a valid AST.  This requires recursing down the AST and
638390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // finding it, not something we are set up to do right now.  For now,
639390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // just lop the entire switch stmt out of the AST.
640b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner        CaseListIsErroneous = true;
641c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson      }
642f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      TheDefaultStmt = DS;
6431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
644f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    } else {
645f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      CaseStmt *CS = cast<CaseStmt>(SC);
6461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
647f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // We already verified that the expression has a i-c-e value (C99
648f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // 6.8.4.2p3) - get that value now.
6491e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      Expr *Lo = CS->getLHS();
650dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
651dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      if (Lo->isTypeDependent() || Lo->isValueDependent()) {
652dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        HasDependentValue = true;
653dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        break;
654dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      }
6551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
65651fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson      llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
6571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
658f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // Convert the value to the same width/sign as the condition.
659f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
660f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                         CS->getLHS()->getLocStart(),
661f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                         diag::warn_case_value_overflow);
6626c36be5b383875b490684bcf439d6d427298c1afChris Lattner
6631e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      // If the LHS is not the same type as the condition, insert an implicit
6641e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      // cast.
66573c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman      ImpCastExprToType(Lo, CondType, CastExpr::CK_IntegralCast);
6661e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      CS->setLHS(Lo);
6671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
668b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner      // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
669dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      if (CS->getRHS()) {
6701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (CS->getRHS()->isTypeDependent() ||
671dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            CS->getRHS()->isValueDependent()) {
672dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          HasDependentValue = true;
673dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          break;
674dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
675f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        CaseRanges.push_back(std::make_pair(LoVal, CS));
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else
677b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner        CaseVals.push_back(std::make_pair(LoVal, CS));
678f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    }
679f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  }
680b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner
681dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (!HasDependentValue) {
682dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // Sort all the scalar case values so we can easily detect duplicates.
683dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
684dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
685dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    if (!CaseVals.empty()) {
686dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
687dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (CaseVals[i].first == CaseVals[i+1].first) {
688dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          // If we have a duplicate, report it.
689dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
690dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::err_duplicate_case) << CaseVals[i].first.toString(10);
6911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          Diag(CaseVals[i].second->getLHS()->getLocStart(),
692dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::note_duplicate_case_prev);
693390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // FIXME: We really want to remove the bogus case stmt from the
694390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // substmt, but we have no way to do this right now.
695dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseListIsErroneous = true;
696dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6976efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner      }
698b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner    }
6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
700dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // Detect duplicate case ranges, which usually don't exist at all in
701dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // the first place.
702dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    if (!CaseRanges.empty()) {
703dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Sort all the case ranges by their low value so we can easily detect
704dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // overlaps between ranges.
705dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      std::stable_sort(CaseRanges.begin(), CaseRanges.end());
7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
707dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Scan the ranges, computing the high values and removing empty ranges.
708dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      std::vector<llvm::APSInt> HiVals;
709dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
710dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *CR = CaseRanges[i].second;
711dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        Expr *Hi = CR->getRHS();
712dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
714dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Convert the value to the same width/sign as the condition.
715dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
716dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                           CR->getRHS()->getLocStart(),
717dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                           diag::warn_case_value_overflow);
7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
719dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // If the LHS is not the same type as the condition, insert an implicit
720dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // cast.
72173c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman        ImpCastExprToType(Hi, CondType, CastExpr::CK_IntegralCast);
722dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CR->setRHS(Hi);
7231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
724dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // If the low value is bigger than the high value, the case is empty.
725dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (CaseRanges[i].first > HiVal) {
726dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
727dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            << SourceRange(CR->getLHS()->getLocStart(),
728dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                           CR->getRHS()->getLocEnd());
729dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseRanges.erase(CaseRanges.begin()+i);
730dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          --i, --e;
731dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          continue;
732dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
733dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        HiVals.push_back(HiVal);
7340471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      }
7351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
736dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Rescan the ranges, looking for overlap with singleton values and other
737dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // ranges.  Since the range list is sorted, we only need to compare case
738dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // ranges with their neighbors.
739dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
740dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt &CRLo = CaseRanges[i].first;
741dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt &CRHi = HiVals[i];
742dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *CR = CaseRanges[i].second;
7431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
744dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Check to see whether the case range overlaps with any
745dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // singleton cases.
746dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *OverlapStmt = 0;
747dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt OverlapVal(32);
7481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
749dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Find the smallest value >= the lower bound.  If I is in the
750dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // case range, then we have overlap.
751dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
752dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                                  CaseVals.end(), CRLo,
753dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                                  CaseCompareFunctor());
754dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (I != CaseVals.end() && I->first < CRHi) {
755dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = I->first;   // Found overlap with scalar.
756dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = I->second;
757dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
7581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
759dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Find the smallest value bigger than the upper bound.
760dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
761dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
762dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = (I-1)->first;      // Found overlap with scalar.
763dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = (I-1)->second;
764dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
766dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Check to see if this case stmt overlaps with the subsequent
767dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // case range.
768dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (i && CRLo <= HiVals[i-1]) {
769dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = HiVals[i-1];       // Found overlap with range.
770dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = CaseRanges[i-1].second;
771dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
7721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
773dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (OverlapStmt) {
774dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          // If we have a duplicate, report it.
775dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
776dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            << OverlapVal.toString(10);
7771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          Diag(OverlapStmt->getLHS()->getLocStart(),
778dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::note_duplicate_case_prev);
779390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // FIXME: We really want to remove the bogus case stmt from the
780390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // substmt, but we have no way to do this right now.
781dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseListIsErroneous = true;
782dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
7830471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      }
784b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner    }
785ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
786ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor    // Check to see if switch is over an Enum and handles all of its
787ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor    // values
78830ab37122300a5f6664b8ae2d0b43b4396eb6bcbDouglas Gregor    const EnumType* ET = CondTypeBeforePromotion->getAs<EnumType>();
789ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor    // If switch has default case, then ignore it.
790ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor    if (!CaseListIsErroneous && !TheDefaultStmt && ET) {
791ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      const EnumDecl *ED = ET->getDecl();
792ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      typedef llvm::SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> EnumValsTy;
793ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EnumValsTy EnumVals;
794ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
795ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      // Gather all enum values, set their type and sort them, allowing easier comparison
796ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      // with CaseVals.
797ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin(); EDI != ED->enumerator_end(); EDI++) {
798ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        llvm::APSInt Val = (*EDI)->getInitVal();
799ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if(Val.getBitWidth() < CondWidth)
800ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          Val.extend(CondWidth);
801ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        Val.setIsSigned(CondIsSigned);
802ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        EnumVals.push_back(std::make_pair(Val, (*EDI)));
803ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
804ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals);
805ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EnumValsTy::iterator EIend = std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals);
806ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      // See which case values aren't in enum
807ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EnumValsTy::const_iterator EI = EnumVals.begin();
808ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      for (CaseValsTy::const_iterator CI = CaseVals.begin(); CI != CaseVals.end(); CI++) {
809ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        while (EI != EIend && EI->first < CI->first)
810ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          EI++;
811ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (EI == EIend || EI->first > CI->first)
812ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor            Diag(CI->second->getLHS()->getExprLoc(), diag::not_in_enum) << ED->getDeclName();
813ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
814ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      // See which of case ranges aren't in enum
815ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EI = EnumVals.begin();
816ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      for (CaseRangesTy::const_iterator RI = CaseRanges.begin(); RI != CaseRanges.end() && EI != EIend; RI++) {
817ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        while (EI != EIend && EI->first < RI->first)
818ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          EI++;
819ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
820ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (EI == EIend || EI->first != RI->first) {
821ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          Diag(RI->second->getLHS()->getExprLoc(), diag::not_in_enum) << ED->getDeclName();
822ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        }
823ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
824ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context);
825ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        while (EI != EIend && EI->first < Hi)
826ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          EI++;
827ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (EI == EIend || EI->first != Hi)
828ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          Diag(RI->second->getRHS()->getExprLoc(), diag::not_in_enum) << ED->getDeclName();
829ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
830ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      //Check which enum vals aren't in switch
831ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      CaseValsTy::const_iterator CI = CaseVals.begin();
832ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      CaseRangesTy::const_iterator RI = CaseRanges.begin();
833ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EI = EnumVals.begin();
834ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      for (; EI != EIend; EI++) {
835ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        //Drop unneeded case values
836ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        llvm::APSInt CIVal;
837ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        while (CI != CaseVals.end() && CI->first < EI->first)
838ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          CI++;
839ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
840ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (CI != CaseVals.end() && CI->first == EI->first)
841ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          continue;
842ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
843ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        //Drop unneeded case ranges
844ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        for (; RI != CaseRanges.end(); RI++) {
845ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context);
846ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          if (EI->first <= Hi)
847ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor            break;
848ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        }
849ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
850ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (RI == CaseRanges.end() || EI->first < RI->first)
851ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          Diag(CondExpr->getExprLoc(), diag::warn_missing_cases) << EI->second->getDeclName();
852ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
853ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor    }
854b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  }
855dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
856390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: If the case list was broken is some way, we don't have a good system
857390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // to patch it up.  Instead, just return the whole substmt as broken.
858b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  if (CaseListIsErroneous)
859de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl    return StmtError();
860de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
861de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  Switch.release();
862de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(SS);
8635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
865f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult
86699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas GregorSema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond,
86799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor                     DeclPtrTy CondVar, StmtArg Body) {
86899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  OwningExprResult CondResult(Cond.release());
86999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
8705656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  VarDecl *ConditionVar = 0;
87199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (CondVar.get()) {
87299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    ConditionVar = CondVar.getAs<VarDecl>();
87399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    CondResult = CheckConditionVariable(ConditionVar);
87499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (CondResult.isInvalid())
87599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return StmtError();
8765656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  }
87799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  Expr *ConditionExpr = CondResult.takeAs<Expr>();
87899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!ConditionExpr)
87999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    return StmtError();
88099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
88199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (CheckBooleanCondition(ConditionExpr, WhileLoc)) {
88299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    CondResult = ConditionExpr;
8835a881bb09928b7ade891efc680088aaad276f8d6John McCall    return StmtError();
8844a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor  }
8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
886754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  Stmt *bodyStmt = Body.takeAs<Stmt>();
887754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(bodyStmt);
8881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  CondResult.release();
89099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  return Owned(new (Context) WhileStmt(ConditionVar, ConditionExpr, bodyStmt,
8915656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor                                       WhileLoc));
8925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
894f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult
895f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body,
896989135901c750af61ef012b6b0a0368be415bc46Chris Lattner                  SourceLocation WhileLoc, SourceLocation CondLParen,
897989135901c750af61ef012b6b0a0368be415bc46Chris Lattner                  ExprArg Cond, SourceLocation CondRParen) {
898e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson  Expr *condExpr = Cond.takeAs<Expr>();
8991b273c403734d343d720acb28f04011807c8aa56Steve Naroff  assert(condExpr && "ActOnDoStmt(): missing expression");
900f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
9015a881bb09928b7ade891efc680088aaad276f8d6John McCall  if (CheckBooleanCondition(condExpr, DoLoc)) {
9029f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor    Cond = condExpr;
9035a881bb09928b7ade891efc680088aaad276f8d6John McCall    return StmtError();
9049f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor  }
9055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
906754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  Stmt *bodyStmt = Body.takeAs<Stmt>();
907754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(bodyStmt);
908754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
909f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Cond.release();
910754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  return Owned(new (Context) DoStmt(bodyStmt, condExpr, DoLoc,
911989135901c750af61ef012b6b0a0368be415bc46Chris Lattner                                    WhileLoc, CondRParen));
9125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
914f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult
915f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
91699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor                   StmtArg first, FullExprArg second, DeclPtrTy secondVar,
91799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor                   FullExprArg third,
918f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                   SourceLocation RParenLoc, StmtArg body) {
919f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Stmt *First  = static_cast<Stmt*>(first.get());
920f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
9215921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  if (!getLangOptions().CPlusPlus) {
9225921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
923f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // C99 6.8.5p3: The declaration part of a 'for' statement shall only
924f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // declare identifiers for objects having storage class 'auto' or
925f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // 'register'.
9265921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis      for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
9275921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis           DI!=DE; ++DI) {
9285921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        VarDecl *VD = dyn_cast<VarDecl>(*DI);
9295921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
9305921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis          VD = 0;
9315921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        if (VD == 0)
9325921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis          Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
9335921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        // FIXME: mark decl erroneous!
9345921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis      }
935ae3b701f59e78e058b83344be17206af3bf5d277Chris Lattner    }
9365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
93799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
93899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  OwningExprResult SecondResult(second.release());
93999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  VarDecl *ConditionVar = 0;
94099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (secondVar.get()) {
94199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    ConditionVar = secondVar.getAs<VarDecl>();
94299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    SecondResult = CheckConditionVariable(ConditionVar);
94399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (SecondResult.isInvalid())
94499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return StmtError();
94599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
94699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
94799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  Expr *Second = SecondResult.takeAs<Expr>();
9485a881bb09928b7ade891efc680088aaad276f8d6John McCall  if (Second && CheckBooleanCondition(Second, ForLoc)) {
94999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    SecondResult = Second;
9505a881bb09928b7ade891efc680088aaad276f8d6John McCall    return StmtError();
9515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
95399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  Expr *Third  = third.release().takeAs<Expr>();
95499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  Stmt *Body  = static_cast<Stmt*>(body.get());
95599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
9563af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson  DiagnoseUnusedExprResult(First);
9573af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson  DiagnoseUnusedExprResult(Third);
958754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(Body);
959754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
960f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  first.release();
961f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  body.release();
96299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  return Owned(new (Context) ForStmt(First, Second, ConditionVar, Third, Body,
96399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor                                     ForLoc, LParenLoc, RParenLoc));
9645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
966f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult
967f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
968f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                                 SourceLocation LParenLoc,
969f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                                 StmtArg first, ExprArg second,
970f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                                 SourceLocation RParenLoc, StmtArg body) {
971f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Stmt *First  = static_cast<Stmt*>(first.get());
972f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Expr *Second = static_cast<Expr*>(second.get());
973f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Stmt *Body  = static_cast<Stmt*>(body.get());
97420552d2842245692b649e0d25380670922f954a2Fariborz Jahanian  if (First) {
97520552d2842245692b649e0d25380670922f954a2Fariborz Jahanian    QualType FirstType;
97620552d2842245692b649e0d25380670922f954a2Fariborz Jahanian    if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
9777e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner      if (!DS->isSingleDecl())
978f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag((*DS->decl_begin())->getLocation(),
979f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                         diag::err_toomany_element_decls));
980f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
9817e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner      Decl *D = DS->getSingleDecl();
982f34afeed9a0112bf31fee185b6c80556111d3834Ted Kremenek      FirstType = cast<ValueDecl>(D)->getType();
983f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // C99 6.8.5p3: The declaration part of a 'for' statement shall only
984f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // declare identifiers for objects having storage class 'auto' or
985f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // 'register'.
986248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff      VarDecl *VD = cast<VarDecl>(D);
987248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff      if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
988f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag(VD->getLocation(),
989f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                              diag::err_non_variable_decl_in_for));
9901fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson    } else {
991810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      if (cast<Expr>(First)->isLvalue(Context) != Expr::LV_Valid)
992f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag(First->getLocStart(),
993f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                   diag::err_selector_element_not_lvalue)
994f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl          << First->getSourceRange());
9951fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson
9961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FirstType = static_cast<Expr*>(First)->getType();
9971fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson    }
9981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (!FirstType->isObjCObjectPointerType() &&
999a5e42a82ce055f29f3733f3a1f10da6cb9877deeFariborz Jahanian        !FirstType->isBlockPointerType())
1000dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        Diag(ForLoc, diag::err_selector_element_type)
1001d162584991885ab004a02573a73ce06422b921fcChris Lattner          << FirstType << First->getSourceRange();
10023ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  }
10033ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  if (Second) {
1004a873dfc9e7314681bb37efd9ab185045de121e43Douglas Gregor    DefaultFunctionArrayLvalueConversion(Second);
10053ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian    QualType SecondType = Second->getType();
1006f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    if (!SecondType->isObjCObjectPointerType())
1007dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      Diag(ForLoc, diag::err_collection_expr_type)
1008d162584991885ab004a02573a73ce06422b921fcChris Lattner        << SecondType << Second->getSourceRange();
10093ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  }
1010f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  first.release();
1011f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  second.release();
1012f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  body.release();
10138189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body,
10148189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                                                   ForLoc, RParenLoc));
10153ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian}
10165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10174cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
10181b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
10195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                    IdentifierInfo *LabelII) {
10205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Look up the record for this label identifier.
1021ea29a3a0d6948c4a51a261d19ec1a585d2a9c779Chris Lattner  LabelStmt *&LabelDecl = getLabelMap()[LabelII];
10225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1023caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  // If we haven't seen this label yet, create a forward reference.
1024caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  if (LabelDecl == 0)
10258189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0);
10264cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
10278189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc));
10285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10304cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
1031ad56d684259f706b7c0ae5ad9c23adb4f2926817Chris LattnerSema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc,
10324cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl                            ExprArg DestExp) {
1033bbf462314b1dc8e422b7c4dd4cac47e566aedf6dEli Friedman  // Convert operand to void*
103433083823342649b1fccec856c1f239c09fc0d7e1Eli Friedman  Expr* E = DestExp.takeAs<Expr>();
10355f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  if (!E->isTypeDependent()) {
10365f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    QualType ETy = E->getType();
10372877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth    QualType DestTy = Context.getPointerType(Context.VoidTy.withConst());
10385f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    AssignConvertType ConvTy =
10392877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth      CheckSingleAssignmentConstraints(DestTy, E);
10402877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth    if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E, AA_Passing))
10415f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor      return StmtError();
10425f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  }
10435f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  return Owned(new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E));
10445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10464cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
10471b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
10485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Scope *S = CurScope->getContinueParent();
10495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!S) {
10505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
10514cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
10525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10534cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
10548189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ContinueStmt(ContinueLoc));
10555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10574cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
10581b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Scope *S = CurScope->getBreakParent();
10605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!S) {
10615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
10624cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
10635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10644cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
10658189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) BreakStmt(BreakLoc));
10665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
106827c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
10694eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
10704cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
10714eb206bebcdab28ababe8df55c6185cec2cdc071Steve NaroffSema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
10724eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // If this is the first return we've seen in the block, infer the type of
10734eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // the block from it.
10749ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  BlockScopeInfo *CurBlock = getCurBlock();
10757d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian  if (CurBlock->ReturnType.isNull()) {
1076c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff    if (RetValExp) {
107716564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      // Don't call UsualUnaryConversions(), since we don't want to do
107816564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      // integer promotions here.
1079a873dfc9e7314681bb37efd9ab185045de121e43Douglas Gregor      DefaultFunctionArrayLvalueConversion(RetValExp);
10807d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      CurBlock->ReturnType = RetValExp->getType();
10817d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(RetValExp)) {
10827d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // We have to remove a 'const' added to copied-in variable which was
10837d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // part of the implementation spec. and not the actual qualifier for
10847d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // the variable.
10857d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        if (CDRE->isConstQualAdded())
10867d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian           CurBlock->ReturnType.removeConst();
10877d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      }
1088c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff    } else
10897d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      CurBlock->ReturnType = Context.VoidTy;
10904eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  }
10917d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian  QualType FnRetType = CurBlock->ReturnType;
10924cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
109340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (CurBlock->TheDecl->hasAttr<NoReturnAttr>()) {
10946c92fa75e62937f9738696840efcb258560f4568Mike Stump    Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr)
10956c92fa75e62937f9738696840efcb258560f4568Mike Stump      << getCurFunctionOrMethodDecl()->getDeclName();
10966c92fa75e62937f9738696840efcb258560f4568Mike Stump    return StmtError();
10976c92fa75e62937f9738696840efcb258560f4568Mike Stump  }
10986c92fa75e62937f9738696840efcb258560f4568Mike Stump
10994eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // Otherwise, verify that this result type matches the previous one.  We are
11004eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // pickier with blocks than for normal functions because we don't have GCC
11014eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // compatibility to worry about here.
11024eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  if (CurBlock->ReturnType->isVoidType()) {
11034eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    if (RetValExp) {
11044eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff      Diag(ReturnLoc, diag::err_return_block_has_expr);
11058189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek      RetValExp->Destroy(Context);
11064eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff      RetValExp = 0;
11074eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    }
11088189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
11094eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  }
11104cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
11114cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl  if (!RetValExp)
11124cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr));
11134cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
111498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
111598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // we have a non-void block with an expression, continue checking
111698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
11171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // C99 6.8.6.4p3(136): The return statement is not an assignment. The
11181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // overlap restriction of subclause 6.5.16.1 does not apply to the case of
111998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // function return.
11204cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
112198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // In C++ the return statement is handled via a copy initialization.
112298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // the C version of which boils down to CheckSingleAssignmentConstraints.
1123c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson    OwningExprResult Res = PerformCopyInitialization(
1124c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson                             InitializedEntity::InitializeResult(ReturnLoc,
1125c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson                                                                 FnRetType),
1126c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson                             SourceLocation(),
1127c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson                             Owned(RetValExp));
1128c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson    if (Res.isInvalid()) {
1129c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson      // FIXME: Cleanup temporaries here, anyway?
113098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      return StmtError();
1131c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson    }
1132c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson
1133c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson    RetValExp = Res.takeAs<Expr>();
1134c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson    if (RetValExp)
1135c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson      CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
113698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  }
11374cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
11388189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
11394eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1141e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl/// IsReturnCopyElidable - Whether returning @p RetExpr from a function that
1142e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl/// returns a @p RetType fulfills the criteria for copy elision (C++0x 12.8p15).
1143e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redlstatic bool IsReturnCopyElidable(ASTContext &Ctx, QualType RetType,
1144e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl                                 Expr *RetExpr) {
1145e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  QualType ExprType = RetExpr->getType();
1146e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // - in a return statement in a function with ...
1147e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // ... a class return type ...
1148e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  if (!RetType->isRecordType())
1149e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    return false;
1150e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // ... the same cv-unqualified type as the function return type ...
1151a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (!Ctx.hasSameUnqualifiedType(RetType, ExprType))
1152e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    return false;
1153e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // ... the expression is the name of a non-volatile automatic object ...
1154e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // We ignore parentheses here.
1155e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // FIXME: Is this compliant?
1156e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetExpr->IgnoreParens());
1157e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  if (!DR)
1158e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    return false;
1159e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
1160e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  if (!VD)
1161e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    return false;
1162e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  return VD->hasLocalStorage() && !VD->getType()->isReferenceType()
1163e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    && !VD->getType().isVolatileQualified();
1164e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl}
1165e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl
11664cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
1167f53b4433abb7a3bc14c0329d3175cbc291280137Anders CarlssonSema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) {
1168f53b4433abb7a3bc14c0329d3175cbc291280137Anders Carlsson  Expr *RetValExp = rex.takeAs<Expr>();
11699ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (getCurBlock())
11704eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
11714cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1172371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  QualType FnRetType;
1173f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump  if (const FunctionDecl *FD = getCurFunctionDecl()) {
1174371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner    FnRetType = FD->getResultType();
117504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FD->hasAttr<NoReturnAttr>() ||
117604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        FD->getType()->getAs<FunctionType>()->getNoReturnAttr())
11778662587fa75d3fb04f873e265841c9314c7f5523Chris Lattner      Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr)
1178f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump        << getCurFunctionOrMethodDecl()->getDeclName();
1179f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump  } else if (ObjCMethodDecl *MD = getCurMethodDecl())
1180c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff    FnRetType = MD->getResultType();
1181c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff  else // If we don't have a function/method context, bail.
1182c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff    return StmtError();
11831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11845cf216b7fa64b933b60743b0b26053e8e7aa87beChris Lattner  if (FnRetType->isVoidType()) {
11851be8aee8745e8b814ad2f151aa214b0ef07833dbDouglas Gregor    if (RetValExp && !RetValExp->isTypeDependent()) {
11861be8aee8745e8b814ad2f151aa214b0ef07833dbDouglas Gregor      // C99 6.8.6.4p1 (ext_ since GCC warns)
118765ce04bef06696379682410f399f37b43996d824Chris Lattner      unsigned D = diag::ext_return_has_expr;
118865ce04bef06696379682410f399f37b43996d824Chris Lattner      if (RetValExp->getType()->isVoidType())
118965ce04bef06696379682410f399f37b43996d824Chris Lattner        D = diag::ext_return_has_void_expr;
11904cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1191e878eb035b343d7d819c092102364ec9849716aeChris Lattner      // return (some void expression); is legal in C++.
1192e878eb035b343d7d819c092102364ec9849716aeChris Lattner      if (D != diag::ext_return_has_void_expr ||
1193e878eb035b343d7d819c092102364ec9849716aeChris Lattner          !getLangOptions().CPlusPlus) {
1194e878eb035b343d7d819c092102364ec9849716aeChris Lattner        NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
1195e878eb035b343d7d819c092102364ec9849716aeChris Lattner        Diag(ReturnLoc, D)
1196e878eb035b343d7d819c092102364ec9849716aeChris Lattner          << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
1197e878eb035b343d7d819c092102364ec9849716aeChris Lattner          << RetValExp->getSourceRange();
1198e878eb035b343d7d819c092102364ec9849716aeChris Lattner      }
11991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12000ece491d8f62ce67f047491a6703fac0d3bd4ff4Anders Carlsson      RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp);
12015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
12028189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
12035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12044cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
120503d77760a5db7990724b6901cea958a673ce0b39Anders Carlsson  if (!RetValExp && !FnRetType->isDependentType()) {
12063c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    unsigned DiagID = diag::warn_return_missing_expr;  // C90 6.6.6.4p4
12073c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    // C99 6.8.6.4p1 (ext_ since GCC warns)
12083c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
12093c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner
12103c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    if (FunctionDecl *FD = getCurFunctionDecl())
121108631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
12123c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    else
121308631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
12148189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return Owned(new (Context) ReturnStmt(ReturnLoc, (Expr*)0));
12153c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  }
12164cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1217898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
1218898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    // we have a non-void function with an expression, continue checking
12194cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // C99 6.8.6.4p3(136): The return statement is not an assignment. The
12211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // overlap restriction of subclause 6.5.16.1 does not apply to the case of
12224cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    // function return.
12234cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1224e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // C++0x 12.8p15: When certain criteria are met, an implementation is
1225e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   allowed to omit the copy construction of a class object, [...]
1226e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   - in a return statement in a function with a class return type, when
1227e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //     the expression is the name of a non-volatile automatic object with
1228e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //     the same cv-unqualified type as the function return type, the copy
1229e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //     operation can be omitted [...]
1230e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // C++0x 12.8p16: When the criteria for elision of a copy operation are met
1231e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   and the object to be copied is designated by an lvalue, overload
1232e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   resolution to select the constructor for the copy is first performed
1233e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   as if the object were designated by an rvalue.
1234e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // Note that we only compute Elidable if we're in C++0x, since we don't
1235e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // care otherwise.
1236e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    bool Elidable = getLangOptions().CPlusPlus0x ?
1237e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl                      IsReturnCopyElidable(Context, FnRetType, RetValExp) :
1238e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl                      false;
123918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    // FIXME: Elidable
124018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    (void)Elidable;
124118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
1242898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    // In C++ the return statement is handled via a copy initialization.
12434cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    // the C version of which boils down to CheckSingleAssignmentConstraints.
1244bd8f371fec3d69e5fd8e173e1bf4020225dd47d9Douglas Gregor    OwningExprResult Res = PerformCopyInitialization(
1245bd8f371fec3d69e5fd8e173e1bf4020225dd47d9Douglas Gregor                             InitializedEntity::InitializeResult(ReturnLoc,
1246d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor                                                                 FnRetType),
1247bd8f371fec3d69e5fd8e173e1bf4020225dd47d9Douglas Gregor                             SourceLocation(),
1248bd8f371fec3d69e5fd8e173e1bf4020225dd47d9Douglas Gregor                             Owned(RetValExp));
1249bd8f371fec3d69e5fd8e173e1bf4020225dd47d9Douglas Gregor    if (Res.isInvalid()) {
125018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      // FIXME: Cleanup temporaries here, anyway?
12514cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl      return StmtError();
125266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    }
125318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
1254bd8f371fec3d69e5fd8e173e1bf4020225dd47d9Douglas Gregor    RetValExp = Res.takeAs<Expr>();
125518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    if (RetValExp)
125618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
1257898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  }
1258898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1259f53b4433abb7a3bc14c0329d3175cbc291280137Anders Carlsson  if (RetValExp)
12600ece491d8f62ce67f047491a6703fac0d3bd4ff4Anders Carlsson    RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp);
12618189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
12625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1264810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently
1265810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// ignore "noop" casts in places where an lvalue is required by an inline asm.
1266810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// We emulate this behavior when -fheinous-gnu-extensions is specified, but
1267810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// provide a strong guidance to not use it.
1268810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner///
1269810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// This method checks to see if the argument is an acceptable l-value and
1270810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// returns false if it is a case we can handle.
1271810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattnerstatic bool CheckAsmLValue(const Expr *E, Sema &S) {
1272703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson  // Type dependent expressions will be checked during instantiation.
1273703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson  if (E->isTypeDependent())
1274703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson    return false;
1275703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson
1276810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  if (E->isLvalue(S.Context) == Expr::LV_Valid)
1277810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    return false;  // Cool, this is an lvalue.
1278810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1279810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // Okay, this is not an lvalue, but perhaps it is the result of a cast that we
1280810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // are supposed to allow.
1281810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  const Expr *E2 = E->IgnoreParenNoopCasts(S.Context);
1282810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  if (E != E2 && E2->isLvalue(S.Context) == Expr::LV_Valid) {
1283810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    if (!S.getLangOptions().HeinousExtensions)
1284810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue)
1285810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner        << E->getSourceRange();
1286810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    else
1287810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue)
1288810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner        << E->getSourceRange();
1289810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    // Accept, even if we emitted an error diagnostic.
1290810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    return false;
1291810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  }
1292810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1293810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // None of the above, just randomly invalid non-lvalue.
1294810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  return true;
1295810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner}
1296810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1297810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
12983037ed0a27dba62e522304183718efc149e8b6d9Sebastian RedlSema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
12993037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          bool IsSimple,
13003037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          bool IsVolatile,
13013037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          unsigned NumOutputs,
13023037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          unsigned NumInputs,
1303ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson                                          IdentifierInfo **Names,
13043037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg constraints,
13053037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg exprs,
13063037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          ExprArg asmString,
13073037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg clobbers,
13083b11fd3b52e7b88233c602407c151d07cb093e75Mike Stump                                          SourceLocation RParenLoc,
13093b11fd3b52e7b88233c602407c151d07cb093e75Mike Stump                                          bool MSAsm) {
13103037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  unsigned NumClobbers = clobbers.size();
13113037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral **Constraints =
13123037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl    reinterpret_cast<StringLiteral**>(constraints.get());
13133037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  Expr **Exprs = reinterpret_cast<Expr **>(exprs.get());
13143037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString.get());
13153037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get());
13163037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
131703eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson  llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
13181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13191708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  // The parser verifies that there is a string literal here.
13206bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner  if (AsmString->isWide())
13213037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl    return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
13223037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      << AsmString->getSourceRange());
13233037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
13241708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  for (unsigned i = 0; i != NumOutputs; i++) {
13251708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Constraints[i];
13266bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
13273037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
13283037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
13293037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1330ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    llvm::StringRef OutputName;
1331ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    if (Names[i])
1332ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson      OutputName = Names[i]->getName();
1333ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson
1334ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName);
1335432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner    if (!Context.Target.validateOutputConstraint(Info))
13363037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
1337432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                            diag::err_asm_invalid_output_constraint)
1338432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                       << Info.getConstraintStr());
13393037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1340d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson    // Check that the output exprs are valid lvalues.
134172056a237c536ee63285ab0850cb50f299281767Eli Friedman    Expr *OutputExpr = Exprs[i];
1342810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    if (CheckAsmLValue(OutputExpr, *this)) {
134372056a237c536ee63285ab0850cb50f299281767Eli Friedman      return StmtError(Diag(OutputExpr->getLocStart(),
1344dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                  diag::err_asm_invalid_lvalue_in_output)
134572056a237c536ee63285ab0850cb50f299281767Eli Friedman        << OutputExpr->getSourceRange());
134604728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson    }
13471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
134844def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    OutputConstraintInfos.push_back(Info);
134904728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  }
13503037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1351806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  llvm::SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
1352806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner
135304728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
13541708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Constraints[i];
13556bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
13563037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
13573037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
13583037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1359ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    llvm::StringRef InputName;
1360ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    if (Names[i])
1361ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson      InputName = Names[i]->getName();
1362ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson
1363ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    TargetInfo::ConstraintInfo Info(Literal->getString(), InputName);
1364beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    if (!Context.Target.validateInputConstraint(OutputConstraintInfos.data(),
13652819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                                NumOutputs, Info)) {
13663037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
1367432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                            diag::err_asm_invalid_input_constraint)
1368432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                       << Info.getConstraintStr());
1369d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson    }
13703037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
137172056a237c536ee63285ab0850cb50f299281767Eli Friedman    Expr *InputExpr = Exprs[i];
13723037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1373d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson    // Only allow void types for memory constraints.
137444def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.allowsMemory() && !Info.allowsRegister()) {
1375810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      if (CheckAsmLValue(InputExpr, *this))
137672056a237c536ee63285ab0850cb50f299281767Eli Friedman        return StmtError(Diag(InputExpr->getLocStart(),
1377d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson                              diag::err_asm_invalid_lvalue_in_input)
1378432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                         << Info.getConstraintStr()
137972056a237c536ee63285ab0850cb50f299281767Eli Friedman                         << InputExpr->getSourceRange());
138004728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson    }
13813037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
138244def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.allowsRegister()) {
1383d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson      if (InputExpr->getType()->isVoidType()) {
138472056a237c536ee63285ab0850cb50f299281767Eli Friedman        return StmtError(Diag(InputExpr->getLocStart(),
1385d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson                              diag::err_asm_invalid_type_in_input)
13861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          << InputExpr->getType() << Info.getConstraintStr()
138772056a237c536ee63285ab0850cb50f299281767Eli Friedman          << InputExpr->getSourceRange());
1388d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson      }
1389d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson    }
13901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1391a873dfc9e7314681bb37efd9ab185045de121e43Douglas Gregor    DefaultFunctionArrayLvalueConversion(Exprs[i]);
13921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1393806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    InputConstraintInfos.push_back(Info);
139404728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  }
13953037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
13966fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson  // Check that the clobbers are valid.
13971708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  for (unsigned i = 0; i != NumClobbers; i++) {
13981708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Clobbers[i];
13996bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
14003037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
14013037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
14023037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1403fdba9c069023f686e2608affde02c82131ee1cf8Anders Carlsson    llvm::StringRef Clobber = Literal->getString();
14043037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1405fdba9c069023f686e2608affde02c82131ee1cf8Anders Carlsson    if (!Context.Target.isValidGCCRegisterName(Clobber))
14063037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
14077765934ad7e157b5fcf925792a38e01b1edbcf8aDaniel Dunbar                  diag::err_asm_unknown_register_name) << Clobber);
14086fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson  }
14093037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
14103037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  constraints.release();
14113037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  exprs.release();
14123037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  asmString.release();
14133037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  clobbers.release();
1414fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  AsmStmt *NS =
1415966146e89141804ff6492739a2a6e6592ca671c7Anders Carlsson    new (Context) AsmStmt(Context, AsmLoc, IsSimple, IsVolatile, MSAsm,
1416966146e89141804ff6492739a2a6e6592ca671c7Anders Carlsson                          NumOutputs, NumInputs, Names, Constraints, Exprs,
1417966146e89141804ff6492739a2a6e6592ca671c7Anders Carlsson                          AsmString, NumClobbers, Clobbers, RParenLoc);
1418fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  // Validate the asm string, ensuring it makes sense given the operands we
1419fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  // have.
1420fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  llvm::SmallVector<AsmStmt::AsmStringPiece, 8> Pieces;
1421fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  unsigned DiagOffs;
1422fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
14232ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner    Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
14242ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner           << AsmString->getSourceRange();
1425fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner    DeleteStmt(NS);
1426fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner    return StmtError();
1427fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  }
14281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1429806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  // Validate tied input operands for type mismatches.
1430806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) {
1431806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
14321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1433806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // If this is a tied constraint, verify that the output and input have
1434806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // either exactly the same type, or that they are int/ptr operands with the
1435806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // same size (int/long, int*/long, are ok etc).
1436806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    if (!Info.hasTiedOperand()) continue;
14371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1438806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    unsigned TiedTo = Info.getTiedOperand();
1439f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner    Expr *OutputExpr = Exprs[TiedTo];
1440c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner    Expr *InputExpr = Exprs[i+NumOutputs];
14417adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    QualType InTy = InputExpr->getType();
14427adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    QualType OutTy = OutputExpr->getType();
14437adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    if (Context.hasSameType(InTy, OutTy))
1444806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner      continue;  // All types can be tied to themselves.
14451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14467adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    // Int/ptr operands have some special cases that we allow.
14477adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    if ((OutTy->isIntegerType() || OutTy->isPointerType()) &&
14487adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner        (InTy->isIntegerType() || InTy->isPointerType())) {
14491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14507adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      // They are ok if they are the same size.  Tying void* to int is ok if
14517adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      // they are the same size, for example.  This also allows tying void* to
14527adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      // int*.
14533351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      uint64_t OutSize = Context.getTypeSize(OutTy);
14543351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      uint64_t InSize = Context.getTypeSize(InTy);
14553351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      if (OutSize == InSize)
1456806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner        continue;
14571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14583351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // If the smaller input/output operand is not mentioned in the asm string,
14593351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // then we can promote it and the asm string won't notice.  Check this
1460f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner      // case now.
14613351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      bool SmallerValueMentioned = false;
146258bce89d12b1dfba16637ce3754d6cb24c3099bbChris Lattner      for (unsigned p = 0, e = Pieces.size(); p != e; ++p) {
146358bce89d12b1dfba16637ce3754d6cb24c3099bbChris Lattner        AsmStmt::AsmStringPiece &Piece = Pieces[p];
146458bce89d12b1dfba16637ce3754d6cb24c3099bbChris Lattner        if (!Piece.isOperand()) continue;
14651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14663351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        // If this is a reference to the input and if the input was the smaller
14673351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        // one, then we have to reject this asm.
14683351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        if (Piece.getOperandNo() == i+NumOutputs) {
14693351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner          if (InSize < OutSize) {
14703351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner            SmallerValueMentioned = true;
14713351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner            break;
14723351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner          }
14733351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        }
14743351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner
14753351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        // If this is a reference to the input and if the input was the smaller
14763351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        // one, then we have to reject this asm.
14773351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        if (Piece.getOperandNo() == TiedTo) {
14783351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner          if (InSize > OutSize) {
14793351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner            SmallerValueMentioned = true;
14803351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner            break;
14813351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner          }
14823351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        }
1483f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner      }
14841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14853351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // If the smaller value wasn't mentioned in the asm string, and if the
14863351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // output was a register, just extend the shorter one to the size of the
14873351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // larger one.
14883351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      if (!SmallerValueMentioned &&
1489f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner          OutputConstraintInfos[TiedTo].allowsRegister())
1490f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner        continue;
1491806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    }
14921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1493c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner    Diag(InputExpr->getLocStart(),
1494806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner         diag::err_asm_tying_incompatible_types)
14957adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      << InTy << OutTy << OutputExpr->getSourceRange()
1496806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner      << InputExpr->getSourceRange();
1497806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    DeleteStmt(NS);
1498806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    return StmtError();
1499806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  }
15001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1501fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  return Owned(NS);
1502fe795956194141c91ae555985c9b930595bff43fChris Lattner}
15033b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian
1504431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
1505431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
1506b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                           SourceLocation RParen, DeclPtrTy Parm,
1507431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl                           StmtArg Body, StmtArg catchList) {
1508f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Stmt *CatchList = catchList.takeAs<Stmt>();
1509b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  ParmVarDecl *PVD = cast_or_null<ParmVarDecl>(Parm.getAs<Decl>());
15101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1511f50cb369273c6bd26c9629df92ee53f1d8af4149Steve Naroff  // PVD == 0 implies @catch(...).
15129d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff  if (PVD) {
151393c4945c9ead2d374fe3fc528e3017c7167265beChris Lattner    // If we already know the decl is invalid, reject it.
151493c4945c9ead2d374fe3fc528e3017c7167265beChris Lattner    if (PVD->isInvalidDecl())
151593c4945c9ead2d374fe3fc528e3017c7167265beChris Lattner      return StmtError();
15161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1517f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    if (!PVD->getType()->isObjCObjectPointerType())
15181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return StmtError(Diag(PVD->getLocation(),
15199d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff                       diag::err_catch_param_not_objc_type));
15209d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff    if (PVD->getType()->isObjCQualifiedIdType())
15211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return StmtError(Diag(PVD->getLocation(),
1522d198abae5938262e501a409e833bf01ca8b8253eSteve Naroff                       diag::err_illegal_qualifiers_on_catch_parm));
15239d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff  }
152493c4945c9ead2d374fe3fc528e3017c7167265beChris Lattner
15258189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  ObjCAtCatchStmt *CS = new (Context) ObjCAtCatchStmt(AtLoc, RParen,
1526e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson    PVD, Body.takeAs<Stmt>(), CatchList);
1527431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl  return Owned(CatchList ? CatchList : CS);
15283b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian}
15293b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian
1530431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
1531431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) {
15328189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ObjCAtFinallyStmt(AtLoc,
15338189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                                           static_cast<Stmt*>(Body.release())));
1534161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian}
1535bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
1536431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
1537431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
1538431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl                         StmtArg Try, StmtArg Catch, StmtArg Finally) {
15399ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  FunctionNeedsScopeChecking() = true;
1540e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson  return Owned(new (Context) ObjCAtTryStmt(AtLoc, Try.takeAs<Stmt>(),
1541e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                           Catch.takeAs<Stmt>(),
1542e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                           Finally.takeAs<Stmt>()));
1543bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian}
1544bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
1545d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas GregorSema::OwningStmtResult Sema::BuildObjCAtThrowStmt(SourceLocation AtLoc,
1546d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor                                                  ExprArg ThrowE) {
1547d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  Expr *Throw = static_cast<Expr *>(ThrowE.get());
1548d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  if (Throw) {
1549d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    QualType ThrowType = Throw->getType();
1550d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    // Make sure the expression type is an ObjC pointer or "void *".
1551d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    if (!ThrowType->isDependentType() &&
1552d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor        !ThrowType->isObjCObjectPointerType()) {
1553d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor      const PointerType *PT = ThrowType->getAs<PointerType>();
1554d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor      if (!PT || !PT->getPointeeType()->isVoidType())
1555d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor        return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object)
1556d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor                         << Throw->getType() << Throw->getSourceRange());
1557d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    }
1558d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  }
1559d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor
1560d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  return Owned(new (Context) ObjCAtThrowStmt(AtLoc, ThrowE.takeAs<Expr>()));
1561d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor}
1562d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor
1563431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
1564d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas GregorSema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg Throw,
1565d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor                           Scope *CurScope) {
1566d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  if (!Throw.get()) {
1567e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    // @throw without an expression designates a rethrow (which much occur
1568e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    // in the context of an @catch clause).
1569e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    Scope *AtCatchParent = CurScope;
1570e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    while (AtCatchParent && !AtCatchParent->isAtCatchScope())
1571e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff      AtCatchParent = AtCatchParent->getParent();
1572e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    if (!AtCatchParent)
15734ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff      return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch));
1574d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  }
1575d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor
1576d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  return BuildObjCAtThrowStmt(AtLoc, move(Throw));
157739f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian}
1578bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
1579431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
1580431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr,
1581431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl                                  StmtArg SynchBody) {
15829ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  FunctionNeedsScopeChecking() = true;
158346c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner
1584a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  // Make sure the expression type is an ObjC pointer or "void *".
1585a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  Expr *SyncExpr = static_cast<Expr*>(SynchExpr.get());
1586f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff  if (!SyncExpr->getType()->isObjCObjectPointerType()) {
15876217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    const PointerType *PT = SyncExpr->getType()->getAs<PointerType>();
1588a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner    if (!PT || !PT->getPointeeType()->isVoidType())
1589a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner      return StmtError(Diag(AtLoc, diag::error_objc_synchronized_expects_object)
1590a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner                       << SyncExpr->getType() << SyncExpr->getSourceRange());
1591a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  }
15921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc,
1594e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                                    SynchExpr.takeAs<Stmt>(),
1595e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                                    SynchBody.takeAs<Stmt>()));
1596fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian}
15974b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl
15984b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
15994b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// and creates a proper catch handler from them.
16004b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian RedlAction::OwningStmtResult
1601b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerSema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclPtrTy ExDecl,
16024b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl                         StmtArg HandlerBlock) {
16034b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl  // There's nothing to test that ActOnExceptionDecl didn't already test.
16048189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CXXCatchStmt(CatchLoc,
1605b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                  cast_or_null<VarDecl>(ExDecl.getAs<Decl>()),
1606e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                          HandlerBlock.takeAs<Stmt>()));
16074b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl}
16088351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
1609c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlclass TypeWithHandler {
1610c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  QualType t;
1611c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  CXXCatchStmt *stmt;
1612c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlpublic:
1613c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  TypeWithHandler(const QualType &type, CXXCatchStmt *statement)
1614c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  : t(type), stmt(statement) {}
1615c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
16160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // An arbitrary order is fine as long as it places identical
16170953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // types next to each other.
1618c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  bool operator<(const TypeWithHandler &y) const {
16190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (t.getAsOpaquePtr() < y.t.getAsOpaquePtr())
1620c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return true;
16210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (t.getAsOpaquePtr() > y.t.getAsOpaquePtr())
1622c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return false;
1623c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    else
1624c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return getTypeSpecStartLoc() < y.getTypeSpecStartLoc();
1625c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
16261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1627c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  bool operator==(const TypeWithHandler& other) const {
16280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return t == other.t;
1629c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
16301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1631c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  QualType getQualType() const { return t; }
1632c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  CXXCatchStmt *getCatchStmt() const { return stmt; }
1633c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  SourceLocation getTypeSpecStartLoc() const {
1634c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    return stmt->getExceptionDecl()->getTypeSpecStartLoc();
1635c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1636c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl};
1637c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
16388351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
16398351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// handlers and creates a try statement from them.
16408351da06ca3082dfd49dd8e3c1785a986920f57cSebastian RedlAction::OwningStmtResult
16418351da06ca3082dfd49dd8e3c1785a986920f57cSebastian RedlSema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock,
16428351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl                       MultiStmtArg RawHandlers) {
16438351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  unsigned NumHandlers = RawHandlers.size();
16448351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  assert(NumHandlers > 0 &&
16458351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl         "The parser shouldn't call this if there are no handlers.");
16468351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get());
16478351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
1648c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  llvm::SmallVector<TypeWithHandler, 8> TypesWithHandlers;
16491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (unsigned i = 0; i < NumHandlers; ++i) {
16518351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl    CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
1652c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    if (!Handler->getExceptionDecl()) {
1653c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      if (i < NumHandlers - 1)
1654c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        return StmtError(Diag(Handler->getLocStart(),
1655c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl                              diag::err_early_catch_all));
16561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1657c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      continue;
1658c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    }
16591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1660c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    const QualType CaughtType = Handler->getCaughtType();
1661c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    const QualType CanonicalCaughtType = Context.getCanonicalType(CaughtType);
1662c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    TypesWithHandlers.push_back(TypeWithHandler(CanonicalCaughtType, Handler));
1663c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1664c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1665c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  // Detect handlers for the same type as an earlier one.
1666c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  if (NumHandlers > 1) {
1667c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    llvm::array_pod_sort(TypesWithHandlers.begin(), TypesWithHandlers.end());
16681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1669c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    TypeWithHandler prev = TypesWithHandlers[0];
1670c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    for (unsigned i = 1; i < TypesWithHandlers.size(); ++i) {
1671c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      TypeWithHandler curr = TypesWithHandlers[i];
16721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1673c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      if (curr == prev) {
1674c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        Diag(curr.getTypeSpecStartLoc(),
1675c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl             diag::warn_exception_caught_by_earlier_handler)
1676c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl          << curr.getCatchStmt()->getCaughtType().getAsString();
1677c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        Diag(prev.getTypeSpecStartLoc(),
1678c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl             diag::note_previous_exception_handler)
1679c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl          << prev.getCatchStmt()->getCaughtType().getAsString();
1680c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      }
16811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1682c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      prev = curr;
1683c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    }
16848351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  }
16851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16868351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // FIXME: We should detect handlers that cannot catch anything because an
16878351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // earlier handler catches a superclass. Need to find a method that is not
16888351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // quadratic for this.
16898351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // Neither of these are explicitly forbidden, but every compiler detects them
16908351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // and warns.
16918351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
16929ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  FunctionNeedsScopeChecking() = true;
16938351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  RawHandlers.release();
1694a1a396df16c02b22983b5c9592022fd9237d4866Sam Weinig  return Owned(CXXTryStmt::Create(Context, TryLoc,
1695a1a396df16c02b22983b5c9592022fd9237d4866Sam Weinig                                  static_cast<Stmt*>(TryBlock.release()),
1696a1a396df16c02b22983b5c9592022fd9237d4866Sam Weinig                                  Handlers, NumHandlers));
16978351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl}
1698