SemaStmt.cpp revision 57ad37823e198f977cac605dbfbaefb4daf325e9
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements semantic analysis for statements.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
142d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
155f1e0942a32657b625702aa52f82430d0120f424John McCall#include "clang/Sema/Scope.h"
16781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "clang/Sema/ScopeInfo.h"
17e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Initialization.h"
1851fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson#include "clang/AST/APValue.h"
19f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner#include "clang/AST/ASTContext.h"
20c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
2184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/AST/ExprCXX.h"
22419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner#include "clang/AST/ExprObjC.h"
2316f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h"
2416f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtCXX.h"
25209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall#include "clang/AST/TypeLoc.h"
2684fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/Lex/Preprocessor.h"
276fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson#include "clang/Basic/TargetInfo.h"
28c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/STLExtras.h"
29c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/SmallVector.h"
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
31781472fe99a120098c631b0cbe33c89f8cef5e70John McCallusing namespace sema;
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnExprStmt(FullExprArg expr) {
349ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Expr *E = expr.get();
35bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  if (!E) // FIXME: FullExprArg has no error state?
36bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    return StmtError();
37bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
38834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // C99 6.8.3p2: The expression in an expression statement is evaluated as a
39834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // void expression for its side effects.  Conversion to void allows any
40834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // operand, even incomplete types.
41a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl
42834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // Same thing in for stmt first clause (when expr) and third clause.
43a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  return Owned(static_cast<Stmt*>(E));
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4744aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios KyrtzidisStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc, bool LeadingEmptyMacro) {
4844aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis  return Owned(new (Context) NullStmt(SemiLoc, LeadingEmptyMacro));
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg,
52a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                           SourceLocation StartLoc,
53a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                           SourceLocation EndLoc) {
5420401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner  DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // If we have an invalid decl, just return an error.
5720401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner  if (DG.isNull()) return StmtError();
581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5924e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
62a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanianvoid Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) {
63a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
64dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
65a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  // If we have an invalid decl, just return.
66a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  if (DG.isNull() || !DG.isSingleDecl()) return;
67a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  // suppress any potential 'unused variable' warning.
68a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  DG.getSingleDecl()->setUsed();
69a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian}
70a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian
71636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlssonvoid Sema::DiagnoseUnusedExprResult(const Stmt *S) {
72d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis  if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S))
73d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis    return DiagnoseUnusedExprResult(Label->getSubStmt());
74d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis
75754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  const Expr *E = dyn_cast_or_null<Expr>(S);
76636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  if (!E)
77636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
78636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
7911ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis  if (E->isBoundMemberFunction(Context)) {
8011ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis    Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func)
8111ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis      << E->getSourceRange();
8211ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis    return;
8311ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis  }
8411ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis
85636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  SourceLocation Loc;
86636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  SourceRange R1, R2;
87df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump  if (!E->isUnusedResultAWarning(Loc, R1, R2, Context))
88636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // Okay, we have an unused result.  Depending on what the base expression is,
91419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // we might want to make a more specific diagnostic.  Check for one of these
92419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // cases now.
93419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  unsigned DiagID = diag::warn_unused_expr;
944765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (const ExprWithCleanups *Temps = dyn_cast<ExprWithCleanups>(E))
954dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor    E = Temps->getSubExpr();
9612f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall
97f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  E = E->IgnoreParenImpCasts();
98bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner  if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
990faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall    if (E->getType()->isVoidType())
1000faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall      return;
1010faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall
102bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    // If the callee has attribute pure, const, or warn_unused_result, warn with
103bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    // a more specific message to make it clear what is happening.
104d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes    if (const Decl *FD = CE->getCalleeDecl()) {
105bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<WarnUnusedResultAttr>()) {
106bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result";
107bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
108bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
109bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<PureAttr>()) {
110bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure";
111bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
112bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
113bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<ConstAttr>()) {
114bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const";
115bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
116bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
117dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    }
11812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  } else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) {
119f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    const ObjCMethodDecl *MD = ME->getMethodDecl();
120f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
121f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result";
122f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      return;
123f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    }
12412f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall  } else if (isa<ObjCPropertyRefExpr>(E)) {
12512f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    DiagID = diag::warn_unused_property_expr;
126d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor  } else if (const CXXFunctionalCastExpr *FC
127d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor                                       = dyn_cast<CXXFunctionalCastExpr>(E)) {
128d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor    if (isa<CXXConstructExpr>(FC->getSubExpr()) ||
129d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor        isa<CXXTemporaryObjectExpr>(FC->getSubExpr()))
130d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor      return;
131f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  }
132209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall  // Diagnose "(void*) blah" as a typo for "(void) blah".
133209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall  else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) {
134209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    TypeSourceInfo *TI = CE->getTypeInfoAsWritten();
135209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    QualType T = TI->getType();
136209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall
137209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    // We really do want to use the non-canonical type here.
138209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    if (T == Context.VoidPtrTy) {
139209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall      PointerTypeLoc TL = cast<PointerTypeLoc>(TI->getTypeLoc());
140209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall
141209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall      Diag(Loc, diag::warn_unused_voidptr)
142209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall        << FixItHint::CreateRemoval(TL.getStarLoc());
143209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall      return;
144209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall    }
145209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall  }
146209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall
14735e12c90c1b107a75c5615aa76fdbd403661aaa6Douglas Gregor  DiagRuntimeBehavior(Loc, PDiag(DiagID) << R1 << R2);
148636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson}
149636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
15060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1511b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
152a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                        MultiStmtArg elts, bool isStmtExpr) {
153a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  unsigned NumElts = elts.size();
154a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
155c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  // If we're in C89 mode, check that we don't have any decls after stmts.  If
156c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  // so, emit an extension diagnostic.
157c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
158c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // Note that __extension__ can be around a decl.
159c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    unsigned i = 0;
160c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // Skip over all declarations.
161c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
162c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      /*empty*/;
163c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner
164c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // We found the end of the list or a statement.  Scan for another declstmt.
165c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
166c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      /*empty*/;
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
168c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    if (i != NumElts) {
1694afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
170c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      Diag(D->getLocation(), diag::ext_mixed_decls_code);
171c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    }
172c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  }
17398414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  // Warn about unused expressions in statements.
17498414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  for (unsigned i = 0; i != NumElts; ++i) {
175636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    // Ignore statements that are last in a statement expression.
176636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    if (isStmtExpr && i == NumElts - 1)
17798414c1b7d1944a57156d52e29bd41c005de09acChris Lattner      continue;
1781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
179636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    DiagnoseUnusedExprResult(Elts[i]);
18098414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  }
181a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl
1828189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R));
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1869ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCaseStmt(SourceLocation CaseLoc, Expr *LHSVal,
1879ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                    SourceLocation DotDotDotLoc, Expr *RHSVal,
18824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner                    SourceLocation ColonLoc) {
1899ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  assert((LHSVal != 0) && "missing expression in case statement");
190117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.4.2p3: The expression shall be an integer constant.
1921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // However, GCC allows any evaluatable integer expression.
1931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent() &&
194dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      VerifyIntegerConstantExpression(LHSVal))
19524e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner    return StmtError();
1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1976c36be5b383875b490684bcf439d6d427298c1afChris Lattner  // GCC extension: The expression shall be an integer constant.
198117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
199dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent() &&
200dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      VerifyIntegerConstantExpression(RHSVal)) {
201f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    RHSVal = 0;  // Recover by just forgetting about it.
202117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  }
203117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
204781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (getCurFunction()->SwitchStack.empty()) {
2058a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner    Diag(CaseLoc, diag::err_case_not_in_switch);
20624e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner    return StmtError();
2078a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner  }
2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
209dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc,
210dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                        ColonLoc);
211781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->SwitchStack.back()->addSwitchCase(CS);
212117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  return Owned(CS);
2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21524e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner/// ActOnCaseStmtBody - This installs a statement as the body of a case.
2169ae2f076ca5ab1feb3ba95629099ec2319833701John McCallvoid Sema::ActOnCaseStmtBody(Stmt *caseStmt, Stmt *SubStmt) {
21724e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  CaseStmt *CS = static_cast<CaseStmt*>(caseStmt);
21824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  CS->setSubStmt(SubStmt);
21924e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner}
22024e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner
22160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
2221eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
2239ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                       Stmt *SubStmt, Scope *CurScope) {
224781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  if (getCurFunction()->SwitchStack.empty()) {
2250fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner    Diag(DefaultLoc, diag::err_default_not_in_switch);
226117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl    return Owned(SubStmt);
2270fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner  }
228117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
229dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt);
230781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->SwitchStack.back()->addSwitchCase(DS);
231117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  return Owned(DS);
2325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
2351b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
2361a18600b85aaa691122983dd8dcf4225cfc9ef68Argyrios Kyrtzidis                     SourceLocation ColonLoc, Stmt *SubStmt,
23757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner                     AttributeList *Attr) {
2385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Look up the record for this label identifier.
239ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  LabelDecl *&TheDecl = getCurFunction()->LabelMap[II];
240de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
241ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  // If not forward referenced or defined already, create the backing decl.
242ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  if (TheDecl == 0)
243ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    TheDecl = LabelDecl::Create(Context, CurContext, IdentLoc, II);
244de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
245ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  assert(TheDecl->getIdentifier() == II && "Label mismatch!");
246de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
24757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  if (Attr)
24857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    ProcessDeclAttributeList(CurScope, TheDecl, Attr);
24957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
25057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  return ActOnLabelStmt(IdentLoc, TheDecl, ColonLoc, SubStmt);
25157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner}
25257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
25357ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerStmtResult
25457ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerSema::ActOnLabelStmt(SourceLocation IdentLoc, LabelDecl *TheDecl,
25557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner                     SourceLocation ColonLoc, Stmt *SubStmt) {
25657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
257ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  // If the label was multiply defined, reject it now.
258ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  if (TheDecl->getStmt()) {
259ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    Diag(IdentLoc, diag::err_redefinition_of_label) << TheDecl->getDeclName();
260ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    Diag(TheDecl->getLocation(), diag::note_previous_definition);
261de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl    return Owned(SubStmt);
2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
263de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
264ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  // Otherwise, things are good.  Fill in the declaration and return it.
265ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  TheDecl->setLocation(IdentLoc);
266ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
267ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  LabelStmt *LS = new (Context) LabelStmt(IdentLoc, TheDecl, SubStmt);
268ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  TheDecl->setStmt(LS);
269ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  TheDecl->setLocation(IdentLoc);
270ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  return Owned(LS);
2715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
27360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
274d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, Decl *CondVar,
27544aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis                  Stmt *thenStmt, SourceLocation ElseLoc,
27644aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis                  Stmt *elseStmt) {
27760d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult CondResult(CondVal.release());
2781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2798cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  VarDecl *ConditionVar = 0;
280d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (CondVar) {
281d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    ConditionVar = cast<VarDecl>(CondVar);
282586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    CondResult = CheckConditionVariable(ConditionVar, IfLoc, true);
28399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (CondResult.isInvalid())
28499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return StmtError();
2858cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  }
28699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  Expr *ConditionExpr = CondResult.takeAs<Expr>();
28799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!ConditionExpr)
28899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    return StmtError();
289dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
290754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(thenStmt);
2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2922d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // Warn if the if block has a null body without an else value.
2932d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // this helps prevent bugs due to typos, such as
2942d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // if (condition);
2952d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  //   do_stuff();
296b3198178daf9d33466fc18e009e2c6692fdf29c0Ted Kremenek  //
2979ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!elseStmt) {
2982d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson    if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
299a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis      // But do not warn if the body is a macro that expands to nothing, e.g:
300a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis      //
301a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis      // #define CALL(x)
302a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis      // if (condition)
303a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis      //   CALL(0);
304a25b6a4b43e8b9611f7506e5fe1b448833b10a46Argyrios Kyrtzidis      //
30544aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis      if (!stmt->hasLeadingEmptyMacro())
306b3198178daf9d33466fc18e009e2c6692fdf29c0Ted Kremenek        Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
3072d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  }
3082d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson
309754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(elseStmt);
3101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
311dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  return Owned(new (Context) IfStmt(Context, IfLoc, ConditionVar, ConditionExpr,
31244aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis                                    thenStmt, ElseLoc, elseStmt));
3135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
315f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
316f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified width and sign.  If an overflow occurs, detect it and emit
317f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified diagnostic.
318f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattnervoid Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
319f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                              unsigned NewWidth, bool NewSign,
3201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              SourceLocation Loc,
321f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                              unsigned DiagID) {
322f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Perform a conversion to the promoted condition type if needed.
323f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  if (NewWidth > Val.getBitWidth()) {
324f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If this is an extension, just do it.
3259f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    Val = Val.extend(NewWidth);
326f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.setIsSigned(NewSign);
327f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor
328f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // If the input was signed and negative and the output is
329f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // unsigned, don't bother to warn: this is implementation-defined
330f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // behavior.
331f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor    // FIXME: Introduce a second, default-ignored warning for this case?
332f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  } else if (NewWidth < Val.getBitWidth()) {
333f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If this is a truncation, check for overflow.
334f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt ConvVal(Val);
3359f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    ConvVal = ConvVal.trunc(NewWidth);
336b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    ConvVal.setIsSigned(NewSign);
3379f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    ConvVal = ConvVal.extend(Val.getBitWidth());
338b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    ConvVal.setIsSigned(Val.isSigned());
339f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    if (ConvVal != Val)
340d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
3411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
342f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // Regardless of whether a diagnostic was emitted, really do the
343f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // truncation.
3449f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    Val = Val.trunc(NewWidth);
345b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    Val.setIsSigned(NewSign);
346f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  } else if (NewSign != Val.isSigned()) {
347f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // Convert the sign to match the sign of the condition.  This can cause
348f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // overflow as well: unsigned(INTMIN)
349dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // We don't diagnose this overflow, because it is implementation-defined
3502853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor    // behavior.
3512853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor    // FIXME: Introduce a second, default-ignored warning for this case?
352f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt OldVal(Val);
353f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.setIsSigned(NewSign);
354f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  }
355f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner}
356f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
3570471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattnernamespace {
3580471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  struct CaseCompareFunctor {
3590471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
3600471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner                    const llvm::APSInt &RHS) {
3610471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      return LHS.first < RHS;
3620471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    }
3630e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner    bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
3640e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner                    const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
3650e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner      return LHS.first < RHS.first;
3660e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner    }
3670471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    bool operator()(const llvm::APSInt &LHS,
3680471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner                    const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
3690471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      return LHS < RHS.first;
3700471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    }
3710471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  };
3720471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner}
3730471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner
374764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// CmpCaseVals - Comparison predicate for sorting case values.
375764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner///
376764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattnerstatic bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
377764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner                        const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
378764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  if (lhs.first < rhs.first)
379764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner    return true;
380764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner
381764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  if (lhs.first == rhs.first &&
382764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner      lhs.second->getCaseLoc().getRawEncoding()
383764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner       < rhs.second->getCaseLoc().getRawEncoding())
384764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner    return true;
385764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  return false;
386764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner}
387764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner
388ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// CmpEnumVals - Comparison predicate for sorting enumeration values.
389ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor///
390ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
391ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor                        const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
392ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{
393ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  return lhs.first < rhs.first;
394ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor}
395ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
396ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// EqEnumVals - Comparison preficate for uniqing enumeration values.
397ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor///
398ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
399ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor                       const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
400ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{
401ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  return lhs.first == rhs.first;
402ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor}
403ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
4045f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of
4055f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// potentially integral-promoted expression @p expr.
4065f04881eb025f61396d0555d8173730fe2759e0aChris Lattnerstatic QualType GetTypeBeforeIntegralPromotion(const Expr* expr) {
4076907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall  if (const CastExpr *ImplicitCast = dyn_cast<ImplicitCastExpr>(expr)) {
4085f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    const Expr *ExprBeforePromotion = ImplicitCast->getSubExpr();
4095f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    QualType TypeBeforePromotion = ExprBeforePromotion->getType();
4102ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    if (TypeBeforePromotion->isIntegralOrEnumerationType()) {
4115f04881eb025f61396d0555d8173730fe2759e0aChris Lattner      return TypeBeforePromotion;
4125f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    }
4135f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  }
4145f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  return expr->getType();
4155f04881eb025f61396d0555d8173730fe2759e0aChris Lattner}
4165f04881eb025f61396d0555d8173730fe2759e0aChris Lattner
41760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
418dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnStartOfSwitchStmt(SourceLocation SwitchLoc, Expr *Cond,
419d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                             Decl *CondVar) {
42060d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult CondResult;
4219ae2f076ca5ab1feb3ba95629099ec2319833701John McCall
422586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  VarDecl *ConditionVar = 0;
423d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (CondVar) {
424d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    ConditionVar = cast<VarDecl>(CondVar);
4259ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    CondResult = CheckConditionVariable(ConditionVar, SourceLocation(), false);
4269ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (CondResult.isInvalid())
427586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      return StmtError();
428dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4299ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    Cond = CondResult.release();
430586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  }
431dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4329ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!Cond)
433586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    return StmtError();
434dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4359ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  CondResult
436dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    = ConvertToIntegralOrEnumerationType(SwitchLoc, Cond,
437c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                          PDiag(diag::err_typecheck_statement_requires_integer),
438c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                                   PDiag(diag::err_switch_incomplete_class_type)
4399ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     << Cond->getSourceRange(),
440c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                                   PDiag(diag::err_switch_explicit_conversion),
441c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                                         PDiag(diag::note_switch_conversion),
442c30614b7e2bad089f2509499379de509f33162d6Douglas Gregor                                   PDiag(diag::err_switch_multiple_conversions),
4436bc574daab3d3571d888cc4a21df67f2e2a14792Douglas Gregor                                         PDiag(diag::note_switch_conversion),
4446bc574daab3d3571d888cc4a21df67f2e2a14792Douglas Gregor                                         PDiag(0));
4459ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (CondResult.isInvalid()) return StmtError();
4469ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Cond = CondResult.take();
447dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
448d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (!CondVar) {
449b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall    CheckImplicitConversions(Cond, SwitchLoc);
4504765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall    CondResult = MaybeCreateExprWithCleanups(Cond);
4519ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    if (CondResult.isInvalid())
452586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor      return StmtError();
4539ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    Cond = CondResult.take();
454586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  }
455b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
456781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->setHasBranchIntoScope();
457dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4589ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  SwitchStmt *SS = new (Context) SwitchStmt(Context, ConditionVar, Cond);
459781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->SwitchStack.push_back(SS);
460586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor  return Owned(SS);
4617e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner}
4627e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner
46328164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greifstatic void AdjustAPSInt(llvm::APSInt &Val, unsigned BitWidth, bool IsSigned) {
46428164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif  if (Val.getBitWidth() < BitWidth)
4659f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    Val = Val.extend(BitWidth);
46628164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif  else if (Val.getBitWidth() > BitWidth)
4679f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    Val = Val.trunc(BitWidth);
46828164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif  Val.setIsSigned(IsSigned);
46928164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif}
47028164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif
47160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
4729ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
4739ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                            Stmt *BodyStmt) {
4749ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  SwitchStmt *SS = cast<SwitchStmt>(Switch);
475781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  assert(SS == getCurFunction()->SwitchStack.back() &&
476781472fe99a120098c631b0cbe33c89f8cef5e70John McCall         "switch stack missing push/pop!");
477de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
4789dcbfa450d751bd68fc4af8b75da381d4f6984b9Steve Naroff  SS->setBody(BodyStmt, SwitchLoc);
479781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->SwitchStack.pop_back();
480c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson
481ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (SS->getCond() == 0)
482be724bab2ba7ad47aebced25e7c8ec551eb72d28Douglas Gregor    return StmtError();
483dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
484f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  Expr *CondExpr = SS->getCond();
4850fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall  Expr *CondExprBeforePromotion = CondExpr;
48684fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  QualType CondTypeBeforePromotion =
48784fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor      GetTypeBeforeIntegralPromotion(CondExpr);
488de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
4890de55e7e6b8a53c5d1f2e9a811fd0a4ea13ed5b0Douglas Gregor  // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
4900de55e7e6b8a53c5d1f2e9a811fd0a4ea13ed5b0Douglas Gregor  UsualUnaryConversions(CondExpr);
491a0d3ca1ea5578bc736bb71bcec50ab41fefc87b9Douglas Gregor  QualType CondType = CondExpr->getType();
49284fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  SS->setCond(CondExpr);
49384fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor
4945f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // C++ 6.4.2.p2:
4955f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // Integral promotions are performed (on the switch condition).
4965f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  //
4975f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // A case value unrepresentable by the original switch condition
4985f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // type (before the promotion) doesn't make sense, even when it can
4995f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // be represented by the promoted type.  Therefore we need to find
5005f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  // the pre-promotion type of the switch condition.
50112356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan  if (!CondExpr->isTypeDependent()) {
502acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor    // We have already converted the expression to an integral or enumeration
503dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // type, when we started the switch statement. If we don't have an
504acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor    // appropriate type now, just return an error.
505acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor    if (!CondType->isIntegralOrEnumerationType())
50612356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      return StmtError();
50712356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan
5082b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    if (CondExpr->isKnownToHaveBooleanValue()) {
50912356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      // switch(bool_expr) {...} is often a programmer error, e.g.
51012356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      //   switch(n && mask) { ... }  // Doh - should be "n & mask".
51112356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      // One can always use an if statement instead of switch(bool_expr).
51212356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      Diag(SwitchLoc, diag::warn_bool_switch_condition)
51312356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan          << CondExpr->getSourceRange();
51412356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan    }
515c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson  }
516de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
517f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Get the bitwidth of the switched-on value before promotions.  We must
518f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // convert the integer case values to this width before comparison.
5191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  bool HasDependentValue
520dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    = CondExpr->isTypeDependent() || CondExpr->isValueDependent();
5211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned CondWidth
522dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    = HasDependentValue? 0
5235f04881eb025f61396d0555d8173730fe2759e0aChris Lattner      : static_cast<unsigned>(Context.getTypeSize(CondTypeBeforePromotion));
5245f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  bool CondIsSigned = CondTypeBeforePromotion->isSignedIntegerType();
5251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
526f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Accumulate all of the case values in a vector so that we can sort them
527f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // and detect duplicates.  This vector contains the APInt for the case after
528f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // it has been converted to the condition type.
5290471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
5300471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  CaseValsTy CaseVals;
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
532f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Keep track of any GNU case ranges we see.  The APSInt is the low value.
533ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy;
534ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor  CaseRangesTy CaseRanges;
5351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
536f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  DefaultStmt *TheDefaultStmt = 0;
5371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
538b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  bool CaseListIsErroneous = false;
5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
540dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue;
541c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson       SC = SC->getNextSwitchCase()) {
5421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
543c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson    if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
544f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      if (TheDefaultStmt) {
545f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
5465f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner        Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
547de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
548f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        // FIXME: Remove the default statement from the switch block so that
549390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // we'll return a valid AST.  This requires recursing down the AST and
550390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // finding it, not something we are set up to do right now.  For now,
551390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // just lop the entire switch stmt out of the AST.
552b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner        CaseListIsErroneous = true;
553c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson      }
554f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      TheDefaultStmt = DS;
5551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
556f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    } else {
557f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      CaseStmt *CS = cast<CaseStmt>(SC);
5581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
559f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // We already verified that the expression has a i-c-e value (C99
560f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // 6.8.4.2p3) - get that value now.
5611e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      Expr *Lo = CS->getLHS();
562dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
563dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      if (Lo->isTypeDependent() || Lo->isValueDependent()) {
564dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        HasDependentValue = true;
565dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        break;
566dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      }
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56851fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson      llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
570f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // Convert the value to the same width/sign as the condition.
571f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
57228164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif                                         Lo->getLocStart(),
573f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                         diag::warn_case_value_overflow);
5746c36be5b383875b490684bcf439d6d427298c1afChris Lattner
5751e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      // If the LHS is not the same type as the condition, insert an implicit
5761e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      // cast.
5772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      ImpCastExprToType(Lo, CondType, CK_IntegralCast);
5781e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      CS->setLHS(Lo);
5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
580b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner      // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
581dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      if (CS->getRHS()) {
5821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (CS->getRHS()->isTypeDependent() ||
583dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            CS->getRHS()->isValueDependent()) {
584dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          HasDependentValue = true;
585dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          break;
586dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
587f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        CaseRanges.push_back(std::make_pair(LoVal, CS));
5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else
589b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner        CaseVals.push_back(std::make_pair(LoVal, CS));
590f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    }
591f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  }
592b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner
593dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (!HasDependentValue) {
5940fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // If we don't have a default statement, check whether the
5950fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // condition is constant.
5960fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    llvm::APSInt ConstantCondValue;
5970fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    bool HasConstantCond = false;
5980fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    bool ShouldCheckConstantCond = false;
5990fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    if (!HasDependentValue && !TheDefaultStmt) {
6000fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      Expr::EvalResult Result;
6010fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      HasConstantCond = CondExprBeforePromotion->Evaluate(Result, Context);
6020fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      if (HasConstantCond) {
6030fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        assert(Result.Val.isInt() && "switch condition evaluated to non-int");
6040fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        ConstantCondValue = Result.Val.getInt();
6050fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        ShouldCheckConstantCond = true;
6060fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
6070fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        assert(ConstantCondValue.getBitWidth() == CondWidth &&
6080fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall               ConstantCondValue.isSigned() == CondIsSigned);
6090fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      }
6100fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    }
6110fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
612dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // Sort all the scalar case values so we can easily detect duplicates.
613dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
614dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
615dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    if (!CaseVals.empty()) {
6160fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) {
6170fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        if (ShouldCheckConstantCond &&
6180fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            CaseVals[i].first == ConstantCondValue)
6190fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          ShouldCheckConstantCond = false;
6200fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
6210fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) {
622dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          // If we have a duplicate, report it.
6231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          Diag(CaseVals[i].second->getLHS()->getLocStart(),
6240fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall               diag::err_duplicate_case) << CaseVals[i].first.toString(10);
6250fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          Diag(CaseVals[i-1].second->getLHS()->getLocStart(),
626dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::note_duplicate_case_prev);
627390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // FIXME: We really want to remove the bogus case stmt from the
628390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // substmt, but we have no way to do this right now.
629dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseListIsErroneous = true;
630dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6316efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner      }
632b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner    }
6331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
634dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // Detect duplicate case ranges, which usually don't exist at all in
635dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // the first place.
636dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    if (!CaseRanges.empty()) {
637dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Sort all the case ranges by their low value so we can easily detect
638dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // overlaps between ranges.
639dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      std::stable_sort(CaseRanges.begin(), CaseRanges.end());
6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
641dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Scan the ranges, computing the high values and removing empty ranges.
642dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      std::vector<llvm::APSInt> HiVals;
643dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
6440fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        llvm::APSInt &LoVal = CaseRanges[i].first;
645dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *CR = CaseRanges[i].second;
646dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        Expr *Hi = CR->getRHS();
647dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
6481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
649dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Convert the value to the same width/sign as the condition.
650dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
65128164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif                                           Hi->getLocStart(),
652dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                           diag::warn_case_value_overflow);
6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
654dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // If the LHS is not the same type as the condition, insert an implicit
655dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // cast.
6562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        ImpCastExprToType(Hi, CondType, CK_IntegralCast);
657dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CR->setRHS(Hi);
6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
659dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // If the low value is bigger than the high value, the case is empty.
6600fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        if (LoVal > HiVal) {
661dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
662dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            << SourceRange(CR->getLHS()->getLocStart(),
66328164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif                           Hi->getLocEnd());
664dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseRanges.erase(CaseRanges.begin()+i);
665dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          --i, --e;
666dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          continue;
667dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6680fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
6690fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        if (ShouldCheckConstantCond &&
6700fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            LoVal <= ConstantCondValue &&
6710fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            ConstantCondValue <= HiVal)
6720fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall          ShouldCheckConstantCond = false;
6730fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
674dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        HiVals.push_back(HiVal);
6750471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      }
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
677dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Rescan the ranges, looking for overlap with singleton values and other
678dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // ranges.  Since the range list is sorted, we only need to compare case
679dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // ranges with their neighbors.
680dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
681dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt &CRLo = CaseRanges[i].first;
682dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt &CRHi = HiVals[i];
683dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *CR = CaseRanges[i].second;
6841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
685dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Check to see whether the case range overlaps with any
686dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // singleton cases.
687dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *OverlapStmt = 0;
688dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt OverlapVal(32);
6891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
690dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Find the smallest value >= the lower bound.  If I is in the
691dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // case range, then we have overlap.
692dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
693dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                                  CaseVals.end(), CRLo,
694dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                                  CaseCompareFunctor());
695dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (I != CaseVals.end() && I->first < CRHi) {
696dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = I->first;   // Found overlap with scalar.
697dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = I->second;
698dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
700dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Find the smallest value bigger than the upper bound.
701dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
702dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
703dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = (I-1)->first;      // Found overlap with scalar.
704dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = (I-1)->second;
705dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
707dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Check to see if this case stmt overlaps with the subsequent
708dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // case range.
709dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (i && CRLo <= HiVals[i-1]) {
710dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = HiVals[i-1];       // Found overlap with range.
711dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = CaseRanges[i-1].second;
712dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
714dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (OverlapStmt) {
715dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          // If we have a duplicate, report it.
716dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
717dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            << OverlapVal.toString(10);
7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          Diag(OverlapStmt->getLHS()->getLocStart(),
719dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::note_duplicate_case_prev);
720390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // FIXME: We really want to remove the bogus case stmt from the
721390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // substmt, but we have no way to do this right now.
722dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseListIsErroneous = true;
723dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
7240471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      }
725b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner    }
726ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
7270fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // Complain if we have a constant condition and we didn't find a match.
7280fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    if (!CaseListIsErroneous && ShouldCheckConstantCond) {
7290fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      // TODO: it would be nice if we printed enums as enums, chars as
7300fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      // chars, etc.
7310fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      Diag(CondExpr->getExprLoc(), diag::warn_missing_case_for_condition)
7320fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        << ConstantCondValue.toString(10)
7330fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        << CondExpr->getSourceRange();
7340fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    }
7350fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall
7360fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    // Check to see if switch is over an Enum and handles all of its
737559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek    // values.  We only issue a warning if there is not 'default:', but
738559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek    // we still do the analysis to preserve this information in the AST
739559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek    // (which can be used by flow-based analyes).
7400fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall    //
741ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner    const EnumType *ET = CondTypeBeforePromotion->getAs<EnumType>();
742559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek
743ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor    // If switch has default case, then ignore it.
744559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek    if (!CaseListIsErroneous  && !HasConstantCond && ET) {
745ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      const EnumDecl *ED = ET->getDecl();
746ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      typedef llvm::SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> EnumValsTy;
747ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      EnumValsTy EnumVals;
748ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
7490fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      // Gather all enum values, set their type and sort them,
7500fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      // allowing easier comparison with CaseVals.
7510fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin();
75228164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif           EDI != ED->enumerator_end(); ++EDI) {
75328164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif        llvm::APSInt Val = EDI->getInitVal();
75428164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif        AdjustAPSInt(Val, CondWidth, CondIsSigned);
75528164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif        EnumVals.push_back(std::make_pair(Val, *EDI));
756ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
757ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals);
7580fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall      EnumValsTy::iterator EIend =
7590fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall        std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals);
760559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek
761559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek      // See which case values aren't in enum.
762559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek      // TODO: we might want to check whether case values are out of the
763559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek      // enum even if we don't want to check whether all cases are handled.
764559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek      if (!TheDefaultStmt) {
76547bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek        EnumValsTy::const_iterator EI = EnumVals.begin();
76647bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek        for (CaseValsTy::const_iterator CI = CaseVals.begin();
7670fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall             CI != CaseVals.end(); CI++) {
76847bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek          while (EI != EIend && EI->first < CI->first)
76947bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek            EI++;
77047bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek          if (EI == EIend || EI->first > CI->first)
7710fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall            Diag(CI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum)
7720fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall              << ED->getDeclName();
77347bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek        }
77447bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek        // See which of case ranges aren't in enum
77547bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek        EI = EnumVals.begin();
77647bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek        for (CaseRangesTy::const_iterator RI = CaseRanges.begin();
7770fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall             RI != CaseRanges.end() && EI != EIend; RI++) {
77847bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek          while (EI != EIend && EI->first < RI->first)
77947bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek            EI++;
780dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
78147bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek          if (EI == EIend || EI->first != RI->first) {
78247bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek            Diag(RI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum)
78347bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek              << ED->getDeclName();
78447bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek          }
785e0ba9d1beeba01a96808c2fc61f9ca89acec313bTed Kremenek
78647bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek          llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context);
78728164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif          AdjustAPSInt(Hi, CondWidth, CondIsSigned);
78847bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek          while (EI != EIend && EI->first < Hi)
78947bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek            EI++;
79047bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek          if (EI == EIend || EI->first != Hi)
79147bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek            Diag(RI->second->getRHS()->getExprLoc(), diag::warn_not_in_enum)
79247bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek              << ED->getDeclName();
79347bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek        }
794ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
795dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
796559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek      // Check which enum vals aren't in switch
797ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      CaseValsTy::const_iterator CI = CaseVals.begin();
798ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      CaseRangesTy::const_iterator RI = CaseRanges.begin();
799559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek      bool hasCasesNotInSwitch = false;
800559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek
801ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner      llvm::SmallVector<DeclarationName,8> UnhandledNames;
802dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
803559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek      for (EnumValsTy::const_iterator EI = EnumVals.begin(); EI != EIend; EI++){
804ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner        // Drop unneeded case values
805ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        llvm::APSInt CIVal;
806ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        while (CI != CaseVals.end() && CI->first < EI->first)
807ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          CI++;
808dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
809ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        if (CI != CaseVals.end() && CI->first == EI->first)
810ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          continue;
811ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
812559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek        // Drop unneeded case ranges
813ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        for (; RI != CaseRanges.end(); RI++) {
814ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          llvm::APSInt Hi = RI->second->getRHS()->EvaluateAsInt(Context);
81528164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif          AdjustAPSInt(Hi, CondWidth, CondIsSigned);
816ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor          if (EI->first <= Hi)
817ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor            break;
818ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor        }
819ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor
820559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek        if (RI == CaseRanges.end() || EI->first < RI->first) {
82147bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek          hasCasesNotInSwitch = true;
82247bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek          if (!TheDefaultStmt)
823ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner            UnhandledNames.push_back(EI->second->getDeclName());
82447bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek        }
825ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor      }
826dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
827ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner      // Produce a nice diagnostic if multiple values aren't handled.
828ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner      switch (UnhandledNames.size()) {
829ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner      case 0: break;
830ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner      case 1:
831ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner        Diag(CondExpr->getExprLoc(), diag::warn_missing_case1)
832ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner          << UnhandledNames[0];
833ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner        break;
834ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner      case 2:
835ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner        Diag(CondExpr->getExprLoc(), diag::warn_missing_case2)
836ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner          << UnhandledNames[0] << UnhandledNames[1];
837ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner        break;
838ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner      case 3:
839ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner        Diag(CondExpr->getExprLoc(), diag::warn_missing_case3)
840ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner          << UnhandledNames[0] << UnhandledNames[1] << UnhandledNames[2];
841ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner        break;
842ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner      default:
843ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner        Diag(CondExpr->getExprLoc(), diag::warn_missing_cases)
844ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner          << (unsigned)UnhandledNames.size()
845ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner          << UnhandledNames[0] << UnhandledNames[1] << UnhandledNames[2];
846ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner        break;
847ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner      }
848559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek
849559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek      if (!hasCasesNotInSwitch)
85047bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek        SS->setAllEnumCasesCovered();
851ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor    }
852b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  }
853dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
854390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: If the case list was broken is some way, we don't have a good system
855390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // to patch it up.  Instead, just return the whole substmt as broken.
856b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  if (CaseListIsErroneous)
857de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl    return StmtError();
858de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
859de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(SS);
8605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
86260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
863dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond,
8649ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                     Decl *CondVar, Stmt *Body) {
86560d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult CondResult(Cond.release());
866dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
8675656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  VarDecl *ConditionVar = 0;
868d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (CondVar) {
869d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    ConditionVar = cast<VarDecl>(CondVar);
870586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    CondResult = CheckConditionVariable(ConditionVar, WhileLoc, true);
87199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (CondResult.isInvalid())
87299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return StmtError();
8735656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor  }
8749ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Expr *ConditionExpr = CondResult.take();
87599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  if (!ConditionExpr)
87699e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    return StmtError();
877dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
8789ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  DiagnoseUnusedExprResult(Body);
8791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88043dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor  return Owned(new (Context) WhileStmt(Context, ConditionVar, ConditionExpr,
8819ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                       Body, WhileLoc));
8825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
88460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
8859ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnDoStmt(SourceLocation DoLoc, Stmt *Body,
886989135901c750af61ef012b6b0a0368be415bc46Chris Lattner                  SourceLocation WhileLoc, SourceLocation CondLParen,
8879ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                  Expr *Cond, SourceLocation CondRParen) {
8889ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  assert(Cond && "ActOnDoStmt(): missing expression");
889f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
8909ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (CheckBooleanCondition(Cond, DoLoc))
8915a881bb09928b7ade891efc680088aaad276f8d6John McCall    return StmtError();
8925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
893b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall  CheckImplicitConversions(Cond, DoLoc);
8944765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  ExprResult CondResult = MaybeCreateExprWithCleanups(Cond);
8959ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (CondResult.isInvalid())
896586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    return StmtError();
8979ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Cond = CondResult.take();
898dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
8999ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  DiagnoseUnusedExprResult(Body);
900754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
9019ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) DoStmt(Body, Cond, DoLoc, WhileLoc, CondRParen));
9025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
90460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
905f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
9069ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                   Stmt *First, FullExprArg second, Decl *secondVar,
90799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor                   FullExprArg third,
9089ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                   SourceLocation RParenLoc, Stmt *Body) {
9095921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  if (!getLangOptions().CPlusPlus) {
9105921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
911f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // C99 6.8.5p3: The declaration part of a 'for' statement shall only
912f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // declare identifiers for objects having storage class 'auto' or
913f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // 'register'.
9145921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis      for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
9155921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis           DI!=DE; ++DI) {
9165921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        VarDecl *VD = dyn_cast<VarDecl>(*DI);
917b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall        if (VD && VD->isLocalVarDecl() && !VD->hasLocalStorage())
9185921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis          VD = 0;
9195921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        if (VD == 0)
9205921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis          Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
9215921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        // FIXME: mark decl erroneous!
9225921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis      }
923ae3b701f59e78e058b83344be17206af3bf5d277Chris Lattner    }
9245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
92599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor
92660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult SecondResult(second.release());
92799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  VarDecl *ConditionVar = 0;
928d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  if (secondVar) {
929d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    ConditionVar = cast<VarDecl>(secondVar);
930586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor    SecondResult = CheckConditionVariable(ConditionVar, ForLoc, true);
93199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor    if (SecondResult.isInvalid())
93299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor      return StmtError();
93399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  }
934dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
93599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor  Expr *Third  = third.release().takeAs<Expr>();
936dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
9373af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson  DiagnoseUnusedExprResult(First);
9383af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson  DiagnoseUnusedExprResult(Third);
939754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(Body);
940754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
941dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  return Owned(new (Context) ForStmt(Context, First,
942dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                     SecondResult.take(), ConditionVar,
943dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                     Third, Body, ForLoc, LParenLoc,
94443dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor                                     RParenLoc));
9455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
947f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// In an Objective C collection iteration statement:
948f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall///   for (x in y)
949f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// x can be an arbitrary l-value expression.  Bind it up as a
950f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// full-expression.
951f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallStmtResult Sema::ActOnForEachLValueExpr(Expr *E) {
952f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  CheckImplicitConversions(E);
9534765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  ExprResult Result = MaybeCreateExprWithCleanups(E);
954f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  if (Result.isInvalid()) return StmtError();
955f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  return Owned(static_cast<Stmt*>(Result.get()));
956f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall}
957f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
95860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
959f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
960f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                                 SourceLocation LParenLoc,
9619ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                 Stmt *First, Expr *Second,
9629ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                 SourceLocation RParenLoc, Stmt *Body) {
96320552d2842245692b649e0d25380670922f954a2Fariborz Jahanian  if (First) {
96420552d2842245692b649e0d25380670922f954a2Fariborz Jahanian    QualType FirstType;
96520552d2842245692b649e0d25380670922f954a2Fariborz Jahanian    if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
9667e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner      if (!DS->isSingleDecl())
967f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag((*DS->decl_begin())->getLocation(),
968f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                         diag::err_toomany_element_decls));
969f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
9707e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner      Decl *D = DS->getSingleDecl();
971f34afeed9a0112bf31fee185b6c80556111d3834Ted Kremenek      FirstType = cast<ValueDecl>(D)->getType();
972f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // C99 6.8.5p3: The declaration part of a 'for' statement shall only
973f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // declare identifiers for objects having storage class 'auto' or
974f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // 'register'.
975248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff      VarDecl *VD = cast<VarDecl>(D);
976b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall      if (VD->isLocalVarDecl() && !VD->hasLocalStorage())
977f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag(VD->getLocation(),
978f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                              diag::err_non_variable_decl_in_for));
9791fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson    } else {
980c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor      Expr *FirstE = cast<Expr>(First);
9817eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall      if (!FirstE->isTypeDependent() && !FirstE->isLValue())
982f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag(First->getLocStart(),
983f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                   diag::err_selector_element_not_lvalue)
984f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl          << First->getSourceRange());
9851fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson
9861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FirstType = static_cast<Expr*>(First)->getType();
9871fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson    }
988c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor    if (!FirstType->isDependentType() &&
989c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor        !FirstType->isObjCObjectPointerType() &&
990a5e42a82ce055f29f3733f3a1f10da6cb9877deeFariborz Jahanian        !FirstType->isBlockPointerType())
991dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        Diag(ForLoc, diag::err_selector_element_type)
992d162584991885ab004a02573a73ce06422b921fcChris Lattner          << FirstType << First->getSourceRange();
9933ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  }
994c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor  if (Second && !Second->isTypeDependent()) {
995a873dfc9e7314681bb37efd9ab185045de121e43Douglas Gregor    DefaultFunctionArrayLvalueConversion(Second);
9963ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian    QualType SecondType = Second->getType();
997f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    if (!SecondType->isObjCObjectPointerType())
998dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      Diag(ForLoc, diag::err_collection_expr_type)
999d162584991885ab004a02573a73ce06422b921fcChris Lattner        << SecondType << Second->getSourceRange();
1000ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian    else if (const ObjCObjectPointerType *OPT =
1001ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian             SecondType->getAsObjCInterfacePointerType()) {
1002ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      llvm::SmallVector<IdentifierInfo *, 4> KeyIdents;
1003dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      IdentifierInfo* selIdent =
1004ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian        &Context.Idents.get("countByEnumeratingWithState");
1005ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      KeyIdents.push_back(selIdent);
1006ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      selIdent = &Context.Idents.get("objects");
1007ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      KeyIdents.push_back(selIdent);
1008ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      selIdent = &Context.Idents.get("count");
1009ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      KeyIdents.push_back(selIdent);
1010ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      Selector CSelector = Context.Selectors.getSelector(3, &KeyIdents[0]);
1011ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      if (ObjCInterfaceDecl *IDecl = OPT->getInterfaceDecl()) {
1012dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        if (!IDecl->isForwardDecl() &&
1013ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian            !IDecl->lookupInstanceMethod(CSelector)) {
101480a785c2452c73b7c41d4a867edcf5a090c78c46Fariborz Jahanian          // Must further look into private implementation methods.
1015ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian          if (!LookupPrivateInstanceMethod(CSelector, IDecl))
1016ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian            Diag(ForLoc, diag::warn_collection_expr_type)
1017ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian              << SecondType << CSelector << Second->getSourceRange();
1018ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian        }
1019ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian      }
1020ea16110ce3a4fbb205735f0a55050bc11292e82cFariborz Jahanian    }
10213ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  }
10228189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body,
10238189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                                                   ForLoc, RParenLoc));
10243ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian}
10255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
102660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
10271b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
10285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                    IdentifierInfo *LabelII) {
1029ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  // Look up the record for this label identifier.
1030ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  LabelDecl *&TheDecl = getCurFunction()->LabelMap[LabelII];
1031b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
1032caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  // If we haven't seen this label yet, create a forward reference.
1033ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  if (TheDecl == 0)
1034ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner    TheDecl = LabelDecl::Create(Context, CurContext, LabelLoc, LabelII);
10354cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
103657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  return ActOnGotoStmt(GotoLoc, LabelLoc, TheDecl);
103757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner}
103857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
103957ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerStmtResult Sema::ActOnGotoStmt(SourceLocation GotoLoc,
104057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner                               SourceLocation LabelLoc,
104157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner                               LabelDecl *TheDecl) {
104257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  getCurFunction()->setHasBranchIntoScope();
1043ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  TheDecl->setUsed();
1044ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  return Owned(new (Context) GotoStmt(TheDecl, GotoLoc, LabelLoc));
10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
104760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1048ad56d684259f706b7c0ae5ad9c23adb4f2926817Chris LattnerSema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc,
10499ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                            Expr *E) {
1050bbf462314b1dc8e422b7c4dd4cac47e566aedf6dEli Friedman  // Convert operand to void*
10515f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  if (!E->isTypeDependent()) {
10525f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    QualType ETy = E->getType();
10532877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth    QualType DestTy = Context.getPointerType(Context.VoidTy.withConst());
10545f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    AssignConvertType ConvTy =
10552877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth      CheckSingleAssignmentConstraints(DestTy, E);
10562877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth    if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E, AA_Passing))
10575f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor      return StmtError();
10585f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  }
1059b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
1060781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->setHasIndirectGoto();
1061b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
10625f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  return Owned(new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E));
10635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
106560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
10661b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
10675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Scope *S = CurScope->getContinueParent();
10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!S) {
10695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
10704cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10724cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
10738189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ContinueStmt(ContinueLoc));
10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
107660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
10771b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Scope *S = CurScope->getBreakParent();
10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!S) {
10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
10814cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10834cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
10848189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) BreakStmt(BreakLoc));
10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1087dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief Determine whether the given expression is a candidate for
1088f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// copy elision in either a return statement or a throw expression.
10895077c3876beeaed32280af88244e8050078619a8Douglas Gregor///
1090f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// \param ReturnType If we're determining the copy elision candidate for
1091f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// a return statement, this is the return type of the function. If we're
1092f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// determining the copy elision candidate for a throw expression, this will
1093f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// be a NULL type.
10945077c3876beeaed32280af88244e8050078619a8Douglas Gregor///
1095f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// \param E The expression being returned from the function or block, or
1096f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// being thrown.
10975077c3876beeaed32280af88244e8050078619a8Douglas Gregor///
1098f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// \param AllowFunctionParameter
10995077c3876beeaed32280af88244e8050078619a8Douglas Gregor///
11005077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \returns The NRVO candidate variable, if the return statement may use the
11015077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// NRVO, or NULL if there is no such candidate.
1102f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregorconst VarDecl *Sema::getCopyElisionCandidate(QualType ReturnType,
1103f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor                                             Expr *E,
1104f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor                                             bool AllowFunctionParameter) {
1105f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor  QualType ExprType = E->getType();
11063c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // - in a return statement in a function with ...
11073c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // ... a class return type ...
1108f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor  if (!ReturnType.isNull()) {
1109f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor    if (!ReturnType->isRecordType())
1110f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor      return 0;
1111f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor    // ... the same cv-unqualified type as the function return type ...
1112f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor    if (!Context.hasSameUnqualifiedType(ReturnType, ExprType))
1113f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor      return 0;
1114f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor  }
1115dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1116dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // ... the expression is the name of a non-volatile automatic object
1117f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor  // (other than a function or catch-clause parameter)) ...
1118f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor  const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParens());
11193c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  if (!DR)
11205077c3876beeaed32280af88244e8050078619a8Douglas Gregor    return 0;
11213c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
11223c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  if (!VD)
11235077c3876beeaed32280af88244e8050078619a8Douglas Gregor    return 0;
1124dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1125f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor  if (VD->hasLocalStorage() && !VD->isExceptionVariable() &&
1126d86c477fb5d3fc34864afecbbb5443da9355e8fbDouglas Gregor      !VD->getType()->isReferenceType() && !VD->hasAttr<BlocksAttr>() &&
1127f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor      !VD->getType().isVolatileQualified() &&
11284a46c77813af1241139b81a086b539e4d734cb86Douglas Gregor      ((VD->getKind() == Decl::Var) ||
11294a46c77813af1241139b81a086b539e4d734cb86Douglas Gregor       (AllowFunctionParameter && VD->getKind() == Decl::ParmVar)))
11305077c3876beeaed32280af88244e8050078619a8Douglas Gregor    return VD;
1131dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
11325077c3876beeaed32280af88244e8050078619a8Douglas Gregor  return 0;
11333c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor}
11343c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor
113507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor/// \brief Perform the initialization of a potentially-movable value, which
113607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor/// is the result of return value.
1137cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor///
1138cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// This routine implements C++0x [class.copy]p33, which attempts to treat
1139cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// returned lvalues as rvalues in certain cases (to prefer move construction),
1140cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// then falls back to treating them as lvalues if that failed.
1141dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiExprResult
114207f402cff25354c5f06f307f19b0c57c09d964bdDouglas GregorSema::PerformMoveOrCopyInitialization(const InitializedEntity &Entity,
114307f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                                      const VarDecl *NRVOCandidate,
114407f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                                      QualType ResultType,
114507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                                      Expr *Value) {
1146cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor  // C++0x [class.copy]p33:
1147dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   When the criteria for elision of a copy operation are met or would
1148dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   be met save for the fact that the source object is a function
1149dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   parameter, and the object to be copied is designated by an lvalue,
1150cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor  //   overload resolution to select the constructor for the copy is first
1151cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor  //   performed as if the object were designated by an rvalue.
1152cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor  ExprResult Res = ExprError();
115307f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor  if (NRVOCandidate || getCopyElisionCandidate(ResultType, Value, true)) {
1154dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ImplicitCastExpr AsRvalue(ImplicitCastExpr::OnStack,
115507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                              Value->getType(), CK_LValueToRValue,
115607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                              Value, VK_XValue);
1157dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1158cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor    Expr *InitExpr = &AsRvalue;
1159dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    InitializationKind Kind
116007f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor      = InitializationKind::CreateCopy(Value->getLocStart(),
116107f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                                       Value->getLocStart());
116207f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor    InitializationSequence Seq(*this, Entity, Kind, &InitExpr, 1);
1163dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1164dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   [...] If overload resolution fails, or if the type of the first
1165cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor    //   parameter of the selected constructor is not an rvalue reference
11660099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi    //   to the object's type (possibly cv-qualified), overload resolution
1167cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor    //   is performed again, considering the object as an lvalue.
1168cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor    if (Seq.getKind() != InitializationSequence::FailedSequence) {
1169cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor      for (InitializationSequence::step_iterator Step = Seq.step_begin(),
1170cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor           StepEnd = Seq.step_end();
1171cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor           Step != StepEnd; ++Step) {
1172dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        if (Step->Kind
1173cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor            != InitializationSequence::SK_ConstructorInitialization)
1174cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor          continue;
1175dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1176dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        CXXConstructorDecl *Constructor
1177cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor        = cast<CXXConstructorDecl>(Step->Function.Function);
1178dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1179cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor        const RValueReferenceType *RRefType
118007f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor          = Constructor->getParamDecl(0)->getType()
118107f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                                                 ->getAs<RValueReferenceType>();
1182dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1183cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor        // If we don't meet the criteria, break out now.
1184dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        if (!RRefType ||
118507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor            !Context.hasSameUnqualifiedType(RRefType->getPointeeType(),
118607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                            Context.getTypeDeclType(Constructor->getParent())))
1187cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor          break;
1188dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1189cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor        // Promote "AsRvalue" to the heap, since we now need this
1190cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor        // expression node to persist.
119107f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor        Value = ImplicitCastExpr::Create(Context, Value->getType(),
1192dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                         CK_LValueToRValue, Value, 0,
119307f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                                         VK_XValue);
1194dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1195cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor        // Complete type-checking the initialization of the return type
1196cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor        // using the constructor we found.
119707f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor        Res = Seq.Perform(*this, Entity, Kind, MultiExprArg(&Value, 1));
1198cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor      }
1199cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor    }
1200cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor  }
1201dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1202cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor  // Either we didn't meet the criteria for treating an lvalue as an rvalue,
1203dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // above, or overload resolution failed. Either way, we need to try
1204cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor  // (again) now with the return value expression as written.
1205cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor  if (Res.isInvalid())
120607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor    Res = PerformCopyInitialization(Entity, SourceLocation(), Value);
1207dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1208cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor  return Res;
1209cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor}
1210cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor
121127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
12124eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
121360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
12144eb206bebcdab28ababe8df55c6185cec2cdc071Steve NaroffSema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
12154eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // If this is the first return we've seen in the block, infer the type of
12164eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // the block from it.
12179ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  BlockScopeInfo *CurBlock = getCurBlock();
12187d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian  if (CurBlock->ReturnType.isNull()) {
1219c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff    if (RetValExp) {
122016564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      // Don't call UsualUnaryConversions(), since we don't want to do
122116564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      // integer promotions here.
1222a873dfc9e7314681bb37efd9ab185045de121e43Douglas Gregor      DefaultFunctionArrayLvalueConversion(RetValExp);
12237d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      CurBlock->ReturnType = RetValExp->getType();
12247d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(RetValExp)) {
12257d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // We have to remove a 'const' added to copied-in variable which was
12267d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // part of the implementation spec. and not the actual qualifier for
12277d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // the variable.
12287d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        if (CDRE->isConstQualAdded())
122949f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall          CurBlock->ReturnType.removeLocalConst(); // FIXME: local???
12307d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      }
1231c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff    } else
12327d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      CurBlock->ReturnType = Context.VoidTy;
12334eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  }
12347d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian  QualType FnRetType = CurBlock->ReturnType;
12354cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1236711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (CurBlock->FunctionType->getAs<FunctionType>()->getNoReturnAttr()) {
12376c92fa75e62937f9738696840efcb258560f4568Mike Stump    Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr)
12386c92fa75e62937f9738696840efcb258560f4568Mike Stump      << getCurFunctionOrMethodDecl()->getDeclName();
12396c92fa75e62937f9738696840efcb258560f4568Mike Stump    return StmtError();
12406c92fa75e62937f9738696840efcb258560f4568Mike Stump  }
12416c92fa75e62937f9738696840efcb258560f4568Mike Stump
12424eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // Otherwise, verify that this result type matches the previous one.  We are
12434eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // pickier with blocks than for normal functions because we don't have GCC
12444eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // compatibility to worry about here.
12455077c3876beeaed32280af88244e8050078619a8Douglas Gregor  ReturnStmt *Result = 0;
12464eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  if (CurBlock->ReturnType->isVoidType()) {
12474eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    if (RetValExp) {
12484eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff      Diag(ReturnLoc, diag::err_return_block_has_expr);
12494eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff      RetValExp = 0;
12504eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    }
12515077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0);
12525077c3876beeaed32280af88244e8050078619a8Douglas Gregor  } else if (!RetValExp) {
12534cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr));
12545077c3876beeaed32280af88244e8050078619a8Douglas Gregor  } else {
12555077c3876beeaed32280af88244e8050078619a8Douglas Gregor    const VarDecl *NRVOCandidate = 0;
1256dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
12575077c3876beeaed32280af88244e8050078619a8Douglas Gregor    if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
12585077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // we have a non-void block with an expression, continue checking
12595077c3876beeaed32280af88244e8050078619a8Douglas Gregor
12605077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // C99 6.8.6.4p3(136): The return statement is not an assignment. The
12615077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // overlap restriction of subclause 6.5.16.1 does not apply to the case of
12625077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // function return.
12635077c3876beeaed32280af88244e8050078619a8Douglas Gregor
12645077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // In C++ the return statement is handled via a copy initialization.
12655077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // the C version of which boils down to CheckSingleAssignmentConstraints.
1266f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor      NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false);
1267dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc,
126807f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                                                                     FnRetType,
126907f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                                                           NRVOCandidate != 0);
1270dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate,
127107f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                                                       FnRetType, RetValExp);
12725077c3876beeaed32280af88244e8050078619a8Douglas Gregor      if (Res.isInvalid()) {
12735077c3876beeaed32280af88244e8050078619a8Douglas Gregor        // FIXME: Cleanup temporaries here, anyway?
12745077c3876beeaed32280af88244e8050078619a8Douglas Gregor        return StmtError();
12755077c3876beeaed32280af88244e8050078619a8Douglas Gregor      }
1276dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1277b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall      if (RetValExp) {
1278b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall        CheckImplicitConversions(RetValExp, ReturnLoc);
12794765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall        RetValExp = MaybeCreateExprWithCleanups(RetValExp);
1280b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall      }
12814cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
12825077c3876beeaed32280af88244e8050078619a8Douglas Gregor      RetValExp = Res.takeAs<Expr>();
1283dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (RetValExp)
12845077c3876beeaed32280af88244e8050078619a8Douglas Gregor        CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
1285c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson    }
1286dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
12875077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate);
128898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  }
12894cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1290dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // If we need to check for the named return value optimization, save the
12915077c3876beeaed32280af88244e8050078619a8Douglas Gregor  // return statement in our scope for later processing.
12925077c3876beeaed32280af88244e8050078619a8Douglas Gregor  if (getLangOptions().CPlusPlus && FnRetType->isRecordType() &&
12935077c3876beeaed32280af88244e8050078619a8Douglas Gregor      !CurContext->isDependentContext())
12945077c3876beeaed32280af88244e8050078619a8Douglas Gregor    FunctionScopes.back()->Returns.push_back(Result);
1295dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
12965077c3876beeaed32280af88244e8050078619a8Douglas Gregor  return Owned(Result);
12974eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
12985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
129960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
13009ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
13019ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor  if (getCurBlock())
13024eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
13034cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1304371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  QualType FnRetType;
1305f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump  if (const FunctionDecl *FD = getCurFunctionDecl()) {
1306371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner    FnRetType = FD->getResultType();
130704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (FD->hasAttr<NoReturnAttr>() ||
130804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall        FD->getType()->getAs<FunctionType>()->getNoReturnAttr())
13098662587fa75d3fb04f873e265841c9314c7f5523Chris Lattner      Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr)
1310f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump        << getCurFunctionOrMethodDecl()->getDeclName();
1311f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump  } else if (ObjCMethodDecl *MD = getCurMethodDecl())
1312c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff    FnRetType = MD->getResultType();
1313c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff  else // If we don't have a function/method context, bail.
1314c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff    return StmtError();
13151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13165077c3876beeaed32280af88244e8050078619a8Douglas Gregor  ReturnStmt *Result = 0;
13175cf216b7fa64b933b60743b0b26053e8e7aa87beChris Lattner  if (FnRetType->isVoidType()) {
13181be8aee8745e8b814ad2f151aa214b0ef07833dbDouglas Gregor    if (RetValExp && !RetValExp->isTypeDependent()) {
13191be8aee8745e8b814ad2f151aa214b0ef07833dbDouglas Gregor      // C99 6.8.6.4p1 (ext_ since GCC warns)
132065ce04bef06696379682410f399f37b43996d824Chris Lattner      unsigned D = diag::ext_return_has_expr;
132165ce04bef06696379682410f399f37b43996d824Chris Lattner      if (RetValExp->getType()->isVoidType())
132265ce04bef06696379682410f399f37b43996d824Chris Lattner        D = diag::ext_return_has_void_expr;
1323f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      else {
1324f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        IgnoredValueConversions(RetValExp);
1325f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall        ImpCastExprToType(RetValExp, Context.VoidTy, CK_ToVoid);
1326f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall      }
13274cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1328e878eb035b343d7d819c092102364ec9849716aeChris Lattner      // return (some void expression); is legal in C++.
1329e878eb035b343d7d819c092102364ec9849716aeChris Lattner      if (D != diag::ext_return_has_void_expr ||
1330e878eb035b343d7d819c092102364ec9849716aeChris Lattner          !getLangOptions().CPlusPlus) {
1331e878eb035b343d7d819c092102364ec9849716aeChris Lattner        NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
1332e878eb035b343d7d819c092102364ec9849716aeChris Lattner        Diag(ReturnLoc, D)
1333e878eb035b343d7d819c092102364ec9849716aeChris Lattner          << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
1334e878eb035b343d7d819c092102364ec9849716aeChris Lattner          << RetValExp->getSourceRange();
1335e878eb035b343d7d819c092102364ec9849716aeChris Lattner      }
13361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1337b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall      CheckImplicitConversions(RetValExp, ReturnLoc);
13384765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall      RetValExp = MaybeCreateExprWithCleanups(RetValExp);
13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1340dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
13415077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0);
13425077c3876beeaed32280af88244e8050078619a8Douglas Gregor  } else if (!RetValExp && !FnRetType->isDependentType()) {
13433c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    unsigned DiagID = diag::warn_return_missing_expr;  // C90 6.6.6.4p4
13443c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    // C99 6.8.6.4p1 (ext_ since GCC warns)
13453c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
13463c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner
13473c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    if (FunctionDecl *FD = getCurFunctionDecl())
134808631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
13493c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    else
135008631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
13515077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc);
13525077c3876beeaed32280af88244e8050078619a8Douglas Gregor  } else {
13535077c3876beeaed32280af88244e8050078619a8Douglas Gregor    const VarDecl *NRVOCandidate = 0;
13545077c3876beeaed32280af88244e8050078619a8Douglas Gregor    if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
13555077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // we have a non-void function with an expression, continue checking
13565077c3876beeaed32280af88244e8050078619a8Douglas Gregor
13575077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // C99 6.8.6.4p3(136): The return statement is not an assignment. The
13585077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // overlap restriction of subclause 6.5.16.1 does not apply to the case of
13595077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // function return.
13605077c3876beeaed32280af88244e8050078619a8Douglas Gregor
13615077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // In C++ the return statement is handled via a copy initialization.
13625077c3876beeaed32280af88244e8050078619a8Douglas Gregor      // the C version of which boils down to CheckSingleAssignmentConstraints.
1363f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor      NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false);
1364dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc,
136507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                                                                     FnRetType,
136607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                                                                     NRVOCandidate != 0);
1367dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate,
136807f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor                                                       FnRetType, RetValExp);
13695077c3876beeaed32280af88244e8050078619a8Douglas Gregor      if (Res.isInvalid()) {
13705077c3876beeaed32280af88244e8050078619a8Douglas Gregor        // FIXME: Cleanup temporaries here, anyway?
13715077c3876beeaed32280af88244e8050078619a8Douglas Gregor        return StmtError();
13725077c3876beeaed32280af88244e8050078619a8Douglas Gregor      }
13734cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
13745077c3876beeaed32280af88244e8050078619a8Douglas Gregor      RetValExp = Res.takeAs<Expr>();
1375dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (RetValExp)
13765077c3876beeaed32280af88244e8050078619a8Douglas Gregor        CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
137766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    }
1378dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1379b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall    if (RetValExp) {
1380b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall      CheckImplicitConversions(RetValExp, ReturnLoc);
13814765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall      RetValExp = MaybeCreateExprWithCleanups(RetValExp);
1382b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall    }
13835077c3876beeaed32280af88244e8050078619a8Douglas Gregor    Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate);
1384898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  }
1385dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1386dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // If we need to check for the named return value optimization, save the
13875077c3876beeaed32280af88244e8050078619a8Douglas Gregor  // return statement in our scope for later processing.
13885077c3876beeaed32280af88244e8050078619a8Douglas Gregor  if (getLangOptions().CPlusPlus && FnRetType->isRecordType() &&
13895077c3876beeaed32280af88244e8050078619a8Douglas Gregor      !CurContext->isDependentContext())
13905077c3876beeaed32280af88244e8050078619a8Douglas Gregor    FunctionScopes.back()->Returns.push_back(Result);
1391dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
13925077c3876beeaed32280af88244e8050078619a8Douglas Gregor  return Owned(Result);
13935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1395810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently
1396810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// ignore "noop" casts in places where an lvalue is required by an inline asm.
1397810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// We emulate this behavior when -fheinous-gnu-extensions is specified, but
1398810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// provide a strong guidance to not use it.
1399810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner///
1400810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// This method checks to see if the argument is an acceptable l-value and
1401810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// returns false if it is a case we can handle.
1402810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattnerstatic bool CheckAsmLValue(const Expr *E, Sema &S) {
1403703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson  // Type dependent expressions will be checked during instantiation.
1404703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson  if (E->isTypeDependent())
1405703e39486689d6660e75f6b6de0068db031a51c7Anders Carlsson    return false;
1406dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
14077eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  if (E->isLValue())
1408810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    return false;  // Cool, this is an lvalue.
1409810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1410810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // Okay, this is not an lvalue, but perhaps it is the result of a cast that we
1411810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // are supposed to allow.
1412810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  const Expr *E2 = E->IgnoreParenNoopCasts(S.Context);
14137eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  if (E != E2 && E2->isLValue()) {
1414810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    if (!S.getLangOptions().HeinousExtensions)
1415810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue)
1416810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner        << E->getSourceRange();
1417810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    else
1418810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue)
1419810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner        << E->getSourceRange();
1420810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    // Accept, even if we emitted an error diagnostic.
1421810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    return false;
1422810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  }
1423810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1424810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // None of the above, just randomly invalid non-lvalue.
1425810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  return true;
1426810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner}
1427810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1428810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
142960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
14303037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          bool IsSimple,
14313037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          bool IsVolatile,
14323037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          unsigned NumOutputs,
14333037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          unsigned NumInputs,
1434ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson                                          IdentifierInfo **Names,
14353037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg constraints,
14363037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg exprs,
14379ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                          Expr *asmString,
14383037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg clobbers,
14393b11fd3b52e7b88233c602407c151d07cb093e75Mike Stump                                          SourceLocation RParenLoc,
14403b11fd3b52e7b88233c602407c151d07cb093e75Mike Stump                                          bool MSAsm) {
14413037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  unsigned NumClobbers = clobbers.size();
14423037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral **Constraints =
14433037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl    reinterpret_cast<StringLiteral**>(constraints.get());
14449ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Expr **Exprs = exprs.get();
14459ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  StringLiteral *AsmString = cast<StringLiteral>(asmString);
14463037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get());
14473037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
144803eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson  llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
14491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14501708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  // The parser verifies that there is a string literal here.
14516bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner  if (AsmString->isWide())
14523037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl    return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
14533037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      << AsmString->getSourceRange());
14543037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
14551708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  for (unsigned i = 0; i != NumOutputs; i++) {
14561708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Constraints[i];
14576bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
14583037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
14593037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
14603037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1461ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    llvm::StringRef OutputName;
1462ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    if (Names[i])
1463ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson      OutputName = Names[i]->getName();
1464ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson
1465ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName);
1466432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner    if (!Context.Target.validateOutputConstraint(Info))
14673037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
1468432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                            diag::err_asm_invalid_output_constraint)
1469432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                       << Info.getConstraintStr());
14703037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1471d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson    // Check that the output exprs are valid lvalues.
147272056a237c536ee63285ab0850cb50f299281767Eli Friedman    Expr *OutputExpr = Exprs[i];
1473810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    if (CheckAsmLValue(OutputExpr, *this)) {
147472056a237c536ee63285ab0850cb50f299281767Eli Friedman      return StmtError(Diag(OutputExpr->getLocStart(),
1475dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                  diag::err_asm_invalid_lvalue_in_output)
147672056a237c536ee63285ab0850cb50f299281767Eli Friedman        << OutputExpr->getSourceRange());
147704728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson    }
14781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
147944def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    OutputConstraintInfos.push_back(Info);
148004728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  }
14813037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1482806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  llvm::SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
1483806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner
148404728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
14851708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Constraints[i];
14866bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
14873037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
14883037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
14893037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1490ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    llvm::StringRef InputName;
1491ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    if (Names[i])
1492ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson      InputName = Names[i]->getName();
1493ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson
1494ff93dbd887e40588ed55d135037bb9287488b285Anders Carlsson    TargetInfo::ConstraintInfo Info(Literal->getString(), InputName);
1495beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    if (!Context.Target.validateInputConstraint(OutputConstraintInfos.data(),
14962819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                                NumOutputs, Info)) {
14973037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
1498432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                            diag::err_asm_invalid_input_constraint)
1499432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                       << Info.getConstraintStr());
1500d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson    }
15013037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
150272056a237c536ee63285ab0850cb50f299281767Eli Friedman    Expr *InputExpr = Exprs[i];
15033037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1504d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson    // Only allow void types for memory constraints.
150544def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.allowsMemory() && !Info.allowsRegister()) {
1506810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      if (CheckAsmLValue(InputExpr, *this))
150772056a237c536ee63285ab0850cb50f299281767Eli Friedman        return StmtError(Diag(InputExpr->getLocStart(),
1508d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson                              diag::err_asm_invalid_lvalue_in_input)
1509432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                         << Info.getConstraintStr()
151072056a237c536ee63285ab0850cb50f299281767Eli Friedman                         << InputExpr->getSourceRange());
151104728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson    }
15123037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
151344def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.allowsRegister()) {
1514d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson      if (InputExpr->getType()->isVoidType()) {
151572056a237c536ee63285ab0850cb50f299281767Eli Friedman        return StmtError(Diag(InputExpr->getLocStart(),
1516d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson                              diag::err_asm_invalid_type_in_input)
15171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          << InputExpr->getType() << Info.getConstraintStr()
151872056a237c536ee63285ab0850cb50f299281767Eli Friedman          << InputExpr->getSourceRange());
1519d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson      }
1520d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson    }
15211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1522a873dfc9e7314681bb37efd9ab185045de121e43Douglas Gregor    DefaultFunctionArrayLvalueConversion(Exprs[i]);
15231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1524806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    InputConstraintInfos.push_back(Info);
152504728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  }
15263037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
15276fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson  // Check that the clobbers are valid.
15281708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  for (unsigned i = 0; i != NumClobbers; i++) {
15291708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Clobbers[i];
15306bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
15313037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
15323037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
15333037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1534fdba9c069023f686e2608affde02c82131ee1cf8Anders Carlsson    llvm::StringRef Clobber = Literal->getString();
15353037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1536fdba9c069023f686e2608affde02c82131ee1cf8Anders Carlsson    if (!Context.Target.isValidGCCRegisterName(Clobber))
15373037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
15387765934ad7e157b5fcf925792a38e01b1edbcf8aDaniel Dunbar                  diag::err_asm_unknown_register_name) << Clobber);
15396fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson  }
15403037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1541fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  AsmStmt *NS =
1542dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    new (Context) AsmStmt(Context, AsmLoc, IsSimple, IsVolatile, MSAsm,
1543dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                          NumOutputs, NumInputs, Names, Constraints, Exprs,
1544966146e89141804ff6492739a2a6e6592ca671c7Anders Carlsson                          AsmString, NumClobbers, Clobbers, RParenLoc);
1545fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  // Validate the asm string, ensuring it makes sense given the operands we
1546fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  // have.
1547fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  llvm::SmallVector<AsmStmt::AsmStringPiece, 8> Pieces;
1548fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  unsigned DiagOffs;
1549fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
15502ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner    Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
15512ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner           << AsmString->getSourceRange();
1552fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner    return StmtError();
1553fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  }
15541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1555806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  // Validate tied input operands for type mismatches.
1556806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) {
1557806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
15581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1559806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // If this is a tied constraint, verify that the output and input have
1560806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // either exactly the same type, or that they are int/ptr operands with the
1561806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // same size (int/long, int*/long, are ok etc).
1562806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    if (!Info.hasTiedOperand()) continue;
15631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1564806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    unsigned TiedTo = Info.getTiedOperand();
1565f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner    Expr *OutputExpr = Exprs[TiedTo];
1566c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner    Expr *InputExpr = Exprs[i+NumOutputs];
15677adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    QualType InTy = InputExpr->getType();
15687adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    QualType OutTy = OutputExpr->getType();
15697adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    if (Context.hasSameType(InTy, OutTy))
1570806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner      continue;  // All types can be tied to themselves.
15711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1572aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // Decide if the input and output are in the same domain (integer/ptr or
1573aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // floating point.
1574aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    enum AsmDomain {
1575aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      AD_Int, AD_FP, AD_Other
1576aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    } InputDomain, OutputDomain;
1577dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1578aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    if (InTy->isIntegerType() || InTy->isPointerType())
1579aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      InputDomain = AD_Int;
15800c293ea13d452c1a47a05ada5a5ee9acc69c66ccDouglas Gregor    else if (InTy->isRealFloatingType())
1581aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      InputDomain = AD_FP;
1582aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    else
1583aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      InputDomain = AD_Other;
15843351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner
1585aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    if (OutTy->isIntegerType() || OutTy->isPointerType())
1586aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      OutputDomain = AD_Int;
15870c293ea13d452c1a47a05ada5a5ee9acc69c66ccDouglas Gregor    else if (OutTy->isRealFloatingType())
1588aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      OutputDomain = AD_FP;
1589aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    else
1590aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      OutputDomain = AD_Other;
1591dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1592aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // They are ok if they are the same size and in the same domain.  This
1593aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // allows tying things like:
1594aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    //   void* to int*
1595aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    //   void* to int            if they are the same size.
1596aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    //   double to long double   if they are the same size.
1597dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //
1598aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    uint64_t OutSize = Context.getTypeSize(OutTy);
1599aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    uint64_t InSize = Context.getTypeSize(InTy);
1600aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    if (OutSize == InSize && InputDomain == OutputDomain &&
1601aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        InputDomain != AD_Other)
1602aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      continue;
1603dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1604aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // If the smaller input/output operand is not mentioned in the asm string,
1605aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // then we can promote it and the asm string won't notice.  Check this
1606aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // case now.
1607aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    bool SmallerValueMentioned = false;
1608aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    for (unsigned p = 0, e = Pieces.size(); p != e; ++p) {
1609aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      AsmStmt::AsmStringPiece &Piece = Pieces[p];
1610aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (!Piece.isOperand()) continue;
1611aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner
1612aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // If this is a reference to the input and if the input was the smaller
1613aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // one, then we have to reject this asm.
1614aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (Piece.getOperandNo() == i+NumOutputs) {
1615aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        if (InSize < OutSize) {
1616aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          SmallerValueMentioned = true;
1617aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          break;
16183351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        }
1619f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner      }
16201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1621aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // If this is a reference to the input and if the input was the smaller
1622aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      // one, then we have to reject this asm.
1623aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      if (Piece.getOperandNo() == TiedTo) {
1624aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        if (InSize > OutSize) {
1625aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          SmallerValueMentioned = true;
1626aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner          break;
1627aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        }
1628aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      }
1629806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    }
16301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1631aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // If the smaller value wasn't mentioned in the asm string, and if the
1632aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // output was a register, just extend the shorter one to the size of the
1633aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    // larger one.
1634aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner    if (!SmallerValueMentioned && InputDomain != AD_Other &&
1635aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner        OutputConstraintInfos[TiedTo].allowsRegister())
1636aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner      continue;
1637aab64d0b7f41ed30b15ba9210ed859424cbc7455Chris Lattner
1638c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner    Diag(InputExpr->getLocStart(),
1639806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner         diag::err_asm_tying_incompatible_types)
16407adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      << InTy << OutTy << OutputExpr->getSourceRange()
1641806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner      << InputExpr->getSourceRange();
1642806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    return StmtError();
1643806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  }
16441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1645fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  return Owned(NS);
1646fe795956194141c91ae555985c9b930595bff43fChris Lattner}
16473b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian
164860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1649431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
1650d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                           SourceLocation RParen, Decl *Parm,
16519ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                           Stmt *Body) {
1652d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  VarDecl *Var = cast_or_null<VarDecl>(Parm);
1653160b5630aa781ac348303e1ae088d27016637778Douglas Gregor  if (Var && Var->isInvalidDecl())
1654160b5630aa781ac348303e1ae088d27016637778Douglas Gregor    return StmtError();
1655dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
16569ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) ObjCAtCatchStmt(AtLoc, RParen, Var, Body));
16573b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian}
16583b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian
165960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
16609ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, Stmt *Body) {
16619ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) ObjCAtFinallyStmt(AtLoc, Body));
1662161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian}
1663bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
166460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1665dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnObjCAtTryStmt(SourceLocation AtLoc, Stmt *Try,
16669ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                         MultiStmtArg CatchStmts, Stmt *Finally) {
1667781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->setHasBranchProtectedScope();
16688f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor  unsigned NumCatchStmts = CatchStmts.size();
16699ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(ObjCAtTryStmt::Create(Context, AtLoc, Try,
16709ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     CatchStmts.release(),
16718f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                     NumCatchStmts,
16729ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                     Finally));
1673bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian}
1674bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
167560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::BuildObjCAtThrowStmt(SourceLocation AtLoc,
16769ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                  Expr *Throw) {
1677d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  if (Throw) {
16785e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall    DefaultLvalueConversion(Throw);
16795e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1680d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    QualType ThrowType = Throw->getType();
1681d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    // Make sure the expression type is an ObjC pointer or "void *".
1682d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    if (!ThrowType->isDependentType() &&
1683d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor        !ThrowType->isObjCObjectPointerType()) {
1684d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor      const PointerType *PT = ThrowType->getAs<PointerType>();
1685d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor      if (!PT || !PT->getPointeeType()->isVoidType())
1686d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor        return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object)
1687d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor                         << Throw->getType() << Throw->getSourceRange());
1688d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor    }
1689d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor  }
1690dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
16919ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) ObjCAtThrowStmt(AtLoc, Throw));
1692d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor}
1693d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor
169460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1695dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw,
1696d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor                           Scope *CurScope) {
16979ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (!Throw) {
1698e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    // @throw without an expression designates a rethrow (which much occur
1699e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    // in the context of an @catch clause).
1700e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    Scope *AtCatchParent = CurScope;
1701e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    while (AtCatchParent && !AtCatchParent->isAtCatchScope())
1702e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff      AtCatchParent = AtCatchParent->getParent();
1703e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    if (!AtCatchParent)
17044ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff      return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch));
1705dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  }
1706dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
17079ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return BuildObjCAtThrowStmt(AtLoc, Throw);
170839f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian}
1709bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
171060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
17119ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, Expr *SyncExpr,
17129ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Stmt *SyncBody) {
1713781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->setHasBranchProtectedScope();
171446c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner
17155e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall  DefaultLvalueConversion(SyncExpr);
17165e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall
1717a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  // Make sure the expression type is an ObjC pointer or "void *".
17188fdc13a78a43f09ac396e682c35d57ca0b48216dDouglas Gregor  if (!SyncExpr->getType()->isDependentType() &&
17198fdc13a78a43f09ac396e682c35d57ca0b48216dDouglas Gregor      !SyncExpr->getType()->isObjCObjectPointerType()) {
17206217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    const PointerType *PT = SyncExpr->getType()->getAs<PointerType>();
1721a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner    if (!PT || !PT->getPointeeType()->isVoidType())
1722a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner      return StmtError(Diag(AtLoc, diag::error_objc_synchronized_expects_object)
1723a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner                       << SyncExpr->getType() << SyncExpr->getSourceRange());
1724a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  }
17251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17269ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc, SyncExpr, SyncBody));
1727fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian}
17284b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl
17294b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
17304b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// and creates a proper catch handler from them.
173160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
1732d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnCXXCatchBlock(SourceLocation CatchLoc, Decl *ExDecl,
17339ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                         Stmt *HandlerBlock) {
17344b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl  // There's nothing to test that ActOnExceptionDecl didn't already test.
17358189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CXXCatchStmt(CatchLoc,
1736d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                                          cast_or_null<VarDecl>(ExDecl),
17379ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                          HandlerBlock));
17384b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl}
17398351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
17403c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace {
17413c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
1742c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlclass TypeWithHandler {
1743c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  QualType t;
1744c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  CXXCatchStmt *stmt;
1745c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlpublic:
1746c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  TypeWithHandler(const QualType &type, CXXCatchStmt *statement)
1747c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  : t(type), stmt(statement) {}
1748c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
17490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // An arbitrary order is fine as long as it places identical
17500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // types next to each other.
1751c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  bool operator<(const TypeWithHandler &y) const {
17520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (t.getAsOpaquePtr() < y.t.getAsOpaquePtr())
1753c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return true;
17540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (t.getAsOpaquePtr() > y.t.getAsOpaquePtr())
1755c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return false;
1756c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    else
1757c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return getTypeSpecStartLoc() < y.getTypeSpecStartLoc();
1758c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
17591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1760c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  bool operator==(const TypeWithHandler& other) const {
17610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return t == other.t;
1762c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
17631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1764c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  CXXCatchStmt *getCatchStmt() const { return stmt; }
1765c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  SourceLocation getTypeSpecStartLoc() const {
1766c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    return stmt->getExceptionDecl()->getTypeSpecStartLoc();
1767c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1768c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl};
1769c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
17703c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman}
17713c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
17728351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
17738351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// handlers and creates a try statement from them.
177460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
17759ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock,
17768351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl                       MultiStmtArg RawHandlers) {
17778351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  unsigned NumHandlers = RawHandlers.size();
17788351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  assert(NumHandlers > 0 &&
17798351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl         "The parser shouldn't call this if there are no handlers.");
17809ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Stmt **Handlers = RawHandlers.get();
17818351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
1782c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  llvm::SmallVector<TypeWithHandler, 8> TypesWithHandlers;
17831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (unsigned i = 0; i < NumHandlers; ++i) {
17858351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl    CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
1786c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    if (!Handler->getExceptionDecl()) {
1787c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      if (i < NumHandlers - 1)
1788c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        return StmtError(Diag(Handler->getLocStart(),
1789c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl                              diag::err_early_catch_all));
17901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1791c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      continue;
1792c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    }
17931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1794c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    const QualType CaughtType = Handler->getCaughtType();
1795c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    const QualType CanonicalCaughtType = Context.getCanonicalType(CaughtType);
1796c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    TypesWithHandlers.push_back(TypeWithHandler(CanonicalCaughtType, Handler));
1797c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1798c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1799c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  // Detect handlers for the same type as an earlier one.
1800c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  if (NumHandlers > 1) {
1801c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    llvm::array_pod_sort(TypesWithHandlers.begin(), TypesWithHandlers.end());
18021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1803c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    TypeWithHandler prev = TypesWithHandlers[0];
1804c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    for (unsigned i = 1; i < TypesWithHandlers.size(); ++i) {
1805c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      TypeWithHandler curr = TypesWithHandlers[i];
18061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1807c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      if (curr == prev) {
1808c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        Diag(curr.getTypeSpecStartLoc(),
1809c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl             diag::warn_exception_caught_by_earlier_handler)
1810c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl          << curr.getCatchStmt()->getCaughtType().getAsString();
1811c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        Diag(prev.getTypeSpecStartLoc(),
1812c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl             diag::note_previous_exception_handler)
1813c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl          << prev.getCatchStmt()->getCaughtType().getAsString();
1814c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      }
18151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1816c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      prev = curr;
1817c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    }
18188351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  }
18191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1820781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  getCurFunction()->setHasBranchProtectedScope();
1821b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall
18228351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // FIXME: We should detect handlers that cannot catch anything because an
18238351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // earlier handler catches a superclass. Need to find a method that is not
18248351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // quadratic for this.
18258351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // Neither of these are explicitly forbidden, but every compiler detects them
18268351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // and warns.
18278351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
18289ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Owned(CXXTryStmt::Create(Context, TryLoc, TryBlock,
1829a1a396df16c02b22983b5c9592022fd9237d4866Sam Weinig                                  Handlers, NumHandlers));
18308351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl}
1831