SemaStmt.cpp revision 754431107b61a0523df5271c2876a73dd5a051e9
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"
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/Expr.h"
1916f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h"
2016f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtCXX.h"
216fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson#include "clang/Basic/TargetInfo.h"
22c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/STLExtras.h"
23c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/SmallVector.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
266b1d283fe879fb11d7ce7a69feecf66e77b0eaf3Anders CarlssonSema::OwningStmtResult Sema::ActOnExprStmt(FullExprArg expr) {
276b1d283fe879fb11d7ce7a69feecf66e77b0eaf3Anders Carlsson  Expr *E = expr->takeAs<Expr>();
281b273c403734d343d720acb28f04011807c8aa56Steve Naroff  assert(E && "ActOnExprStmt(): missing expression");
29a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl
30834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // C99 6.8.3p2: The expression in an expression statement is evaluated as a
31834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // void expression for its side effects.  Conversion to void allows any
32834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // operand, even incomplete types.
33a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl
34834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner  // Same thing in for stmt first clause (when expr) and third clause.
35a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  return Owned(static_cast<Stmt*>(E));
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
39a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian RedlSema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
408189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) NullStmt(SemiLoc));
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
43682bf92db408a6cbc3d37b5496a99b6ef85041ecChris LattnerSema::OwningStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg,
44a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                           SourceLocation StartLoc,
45a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                           SourceLocation EndLoc) {
4620401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner  DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
4720401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner
48682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  // If we have an invalid decl, just return an error.
4920401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner  if (DG.isNull()) return StmtError();
50682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner
5124e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
54636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlssonvoid Sema::DiagnoseUnusedExprResult(const Stmt *S) {
55754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  const Expr *E = dyn_cast_or_null<Expr>(S);
56636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  if (!E)
57636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
58636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
59636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  // Ignore expressions that have void type.
60636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  if (E->getType()->isVoidType())
61636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
62636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
63636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  SourceLocation Loc;
64636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  SourceRange R1, R2;
65636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  if (!E->isUnusedResultAWarning(Loc, R1, R2))
66636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    return;
67636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
68636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson  Diag(Loc, diag::warn_unused_expr) << R1 << R2;
69636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson}
70636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson
71a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian RedlAction::OwningStmtResult
721b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
73a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                        MultiStmtArg elts, bool isStmtExpr) {
74a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  unsigned NumElts = elts.size();
75a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
76c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  // If we're in C89 mode, check that we don't have any decls after stmts.  If
77c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  // so, emit an extension diagnostic.
78c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
79c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // Note that __extension__ can be around a decl.
80c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    unsigned i = 0;
81c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // Skip over all declarations.
82c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
83c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      /*empty*/;
84c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner
85c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    // We found the end of the list or a statement.  Scan for another declstmt.
86c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
87c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      /*empty*/;
88c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner
89c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    if (i != NumElts) {
904afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
91c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner      Diag(D->getLocation(), diag::ext_mixed_decls_code);
92c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner    }
93c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner  }
9498414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  // Warn about unused expressions in statements.
9598414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  for (unsigned i = 0; i != NumElts; ++i) {
96636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    // Ignore statements that are last in a statement expression.
97636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    if (isStmtExpr && i == NumElts - 1)
9898414c1b7d1944a57156d52e29bd41c005de09acChris Lattner      continue;
9998414c1b7d1944a57156d52e29bd41c005de09acChris Lattner
100636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson    DiagnoseUnusedExprResult(Elts[i]);
10198414c1b7d1944a57156d52e29bd41c005de09acChris Lattner  }
102a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl
1038189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R));
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
106117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian RedlAction::OwningStmtResult
107117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian RedlSema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval,
108117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl                    SourceLocation DotDotDotLoc, ExprArg rhsval,
10924e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner                    SourceLocation ColonLoc) {
110117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  assert((lhsval.get() != 0) && "missing expression in case statement");
111117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.8.4.2p3: The expression shall be an integer constant.
11351fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson  // However, GCC allows any evaluatable integer expression.
114117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  Expr *LHSVal = static_cast<Expr*>(lhsval.get());
115dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent() &&
116dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      VerifyIntegerConstantExpression(LHSVal))
11724e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner    return StmtError();
1185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1196c36be5b383875b490684bcf439d6d427298c1afChris Lattner  // GCC extension: The expression shall be an integer constant.
120117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
121117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  Expr *RHSVal = static_cast<Expr*>(rhsval.get());
122dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent() &&
123dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      VerifyIntegerConstantExpression(RHSVal)) {
124f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    RHSVal = 0;  // Recover by just forgetting about it.
125117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl    rhsval = 0;
126117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  }
127117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
128bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  if (getSwitchStack().empty()) {
1298a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner    Diag(CaseLoc, diag::err_case_not_in_switch);
13024e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner    return StmtError();
1318a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner  }
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
133117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  // Only now release the smart pointers.
134117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  lhsval.release();
135117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  rhsval.release();
136dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc,
137dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                        ColonLoc);
138bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  getSwitchStack().back()->addSwitchCase(CS);
139117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  return Owned(CS);
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14224e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner/// ActOnCaseStmtBody - This installs a statement as the body of a case.
14324e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattnervoid Sema::ActOnCaseStmtBody(StmtTy *caseStmt, StmtArg subStmt) {
14424e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  CaseStmt *CS = static_cast<CaseStmt*>(caseStmt);
145f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Stmt *SubStmt = subStmt.takeAs<Stmt>();
14624e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner  CS->setSubStmt(SubStmt);
14724e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner}
14824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner
149117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian RedlAction::OwningStmtResult
1501b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
151117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl                       StmtArg subStmt, Scope *CurScope) {
152f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Stmt *SubStmt = subStmt.takeAs<Stmt>();
153117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
154bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  if (getSwitchStack().empty()) {
1550fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner    Diag(DefaultLoc, diag::err_default_not_in_switch);
156117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl    return Owned(SubStmt);
1570fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner  }
158117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl
159dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt);
160bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  getSwitchStack().back()->addSwitchCase(DS);
161117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl  return Owned(DS);
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
164de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult
1651b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
166de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl                     SourceLocation ColonLoc, StmtArg subStmt) {
167f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Stmt *SubStmt = subStmt.takeAs<Stmt>();
1685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Look up the record for this label identifier.
169ea29a3a0d6948c4a51a261d19ec1a585d2a9c779Chris Lattner  LabelStmt *&LabelDecl = getLabelMap()[II];
170de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If not forward referenced or defined already, just create a new LabelStmt.
172caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  if (LabelDecl == 0)
173caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff    return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt));
174de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(LabelDecl->getID() == II && "Label mismatch!");
176de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
1775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, this label was either forward reference or multiply defined.  If
1785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // multiply defined, reject it now.
1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (LabelDecl->getSubStmt()) {
18008631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID();
1815f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner    Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition);
182de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl    return Owned(SubStmt);
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
184de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
1855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, this label was forward declared, and we just found its real
1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // definition.  Fill in the forward definition and return it.
1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LabelDecl->setIdentLoc(IdentLoc);
1880fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner  LabelDecl->setSubStmt(SubStmt);
189de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(LabelDecl);
1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
192de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult
193a99fad8ff134273fe85f2970c7d89133d1218900Anders CarlssonSema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal,
194de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl                  StmtArg ThenVal, SourceLocation ElseLoc,
195de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl                  StmtArg ElseVal) {
196a99fad8ff134273fe85f2970c7d89133d1218900Anders Carlsson  OwningExprResult CondResult(CondVal.release());
197a99fad8ff134273fe85f2970c7d89133d1218900Anders Carlsson
198a99fad8ff134273fe85f2970c7d89133d1218900Anders Carlsson  Expr *condExpr = CondResult.takeAs<Expr>();
199de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2001b273c403734d343d720acb28f04011807c8aa56Steve Naroff  assert(condExpr && "ActOnIfStmt(): missing expression");
201de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
202d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor  if (!condExpr->isTypeDependent()) {
203d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor    DefaultFunctionArrayConversion(condExpr);
204d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor    // Take ownership again until we're past the error checking.
205a99fad8ff134273fe85f2970c7d89133d1218900Anders Carlsson    CondResult = condExpr;
206d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor    QualType condType = condExpr->getType();
207d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor
208d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor    if (getLangOptions().CPlusPlus) {
209d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor      if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
210d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor        return StmtError();
211d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor    } else if (!condType->isScalarType()) // C99 6.8.4.1p1
212d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor      return StmtError(Diag(IfLoc,
213d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor                            diag::err_typecheck_statement_requires_scalar)
214d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor                       << condType << condExpr->getSourceRange());
215d06f6ca61062f85926eb9d409eb3d4f8afcf93c7Douglas Gregor  }
216de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
217e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson  Stmt *thenStmt = ThenVal.takeAs<Stmt>();
218754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(thenStmt);
2195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2202d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // Warn if the if block has a null body without an else value.
2212d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // this helps prevent bugs due to typos, such as
2222d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  // if (condition);
2232d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  //   do_stuff();
224de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  if (!ElseVal.get()) {
2252d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson    if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
2262d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson      Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
2272d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson  }
2282d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson
229754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  Stmt *elseStmt = ElseVal.takeAs<Stmt>();
230754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(elseStmt);
231754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
232a99fad8ff134273fe85f2970c7d89133d1218900Anders Carlsson  CondResult.release();
2338189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt,
234754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson                                    ElseLoc, elseStmt));
2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
237de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult
238de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlSema::ActOnStartOfSwitchStmt(ExprArg cond) {
239f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Expr *Cond = cond.takeAs<Expr>();
240de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2415921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  if (getLangOptions().CPlusPlus) {
2425921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    // C++ 6.4.2.p2:
2435921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    // The condition shall be of integral type, enumeration type, or of a class
2445921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    // type for which a single conversion function to integral or enumeration
2455921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    // type exists (12.3). If the condition is of class type, the condition is
2465921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    // converted by calling that conversion function, and the result of the
2475921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    // conversion is used in place of the original condition for the remainder
2485921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    // of this section. Integral promotions are performed.
249dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    if (!Cond->isTypeDependent()) {
250dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      QualType Ty = Cond->getType();
251dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
252dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // FIXME: Handle class types.
253dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
254dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // If the type is wrong a diagnostic will be emitted later at
255dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // ActOnFinishSwitchStmt.
256dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      if (Ty->isIntegralType() || Ty->isEnumeralType()) {
257dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Integral promotions are performed.
258dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // FIXME: Integral promotions for C++ are not complete.
259dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        UsualUnaryConversions(Cond);
260dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      }
2615921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    }
2625921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  } else {
2635921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
2645921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    UsualUnaryConversions(Cond);
2655921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  }
266de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
2678189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  SwitchStmt *SS = new (Context) SwitchStmt(Cond);
268bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  getSwitchStack().push_back(SS);
269de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(SS);
270c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson}
2716c36be5b383875b490684bcf439d6d427298c1afChris Lattner
272f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
273f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified width and sign.  If an overflow occurs, detect it and emit
274f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified diagnostic.
275f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattnervoid Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
276f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                              unsigned NewWidth, bool NewSign,
277f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                              SourceLocation Loc,
278f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                              unsigned DiagID) {
279f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Perform a conversion to the promoted condition type if needed.
280f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  if (NewWidth > Val.getBitWidth()) {
281f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If this is an extension, just do it.
282f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt OldVal(Val);
283f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.extend(NewWidth);
284f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
285f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If the input was signed and negative and the output is unsigned,
286f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // warn.
287f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
288d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
289f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
290f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.setIsSigned(NewSign);
291f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  } else if (NewWidth < Val.getBitWidth()) {
292f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // If this is a truncation, check for overflow.
293f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt ConvVal(Val);
294f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    ConvVal.trunc(NewWidth);
295b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    ConvVal.setIsSigned(NewSign);
296f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    ConvVal.extend(Val.getBitWidth());
297b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    ConvVal.setIsSigned(Val.isSigned());
298f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    if (ConvVal != Val)
299d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
300f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
301f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // Regardless of whether a diagnostic was emitted, really do the
302f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // truncation.
303f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.trunc(NewWidth);
304b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner    Val.setIsSigned(NewSign);
305f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  } else if (NewSign != Val.isSigned()) {
306f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // Convert the sign to match the sign of the condition.  This can cause
307f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    // overflow as well: unsigned(INTMIN)
308f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    llvm::APSInt OldVal(Val);
309f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    Val.setIsSigned(NewSign);
310f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
311f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    if (Val.isNegative())  // Sign bit changes meaning.
312d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner      Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
313f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  }
314f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner}
315f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
3160471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattnernamespace {
3170471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  struct CaseCompareFunctor {
3180471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
3190471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner                    const llvm::APSInt &RHS) {
3200471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      return LHS.first < RHS;
3210471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    }
3220e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner    bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
3230e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner                    const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
3240e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner      return LHS.first < RHS.first;
3250e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner    }
3260471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    bool operator()(const llvm::APSInt &LHS,
3270471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner                    const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
3280471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      return LHS < RHS.first;
3290471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner    }
3300471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  };
3310471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner}
3320471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner
333764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// CmpCaseVals - Comparison predicate for sorting case values.
334764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner///
335764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattnerstatic bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
336764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner                        const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
337764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  if (lhs.first < rhs.first)
338764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner    return true;
339764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner
340764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  if (lhs.first == rhs.first &&
341764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner      lhs.second->getCaseLoc().getRawEncoding()
342764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner       < rhs.second->getCaseLoc().getRawEncoding())
343764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner    return true;
344764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner  return false;
345764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner}
346764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner
347de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlAction::OwningStmtResult
348de307473448fb3cebcb4c10090728300b53bca03Sebastian RedlSema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch,
349de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl                            StmtArg Body) {
350e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson  Stmt *BodyStmt = Body.takeAs<Stmt>();
351de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
352bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  SwitchStmt *SS = getSwitchStack().back();
353de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  assert(SS == (SwitchStmt*)Switch.get() && "switch stack missing push/pop!");
354de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
3559dcbfa450d751bd68fc4af8b75da381d4f6984b9Steve Naroff  SS->setBody(BodyStmt, SwitchLoc);
356bcfce66584e47bb07f49a86b7cb39b4fdd269a5aChris Lattner  getSwitchStack().pop_back();
357c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson
358f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  Expr *CondExpr = SS->getCond();
359f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  QualType CondType = CondExpr->getType();
360de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
361dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (!CondExpr->isTypeDependent() &&
362dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      !CondType->isIntegerType()) { // C99 6.8.4.2p1
363d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner    Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
364d162584991885ab004a02573a73ce06422b921fcChris Lattner      << CondType << CondExpr->getSourceRange();
365de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl    return StmtError();
366c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson  }
367de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
368f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Get the bitwidth of the switched-on value before promotions.  We must
369f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // convert the integer case values to this width before comparison.
370dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  bool HasDependentValue
371dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    = CondExpr->isTypeDependent() || CondExpr->isValueDependent();
372dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  unsigned CondWidth
373dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    = HasDependentValue? 0
374dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                       : static_cast<unsigned>(Context.getTypeSize(CondType));
375f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  bool CondIsSigned = CondType->isSignedIntegerType();
376f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
377f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Accumulate all of the case values in a vector so that we can sort them
378f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // and detect duplicates.  This vector contains the APInt for the case after
379f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // it has been converted to the condition type.
3800471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
3810471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner  CaseValsTy CaseVals;
382f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
383f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  // Keep track of any GNU case ranges we see.  The APSInt is the low value.
384f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
385f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
386f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  DefaultStmt *TheDefaultStmt = 0;
387c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson
388b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  bool CaseListIsErroneous = false;
389b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner
390dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue;
391c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson       SC = SC->getNextSwitchCase()) {
392b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner
393c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson    if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
394f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      if (TheDefaultStmt) {
395f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
3965f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner        Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
397de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
398f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        // FIXME: Remove the default statement from the switch block so that
399390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // we'll return a valid AST.  This requires recursing down the AST and
400390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // finding it, not something we are set up to do right now.  For now,
401390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump        // just lop the entire switch stmt out of the AST.
402b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner        CaseListIsErroneous = true;
403c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson      }
404f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      TheDefaultStmt = DS;
405c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson
406f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    } else {
407f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      CaseStmt *CS = cast<CaseStmt>(SC);
408f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
409f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // We already verified that the expression has a i-c-e value (C99
410f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // 6.8.4.2p3) - get that value now.
4111e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      Expr *Lo = CS->getLHS();
412dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
413dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      if (Lo->isTypeDependent() || Lo->isValueDependent()) {
414dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        HasDependentValue = true;
415dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        break;
416dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      }
417dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
41851fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson      llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
419f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner
420f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      // Convert the value to the same width/sign as the condition.
421f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner      ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
422f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                         CS->getLHS()->getLocStart(),
423f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner                                         diag::warn_case_value_overflow);
4246c36be5b383875b490684bcf439d6d427298c1afChris Lattner
4251e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      // If the LHS is not the same type as the condition, insert an implicit
4261e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      // cast.
4271e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      ImpCastExprToType(Lo, CondType);
4281e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner      CS->setLHS(Lo);
4291e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner
430b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner      // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
431dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      if (CS->getRHS()) {
432dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (CS->getRHS()->isTypeDependent() ||
433dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            CS->getRHS()->isValueDependent()) {
434dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          HasDependentValue = true;
435dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          break;
436dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
437f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner        CaseRanges.push_back(std::make_pair(LoVal, CS));
438dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      } else
439b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner        CaseVals.push_back(std::make_pair(LoVal, CS));
440f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner    }
441f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner  }
442b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner
443dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor  if (!HasDependentValue) {
444dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // Sort all the scalar case values so we can easily detect duplicates.
445dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
446dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
447dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    if (!CaseVals.empty()) {
448dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
449dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (CaseVals[i].first == CaseVals[i+1].first) {
450dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          // If we have a duplicate, report it.
451dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
452dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::err_duplicate_case) << CaseVals[i].first.toString(10);
453dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CaseVals[i].second->getLHS()->getLocStart(),
454dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::note_duplicate_case_prev);
455390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // FIXME: We really want to remove the bogus case stmt from the
456390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // substmt, but we have no way to do this right now.
457dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseListIsErroneous = true;
458dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
4596efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner      }
460b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner    }
461dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
462dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // Detect duplicate case ranges, which usually don't exist at all in
463dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    // the first place.
464dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor    if (!CaseRanges.empty()) {
465dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Sort all the case ranges by their low value so we can easily detect
466dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // overlaps between ranges.
467dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      std::stable_sort(CaseRanges.begin(), CaseRanges.end());
4680471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner
469dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Scan the ranges, computing the high values and removing empty ranges.
470dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      std::vector<llvm::APSInt> HiVals;
471dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
472dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *CR = CaseRanges[i].second;
473dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        Expr *Hi = CR->getRHS();
474dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
475dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
476dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Convert the value to the same width/sign as the condition.
477dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
478dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                           CR->getRHS()->getLocStart(),
479dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                           diag::warn_case_value_overflow);
480dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
481dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // If the LHS is not the same type as the condition, insert an implicit
482dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // cast.
483dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        ImpCastExprToType(Hi, CondType);
484dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CR->setRHS(Hi);
485dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
486dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // If the low value is bigger than the high value, the case is empty.
487dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (CaseRanges[i].first > HiVal) {
488dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
489dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            << SourceRange(CR->getLHS()->getLocStart(),
490dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                           CR->getRHS()->getLocEnd());
491dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseRanges.erase(CaseRanges.begin()+i);
492dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          --i, --e;
493dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          continue;
494dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
495dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        HiVals.push_back(HiVal);
4960471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      }
497b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner
498dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // Rescan the ranges, looking for overlap with singleton values and other
499dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // ranges.  Since the range list is sorted, we only need to compare case
500dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      // ranges with their neighbors.
501dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor      for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
502dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt &CRLo = CaseRanges[i].first;
503dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt &CRHi = HiVals[i];
504dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *CR = CaseRanges[i].second;
505dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
506dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Check to see whether the case range overlaps with any
507dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // singleton cases.
508dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseStmt *OverlapStmt = 0;
509dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        llvm::APSInt OverlapVal(32);
510dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
511dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Find the smallest value >= the lower bound.  If I is in the
512dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // case range, then we have overlap.
513dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
514dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                                  CaseVals.end(), CRLo,
515dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor                                                  CaseCompareFunctor());
516dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (I != CaseVals.end() && I->first < CRHi) {
517dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = I->first;   // Found overlap with scalar.
518dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = I->second;
519dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
520dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
521dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Find the smallest value bigger than the upper bound.
522dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
523dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
524dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = (I-1)->first;      // Found overlap with scalar.
525dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = (I-1)->second;
526dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
527dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
528dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // Check to see if this case stmt overlaps with the subsequent
529dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        // case range.
530dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (i && CRLo <= HiVals[i-1]) {
531dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapVal  = HiVals[i-1];       // Found overlap with range.
532dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          OverlapStmt = CaseRanges[i-1].second;
533dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
534dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
535dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        if (OverlapStmt) {
536dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          // If we have a duplicate, report it.
537dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
538dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor            << OverlapVal.toString(10);
539dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          Diag(OverlapStmt->getLHS()->getLocStart(),
540dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor               diag::note_duplicate_case_prev);
541390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // FIXME: We really want to remove the bogus case stmt from the
542390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump          // substmt, but we have no way to do this right now.
543dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor          CaseListIsErroneous = true;
544dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor        }
5450471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner      }
546b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner    }
547b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  }
548dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor
549390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: If the case list was broken is some way, we don't have a good system
550390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // to patch it up.  Instead, just return the whole substmt as broken.
551b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner  if (CaseListIsErroneous)
552de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl    return StmtError();
553de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl
554de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  Switch.release();
555de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl  return Owned(SS);
5565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
558f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult
5597f537c18c7029e73f0bd555be3782c066e7e2c1eAnders CarlssonSema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond, StmtArg Body) {
5607f537c18c7029e73f0bd555be3782c066e7e2c1eAnders Carlsson  ExprArg CondArg(Cond.release());
5617f537c18c7029e73f0bd555be3782c066e7e2c1eAnders Carlsson  Expr *condExpr = CondArg.takeAs<Expr>();
5621b273c403734d343d720acb28f04011807c8aa56Steve Naroff  assert(condExpr && "ActOnWhileStmt(): missing expression");
563f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
5644a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor  if (!condExpr->isTypeDependent()) {
5654a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor    DefaultFunctionArrayConversion(condExpr);
5667f537c18c7029e73f0bd555be3782c066e7e2c1eAnders Carlsson    CondArg = condExpr;
5674a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor    QualType condType = condExpr->getType();
5684a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor
5694a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor    if (getLangOptions().CPlusPlus) {
5704a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor      if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
5714a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor        return StmtError();
5724a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor    } else if (!condType->isScalarType()) // C99 6.8.5p2
5734a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor      return StmtError(Diag(WhileLoc,
5744a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor                            diag::err_typecheck_statement_requires_scalar)
5754a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor                       << condType << condExpr->getSourceRange());
5764a2e2041edc63db687677325e113b39b9d123c40Douglas Gregor  }
5775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
578754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  Stmt *bodyStmt = Body.takeAs<Stmt>();
579754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(bodyStmt);
580754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
5817f537c18c7029e73f0bd555be3782c066e7e2c1eAnders Carlsson  CondArg.release();
582754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  return Owned(new (Context) WhileStmt(condExpr, bodyStmt, WhileLoc));
5835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
585f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult
586f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body,
587989135901c750af61ef012b6b0a0368be415bc46Chris Lattner                  SourceLocation WhileLoc, SourceLocation CondLParen,
588989135901c750af61ef012b6b0a0368be415bc46Chris Lattner                  ExprArg Cond, SourceLocation CondRParen) {
589e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson  Expr *condExpr = Cond.takeAs<Expr>();
5901b273c403734d343d720acb28f04011807c8aa56Steve Naroff  assert(condExpr && "ActOnDoStmt(): missing expression");
591f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
5929f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor  if (!condExpr->isTypeDependent()) {
5939f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor    DefaultFunctionArrayConversion(condExpr);
5949f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor    Cond = condExpr;
5959f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor    QualType condType = condExpr->getType();
5969f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor
5979f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor    if (getLangOptions().CPlusPlus) {
5989f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor      if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
5999f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor        return StmtError();
6009f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor    } else if (!condType->isScalarType()) // C99 6.8.5p2
6019f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor      return StmtError(Diag(DoLoc,
6029f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor                            diag::err_typecheck_statement_requires_scalar)
6039f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor                       << condType << condExpr->getSourceRange());
6049f3ca2a7747bd47f14d7693f333103fac29a24d2Douglas Gregor  }
6055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
606754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  Stmt *bodyStmt = Body.takeAs<Stmt>();
607754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(bodyStmt);
608754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
609f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Cond.release();
610754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  return Owned(new (Context) DoStmt(bodyStmt, condExpr, DoLoc,
611989135901c750af61ef012b6b0a0368be415bc46Chris Lattner                                    WhileLoc, CondRParen));
6125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
614f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult
615f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
616f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                   StmtArg first, ExprArg second, ExprArg third,
617f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                   SourceLocation RParenLoc, StmtArg body) {
618f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Stmt *First  = static_cast<Stmt*>(first.get());
619f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Expr *Second = static_cast<Expr*>(second.get());
620f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Expr *Third  = static_cast<Expr*>(third.get());
621f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Stmt *Body  = static_cast<Stmt*>(body.get());
622f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
6235921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis  if (!getLangOptions().CPlusPlus) {
6245921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
625f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // C99 6.8.5p3: The declaration part of a 'for' statement shall only
626f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // declare identifiers for objects having storage class 'auto' or
627f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // 'register'.
6285921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis      for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
6295921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis           DI!=DE; ++DI) {
6305921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        VarDecl *VD = dyn_cast<VarDecl>(*DI);
6315921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
6325921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis          VD = 0;
6335921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        if (VD == 0)
6345921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis          Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
6355921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis        // FIXME: mark decl erroneous!
6365921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis      }
637ae3b701f59e78e058b83344be17206af3bf5d277Chris Lattner    }
6385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6395831c6a1efc47e6a19d82fe3dd25b5b8fef6979dDouglas Gregor  if (Second && !Second->isTypeDependent()) {
64036c4b0ec13453bc003bbac380770b45ae35ef19cChris Lattner    DefaultFunctionArrayConversion(Second);
64136c4b0ec13453bc003bbac380770b45ae35ef19cChris Lattner    QualType SecondType = Second->getType();
642f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
6435921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    if (getLangOptions().CPlusPlus) {
6445921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis      if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4
645f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError();
6465921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis    } else if (!SecondType->isScalarType()) // C99 6.8.5p2
647f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl      return StmtError(Diag(ForLoc,
648f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                            diag::err_typecheck_statement_requires_scalar)
649f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        << SecondType << Second->getSourceRange());
6505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
651754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
652754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson  DiagnoseUnusedExprResult(Body);
653754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson
654f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  first.release();
655f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  second.release();
656f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  third.release();
657f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  body.release();
6585831c6a1efc47e6a19d82fe3dd25b5b8fef6979dDouglas Gregor  return Owned(new (Context) ForStmt(First, Second, Third, Body, ForLoc,
6595831c6a1efc47e6a19d82fe3dd25b5b8fef6979dDouglas Gregor                                     LParenLoc, RParenLoc));
6605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
662f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlAction::OwningStmtResult
663f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
664f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                                 SourceLocation LParenLoc,
665f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                                 StmtArg first, ExprArg second,
666f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                                 SourceLocation RParenLoc, StmtArg body) {
667f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Stmt *First  = static_cast<Stmt*>(first.get());
668f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Expr *Second = static_cast<Expr*>(second.get());
669f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  Stmt *Body  = static_cast<Stmt*>(body.get());
67020552d2842245692b649e0d25380670922f954a2Fariborz Jahanian  if (First) {
67120552d2842245692b649e0d25380670922f954a2Fariborz Jahanian    QualType FirstType;
67220552d2842245692b649e0d25380670922f954a2Fariborz Jahanian    if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
6737e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner      if (!DS->isSingleDecl())
674f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag((*DS->decl_begin())->getLocation(),
675f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                         diag::err_toomany_element_decls));
676f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl
6777e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner      Decl *D = DS->getSingleDecl();
678f34afeed9a0112bf31fee185b6c80556111d3834Ted Kremenek      FirstType = cast<ValueDecl>(D)->getType();
679f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // C99 6.8.5p3: The declaration part of a 'for' statement shall only
680f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // declare identifiers for objects having storage class 'auto' or
681f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner      // 'register'.
682248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff      VarDecl *VD = cast<VarDecl>(D);
683248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff      if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
684f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag(VD->getLocation(),
685f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                              diag::err_non_variable_decl_in_for));
6861fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson    } else {
687810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      if (cast<Expr>(First)->isLvalue(Context) != Expr::LV_Valid)
688f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl        return StmtError(Diag(First->getLocStart(),
689f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl                   diag::err_selector_element_not_lvalue)
690f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl          << First->getSourceRange());
6911fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson
692f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl      FirstType = static_cast<Expr*>(First)->getType();
6931fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson    }
694f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    if (!FirstType->isObjCObjectPointerType())
695dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        Diag(ForLoc, diag::err_selector_element_type)
696d162584991885ab004a02573a73ce06422b921fcChris Lattner          << FirstType << First->getSourceRange();
6973ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  }
6983ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  if (Second) {
6993ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian    DefaultFunctionArrayConversion(Second);
7003ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian    QualType SecondType = Second->getType();
701f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    if (!SecondType->isObjCObjectPointerType())
702dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner      Diag(ForLoc, diag::err_collection_expr_type)
703d162584991885ab004a02573a73ce06422b921fcChris Lattner        << SecondType << Second->getSourceRange();
7043ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  }
705f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  first.release();
706f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  second.release();
707f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl  body.release();
7088189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body,
7098189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                                                   ForLoc, RParenLoc));
7103ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian}
7115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7124cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
7131b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
7145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                    IdentifierInfo *LabelII) {
7154eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // If we are in a block, reject all gotos for now.
7164eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  if (CurBlock)
7174cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(GotoLoc, diag::err_goto_in_block));
7184eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff
7195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Look up the record for this label identifier.
720ea29a3a0d6948c4a51a261d19ec1a585d2a9c779Chris Lattner  LabelStmt *&LabelDecl = getLabelMap()[LabelII];
7215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
722caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  // If we haven't seen this label yet, create a forward reference.
723caaacecb2b64e6d2e402533baffda4cb540f4145Steve Naroff  if (LabelDecl == 0)
7248189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0);
7254cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
7268189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc));
7275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7294cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
730ad56d684259f706b7c0ae5ad9c23adb4f2926817Chris LattnerSema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc,
7314cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl                            ExprArg DestExp) {
732bbf462314b1dc8e422b7c4dd4cac47e566aedf6dEli Friedman  // Convert operand to void*
73333083823342649b1fccec856c1f239c09fc0d7e1Eli Friedman  Expr* E = DestExp.takeAs<Expr>();
7345f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  if (!E->isTypeDependent()) {
7355f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    QualType ETy = E->getType();
7365f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    AssignConvertType ConvTy =
7375f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor      CheckSingleAssignmentConstraints(Context.VoidPtrTy, E);
7385f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor    if (DiagnoseAssignmentResult(ConvTy, StarLoc, Context.VoidPtrTy, ETy,
7395f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor                                 E, "passing"))
7405f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor      return StmtError();
7415f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  }
7425f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor  return Owned(new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E));
7435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7454cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
7461b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Scope *S = CurScope->getContinueParent();
7485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!S) {
7495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
7504cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
7515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7524cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
7538189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ContinueStmt(ContinueLoc));
7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7564cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
7571b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Scope *S = CurScope->getBreakParent();
7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!S) {
7605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
7614cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
7625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7634cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
7648189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) BreakStmt(BreakLoc));
7655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
76727c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
7684eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff///
7694cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
7704eb206bebcdab28ababe8df55c6185cec2cdc071Steve NaroffSema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
7714eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // If this is the first return we've seen in the block, infer the type of
7724eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // the block from it.
7737d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian  if (CurBlock->ReturnType.isNull()) {
774c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff    if (RetValExp) {
77516564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      // Don't call UsualUnaryConversions(), since we don't want to do
77616564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      // integer promotions here.
77716564420ffe679b0e3cf310c418be6ef98d8e658Steve Naroff      DefaultFunctionArrayConversion(RetValExp);
7787d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      CurBlock->ReturnType = RetValExp->getType();
7797d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(RetValExp)) {
7807d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // We have to remove a 'const' added to copied-in variable which was
7817d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // part of the implementation spec. and not the actual qualifier for
7827d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        // the variable.
7837d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian        if (CDRE->isConstQualAdded())
7847d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian           CurBlock->ReturnType.removeConst();
7857d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      }
786c50a4a5f2eac14ac4c631d50b0a55cadc87700ceSteve Naroff    } else
7877d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian      CurBlock->ReturnType = Context.VoidTy;
7884eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  }
7897d5c74ecbbd8719436c071f38657bc8e97ee4a24Fariborz Jahanian  QualType FnRetType = CurBlock->ReturnType;
7904cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
79140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (CurBlock->TheDecl->hasAttr<NoReturnAttr>()) {
7926c92fa75e62937f9738696840efcb258560f4568Mike Stump    Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr)
7936c92fa75e62937f9738696840efcb258560f4568Mike Stump      << getCurFunctionOrMethodDecl()->getDeclName();
7946c92fa75e62937f9738696840efcb258560f4568Mike Stump    return StmtError();
7956c92fa75e62937f9738696840efcb258560f4568Mike Stump  }
7966c92fa75e62937f9738696840efcb258560f4568Mike Stump
7974eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // Otherwise, verify that this result type matches the previous one.  We are
7984eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // pickier with blocks than for normal functions because we don't have GCC
7994eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  // compatibility to worry about here.
8004eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  if (CurBlock->ReturnType->isVoidType()) {
8014eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    if (RetValExp) {
8024eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff      Diag(ReturnLoc, diag::err_return_block_has_expr);
8038189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek      RetValExp->Destroy(Context);
8044eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff      RetValExp = 0;
8054eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    }
8068189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
8074eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  }
8084cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
8094cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl  if (!RetValExp)
8104cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr));
8114cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
81298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
81398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // we have a non-void block with an expression, continue checking
81498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    QualType RetValType = RetValExp->getType();
81598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
81698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // C99 6.8.6.4p3(136): The return statement is not an assignment. The
81798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // overlap restriction of subclause 6.5.16.1 does not apply to the case of
81898eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // function return.
8194cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
82098eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // In C++ the return statement is handled via a copy initialization.
82198eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // the C version of which boils down to CheckSingleAssignmentConstraints.
82298eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    // FIXME: Leaks RetValExp.
82398eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
82498eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump      return StmtError();
82598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump
82698eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump    if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
82798eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump  }
8284cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
8298189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
8304eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff}
8315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
832e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl/// IsReturnCopyElidable - Whether returning @p RetExpr from a function that
833e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl/// returns a @p RetType fulfills the criteria for copy elision (C++0x 12.8p15).
834e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redlstatic bool IsReturnCopyElidable(ASTContext &Ctx, QualType RetType,
835e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl                                 Expr *RetExpr) {
836e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  QualType ExprType = RetExpr->getType();
837e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // - in a return statement in a function with ...
838e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // ... a class return type ...
839e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  if (!RetType->isRecordType())
840e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    return false;
841e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // ... the same cv-unqualified type as the function return type ...
842e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  if (Ctx.getCanonicalType(RetType).getUnqualifiedType() !=
843e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl      Ctx.getCanonicalType(ExprType).getUnqualifiedType())
844e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    return false;
845e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // ... the expression is the name of a non-volatile automatic object ...
846e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // We ignore parentheses here.
847e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  // FIXME: Is this compliant?
848e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetExpr->IgnoreParens());
849e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  if (!DR)
850e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    return false;
851e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
852e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  if (!VD)
853e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    return false;
854e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl  return VD->hasLocalStorage() && !VD->getType()->isReferenceType()
855e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    && !VD->getType().isVolatileQualified();
856e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl}
857e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl
8584cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian RedlAction::OwningStmtResult
859a0ab25d2808fe77ffff54f25a015893791dfd4b6Anders CarlssonSema::ActOnReturnStmt(SourceLocation ReturnLoc, FullExprArg rex) {
860a0ab25d2808fe77ffff54f25a015893791dfd4b6Anders Carlsson  Expr *RetValExp = rex->takeAs<Expr>();
8614eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff  if (CurBlock)
8624eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff    return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
8634cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
864371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner  QualType FnRetType;
865f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump  if (const FunctionDecl *FD = getCurFunctionDecl()) {
866371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner    FnRetType = FD->getResultType();
86740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (FD->hasAttr<NoReturnAttr>())
8688662587fa75d3fb04f873e265841c9314c7f5523Chris Lattner      Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr)
869f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump        << getCurFunctionOrMethodDecl()->getDeclName();
870f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump  } else if (ObjCMethodDecl *MD = getCurMethodDecl())
871c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff    FnRetType = MD->getResultType();
872c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff  else // If we don't have a function/method context, bail.
873c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff    return StmtError();
874c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff
8755cf216b7fa64b933b60743b0b26053e8e7aa87beChris Lattner  if (FnRetType->isVoidType()) {
8763c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns)
87765ce04bef06696379682410f399f37b43996d824Chris Lattner      unsigned D = diag::ext_return_has_expr;
87865ce04bef06696379682410f399f37b43996d824Chris Lattner      if (RetValExp->getType()->isVoidType())
87965ce04bef06696379682410f399f37b43996d824Chris Lattner        D = diag::ext_return_has_void_expr;
8804cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
881e878eb035b343d7d819c092102364ec9849716aeChris Lattner      // return (some void expression); is legal in C++.
882e878eb035b343d7d819c092102364ec9849716aeChris Lattner      if (D != diag::ext_return_has_void_expr ||
883e878eb035b343d7d819c092102364ec9849716aeChris Lattner          !getLangOptions().CPlusPlus) {
884e878eb035b343d7d819c092102364ec9849716aeChris Lattner        NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
885e878eb035b343d7d819c092102364ec9849716aeChris Lattner        Diag(ReturnLoc, D)
886e878eb035b343d7d819c092102364ec9849716aeChris Lattner          << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
887e878eb035b343d7d819c092102364ec9849716aeChris Lattner          << RetValExp->getSourceRange();
888e878eb035b343d7d819c092102364ec9849716aeChris Lattner      }
8895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
8908189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
8915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8924cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
89303d77760a5db7990724b6901cea958a673ce0b39Anders Carlsson  if (!RetValExp && !FnRetType->isDependentType()) {
8943c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    unsigned DiagID = diag::warn_return_missing_expr;  // C90 6.6.6.4p4
8953c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    // C99 6.8.6.4p1 (ext_ since GCC warns)
8963c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
8973c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner
8983c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    if (FunctionDecl *FD = getCurFunctionDecl())
89908631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
9003c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    else
90108631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
9028189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return Owned(new (Context) ReturnStmt(ReturnLoc, (Expr*)0));
9033c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  }
9044cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
905898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
906898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    // we have a non-void function with an expression, continue checking
9074cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
908898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    // C99 6.8.6.4p3(136): The return statement is not an assignment. The
909898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    // overlap restriction of subclause 6.5.16.1 does not apply to the case of
9104cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    // function return.
9114cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
912e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // C++0x 12.8p15: When certain criteria are met, an implementation is
913e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   allowed to omit the copy construction of a class object, [...]
914e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   - in a return statement in a function with a class return type, when
915e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //     the expression is the name of a non-volatile automatic object with
916e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //     the same cv-unqualified type as the function return type, the copy
917e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //     operation can be omitted [...]
918e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // C++0x 12.8p16: When the criteria for elision of a copy operation are met
919e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   and the object to be copied is designated by an lvalue, overload
920e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   resolution to select the constructor for the copy is first performed
921e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    //   as if the object were designated by an rvalue.
922e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // Note that we only compute Elidable if we're in C++0x, since we don't
923e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // care otherwise.
924e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    bool Elidable = getLangOptions().CPlusPlus0x ?
925e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl                      IsReturnCopyElidable(Context, FnRetType, RetValExp) :
926e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl                      false;
927e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl
928898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    // In C++ the return statement is handled via a copy initialization.
9294cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl    // the C version of which boils down to CheckSingleAssignmentConstraints.
930e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    // FIXME: Leaks RetValExp on error.
931e2b6833d445c7a4ce64f1816c05f176ba1740acaSebastian Redl    if (PerformCopyInitialization(RetValExp, FnRetType, "returning", Elidable))
9324cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl      return StmtError();
9334cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl
934898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
935898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  }
936898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
9378189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
9385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
940810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently
941810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// ignore "noop" casts in places where an lvalue is required by an inline asm.
942810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// We emulate this behavior when -fheinous-gnu-extensions is specified, but
943810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// provide a strong guidance to not use it.
944810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner///
945810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// This method checks to see if the argument is an acceptable l-value and
946810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner/// returns false if it is a case we can handle.
947810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattnerstatic bool CheckAsmLValue(const Expr *E, Sema &S) {
948810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  if (E->isLvalue(S.Context) == Expr::LV_Valid)
949810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    return false;  // Cool, this is an lvalue.
950810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
951810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // Okay, this is not an lvalue, but perhaps it is the result of a cast that we
952810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // are supposed to allow.
953810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  const Expr *E2 = E->IgnoreParenNoopCasts(S.Context);
954810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  if (E != E2 && E2->isLvalue(S.Context) == Expr::LV_Valid) {
955810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    if (!S.getLangOptions().HeinousExtensions)
956810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue)
957810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner        << E->getSourceRange();
958810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    else
959810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue)
960810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner        << E->getSourceRange();
961810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    // Accept, even if we emitted an error diagnostic.
962810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    return false;
963810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  }
964810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
965810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  // None of the above, just randomly invalid non-lvalue.
966810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner  return true;
967810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner}
968810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
969810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner
9703037ed0a27dba62e522304183718efc149e8b6d9Sebastian RedlSema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
9713037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          bool IsSimple,
9723037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          bool IsVolatile,
9733037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          unsigned NumOutputs,
9743037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          unsigned NumInputs,
9753037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          std::string *Names,
9763037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg constraints,
9773037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg exprs,
9783037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          ExprArg asmString,
9793037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          MultiExprArg clobbers,
9803037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                          SourceLocation RParenLoc) {
9813037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  unsigned NumClobbers = clobbers.size();
9823037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral **Constraints =
9833037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl    reinterpret_cast<StringLiteral**>(constraints.get());
9843037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  Expr **Exprs = reinterpret_cast<Expr **>(exprs.get());
9853037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString.get());
9863037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get());
9873037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
98803eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson  llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
98903eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson
9901708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  // The parser verifies that there is a string literal here.
9916bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner  if (AsmString->isWide())
9923037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl    return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
9933037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      << AsmString->getSourceRange());
9943037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
9951708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  for (unsigned i = 0; i != NumOutputs; i++) {
9961708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Constraints[i];
9976bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
9983037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
9993037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
10003037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1001432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner    TargetInfo::ConstraintInfo Info(Literal->getStrData(),
10022819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                    Literal->getByteLength(),
10032819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                    Names[i]);
1004432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner    if (!Context.Target.validateOutputConstraint(Info))
10053037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
1006432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                            diag::err_asm_invalid_output_constraint)
1007432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                       << Info.getConstraintStr());
10083037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1009d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson    // Check that the output exprs are valid lvalues.
101072056a237c536ee63285ab0850cb50f299281767Eli Friedman    Expr *OutputExpr = Exprs[i];
1011810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner    if (CheckAsmLValue(OutputExpr, *this)) {
101272056a237c536ee63285ab0850cb50f299281767Eli Friedman      return StmtError(Diag(OutputExpr->getLocStart(),
1013dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner                  diag::err_asm_invalid_lvalue_in_output)
101472056a237c536ee63285ab0850cb50f299281767Eli Friedman        << OutputExpr->getSourceRange());
101504728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson    }
101603eb543cf7ebee463b33b5802b83ac92c21770cfAnders Carlsson
101744def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    OutputConstraintInfos.push_back(Info);
101804728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  }
10193037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1020806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  llvm::SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
1021806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner
102204728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
10231708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Constraints[i];
10246bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
10253037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
10263037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
10273037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1028432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner    TargetInfo::ConstraintInfo Info(Literal->getStrData(),
10292819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                    Literal->getByteLength(),
10302819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                    Names[i]);
1031beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    if (!Context.Target.validateInputConstraint(OutputConstraintInfos.data(),
10322819fa85651526d59ade4fdc9da2cadd7b132973Chris Lattner                                                NumOutputs, Info)) {
10333037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
1034432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                            diag::err_asm_invalid_input_constraint)
1035432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                       << Info.getConstraintStr());
1036d04c6e23f2e10eeb9936778d67f4a1c4a14cc4f6Anders Carlsson    }
10373037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
103872056a237c536ee63285ab0850cb50f299281767Eli Friedman    Expr *InputExpr = Exprs[i];
10393037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
1040d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson    // Only allow void types for memory constraints.
104144def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.allowsMemory() && !Info.allowsRegister()) {
1042810f6d5d6223adaab0ccf0139f40de6484ad1bb5Chris Lattner      if (CheckAsmLValue(InputExpr, *this))
104372056a237c536ee63285ab0850cb50f299281767Eli Friedman        return StmtError(Diag(InputExpr->getLocStart(),
1044d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson                              diag::err_asm_invalid_lvalue_in_input)
1045432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner                         << Info.getConstraintStr()
104672056a237c536ee63285ab0850cb50f299281767Eli Friedman                         << InputExpr->getSourceRange());
104704728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson    }
10483037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
104944def070435a2b5d67f0534f7a3a85a7389d60f2Chris Lattner    if (Info.allowsRegister()) {
1050d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson      if (InputExpr->getType()->isVoidType()) {
105172056a237c536ee63285ab0850cb50f299281767Eli Friedman        return StmtError(Diag(InputExpr->getLocStart(),
1052d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson                              diag::err_asm_invalid_type_in_input)
1053432c86969eced2ce658b3f3f2aa7407c8864f21bChris Lattner          << InputExpr->getType() << Info.getConstraintStr()
105472056a237c536ee63285ab0850cb50f299281767Eli Friedman          << InputExpr->getSourceRange());
1055d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson      }
1056d9fca6e3950346ea503f92f27ed0f9d8edde9febAnders Carlsson    }
10576032979a5d091e5607c7c467400449727d9beb5aAnders Carlsson
10586032979a5d091e5607c7c467400449727d9beb5aAnders Carlsson    DefaultFunctionArrayConversion(Exprs[i]);
105949ac88166d36f8c436780694f2ce955acde87c6aChris Lattner
1060806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    InputConstraintInfos.push_back(Info);
106104728b7ea928b029f165fc67d32ced40e6868b31Anders Carlsson  }
10623037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
10636fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson  // Check that the clobbers are valid.
10641708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner  for (unsigned i = 0; i != NumClobbers; i++) {
10651708b963a014b48103e78131f960a13ee1aa6d0cChris Lattner    StringLiteral *Literal = Clobbers[i];
10666bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (Literal->isWide())
10673037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
10683037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl        << Literal->getSourceRange());
10693037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
10703037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl    llvm::SmallString<16> Clobber(Literal->getStrData(),
10713037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                                  Literal->getStrData() +
10726fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson                                  Literal->getByteLength());
10733037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
10746bc52112dbc54551bd8e215d95bba4791b2275f7Chris Lattner    if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
10753037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl      return StmtError(Diag(Literal->getLocStart(),
10763037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl                  diag::err_asm_unknown_register_name) << Clobber.c_str());
10776fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson  }
10783037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl
10793037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  constraints.release();
10803037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  exprs.release();
10813037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  asmString.release();
10823037ed0a27dba62e522304183718efc149e8b6d9Sebastian Redl  clobbers.release();
1083fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  AsmStmt *NS =
1084fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner    new (Context) AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs,
1085fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner                          Names, Constraints, Exprs, AsmString, NumClobbers,
1086fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner                          Clobbers, RParenLoc);
1087fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  // Validate the asm string, ensuring it makes sense given the operands we
1088fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  // have.
1089fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  llvm::SmallVector<AsmStmt::AsmStringPiece, 8> Pieces;
1090fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  unsigned DiagOffs;
1091fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
10922ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner    Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
10932ff0f42a962fec5a6300b5986297b417db173e6aChris Lattner           << AsmString->getSourceRange();
1094fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner    DeleteStmt(NS);
1095fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner    return StmtError();
1096fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  }
1097fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner
1098806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  // Validate tied input operands for type mismatches.
1099806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) {
1100806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
1101806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner
1102806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // If this is a tied constraint, verify that the output and input have
1103806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // either exactly the same type, or that they are int/ptr operands with the
1104806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    // same size (int/long, int*/long, are ok etc).
1105806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    if (!Info.hasTiedOperand()) continue;
1106806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner
1107806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    unsigned TiedTo = Info.getTiedOperand();
1108f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner    Expr *OutputExpr = Exprs[TiedTo];
1109c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner    Expr *InputExpr = Exprs[i+NumOutputs];
11107adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    QualType InTy = InputExpr->getType();
11117adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    QualType OutTy = OutputExpr->getType();
11127adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    if (Context.hasSameType(InTy, OutTy))
1113806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner      continue;  // All types can be tied to themselves.
1114806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner
11157adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    // Int/ptr operands have some special cases that we allow.
11167adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner    if ((OutTy->isIntegerType() || OutTy->isPointerType()) &&
11177adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner        (InTy->isIntegerType() || InTy->isPointerType())) {
11187adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner
11197adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      // They are ok if they are the same size.  Tying void* to int is ok if
11207adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      // they are the same size, for example.  This also allows tying void* to
11217adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      // int*.
11223351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      uint64_t OutSize = Context.getTypeSize(OutTy);
11233351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      uint64_t InSize = Context.getTypeSize(InTy);
11243351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      if (OutSize == InSize)
1125806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner        continue;
1126f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner
11273351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // If the smaller input/output operand is not mentioned in the asm string,
11283351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // then we can promote it and the asm string won't notice.  Check this
1129f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner      // case now.
11303351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      bool SmallerValueMentioned = false;
113158bce89d12b1dfba16637ce3754d6cb24c3099bbChris Lattner      for (unsigned p = 0, e = Pieces.size(); p != e; ++p) {
113258bce89d12b1dfba16637ce3754d6cb24c3099bbChris Lattner        AsmStmt::AsmStringPiece &Piece = Pieces[p];
113358bce89d12b1dfba16637ce3754d6cb24c3099bbChris Lattner        if (!Piece.isOperand()) continue;
11343351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner
11353351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        // If this is a reference to the input and if the input was the smaller
11363351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        // one, then we have to reject this asm.
11373351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        if (Piece.getOperandNo() == i+NumOutputs) {
11383351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner          if (InSize < OutSize) {
11393351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner            SmallerValueMentioned = true;
11403351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner            break;
11413351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner          }
11423351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        }
11433351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner
11443351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        // If this is a reference to the input and if the input was the smaller
11453351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        // one, then we have to reject this asm.
11463351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        if (Piece.getOperandNo() == TiedTo) {
11473351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner          if (InSize > OutSize) {
11483351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner            SmallerValueMentioned = true;
11493351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner            break;
11503351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner          }
11513351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner        }
1152f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner      }
1153f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner
11543351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // If the smaller value wasn't mentioned in the asm string, and if the
11553351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // output was a register, just extend the shorter one to the size of the
11563351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      // larger one.
11573351f1145aa91ddd8022fcd3ca16c219db9e8277Chris Lattner      if (!SmallerValueMentioned &&
1158f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner          OutputConstraintInfos[TiedTo].allowsRegister())
1159f69fcaeb3843297757251a19f0a6f5bbffed7f32Chris Lattner        continue;
1160806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    }
1161806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner
1162c1f3b28004a032f4cd13721d4d884c6dcec29c31Chris Lattner    Diag(InputExpr->getLocStart(),
1163806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner         diag::err_asm_tying_incompatible_types)
11647adaa18ef3be65971cd41cc61dd739baeb02af10Chris Lattner      << InTy << OutTy << OutputExpr->getSourceRange()
1165806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner      << InputExpr->getSourceRange();
1166806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    DeleteStmt(NS);
1167806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner    return StmtError();
1168806503f8c839d7f5ebf3fbf7ee848c179be76dd2Chris Lattner  }
1169fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner
1170fb5058ef67c054296c88db18ab1b3717845cb71dChris Lattner  return Owned(NS);
1171fe795956194141c91ae555985c9b930595bff43fChris Lattner}
11723b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian
1173431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
1174431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
1175b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                           SourceLocation RParen, DeclPtrTy Parm,
1176431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl                           StmtArg Body, StmtArg catchList) {
1177f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Stmt *CatchList = catchList.takeAs<Stmt>();
1178b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  ParmVarDecl *PVD = cast_or_null<ParmVarDecl>(Parm.getAs<Decl>());
1179f50cb369273c6bd26c9629df92ee53f1d8af4149Steve Naroff
1180f50cb369273c6bd26c9629df92ee53f1d8af4149Steve Naroff  // PVD == 0 implies @catch(...).
11819d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff  if (PVD) {
118293c4945c9ead2d374fe3fc528e3017c7167265beChris Lattner    // If we already know the decl is invalid, reject it.
118393c4945c9ead2d374fe3fc528e3017c7167265beChris Lattner    if (PVD->isInvalidDecl())
118493c4945c9ead2d374fe3fc528e3017c7167265beChris Lattner      return StmtError();
118593c4945c9ead2d374fe3fc528e3017c7167265beChris Lattner
1186f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    if (!PVD->getType()->isObjCObjectPointerType())
11879d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff      return StmtError(Diag(PVD->getLocation(),
11889d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff                       diag::err_catch_param_not_objc_type));
11899d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff    if (PVD->getType()->isObjCQualifiedIdType())
11909d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff      return StmtError(Diag(PVD->getLocation(),
1191d198abae5938262e501a409e833bf01ca8b8253eSteve Naroff                       diag::err_illegal_qualifiers_on_catch_parm));
11929d40ee50f8a013e5253101648092cf0daa76c335Steve Naroff  }
119393c4945c9ead2d374fe3fc528e3017c7167265beChris Lattner
11948189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  ObjCAtCatchStmt *CS = new (Context) ObjCAtCatchStmt(AtLoc, RParen,
1195e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson    PVD, Body.takeAs<Stmt>(), CatchList);
1196431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl  return Owned(CatchList ? CatchList : CS);
11973b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian}
11983b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian
1199431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
1200431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) {
12018189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) ObjCAtFinallyStmt(AtLoc,
12028189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                                           static_cast<Stmt*>(Body.release())));
1203161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian}
1204bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
1205431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
1206431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
1207431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl                         StmtArg Try, StmtArg Catch, StmtArg Finally) {
120838c5ebd7b1b65304c7b5c7b9bf3f9162df22e77dChris Lattner  CurFunctionNeedsScopeChecking = true;
1209e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson  return Owned(new (Context) ObjCAtTryStmt(AtLoc, Try.takeAs<Stmt>(),
1210e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                           Catch.takeAs<Stmt>(),
1211e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                           Finally.takeAs<Stmt>()));
1212bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian}
1213bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
1214431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
12153dcfe10a6801eb52f4c20f1242bea0a3a98aa146Steve NaroffSema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg expr,Scope *CurScope) {
1216f1b1d59a3f0650ab97b04235a14ae4549ca1c656Anders Carlsson  Expr *ThrowExpr = expr.takeAs<Expr>();
12177151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff  if (!ThrowExpr) {
1218e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    // @throw without an expression designates a rethrow (which much occur
1219e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    // in the context of an @catch clause).
1220e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    Scope *AtCatchParent = CurScope;
1221e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    while (AtCatchParent && !AtCatchParent->isAtCatchScope())
1222e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff      AtCatchParent = AtCatchParent->getParent();
1223e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff    if (!AtCatchParent)
12244ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff      return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch));
12257151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff  } else {
12267151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff    QualType ThrowType = ThrowExpr->getType();
12277151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff    // Make sure the expression type is an ObjC pointer or "void *".
1228f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    if (!ThrowType->isObjCObjectPointerType()) {
12296217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const PointerType *PT = ThrowType->getAs<PointerType>();
12307151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff      if (!PT || !PT->getPointeeType()->isVoidType())
12314ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff        return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object)
12324ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff                        << ThrowExpr->getType() << ThrowExpr->getSourceRange());
12337151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff    }
12347151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff  }
12357151bbb55c8a437073e42f74348c3fd5f1d5b410Steve Naroff  return Owned(new (Context) ObjCAtThrowStmt(AtLoc, ThrowExpr));
123639f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian}
1237bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian
1238431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlAction::OwningStmtResult
1239431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr,
1240431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian Redl                                  StmtArg SynchBody) {
124146c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner  CurFunctionNeedsScopeChecking = true;
124246c3c4ba78766ac0f1c5ec631b424773e21f5271Chris Lattner
1243a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  // Make sure the expression type is an ObjC pointer or "void *".
1244a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  Expr *SyncExpr = static_cast<Expr*>(SynchExpr.get());
1245f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff  if (!SyncExpr->getType()->isObjCObjectPointerType()) {
12466217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    const PointerType *PT = SyncExpr->getType()->getAs<PointerType>();
1247a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner    if (!PT || !PT->getPointeeType()->isVoidType())
1248a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner      return StmtError(Diag(AtLoc, diag::error_objc_synchronized_expects_object)
1249a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner                       << SyncExpr->getType() << SyncExpr->getSourceRange());
1250a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner  }
1251a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner
1252e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson  return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc,
1253e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                                    SynchExpr.takeAs<Stmt>(),
1254e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                                    SynchBody.takeAs<Stmt>()));
1255fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian}
12564b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl
12574b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
12584b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// and creates a proper catch handler from them.
12594b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian RedlAction::OwningStmtResult
1260b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerSema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclPtrTy ExDecl,
12614b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl                         StmtArg HandlerBlock) {
12624b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl  // There's nothing to test that ActOnExceptionDecl didn't already test.
12638189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CXXCatchStmt(CatchLoc,
1264b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                  cast_or_null<VarDecl>(ExDecl.getAs<Decl>()),
1265e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                          HandlerBlock.takeAs<Stmt>()));
12664b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl}
12678351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
1268c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlclass TypeWithHandler {
1269c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  QualType t;
1270c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  CXXCatchStmt *stmt;
1271c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlpublic:
1272c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  TypeWithHandler(const QualType &type, CXXCatchStmt *statement)
1273c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  : t(type), stmt(statement) {}
1274c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1275c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  bool operator<(const TypeWithHandler &y) const {
1276c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    if (t.getTypePtr() < y.t.getTypePtr())
1277c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return true;
1278c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    else if (t.getTypePtr() > y.t.getTypePtr())
1279c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return false;
1280c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    else if (t.getCVRQualifiers() < y.t.getCVRQualifiers())
1281c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return true;
1282c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    else if (t.getCVRQualifiers() < y.t.getCVRQualifiers())
1283c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return false;
1284c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    else
1285c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      return getTypeSpecStartLoc() < y.getTypeSpecStartLoc();
1286c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1287c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1288c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  bool operator==(const TypeWithHandler& other) const {
1289c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    return t.getTypePtr() == other.t.getTypePtr()
1290c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        && t.getCVRQualifiers() == other.t.getCVRQualifiers();
1291c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1292c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1293c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  QualType getQualType() const { return t; }
1294c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  CXXCatchStmt *getCatchStmt() const { return stmt; }
1295c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  SourceLocation getTypeSpecStartLoc() const {
1296c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    return stmt->getExceptionDecl()->getTypeSpecStartLoc();
1297c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1298c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl};
1299c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
13008351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
13018351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// handlers and creates a try statement from them.
13028351da06ca3082dfd49dd8e3c1785a986920f57cSebastian RedlAction::OwningStmtResult
13038351da06ca3082dfd49dd8e3c1785a986920f57cSebastian RedlSema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock,
13048351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl                       MultiStmtArg RawHandlers) {
13058351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  unsigned NumHandlers = RawHandlers.size();
13068351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  assert(NumHandlers > 0 &&
13078351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl         "The parser shouldn't call this if there are no handlers.");
13088351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get());
13098351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
1310c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  llvm::SmallVector<TypeWithHandler, 8> TypesWithHandlers;
1311c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1312c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  for(unsigned i = 0; i < NumHandlers; ++i) {
13138351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl    CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
1314c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    if (!Handler->getExceptionDecl()) {
1315c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      if (i < NumHandlers - 1)
1316c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        return StmtError(Diag(Handler->getLocStart(),
1317c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl                              diag::err_early_catch_all));
1318c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1319c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      continue;
1320c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    }
1321c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1322c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    const QualType CaughtType = Handler->getCaughtType();
1323c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    const QualType CanonicalCaughtType = Context.getCanonicalType(CaughtType);
1324c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    TypesWithHandlers.push_back(TypeWithHandler(CanonicalCaughtType, Handler));
1325c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  }
1326c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1327c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  // Detect handlers for the same type as an earlier one.
1328c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl  if (NumHandlers > 1) {
1329c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    llvm::array_pod_sort(TypesWithHandlers.begin(), TypesWithHandlers.end());
1330c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1331c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    TypeWithHandler prev = TypesWithHandlers[0];
1332c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    for (unsigned i = 1; i < TypesWithHandlers.size(); ++i) {
1333c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      TypeWithHandler curr = TypesWithHandlers[i];
1334c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1335c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      if (curr == prev) {
1336c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        Diag(curr.getTypeSpecStartLoc(),
1337c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl             diag::warn_exception_caught_by_earlier_handler)
1338c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl          << curr.getCatchStmt()->getCaughtType().getAsString();
1339c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl        Diag(prev.getTypeSpecStartLoc(),
1340c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl             diag::note_previous_exception_handler)
1341c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl          << prev.getCatchStmt()->getCaughtType().getAsString();
1342c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      }
1343c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
1344c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl      prev = curr;
1345c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl    }
13468351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  }
1347c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl
13488351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // FIXME: We should detect handlers that cannot catch anything because an
13498351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // earlier handler catches a superclass. Need to find a method that is not
13508351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // quadratic for this.
13518351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // Neither of these are explicitly forbidden, but every compiler detects them
13528351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  // and warns.
13538351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl
1354972041f45bdf8df7ea447221292d7827466ba94bSebastian Redl  CurFunctionNeedsScopeChecking = true;
13558351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl  RawHandlers.release();
13568189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  return Owned(new (Context) CXXTryStmt(TryLoc,
13578189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                                        static_cast<Stmt*>(TryBlock.release()),
13588189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek                                        Handlers, NumHandlers));
13598351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl}
1360