SemaStmt.cpp revision 8cfe5a784133d90bf329fd20801824a6f71bb8ca
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements semantic analysis for statements.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "Sema.h"
1551fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson#include "clang/AST/APValue.h"
16f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner#include "clang/AST/ASTContext.h"
17c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
1884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/AST/ExprCXX.h"
19419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner#include "clang/AST/ExprObjC.h"
2016f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h"
2116f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtCXX.h"
2284fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/Lex/Preprocessor.h"
236fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson#include "clang/Basic/TargetInfo.h"
24c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/STLExtras.h"
25c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/SmallVector.h"
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
286b1d283fe879fb11d7ce7a69feecf66e77b0eaf3Anders CarlssonSema::OwningStmtResult Sema::ActOnExprStmt(FullExprArg expr) {
296b1d283fe879fb11d7ce7a69feecf66e77b0eaf3Anders Carlsson  Expr *E = expr->takeAs<Expr>();
301b273c403734d343d720acb28f04011807c8aa56Steve Naroff  assert(E && "ActOnExprStmt(): missing expression");
3116b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian  if (E->getType()->isObjCInterfaceType()) {
3216b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian    if (LangOpts.ObjCNonFragileABI)
3316b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian      Diag(E->getLocEnd(), diag::err_indirection_requires_nonfragile_object)
3416b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian             << E->getType();
3516b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian    else
3616b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian      Diag(E->getLocEnd(), diag::err_direct_interface_unsupported)
3716b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian             << E->getType();
3816b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian    return StmtError();
3916b10378a93e8644008289fd86c1caf737d1395cFariborz Jahanian  }
40834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // C99 6.8.3p2: The expression in an expression statement is evaluated as a
41834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // void expression for its side effects.  Conversion to void allows any
42834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // operand, even incomplete types.
43a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl
44834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // Same thing in for stmt first clause (when expr) and third clause.
45a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  return Owned(static_cast<Stmt*>(E));
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
49a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian RedlSema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
508189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) NullStmt(SemiLoc));
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
53682bf92db408a6cbc3d37b5496a99b6ef85041ecChris LattnerSema::OwningStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg,
54a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                           SourceLocation StartLoc,
55a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                           SourceLocation EndLoc) {
5620401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner  DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // If we have an invalid decl, just return an error.
5920401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner  if (DG.isNull()) return StmtError();
601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6124e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
64a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanianvoid Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) {
65a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
66a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian
67a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  // If we have an invalid decl, just return.
68a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  if (DG.isNull() || !DG.isSingleDecl()) return;
69a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  // suppress any potential 'unused variable' warning.
70a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian  DG.getSingleDecl()->setUsed();
71a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian}
72a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian
73636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlssonvoid Sema::DiagnoseUnusedExprResult(const Stmt *S) {
74754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  const Expr *E = dyn_cast_or_null<Expr>(S);
75636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  if (!E)
76636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
77636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
78636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  // Ignore expressions that have void type.
79636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  if (E->getType()->isVoidType())
80636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  SourceLocation Loc;
83636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  SourceRange R1, R2;
84df317bf71653eeb235da8337b1e8e790f9653aa4Mike Stump  if (!E->isUnusedResultAWarning(Loc, R1, R2, Context))
85636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // Okay, we have an unused result.  Depending on what the base expression is,
88419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // we might want to make a more specific diagnostic.  Check for one of these
89419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  // cases now.
90419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  unsigned DiagID = diag::warn_unused_expr;
91419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  E = E->IgnoreParens();
9209105f52b1f28cbb1374c27c3c70f5517e2c465dFariborz Jahanian  if (isa<ObjCImplicitSetterGetterRefExpr>(E))
93419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner    DiagID = diag::warn_unused_property_expr;
94bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner
95bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner  if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
96bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    // If the callee has attribute pure, const, or warn_unused_result, warn with
97bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    // a more specific message to make it clear what is happening.
98bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    if (const FunctionDecl *FD = CE->getDirectCallee()) {
99bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<WarnUnusedResultAttr>()) {
100bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result";
101bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
102bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
103bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<PureAttr>()) {
104bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure";
105bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
106bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
107bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      if (FD->getAttr<ConstAttr>()) {
108bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const";
109bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner        return;
110bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner      }
111bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner    }
112bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner  }
1131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
114419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner  Diag(Loc, DiagID) << R1 << R2;
115636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson}
116636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
117a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian RedlAction::OwningStmtResult
1181b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
119a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                        MultiStmtArg elts, bool isStmtExpr) {
120a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  unsigned NumElts = elts.size();
121a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
122c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  // If we're in C89 mode, check that we don't have any decls after stmts.  If
123c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  // so, emit an extension diagnostic.
124c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
125c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // Note that __extension__ can be around a decl.
126c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    unsigned i = 0;
127c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // Skip over all declarations.
128c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
129c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      /*empty*/;
130c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner
131c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // We found the end of the list or a statement.  Scan for another declstmt.
132c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
133c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      /*empty*/;
1341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
135c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    if (i != NumElts) {
1364afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
137c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      Diag(D->getLocation(), diag::ext_mixed_decls_code);
138c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    }
139c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  }
14098414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  // Warn about unused expressions in statements.
14198414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  for (unsigned i = 0; i != NumElts; ++i) {
142636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    // Ignore statements that are last in a statement expression.
143636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    if (isStmtExpr && i == NumElts - 1)
14498414c1b7d1944a57156d52e29bd41c005de09acChris Lattner      continue;
1451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
146636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    DiagnoseUnusedExprResult(Elts[i]);
14798414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  }
148a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl
1498189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R));
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
152117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian RedlAction::OwningStmtResult
153117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian RedlSema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval,
154117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl                    SourceLocation DotDotDotLoc, ExprArg rhsval,
15524e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner                    SourceLocation ColonLoc) {
156117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  assert((lhsval.get() != 0) && "missing expression in case statement");
157117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.4.2p3: The expression shall be an integer constant.
1591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // However, GCC allows any evaluatable integer expression.
160117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  Expr *LHSVal = static_cast<Expr*>(lhsval.get());
1611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent() &&
162dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      VerifyIntegerConstantExpression(LHSVal))
16324e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner    return StmtError();
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1656c36be5b383875b490684bcf439d6d427298c1afChris Lattner  // GCC extension: The expression shall be an integer constant.
166117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
167117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  Expr *RHSVal = static_cast<Expr*>(rhsval.get());
168dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent() &&
169dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      VerifyIntegerConstantExpression(RHSVal)) {
170f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    RHSVal = 0;  // Recover by just forgetting about it.
171117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl    rhsval = 0;
172117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  }
173117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
174bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  if (getSwitchStack().empty()) {
1758a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner    Diag(CaseLoc, diag::err_case_not_in_switch);
17624e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner    return StmtError();
1778a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner  }
1785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
179117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  // Only now release the smart pointers.
180117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  lhsval.release();
181117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  rhsval.release();
182dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc,
183dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                        ColonLoc);
184bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  getSwitchStack().back()->addSwitchCase(CS);
185117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  return Owned(CS);
1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner/// ActOnCaseStmtBody - This installs a statement as the body of a case.
18924e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattnervoid Sema::ActOnCaseStmtBody(StmtTy *caseStmt, StmtArg subStmt) {
19024e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  CaseStmt *CS = static_cast<CaseStmt*>(caseStmt);
191f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Stmt *SubStmt = subStmt.takeAs<Stmt>();
19224e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  CS->setSubStmt(SubStmt);
19324e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner}
19424e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner
195117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian RedlAction::OwningStmtResult
1961eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
197117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl                       StmtArg subStmt, Scope *CurScope) {
198f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Stmt *SubStmt = subStmt.takeAs<Stmt>();
199117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
200bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  if (getSwitchStack().empty()) {
2010fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner    Diag(DefaultLoc, diag::err_default_not_in_switch);
202117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl    return Owned(SubStmt);
2030fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner  }
204117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
205dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt);
206bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  getSwitchStack().back()->addSwitchCase(DS);
207117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  return Owned(DS);
2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
210de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult
2111b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
212de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl                     SourceLocation ColonLoc, StmtArg subStmt) {
213f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Stmt *SubStmt = subStmt.takeAs<Stmt>();
2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Look up the record for this label identifier.
215ea29a3a0d6948c4a51a261d19ec1a585d2a9c779Chris Lattner  LabelStmt *&LabelDecl = getLabelMap()[II];
216de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If not forward referenced or defined already, just create a new LabelStmt.
218caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  if (LabelDecl == 0)
219caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff    return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt));
220de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(LabelDecl->getID() == II && "Label mismatch!");
222de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, this label was either forward reference or multiply defined.  If
2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // multiply defined, reject it now.
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (LabelDecl->getSubStmt()) {
22608631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID();
2275f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner    Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition);
228de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl    return Owned(SubStmt);
2295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
230de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, this label was forward declared, and we just found its real
2325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // definition.  Fill in the forward definition and return it.
2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LabelDecl->setIdentLoc(IdentLoc);
2340fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner  LabelDecl->setSubStmt(SubStmt);
235de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(LabelDecl);
2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
238de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult
239a99fad8ff134273fe85f2970c7d89133d1218900Anders CarlssonSema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal,
240de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl                  StmtArg ThenVal, SourceLocation ElseLoc,
241de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl                  StmtArg ElseVal) {
242a99fad8ff134273fe85f2970c7d89133d1218900Anders Carlsson  OwningExprResult CondResult(CondVal.release());
2431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
244a99fad8ff134273fe85f2970c7d89133d1218900Anders Carlsson  Expr *condExpr = CondResult.takeAs<Expr>();
2451b273c403734d343d720acb28f04011807c8aa56Steve Naroff  assert(condExpr && "ActOnIfStmt(): missing expression");
2468cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor
2478cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  VarDecl *ConditionVar = 0;
2488cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  if (CXXConditionDeclExpr *Cond = dyn_cast<CXXConditionDeclExpr>(condExpr)) {
2498cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor    ConditionVar = Cond->getVarDecl();
2508cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor    condExpr = DeclRefExpr::Create(Context, 0, SourceRange(), ConditionVar,
2518cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor                                   ConditionVar->getLocation(),
2528cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor                                 ConditionVar->getType().getNonReferenceType());
2538cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor    // FIXME: Leaks the old condExpr
2548cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  }
2558cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor
2565a881bb09928b7ade891efc680088aaad276f8d6John McCall  if (CheckBooleanCondition(condExpr, IfLoc)) {
257a99fad8ff134273fe85f2970c7d89133d1218900Anders Carlsson    CondResult = condExpr;
2585a881bb09928b7ade891efc680088aaad276f8d6John McCall    return StmtError();
259d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor  }
260de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
261e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson  Stmt *thenStmt = ThenVal.takeAs<Stmt>();
262754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(thenStmt);
2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2642d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // Warn if the if block has a null body without an else value.
2652d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // this helps prevent bugs due to typos, such as
2662d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // if (condition);
2672d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  //   do_stuff();
2681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ElseVal.get()) {
2692d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson    if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
2702d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson      Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
2712d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  }
2722d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson
273754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  Stmt *elseStmt = ElseVal.takeAs<Stmt>();
274754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(elseStmt);
2751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
276a99fad8ff134273fe85f2970c7d89133d1218900Anders Carlsson  CondResult.release();
2778cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor  return Owned(new (Context) IfStmt(IfLoc, ConditionVar, condExpr, thenStmt,
278754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson                                    ElseLoc, elseStmt));
2795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
281de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult
282de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlSema::ActOnStartOfSwitchStmt(ExprArg cond) {
28384fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  SwitchStmt *SS = new (Context) SwitchStmt(cond.takeAs<Expr>());
284bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  getSwitchStack().push_back(SS);
285de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(SS);
286c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson}
2876c36be5b383875b490684bcf439d6d427298c1afChris Lattner
288f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
289f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified width and sign.  If an overflow occurs, detect it and emit
290f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified diagnostic.
291f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattnervoid Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
292f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                              unsigned NewWidth, bool NewSign,
2931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              SourceLocation Loc,
294f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                              unsigned DiagID) {
295f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Perform a conversion to the promoted condition type if needed.
296f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  if (NewWidth > Val.getBitWidth()) {
297f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If this is an extension, just do it.
298f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt OldVal(Val);
299f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.extend(NewWidth);
3001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
301f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If the input was signed and negative and the output is unsigned,
302f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // warn.
303f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
304d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
3051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
306f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.setIsSigned(NewSign);
307f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  } else if (NewWidth < Val.getBitWidth()) {
308f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If this is a truncation, check for overflow.
309f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt ConvVal(Val);
310f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    ConvVal.trunc(NewWidth);
311b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    ConvVal.setIsSigned(NewSign);
312f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    ConvVal.extend(Val.getBitWidth());
313b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    ConvVal.setIsSigned(Val.isSigned());
314f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    if (ConvVal != Val)
315d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
3161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
317f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // Regardless of whether a diagnostic was emitted, really do the
318f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // truncation.
319f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.trunc(NewWidth);
320b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    Val.setIsSigned(NewSign);
321f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  } else if (NewSign != Val.isSigned()) {
322f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // Convert the sign to match the sign of the condition.  This can cause
323f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // overflow as well: unsigned(INTMIN)
324f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt OldVal(Val);
325f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.setIsSigned(NewSign);
3261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
327f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    if (Val.isNegative())  // Sign bit changes meaning.
328d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
329f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  }
330f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner}
331f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
3320471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattnernamespace {
3330471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  struct CaseCompareFunctor {
3340471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
3350471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner                    const llvm::APSInt &RHS) {
3360471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      return LHS.first < RHS;
3370471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    }
3380e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner    bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
3390e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner                    const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
3400e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner      return LHS.first < RHS.first;
3410e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner    }
3420471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    bool operator()(const llvm::APSInt &LHS,
3430471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner                    const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
3440471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      return LHS < RHS.first;
3450471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    }
3460471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  };
3470471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner}
3480471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner
349764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// CmpCaseVals - Comparison predicate for sorting case values.
350764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner///
351764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattnerstatic bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
352764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner                        const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
353764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  if (lhs.first < rhs.first)
354764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner    return true;
355764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner
356764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  if (lhs.first == rhs.first &&
357764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner      lhs.second->getCaseLoc().getRawEncoding()
358764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner       < rhs.second->getCaseLoc().getRawEncoding())
359764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner    return true;
360764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  return false;
361764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner}
362764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner
3635f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of
3645f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// potentially integral-promoted expression @p expr.
3655f04881eb025f61396d0555d8173730fe2759e0aChris Lattnerstatic QualType GetTypeBeforeIntegralPromotion(const Expr* expr) {
3665f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  const ImplicitCastExpr *ImplicitCast =
3675f04881eb025f61396d0555d8173730fe2759e0aChris Lattner      dyn_cast_or_null<ImplicitCastExpr>(expr);
3685f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  if (ImplicitCast != NULL) {
3695f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    const Expr *ExprBeforePromotion = ImplicitCast->getSubExpr();
3705f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    QualType TypeBeforePromotion = ExprBeforePromotion->getType();
3715f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    if (TypeBeforePromotion->isIntegralType()) {
3725f04881eb025f61396d0555d8173730fe2759e0aChris Lattner      return TypeBeforePromotion;
3735f04881eb025f61396d0555d8173730fe2759e0aChris Lattner    }
3745f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  }
3755f04881eb025f61396d0555d8173730fe2759e0aChris Lattner  return expr->getType();
3765f04881eb025f61396d0555d8173730fe2759e0aChris Lattner}
3775f04881eb025f61396d0555d8173730fe2759e0aChris Lattner
378de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult
379de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlSema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch,
380de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl                            StmtArg Body) {
381e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson  Stmt *BodyStmt = Body.takeAs<Stmt>();
382de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
383bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  SwitchStmt *SS = getSwitchStack().back();
384de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  assert(SS == (SwitchStmt*)Switch.get() && "switch stack missing push/pop!");
385de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
3869dcbfa450d751bd68fc4af8b75da381d4f6984b9Steve Naroff  SS->setBody(BodyStmt, SwitchLoc);
3871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getSwitchStack().pop_back();
388c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson
389f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  Expr *CondExpr = SS->getCond();
39084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  QualType CondTypeBeforePromotion =
39184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor      GetTypeBeforeIntegralPromotion(CondExpr);
392f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  QualType CondType = CondExpr->getType();
393de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
39484fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  if (getLangOptions().CPlusPlus) {
39584fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor    // C++ 6.4.2.p2:
39684fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor    // The condition shall be of integral type, enumeration type, or of a class
39784fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor    // type for which a single conversion function to integral or enumeration
39884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor    // type exists (12.3). If the condition is of class type, the condition is
39984fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor    // converted by calling that conversion function, and the result of the
40084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor    // conversion is used in place of the original condition for the remainder
40184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor    // of this section. Integral promotions are performed.
40284fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor    if (!CondExpr->isTypeDependent()) {
403f21bac640870db0569e697a8a14f59f6ca069f80Douglas Gregor      // Make sure that the condition expression has a complete type,
404f21bac640870db0569e697a8a14f59f6ca069f80Douglas Gregor      // otherwise we'll never find any conversions.
405f21bac640870db0569e697a8a14f59f6ca069f80Douglas Gregor      if (RequireCompleteType(SwitchLoc, CondType,
406f21bac640870db0569e697a8a14f59f6ca069f80Douglas Gregor                              PDiag(diag::err_switch_incomplete_class_type)
407f21bac640870db0569e697a8a14f59f6ca069f80Douglas Gregor                                << CondExpr->getSourceRange()))
408f21bac640870db0569e697a8a14f59f6ca069f80Douglas Gregor        return StmtError();
409f21bac640870db0569e697a8a14f59f6ca069f80Douglas Gregor
41084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor      llvm::SmallVector<CXXConversionDecl *, 4> ViableConversions;
41184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor      llvm::SmallVector<CXXConversionDecl *, 4> ExplicitConversions;
41284fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor      if (const RecordType *RecordTy = CondType->getAs<RecordType>()) {
41384fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor        const UnresolvedSet *Conversions
41484fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor          = cast<CXXRecordDecl>(RecordTy->getDecl())
41584fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor                           ->getVisibleConversionFunctions();
41684fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor        for (UnresolvedSet::iterator I = Conversions->begin(),
41784fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor               E = Conversions->end(); I != E; ++I) {
41884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor          if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(*I))
41984fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            if (Conversion->getConversionType().getNonReferenceType()
42084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor                  ->isIntegralType()) {
42184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor              if (Conversion->isExplicit())
42284fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor                ExplicitConversions.push_back(Conversion);
42384fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor              else
42484fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor              ViableConversions.push_back(Conversion);
42584fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            }
42684fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor        }
42784fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor
42884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor        switch (ViableConversions.size()) {
42984fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor        case 0:
43084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor          if (ExplicitConversions.size() == 1) {
43184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            // The user probably meant to invoke the given explicit
43284fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            // conversion; use it.
43384fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            QualType ConvTy
43484fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor              = ExplicitConversions[0]->getConversionType()
43584fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor                            .getNonReferenceType();
43684fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            std::string TypeStr;
43784fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            ConvTy.getAsStringInternal(TypeStr, Context.PrintingPolicy);
43884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor
43984fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor
44084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            Diag(SwitchLoc, diag::err_switch_explicit_conversion)
44184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor              << CondType << ConvTy << CondExpr->getSourceRange()
44284fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor              << CodeModificationHint::CreateInsertion(CondExpr->getLocStart(),
44384fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor                                             "static_cast<" + TypeStr + ">(")
44484fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor              << CodeModificationHint::CreateInsertion(
44584fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor                                PP.getLocForEndOfToken(CondExpr->getLocEnd()),
44684fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor                                   ")");
44784fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            Diag(ExplicitConversions[0]->getLocation(),
44884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor                 diag::note_switch_conversion)
44984fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor              << ConvTy->isEnumeralType() << ConvTy;
45084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor
45184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            // If we aren't in a SFINAE context, build a call to the
45284fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            // explicit conversion function.
45384fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            if (!isSFINAEContext())
45484fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor              CondExpr = BuildCXXMemberCallExpr(CondExpr,
45584fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor                                                ExplicitConversions[0]);
45684fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor          }
45784fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor
45884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor          // We'll complain below about a non-integral condition type.
45984fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor          break;
46084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor
46184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor        case 1:
46284fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor          // Apply this conversion.
46384fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor          CondExpr = BuildCXXMemberCallExpr(CondExpr, ViableConversions[0]);
46484fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor          break;
46584fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor
46684fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor        default:
46784fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor          Diag(SwitchLoc, diag::err_switch_multiple_conversions)
46884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            << CondType << CondExpr->getSourceRange();
46984fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor          for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) {
47084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            QualType ConvTy
47184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor              = ViableConversions[I]->getConversionType()
47284fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor                            .getNonReferenceType();
47384fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor            Diag(ViableConversions[I]->getLocation(),
47484fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor                 diag::note_switch_conversion)
47584fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor              << ConvTy->isEnumeralType() << ConvTy;
47684fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor          }
47784fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor          return StmtError();
47884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor        }
47984fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor      }
48084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor      CondType = CondExpr->getType();
48184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor
48284fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor      if (CondType->isIntegralType() || CondType->isEnumeralType()) {
48384fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor        // Integral promotions are performed.
48484fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor        UsualUnaryConversions(CondExpr);
48584fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor      }
48684fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor    }
48784fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  } else {
48884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor    // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
48984fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor    UsualUnaryConversions(CondExpr);
49084fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  }
49184fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor  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()) {
50212356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan    if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
50312356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
50412356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan          << CondType << CondExpr->getSourceRange();
50512356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan      return StmtError();
50612356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan    }
50712356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan
50812356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan    if (CondTypeBeforePromotion->isBooleanType()) {
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.
533f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
5341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
535f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  DefaultStmt *TheDefaultStmt = 0;
5361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
537b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  bool CaseListIsErroneous = false;
5381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
539dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue;
540c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson       SC = SC->getNextSwitchCase()) {
5411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
542c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson    if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
543f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      if (TheDefaultStmt) {
544f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
5455f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner        Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
546de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
547f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        // FIXME: Remove the default statement from the switch block so that
548390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // we'll return a valid AST.  This requires recursing down the AST and
549390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // finding it, not something we are set up to do right now.  For now,
550390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // just lop the entire switch stmt out of the AST.
551b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner        CaseListIsErroneous = true;
552c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson      }
553f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      TheDefaultStmt = DS;
5541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
555f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    } else {
556f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      CaseStmt *CS = cast<CaseStmt>(SC);
5571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
558f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // We already verified that the expression has a i-c-e value (C99
559f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // 6.8.4.2p3) - get that value now.
5601e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      Expr *Lo = CS->getLHS();
561dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
562dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      if (Lo->isTypeDependent() || Lo->isValueDependent()) {
563dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        HasDependentValue = true;
564dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        break;
565dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      }
5661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56751fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson      llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
5681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
569f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // Convert the value to the same width/sign as the condition.
570f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
571f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                         CS->getLHS()->getLocStart(),
572f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                         diag::warn_case_value_overflow);
5736c36be5b383875b490684bcf439d6d427298c1afChris Lattner
5741e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      // If the LHS is not the same type as the condition, insert an implicit
5751e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      // cast.
57673c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman      ImpCastExprToType(Lo, CondType, CastExpr::CK_IntegralCast);
5771e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      CS->setLHS(Lo);
5781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
579b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner      // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
580dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      if (CS->getRHS()) {
5811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (CS->getRHS()->isTypeDependent() ||
582dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            CS->getRHS()->isValueDependent()) {
583dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          HasDependentValue = true;
584dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          break;
585dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
586f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        CaseRanges.push_back(std::make_pair(LoVal, CS));
5871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else
588b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner        CaseVals.push_back(std::make_pair(LoVal, CS));
589f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    }
590f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  }
591b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner
592dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (!HasDependentValue) {
593dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // Sort all the scalar case values so we can easily detect duplicates.
594dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
595dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
596dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    if (!CaseVals.empty()) {
597dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
598dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (CaseVals[i].first == CaseVals[i+1].first) {
599dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          // If we have a duplicate, report it.
600dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
601dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::err_duplicate_case) << CaseVals[i].first.toString(10);
6021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          Diag(CaseVals[i].second->getLHS()->getLocStart(),
603dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::note_duplicate_case_prev);
604390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // FIXME: We really want to remove the bogus case stmt from the
605390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // substmt, but we have no way to do this right now.
606dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseListIsErroneous = true;
607dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6086efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner      }
609b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner    }
6101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
611dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // Detect duplicate case ranges, which usually don't exist at all in
612dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // the first place.
613dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    if (!CaseRanges.empty()) {
614dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Sort all the case ranges by their low value so we can easily detect
615dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // overlaps between ranges.
616dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      std::stable_sort(CaseRanges.begin(), CaseRanges.end());
6171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
618dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Scan the ranges, computing the high values and removing empty ranges.
619dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      std::vector<llvm::APSInt> HiVals;
620dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
621dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *CR = CaseRanges[i].second;
622dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        Expr *Hi = CR->getRHS();
623dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
625dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Convert the value to the same width/sign as the condition.
626dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
627dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                           CR->getRHS()->getLocStart(),
628dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                           diag::warn_case_value_overflow);
6291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
630dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // If the LHS is not the same type as the condition, insert an implicit
631dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // cast.
63273c39abdbb79927605d740c93dd9629e3e4f9bfeEli Friedman        ImpCastExprToType(Hi, CondType, CastExpr::CK_IntegralCast);
633dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CR->setRHS(Hi);
6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
635dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // If the low value is bigger than the high value, the case is empty.
636dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (CaseRanges[i].first > HiVal) {
637dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
638dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            << SourceRange(CR->getLHS()->getLocStart(),
639dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                           CR->getRHS()->getLocEnd());
640dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseRanges.erase(CaseRanges.begin()+i);
641dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          --i, --e;
642dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          continue;
643dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
644dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        HiVals.push_back(HiVal);
6450471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      }
6461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
647dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Rescan the ranges, looking for overlap with singleton values and other
648dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // ranges.  Since the range list is sorted, we only need to compare case
649dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // ranges with their neighbors.
650dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
651dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt &CRLo = CaseRanges[i].first;
652dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt &CRHi = HiVals[i];
653dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *CR = CaseRanges[i].second;
6541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
655dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Check to see whether the case range overlaps with any
656dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // singleton cases.
657dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *OverlapStmt = 0;
658dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt OverlapVal(32);
6591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
660dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Find the smallest value >= the lower bound.  If I is in the
661dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // case range, then we have overlap.
662dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
663dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                                  CaseVals.end(), CRLo,
664dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                                  CaseCompareFunctor());
665dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (I != CaseVals.end() && I->first < CRHi) {
666dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = I->first;   // Found overlap with scalar.
667dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = I->second;
668dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
670dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Find the smallest value bigger than the upper bound.
671dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
672dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
673dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = (I-1)->first;      // Found overlap with scalar.
674dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = (I-1)->second;
675dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
677dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Check to see if this case stmt overlaps with the subsequent
678dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // case range.
679dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (i && CRLo <= HiVals[i-1]) {
680dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = HiVals[i-1];       // Found overlap with range.
681dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = CaseRanges[i-1].second;
682dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
684dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (OverlapStmt) {
685dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          // If we have a duplicate, report it.
686dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
687dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            << OverlapVal.toString(10);
6881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          Diag(OverlapStmt->getLHS()->getLocStart(),
689dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::note_duplicate_case_prev);
690390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // FIXME: We really want to remove the bogus case stmt from the
691390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // substmt, but we have no way to do this right now.
692dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseListIsErroneous = true;
693dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
6940471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      }
695b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner    }
696b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  }
697dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
698390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: If the case list was broken is some way, we don't have a good system
699390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // to patch it up.  Instead, just return the whole substmt as broken.
700b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  if (CaseListIsErroneous)
701de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl    return StmtError();
702de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
703de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  Switch.release();
704de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(SS);
7055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
707f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult
7087f537c18c7029e73f0bd555be3782c066e7e2c1eAnders CarlssonSema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond, StmtArg Body) {
7097f537c18c7029e73f0bd555be3782c066e7e2c1eAnders Carlsson  ExprArg CondArg(Cond.release());
7107f537c18c7029e73f0bd555be3782c066e7e2c1eAnders Carlsson  Expr *condExpr = CondArg.takeAs<Expr>();
7111b273c403734d343d720acb28f04011807c8aa56Steve Naroff  assert(condExpr && "ActOnWhileStmt(): missing expression");
712f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
7135a881bb09928b7ade891efc680088aaad276f8d6John McCall  if (CheckBooleanCondition(condExpr, WhileLoc)) {
7147f537c18c7029e73f0bd555be3782c066e7e2c1eAnders Carlsson    CondArg = condExpr;
7155a881bb09928b7ade891efc680088aaad276f8d6John McCall    return StmtError();
7164a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor  }
7175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
718754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  Stmt *bodyStmt = Body.takeAs<Stmt>();
719754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(bodyStmt);
7201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7217f537c18c7029e73f0bd555be3782c066e7e2c1eAnders Carlsson  CondArg.release();
722754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  return Owned(new (Context) WhileStmt(condExpr, bodyStmt, WhileLoc));
7235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
725f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult
726f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body,
727989135901c750af61ef012b6b0a0368be415bc46Chris Lattner                  SourceLocation WhileLoc, SourceLocation CondLParen,
728989135901c750af61ef012b6b0a0368be415bc46Chris Lattner                  ExprArg Cond, SourceLocation CondRParen) {
729e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson  Expr *condExpr = Cond.takeAs<Expr>();
7301b273c403734d343d720acb28f04011807c8aa56Steve Naroff  assert(condExpr && "ActOnDoStmt(): missing expression");
731f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
7325a881bb09928b7ade891efc680088aaad276f8d6John McCall  if (CheckBooleanCondition(condExpr, DoLoc)) {
7339f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor    Cond = condExpr;
7345a881bb09928b7ade891efc680088aaad276f8d6John McCall    return StmtError();
7359f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor  }
7365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
737754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  Stmt *bodyStmt = Body.takeAs<Stmt>();
738754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(bodyStmt);
739754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
740f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Cond.release();
741754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  return Owned(new (Context) DoStmt(bodyStmt, condExpr, DoLoc,
742989135901c750af61ef012b6b0a0368be415bc46Chris Lattner                                    WhileLoc, CondRParen));
7435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
745f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult
746f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
747f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                   StmtArg first, ExprArg second, ExprArg third,
748f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                   SourceLocation RParenLoc, StmtArg body) {
749f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Stmt *First  = static_cast<Stmt*>(first.get());
7505a881bb09928b7ade891efc680088aaad276f8d6John McCall  Expr *Second = second.takeAs<Expr>();
751f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Expr *Third  = static_cast<Expr*>(third.get());
752f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Stmt *Body  = static_cast<Stmt*>(body.get());
753f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
7545921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  if (!getLangOptions().CPlusPlus) {
7555921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
756f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // C99 6.8.5p3: The declaration part of a 'for' statement shall only
757f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // declare identifiers for objects having storage class 'auto' or
758f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // 'register'.
7595921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis      for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
7605921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis           DI!=DE; ++DI) {
7615921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        VarDecl *VD = dyn_cast<VarDecl>(*DI);
7625921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
7635921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis          VD = 0;
7645921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        if (VD == 0)
7655921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis          Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
7665921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        // FIXME: mark decl erroneous!
7675921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis      }
768ae3b701f59e78e058b83344be17206af3bf5d277Chris Lattner    }
7695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7705a881bb09928b7ade891efc680088aaad276f8d6John McCall  if (Second && CheckBooleanCondition(Second, ForLoc)) {
7715a881bb09928b7ade891efc680088aaad276f8d6John McCall    second = Second;
7725a881bb09928b7ade891efc680088aaad276f8d6John McCall    return StmtError();
7735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7753af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson  DiagnoseUnusedExprResult(First);
7763af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson  DiagnoseUnusedExprResult(Third);
777754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(Body);
778754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
779f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  first.release();
780f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  third.release();
781f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  body.release();
7825831c6a1efc47e6a19d82fe3dd25b5b8fef6979dDouglas Gregor  return Owned(new (Context) ForStmt(First, Second, Third, Body, ForLoc,
7835831c6a1efc47e6a19d82fe3dd25b5b8fef6979dDouglas Gregor                                     LParenLoc, RParenLoc));
7845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
786f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult
787f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
788f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                                 SourceLocation LParenLoc,
789f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                                 StmtArg first, ExprArg second,
790f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                                 SourceLocation RParenLoc, StmtArg body) {
791f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Stmt *First  = static_cast<Stmt*>(first.get());
792f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Expr *Second = static_cast<Expr*>(second.get());
793f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Stmt *Body  = static_cast<Stmt*>(body.get());
79420552d2842245692b649e0d25380670922f954a2Fariborz Jahanian  if (First) {
79520552d2842245692b649e0d25380670922f954a2Fariborz Jahanian    QualType FirstType;
79620552d2842245692b649e0d25380670922f954a2Fariborz Jahanian    if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
7977e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner      if (!DS->isSingleDecl())
798f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag((*DS->decl_begin())->getLocation(),
799f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                         diag::err_toomany_element_decls));
800f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
8017e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner      Decl *D = DS->getSingleDecl();
802f34afeed9a0112bf31fee185b6c80556111d3834Ted Kremenek      FirstType = cast<ValueDecl>(D)->getType();
803f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // C99 6.8.5p3: The declaration part of a 'for' statement shall only
804f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // declare identifiers for objects having storage class 'auto' or
805f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // 'register'.
806248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff      VarDecl *VD = cast<VarDecl>(D);
807248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff      if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
808f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag(VD->getLocation(),
809f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                              diag::err_non_variable_decl_in_for));
8101fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson    } else {
811810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      if (cast<Expr>(First)->isLvalue(Context) != Expr::LV_Valid)
812f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag(First->getLocStart(),
813f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                   diag::err_selector_element_not_lvalue)
814f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl          << First->getSourceRange());
8151fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson
8161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FirstType = static_cast<Expr*>(First)->getType();
8171fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson    }
8181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (!FirstType->isObjCObjectPointerType() &&
819a5e42a82ce055f29f3733f3a1f10da6cb9877deeFariborz Jahanian        !FirstType->isBlockPointerType())
820dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        Diag(ForLoc, diag::err_selector_element_type)
821d162584991885ab004a02573a73ce06422b921fcChris Lattner          << FirstType << First->getSourceRange();
8223ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  }
8233ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  if (Second) {
8243ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian    DefaultFunctionArrayConversion(Second);
8253ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian    QualType SecondType = Second->getType();
826f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    if (!SecondType->isObjCObjectPointerType())
827dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      Diag(ForLoc, diag::err_collection_expr_type)
828d162584991885ab004a02573a73ce06422b921fcChris Lattner        << SecondType << Second->getSourceRange();
8293ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  }
830f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  first.release();
831f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  second.release();
832f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  body.release();
8338189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body,
8348189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                                                   ForLoc, RParenLoc));
8353ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian}
8365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8374cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
8381b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
8395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                    IdentifierInfo *LabelII) {
8404eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // If we are in a block, reject all gotos for now.
8414eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  if (CurBlock)
8424cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(GotoLoc, diag::err_goto_in_block));
8434eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff
8445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Look up the record for this label identifier.
845ea29a3a0d6948c4a51a261d19ec1a585d2a9c779Chris Lattner  LabelStmt *&LabelDecl = getLabelMap()[LabelII];
8465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
847caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  // If we haven't seen this label yet, create a forward reference.
848caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  if (LabelDecl == 0)
8498189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0);
8504cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
8518189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc));
8525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8544cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
855ad56d684259f706b7c0ae5ad9c23adb4f2926817Chris LattnerSema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc,
8564cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl                            ExprArg DestExp) {
857bbf462314b1dc8e422b7c4dd4cac47e566aedf6dEli Friedman  // Convert operand to void*
85833083823342649b1fccec856c1f239c09fc0d7e1Eli Friedman  Expr* E = DestExp.takeAs<Expr>();
8595f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  if (!E->isTypeDependent()) {
8605f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    QualType ETy = E->getType();
8615f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    AssignConvertType ConvTy =
8625f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor      CheckSingleAssignmentConstraints(Context.VoidPtrTy, E);
8635f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    if (DiagnoseAssignmentResult(ConvTy, StarLoc, Context.VoidPtrTy, ETy,
8645f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor                                 E, "passing"))
8655f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor      return StmtError();
8665f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  }
8675f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  return Owned(new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E));
8685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8704cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
8711b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
8725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Scope *S = CurScope->getContinueParent();
8735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!S) {
8745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
8754cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8774cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
8788189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ContinueStmt(ContinueLoc));
8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8814cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
8821b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
8835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Scope *S = CurScope->getBreakParent();
8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!S) {
8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
8864cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
8875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8884cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
8898189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) BreakStmt(BreakLoc));
8905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
89227c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
8934eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
8944cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
8954eb206bebcdab28ababe8df55c6185cec2cdc071Steve NaroffSema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
8964eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // If this is the first return we've seen in the block, infer the type of
8974eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // the block from it.
8987d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian  if (CurBlock->ReturnType.isNull()) {
899c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff    if (RetValExp) {
90016564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      // Don't call UsualUnaryConversions(), since we don't want to do
90116564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      // integer promotions here.
90216564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      DefaultFunctionArrayConversion(RetValExp);
9037d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      CurBlock->ReturnType = RetValExp->getType();
9047d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(RetValExp)) {
9057d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // We have to remove a 'const' added to copied-in variable which was
9067d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // part of the implementation spec. and not the actual qualifier for
9077d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // the variable.
9087d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        if (CDRE->isConstQualAdded())
9097d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian           CurBlock->ReturnType.removeConst();
9107d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      }
911c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff    } else
9127d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      CurBlock->ReturnType = Context.VoidTy;
9134eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  }
9147d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian  QualType FnRetType = CurBlock->ReturnType;
9154cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
91640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (CurBlock->TheDecl->hasAttr<NoReturnAttr>()) {
9176c92fa75e62937f9738696840efcb258560f4568Mike Stump    Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr)
9186c92fa75e62937f9738696840efcb258560f4568Mike Stump      << getCurFunctionOrMethodDecl()->getDeclName();
9196c92fa75e62937f9738696840efcb258560f4568Mike Stump    return StmtError();
9206c92fa75e62937f9738696840efcb258560f4568Mike Stump  }
9216c92fa75e62937f9738696840efcb258560f4568Mike Stump
9224eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // Otherwise, verify that this result type matches the previous one.  We are
9234eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // pickier with blocks than for normal functions because we don't have GCC
9244eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // compatibility to worry about here.
9254eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  if (CurBlock->ReturnType->isVoidType()) {
9264eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    if (RetValExp) {
9274eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff      Diag(ReturnLoc, diag::err_return_block_has_expr);
9288189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek      RetValExp->Destroy(Context);
9294eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff      RetValExp = 0;
9304eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    }
9318189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
9324eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  }
9334cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
9344cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl  if (!RetValExp)
9354cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr));
9364cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
93798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
93898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // we have a non-void block with an expression, continue checking
93998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    QualType RetValType = RetValExp->getType();
94098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
9411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // C99 6.8.6.4p3(136): The return statement is not an assignment. The
9421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // overlap restriction of subclause 6.5.16.1 does not apply to the case of
94398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // function return.
9444cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
94598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // In C++ the return statement is handled via a copy initialization.
94698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // the C version of which boils down to CheckSingleAssignmentConstraints.
94798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // FIXME: Leaks RetValExp.
94898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
94998eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      return StmtError();
95098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
95198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
95298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  }
9534cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
9548189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
9554eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
9565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
957e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl/// IsReturnCopyElidable - Whether returning @p RetExpr from a function that
958e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl/// returns a @p RetType fulfills the criteria for copy elision (C++0x 12.8p15).
959e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redlstatic bool IsReturnCopyElidable(ASTContext &Ctx, QualType RetType,
960e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl                                 Expr *RetExpr) {
961e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  QualType ExprType = RetExpr->getType();
962e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // - in a return statement in a function with ...
963e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // ... a class return type ...
964e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  if (!RetType->isRecordType())
965e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    return false;
966e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // ... the same cv-unqualified type as the function return type ...
967a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (!Ctx.hasSameUnqualifiedType(RetType, ExprType))
968e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    return false;
969e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // ... the expression is the name of a non-volatile automatic object ...
970e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // We ignore parentheses here.
971e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // FIXME: Is this compliant?
972e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetExpr->IgnoreParens());
973e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  if (!DR)
974e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    return false;
975e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
976e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  if (!VD)
977e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    return false;
978e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  return VD->hasLocalStorage() && !VD->getType()->isReferenceType()
979e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    && !VD->getType().isVolatileQualified();
980e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl}
981e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl
9824cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
983f53b4433abb7a3bc14c0329d3175cbc291280137Anders CarlssonSema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) {
984f53b4433abb7a3bc14c0329d3175cbc291280137Anders Carlsson  Expr *RetValExp = rex.takeAs<Expr>();
9854eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  if (CurBlock)
9864eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
9874cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
988371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  QualType FnRetType;
989f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump  if (const FunctionDecl *FD = getCurFunctionDecl()) {
990371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner    FnRetType = FD->getResultType();
99140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (FD->hasAttr<NoReturnAttr>())
9928662587fa75d3fb04f873e265841c9314c7f5523Chris Lattner      Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr)
993f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump        << getCurFunctionOrMethodDecl()->getDeclName();
994f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump  } else if (ObjCMethodDecl *MD = getCurMethodDecl())
995c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff    FnRetType = MD->getResultType();
996c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff  else // If we don't have a function/method context, bail.
997c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff    return StmtError();
9981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9995cf216b7fa64b933b60743b0b26053e8e7aa87beChris Lattner  if (FnRetType->isVoidType()) {
10001be8aee8745e8b814ad2f151aa214b0ef07833dbDouglas Gregor    if (RetValExp && !RetValExp->isTypeDependent()) {
10011be8aee8745e8b814ad2f151aa214b0ef07833dbDouglas Gregor      // C99 6.8.6.4p1 (ext_ since GCC warns)
100265ce04bef06696379682410f399f37b43996d824Chris Lattner      unsigned D = diag::ext_return_has_expr;
100365ce04bef06696379682410f399f37b43996d824Chris Lattner      if (RetValExp->getType()->isVoidType())
100465ce04bef06696379682410f399f37b43996d824Chris Lattner        D = diag::ext_return_has_void_expr;
10054cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1006e878eb035b343d7d819c092102364ec9849716aeChris Lattner      // return (some void expression); is legal in C++.
1007e878eb035b343d7d819c092102364ec9849716aeChris Lattner      if (D != diag::ext_return_has_void_expr ||
1008e878eb035b343d7d819c092102364ec9849716aeChris Lattner          !getLangOptions().CPlusPlus) {
1009e878eb035b343d7d819c092102364ec9849716aeChris Lattner        NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
1010e878eb035b343d7d819c092102364ec9849716aeChris Lattner        Diag(ReturnLoc, D)
1011e878eb035b343d7d819c092102364ec9849716aeChris Lattner          << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
1012e878eb035b343d7d819c092102364ec9849716aeChris Lattner          << RetValExp->getSourceRange();
1013e878eb035b343d7d819c092102364ec9849716aeChris Lattner      }
10141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1015f53b4433abb7a3bc14c0329d3175cbc291280137Anders Carlsson      RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp, true);
10165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
10178189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
10185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10194cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
102003d77760a5db7990724b6901cea958a673ce0b39Anders Carlsson  if (!RetValExp && !FnRetType->isDependentType()) {
10213c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    unsigned DiagID = diag::warn_return_missing_expr;  // C90 6.6.6.4p4
10223c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    // C99 6.8.6.4p1 (ext_ since GCC warns)
10233c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
10243c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner
10253c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    if (FunctionDecl *FD = getCurFunctionDecl())
102608631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
10273c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    else
102808631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
10298189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return Owned(new (Context) ReturnStmt(ReturnLoc, (Expr*)0));
10303c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  }
10314cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1032898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
1033898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    // we have a non-void function with an expression, continue checking
10344cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
10351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // C99 6.8.6.4p3(136): The return statement is not an assignment. The
10361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // overlap restriction of subclause 6.5.16.1 does not apply to the case of
10374cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    // function return.
10384cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
1039e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // C++0x 12.8p15: When certain criteria are met, an implementation is
1040e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   allowed to omit the copy construction of a class object, [...]
1041e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   - in a return statement in a function with a class return type, when
1042e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //     the expression is the name of a non-volatile automatic object with
1043e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //     the same cv-unqualified type as the function return type, the copy
1044e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //     operation can be omitted [...]
1045e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // C++0x 12.8p16: When the criteria for elision of a copy operation are met
1046e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   and the object to be copied is designated by an lvalue, overload
1047e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   resolution to select the constructor for the copy is first performed
1048e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   as if the object were designated by an rvalue.
1049e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // Note that we only compute Elidable if we're in C++0x, since we don't
1050e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // care otherwise.
1051e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    bool Elidable = getLangOptions().CPlusPlus0x ?
1052e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl                      IsReturnCopyElidable(Context, FnRetType, RetValExp) :
1053e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl                      false;
1054e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl
1055898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    // In C++ the return statement is handled via a copy initialization.
10564cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    // the C version of which boils down to CheckSingleAssignmentConstraints.
1057e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // FIXME: Leaks RetValExp on error.
105866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    if (PerformCopyInitialization(RetValExp, FnRetType, "returning", Elidable)){
105966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor      // We should still clean up our temporaries, even when we're failing!
106066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor      RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp, true);
10614cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl      return StmtError();
106266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    }
106366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
1064898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
1065898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  }
1066898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1067f53b4433abb7a3bc14c0329d3175cbc291280137Anders Carlsson  if (RetValExp)
1068f53b4433abb7a3bc14c0329d3175cbc291280137Anders Carlsson    RetValExp = MaybeCreateCXXExprWithTemporaries(RetValExp, true);
10698189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1072810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently
1073810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// ignore "noop" casts in places where an lvalue is required by an inline asm.
1074810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// We emulate this behavior when -fheinous-gnu-extensions is specified, but
1075810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// provide a strong guidance to not use it.
1076810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner///
1077810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// This method checks to see if the argument is an acceptable l-value and
1078810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// returns false if it is a case we can handle.
1079810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattnerstatic bool CheckAsmLValue(const Expr *E, Sema &S) {
1080810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  if (E->isLvalue(S.Context) == Expr::LV_Valid)
1081810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    return false;  // Cool, this is an lvalue.
1082810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1083810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // Okay, this is not an lvalue, but perhaps it is the result of a cast that we
1084810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // are supposed to allow.
1085810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  const Expr *E2 = E->IgnoreParenNoopCasts(S.Context);
1086810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  if (E != E2 && E2->isLvalue(S.Context) == Expr::LV_Valid) {
1087810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    if (!S.getLangOptions().HeinousExtensions)
1088810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue)
1089810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner        << E->getSourceRange();
1090810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    else
1091810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue)
1092810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner        << E->getSourceRange();
1093810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    // Accept, even if we emitted an error diagnostic.
1094810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    return false;
1095810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  }
1096810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1097810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // None of the above, just randomly invalid non-lvalue.
1098810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  return true;
1099810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner}
1100810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
1101810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
11023037ed0a27dba62e522304183718efc149e8b6d9Sebastian RedlSema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
11033037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          bool IsSimple,
11043037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          bool IsVolatile,
11053037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          unsigned NumOutputs,
11063037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          unsigned NumInputs,
11073037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          std::string *Names,
11083037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg constraints,
11093037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg exprs,
11103037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          ExprArg asmString,
11113037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg clobbers,
11123037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          SourceLocation RParenLoc) {
11133037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  unsigned NumClobbers = clobbers.size();
11143037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral **Constraints =
11153037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl    reinterpret_cast<StringLiteral**>(constraints.get());
11163037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  Expr **Exprs = reinterpret_cast<Expr **>(exprs.get());
11173037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString.get());
11183037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get());
11193037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
112003eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson  llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
11211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11221708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  // The parser verifies that there is a string literal here.
11236bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner  if (AsmString->isWide())
11243037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl    return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
11253037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      << AsmString->getSourceRange());
11263037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
11271708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  for (unsigned i = 0; i != NumOutputs; i++) {
11281708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Constraints[i];
11296bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
11303037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
11313037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
11323037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
11331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    TargetInfo::ConstraintInfo Info(Literal->getStrData(),
11342819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                    Literal->getByteLength(),
11352819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                    Names[i]);
1136432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner    if (!Context.Target.validateOutputConstraint(Info))
11373037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
1138432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                            diag::err_asm_invalid_output_constraint)
1139432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                       << Info.getConstraintStr());
11403037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1141d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson    // Check that the output exprs are valid lvalues.
114272056a237c536ee63285ab0850cb50f299281767Eli Friedman    Expr *OutputExpr = Exprs[i];
1143810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    if (CheckAsmLValue(OutputExpr, *this)) {
114472056a237c536ee63285ab0850cb50f299281767Eli Friedman      return StmtError(Diag(OutputExpr->getLocStart(),
1145dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                  diag::err_asm_invalid_lvalue_in_output)
114672056a237c536ee63285ab0850cb50f299281767Eli Friedman        << OutputExpr->getSourceRange());
114704728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson    }
11481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
114944def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    OutputConstraintInfos.push_back(Info);
115004728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  }
11513037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1152806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  llvm::SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
1153806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner
115404728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
11551708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Constraints[i];
11566bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
11573037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
11583037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
11593037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
11601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    TargetInfo::ConstraintInfo Info(Literal->getStrData(),
11612819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                    Literal->getByteLength(),
11622819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                    Names[i]);
1163beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    if (!Context.Target.validateInputConstraint(OutputConstraintInfos.data(),
11642819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                                NumOutputs, Info)) {
11653037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
1166432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                            diag::err_asm_invalid_input_constraint)
1167432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                       << Info.getConstraintStr());
1168d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson    }
11693037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
117072056a237c536ee63285ab0850cb50f299281767Eli Friedman    Expr *InputExpr = Exprs[i];
11713037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1172d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson    // Only allow void types for memory constraints.
117344def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.allowsMemory() && !Info.allowsRegister()) {
1174810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      if (CheckAsmLValue(InputExpr, *this))
117572056a237c536ee63285ab0850cb50f299281767Eli Friedman        return StmtError(Diag(InputExpr->getLocStart(),
1176d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson                              diag::err_asm_invalid_lvalue_in_input)
1177432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                         << Info.getConstraintStr()
117872056a237c536ee63285ab0850cb50f299281767Eli Friedman                         << InputExpr->getSourceRange());
117904728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson    }
11803037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
118144def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.allowsRegister()) {
1182d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson      if (InputExpr->getType()->isVoidType()) {
118372056a237c536ee63285ab0850cb50f299281767Eli Friedman        return StmtError(Diag(InputExpr->getLocStart(),
1184d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson                              diag::err_asm_invalid_type_in_input)
11851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          << InputExpr->getType() << Info.getConstraintStr()
118672056a237c536ee63285ab0850cb50f299281767Eli Friedman          << InputExpr->getSourceRange());
1187d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson      }
1188d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson    }
11891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11906032979a5d091e5607c7c467400449727d9beb5aAnders Carlsson    DefaultFunctionArrayConversion(Exprs[i]);
11911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1192806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    InputConstraintInfos.push_back(Info);
119304728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  }
11943037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
11956fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson  // Check that the clobbers are valid.
11961708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  for (unsigned i = 0; i != NumClobbers; i++) {
11971708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Clobbers[i];
11986bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
11993037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
12003037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
12013037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
12027765934ad7e157b5fcf925792a38e01b1edbcf8aDaniel Dunbar    std::string Clobber(Literal->getStrData(),
12037765934ad7e157b5fcf925792a38e01b1edbcf8aDaniel Dunbar                        Literal->getStrData() +
12047765934ad7e157b5fcf925792a38e01b1edbcf8aDaniel Dunbar                        Literal->getByteLength());
12053037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
12066bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
12073037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
12087765934ad7e157b5fcf925792a38e01b1edbcf8aDaniel Dunbar                  diag::err_asm_unknown_register_name) << Clobber);
12096fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson  }
12103037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
12113037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  constraints.release();
12123037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  exprs.release();
12133037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  asmString.release();
12143037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  clobbers.release();
1215fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  AsmStmt *NS =
1216fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner    new (Context) AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs,
1217fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner                          Names, Constraints, Exprs, AsmString, NumClobbers,
1218fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner                          Clobbers, RParenLoc);
1219fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  // Validate the asm string, ensuring it makes sense given the operands we
1220fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  // have.
1221fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  llvm::SmallVector<AsmStmt::AsmStringPiece, 8> Pieces;
1222fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  unsigned DiagOffs;
1223fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
12242ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner    Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
12252ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner           << AsmString->getSourceRange();
1226fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner    DeleteStmt(NS);
1227fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner    return StmtError();
1228fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  }
12291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1230806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  // Validate tied input operands for type mismatches.
1231806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) {
1232806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
12331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1234806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // If this is a tied constraint, verify that the output and input have
1235806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // either exactly the same type, or that they are int/ptr operands with the
1236806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // same size (int/long, int*/long, are ok etc).
1237806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    if (!Info.hasTiedOperand()) continue;
12381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1239806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    unsigned TiedTo = Info.getTiedOperand();
1240f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner    Expr *OutputExpr = Exprs[TiedTo];
1241c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner    Expr *InputExpr = Exprs[i+NumOutputs];
12427adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    QualType InTy = InputExpr->getType();
12437adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    QualType OutTy = OutputExpr->getType();
12447adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    if (Context.hasSameType(InTy, OutTy))
1245806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner      continue;  // All types can be tied to themselves.
12461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12477adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    // Int/ptr operands have some special cases that we allow.
12487adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    if ((OutTy->isIntegerType() || OutTy->isPointerType()) &&
12497adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner        (InTy->isIntegerType() || InTy->isPointerType())) {
12501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12517adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      // They are ok if they are the same size.  Tying void* to int is ok if
12527adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      // they are the same size, for example.  This also allows tying void* to
12537adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      // int*.
12543351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      uint64_t OutSize = Context.getTypeSize(OutTy);
12553351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      uint64_t InSize = Context.getTypeSize(InTy);
12563351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      if (OutSize == InSize)
1257806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner        continue;
12581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12593351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // If the smaller input/output operand is not mentioned in the asm string,
12603351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // then we can promote it and the asm string won't notice.  Check this
1261f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner      // case now.
12623351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      bool SmallerValueMentioned = false;
126358bce89d12b1dfba16637ce3754d6cb24c3099bbChris Lattner      for (unsigned p = 0, e = Pieces.size(); p != e; ++p) {
126458bce89d12b1dfba16637ce3754d6cb24c3099bbChris Lattner        AsmStmt::AsmStringPiece &Piece = Pieces[p];
126558bce89d12b1dfba16637ce3754d6cb24c3099bbChris Lattner        if (!Piece.isOperand()) continue;
12661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12673351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        // If this is a reference to the input and if the input was the smaller
12683351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        // one, then we have to reject this asm.
12693351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        if (Piece.getOperandNo() == i+NumOutputs) {
12703351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner          if (InSize < OutSize) {
12713351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner            SmallerValueMentioned = true;
12723351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner            break;
12733351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner          }
12743351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        }
12753351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner
12763351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        // If this is a reference to the input and if the input was the smaller
12773351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        // one, then we have to reject this asm.
12783351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        if (Piece.getOperandNo() == TiedTo) {
12793351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner          if (InSize > OutSize) {
12803351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner            SmallerValueMentioned = true;
12813351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner            break;
12823351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner          }
12833351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        }
1284f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner      }
12851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12863351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // If the smaller value wasn't mentioned in the asm string, and if the
12873351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // output was a register, just extend the shorter one to the size of the
12883351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // larger one.
12893351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      if (!SmallerValueMentioned &&
1290f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner          OutputConstraintInfos[TiedTo].allowsRegister())
1291f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner        continue;
1292806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    }
12931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1294c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner    Diag(InputExpr->getLocStart(),
1295806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner         diag::err_asm_tying_incompatible_types)
12967adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      << InTy << OutTy << OutputExpr->getSourceRange()
1297806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner      << InputExpr->getSourceRange();
1298806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    DeleteStmt(NS);
1299806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    return StmtError();
1300806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  }
13011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1302fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  return Owned(NS);
1303fe795956194141c91ae555985c9b930595bff43fChris Lattner}
13043b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian
1305431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
1306431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
1307b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                           SourceLocation RParen, DeclPtrTy Parm,
1308431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl                           StmtArg Body, StmtArg catchList) {
1309f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Stmt *CatchList = catchList.takeAs<Stmt>();
1310b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  ParmVarDecl *PVD = cast_or_null<ParmVarDecl>(Parm.getAs<Decl>());
13111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1312f50cb369273c6bd26c9629df92ee53f1d8af4149Steve Naroff  // PVD == 0 implies @catch(...).
13139d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff  if (PVD) {
131493c4945c9ead2d374fe3fc528e3017c7167265beChris Lattner    // If we already know the decl is invalid, reject it.
131593c4945c9ead2d374fe3fc528e3017c7167265beChris Lattner    if (PVD->isInvalidDecl())
131693c4945c9ead2d374fe3fc528e3017c7167265beChris Lattner      return StmtError();
13171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1318f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    if (!PVD->getType()->isObjCObjectPointerType())
13191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return StmtError(Diag(PVD->getLocation(),
13209d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff                       diag::err_catch_param_not_objc_type));
13219d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff    if (PVD->getType()->isObjCQualifiedIdType())
13221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return StmtError(Diag(PVD->getLocation(),
1323d198abae5938262e501a409e833bf01ca8b8253eSteve Naroff                       diag::err_illegal_qualifiers_on_catch_parm));
13249d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff  }
132593c4945c9ead2d374fe3fc528e3017c7167265beChris Lattner
13268189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  ObjCAtCatchStmt *CS = new (Context) ObjCAtCatchStmt(AtLoc, RParen,
1327e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson    PVD, Body.takeAs<Stmt>(), CatchList);
1328431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl  return Owned(CatchList ? CatchList : CS);
13293b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian}
13303b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian
1331431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
1332431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) {
13338189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ObjCAtFinallyStmt(AtLoc,
13348189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                                           static_cast<Stmt*>(Body.release())));
1335161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian}
1336bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
1337431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
1338431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
1339431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl                         StmtArg Try, StmtArg Catch, StmtArg Finally) {
134038c5ebd7b1b65304c7b5c7b9bf3f9162df22e77dChris Lattner  CurFunctionNeedsScopeChecking = true;
1341e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson  return Owned(new (Context) ObjCAtTryStmt(AtLoc, Try.takeAs<Stmt>(),
1342e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                           Catch.takeAs<Stmt>(),
1343e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                           Finally.takeAs<Stmt>()));
1344bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian}
1345bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
1346431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
13473dcfe10a6801eb52f4c20f1242bea0a3a98aa146Steve NaroffSema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg expr,Scope *CurScope) {
1348f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Expr *ThrowExpr = expr.takeAs<Expr>();
13497151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff  if (!ThrowExpr) {
1350e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    // @throw without an expression designates a rethrow (which much occur
1351e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    // in the context of an @catch clause).
1352e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    Scope *AtCatchParent = CurScope;
1353e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    while (AtCatchParent && !AtCatchParent->isAtCatchScope())
1354e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff      AtCatchParent = AtCatchParent->getParent();
1355e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    if (!AtCatchParent)
13564ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff      return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch));
13577151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff  } else {
13587151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff    QualType ThrowType = ThrowExpr->getType();
13597151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff    // Make sure the expression type is an ObjC pointer or "void *".
1360f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    if (!ThrowType->isObjCObjectPointerType()) {
13616217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const PointerType *PT = ThrowType->getAs<PointerType>();
13627151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff      if (!PT || !PT->getPointeeType()->isVoidType())
13634ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff        return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object)
13644ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff                        << ThrowExpr->getType() << ThrowExpr->getSourceRange());
13657151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff    }
13667151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff  }
13677151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff  return Owned(new (Context) ObjCAtThrowStmt(AtLoc, ThrowExpr));
136839f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian}
1369bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
1370431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
1371431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr,
1372431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl                                  StmtArg SynchBody) {
137346c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner  CurFunctionNeedsScopeChecking = true;
137446c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner
1375a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  // Make sure the expression type is an ObjC pointer or "void *".
1376a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  Expr *SyncExpr = static_cast<Expr*>(SynchExpr.get());
1377f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff  if (!SyncExpr->getType()->isObjCObjectPointerType()) {
13786217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    const PointerType *PT = SyncExpr->getType()->getAs<PointerType>();
1379a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner    if (!PT || !PT->getPointeeType()->isVoidType())
1380a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner      return StmtError(Diag(AtLoc, diag::error_objc_synchronized_expects_object)
1381a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner                       << SyncExpr->getType() << SyncExpr->getSourceRange());
1382a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  }
13831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc,
1385e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                                    SynchExpr.takeAs<Stmt>(),
1386e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                                    SynchBody.takeAs<Stmt>()));
1387fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian}
13884b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl
13894b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
13904b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// and creates a proper catch handler from them.
13914b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian RedlAction::OwningStmtResult
1392b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerSema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclPtrTy ExDecl,
13934b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl                         StmtArg HandlerBlock) {
13944b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl  // There's nothing to test that ActOnExceptionDecl didn't already test.
13958189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CXXCatchStmt(CatchLoc,
1396b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                  cast_or_null<VarDecl>(ExDecl.getAs<Decl>()),
1397e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                          HandlerBlock.takeAs<Stmt>()));
13984b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl}
13998351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
1400c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlclass TypeWithHandler {
1401c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  QualType t;
1402c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  CXXCatchStmt *stmt;
1403c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlpublic:
1404c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  TypeWithHandler(const QualType &type, CXXCatchStmt *statement)
1405c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  : t(type), stmt(statement) {}
1406c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
14070953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // An arbitrary order is fine as long as it places identical
14080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // types next to each other.
1409c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  bool operator<(const TypeWithHandler &y) const {
14100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (t.getAsOpaquePtr() < y.t.getAsOpaquePtr())
1411c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return true;
14120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (t.getAsOpaquePtr() > y.t.getAsOpaquePtr())
1413c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return false;
1414c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    else
1415c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return getTypeSpecStartLoc() < y.getTypeSpecStartLoc();
1416c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
14171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1418c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  bool operator==(const TypeWithHandler& other) const {
14190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return t == other.t;
1420c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
14211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1422c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  QualType getQualType() const { return t; }
1423c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  CXXCatchStmt *getCatchStmt() const { return stmt; }
1424c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  SourceLocation getTypeSpecStartLoc() const {
1425c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    return stmt->getExceptionDecl()->getTypeSpecStartLoc();
1426c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1427c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl};
1428c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
14298351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
14308351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// handlers and creates a try statement from them.
14318351da06ca3082dfd49dd8e3c1785a986920f57cSebastian RedlAction::OwningStmtResult
14328351da06ca3082dfd49dd8e3c1785a986920f57cSebastian RedlSema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock,
14338351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl                       MultiStmtArg RawHandlers) {
14348351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  unsigned NumHandlers = RawHandlers.size();
14358351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  assert(NumHandlers > 0 &&
14368351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl         "The parser shouldn't call this if there are no handlers.");
14378351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get());
14388351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
1439c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  llvm::SmallVector<TypeWithHandler, 8> TypesWithHandlers;
14401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (unsigned i = 0; i < NumHandlers; ++i) {
14428351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl    CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
1443c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    if (!Handler->getExceptionDecl()) {
1444c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      if (i < NumHandlers - 1)
1445c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        return StmtError(Diag(Handler->getLocStart(),
1446c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl                              diag::err_early_catch_all));
14471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1448c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      continue;
1449c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    }
14501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1451c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    const QualType CaughtType = Handler->getCaughtType();
1452c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    const QualType CanonicalCaughtType = Context.getCanonicalType(CaughtType);
1453c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    TypesWithHandlers.push_back(TypeWithHandler(CanonicalCaughtType, Handler));
1454c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1455c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1456c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  // Detect handlers for the same type as an earlier one.
1457c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  if (NumHandlers > 1) {
1458c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    llvm::array_pod_sort(TypesWithHandlers.begin(), TypesWithHandlers.end());
14591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1460c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    TypeWithHandler prev = TypesWithHandlers[0];
1461c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    for (unsigned i = 1; i < TypesWithHandlers.size(); ++i) {
1462c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      TypeWithHandler curr = TypesWithHandlers[i];
14631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1464c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      if (curr == prev) {
1465c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        Diag(curr.getTypeSpecStartLoc(),
1466c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl             diag::warn_exception_caught_by_earlier_handler)
1467c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl          << curr.getCatchStmt()->getCaughtType().getAsString();
1468c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        Diag(prev.getTypeSpecStartLoc(),
1469c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl             diag::note_previous_exception_handler)
1470c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl          << prev.getCatchStmt()->getCaughtType().getAsString();
1471c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      }
14721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1473c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      prev = curr;
1474c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    }
14758351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  }
14761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14778351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // FIXME: We should detect handlers that cannot catch anything because an
14788351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // earlier handler catches a superclass. Need to find a method that is not
14798351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // quadratic for this.
14808351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // Neither of these are explicitly forbidden, but every compiler detects them
14818351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // and warns.
14828351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
1483972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl  CurFunctionNeedsScopeChecking = true;
14848351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  RawHandlers.release();
14858189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CXXTryStmt(TryLoc,
14868189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                                        static_cast<Stmt*>(TryBlock.release()),
14878189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                                        Handlers, NumHandlers));
14888351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl}
1489