Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for statements. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 15 | #include "clang/AST/APValue.h" |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 23 | Sema::OwningStmtResult Sema::ActOnExprStmt(ExprArg expr) { |
| 24 | Expr *E = static_cast<Expr*>(expr.release()); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 25 | assert(E && "ActOnExprStmt(): missing expression"); |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 27 | // C99 6.8.3p2: The expression in an expression statement is evaluated as a |
| 28 | // void expression for its side effects. Conversion to void allows any |
| 29 | // operand, even incomplete types. |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 31 | // Same thing in for stmt first clause (when expr) and third clause. |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 32 | return Owned(static_cast<Stmt*>(E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 36 | Sema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 37 | return Owned(new (Context) NullStmt(SemiLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 40 | Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclTy *decl, |
| 41 | SourceLocation StartLoc, |
| 42 | SourceLocation EndLoc) { |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 43 | if (decl == 0) |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 44 | return StmtError(); |
| 45 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 46 | Decl *D = static_cast<Decl *>(decl); |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 47 | |
Ted Kremenek | 8ffb159 | 2008-10-07 23:09:49 +0000 | [diff] [blame] | 48 | // This is a temporary hack until we are always passing around |
| 49 | // DeclGroupRefs. |
| 50 | llvm::SmallVector<Decl*, 10> decls; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 51 | while (D) { |
| 52 | Decl* d = D; |
| 53 | D = D->getNextDeclarator(); |
Ted Kremenek | 8ffb159 | 2008-10-07 23:09:49 +0000 | [diff] [blame] | 54 | d->setNextDeclarator(0); |
| 55 | decls.push_back(d); |
| 56 | } |
| 57 | |
| 58 | assert (!decls.empty()); |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | 8ffb159 | 2008-10-07 23:09:49 +0000 | [diff] [blame] | 60 | if (decls.size() == 1) { |
Douglas Gregor | 9653db7 | 2009-02-13 19:06:18 +0000 | [diff] [blame] | 61 | DeclGroupRef DG(*decls.begin()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 62 | return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc)); |
Ted Kremenek | 8ffb159 | 2008-10-07 23:09:49 +0000 | [diff] [blame] | 63 | } |
| 64 | else { |
Douglas Gregor | 9653db7 | 2009-02-13 19:06:18 +0000 | [diff] [blame] | 65 | DeclGroupRef DG(DeclGroup::Create(Context, decls.size(), &decls[0])); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 66 | return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc)); |
Ted Kremenek | 8ffb159 | 2008-10-07 23:09:49 +0000 | [diff] [blame] | 67 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 70 | Action::OwningStmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 71 | Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 72 | MultiStmtArg elts, bool isStmtExpr) { |
| 73 | unsigned NumElts = elts.size(); |
| 74 | Stmt **Elts = reinterpret_cast<Stmt**>(elts.release()); |
Chris Lattner | c30ebfb | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 75 | // If we're in C89 mode, check that we don't have any decls after stmts. If |
| 76 | // so, emit an extension diagnostic. |
| 77 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) { |
| 78 | // Note that __extension__ can be around a decl. |
| 79 | unsigned i = 0; |
| 80 | // Skip over all declarations. |
| 81 | for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) |
| 82 | /*empty*/; |
| 83 | |
| 84 | // We found the end of the list or a statement. Scan for another declstmt. |
| 85 | for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) |
| 86 | /*empty*/; |
| 87 | |
| 88 | if (i != NumElts) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 89 | Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin(); |
Chris Lattner | c30ebfb | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 90 | Diag(D->getLocation(), diag::ext_mixed_decls_code); |
| 91 | } |
| 92 | } |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 93 | // Warn about unused expressions in statements. |
| 94 | for (unsigned i = 0; i != NumElts; ++i) { |
| 95 | Expr *E = dyn_cast<Expr>(Elts[i]); |
| 96 | if (!E) continue; |
| 97 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 98 | // Warn about expressions with unused results if they are non-void and if |
| 99 | // this not the last stmt in a stmt expr. |
| 100 | if (E->getType()->isVoidType() || (isStmtExpr && i == NumElts-1)) |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 101 | continue; |
| 102 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 103 | SourceLocation Loc; |
| 104 | SourceRange R1, R2; |
| 105 | if (!E->isUnusedResultAWarning(Loc, R1, R2)) |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 106 | continue; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 107 | |
| 108 | Diag(Loc, diag::warn_unused_expr) << R1 << R2; |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 109 | } |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 111 | return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 114 | Action::OwningStmtResult |
| 115 | Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval, |
| 116 | SourceLocation DotDotDotLoc, ExprArg rhsval, |
| 117 | SourceLocation ColonLoc, StmtArg subStmt) { |
| 118 | Stmt *SubStmt = static_cast<Stmt*>(subStmt.release()); |
| 119 | assert((lhsval.get() != 0) && "missing expression in case statement"); |
| 120 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 122 | // However, GCC allows any evaluatable integer expression. |
Anders Carlsson | d3a61d5 | 2008-12-01 02:13:02 +0000 | [diff] [blame] | 123 | |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 124 | Expr *LHSVal = static_cast<Expr*>(lhsval.get()); |
Anders Carlsson | d3a61d5 | 2008-12-01 02:13:02 +0000 | [diff] [blame] | 125 | if (VerifyIntegerConstantExpression(LHSVal)) |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 126 | return Owned(SubStmt); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 128 | // GCC extension: The expression shall be an integer constant. |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 129 | |
| 130 | Expr *RHSVal = static_cast<Expr*>(rhsval.get()); |
| 131 | if (RHSVal && VerifyIntegerConstantExpression(RHSVal)) { |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 132 | RHSVal = 0; // Recover by just forgetting about it. |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 133 | rhsval = 0; |
| 134 | } |
| 135 | |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 136 | if (SwitchStack.empty()) { |
| 137 | Diag(CaseLoc, diag::err_case_not_in_switch); |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 138 | return Owned(SubStmt); |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 139 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 140 | |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 141 | // Only now release the smart pointers. |
| 142 | lhsval.release(); |
| 143 | rhsval.release(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 144 | CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc); |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 145 | SwitchStack.back()->addSwitchCase(CS); |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 146 | return Owned(CS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 149 | Action::OwningStmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 150 | Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 151 | StmtArg subStmt, Scope *CurScope) { |
| 152 | Stmt *SubStmt = static_cast<Stmt*>(subStmt.release()); |
| 153 | |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 154 | if (SwitchStack.empty()) { |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 155 | Diag(DefaultLoc, diag::err_default_not_in_switch); |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 156 | return Owned(SubStmt); |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 157 | } |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 158 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 159 | DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, SubStmt); |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 160 | SwitchStack.back()->addSwitchCase(DS); |
Sebastian Redl | 117054a | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 161 | return Owned(DS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 164 | Action::OwningStmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 165 | Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 166 | SourceLocation ColonLoc, StmtArg subStmt) { |
| 167 | Stmt *SubStmt = static_cast<Stmt*>(subStmt.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | // Look up the record for this label identifier. |
| 169 | LabelStmt *&LabelDecl = LabelMap[II]; |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 170 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | // If not forward referenced or defined already, just create a new LabelStmt. |
| 172 | if (LabelDecl == 0) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 173 | return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt)); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | assert(LabelDecl->getID() == II && "Label mismatch!"); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 176 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 177 | // Otherwise, this label was either forward reference or multiply defined. If |
| 178 | // multiply defined, reject it now. |
| 179 | if (LabelDecl->getSubStmt()) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 180 | Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 181 | Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 182 | return Owned(SubStmt); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 183 | } |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 184 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 185 | // Otherwise, this label was forward declared, and we just found its real |
| 186 | // definition. Fill in the forward definition and return it. |
| 187 | LabelDecl->setIdentLoc(IdentLoc); |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 188 | LabelDecl->setSubStmt(SubStmt); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 189 | return Owned(LabelDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 192 | Action::OwningStmtResult |
| 193 | Sema::ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal, |
| 194 | StmtArg ThenVal, SourceLocation ElseLoc, |
| 195 | StmtArg ElseVal) { |
| 196 | Expr *condExpr = (Expr *)CondVal.release(); |
| 197 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 198 | assert(condExpr && "ActOnIfStmt(): missing expression"); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 199 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 200 | DefaultFunctionArrayConversion(condExpr); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 201 | // Take ownership again until we're past the error checking. |
| 202 | CondVal = condExpr; |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 203 | QualType condType = condExpr->getType(); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 204 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 205 | if (getLangOptions().CPlusPlus) { |
| 206 | if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4 |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 207 | return StmtError(); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 208 | } else if (!condType->isScalarType()) // C99 6.8.4.1p1 |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 209 | return StmtError(Diag(IfLoc, diag::err_typecheck_statement_requires_scalar) |
| 210 | << condType << condExpr->getSourceRange()); |
| 211 | |
| 212 | Stmt *thenStmt = (Stmt *)ThenVal.release(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | |
Anders Carlsson | 2d85f8b | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 214 | // Warn if the if block has a null body without an else value. |
| 215 | // this helps prevent bugs due to typos, such as |
| 216 | // if (condition); |
| 217 | // do_stuff(); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 218 | if (!ElseVal.get()) { |
Anders Carlsson | 2d85f8b | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 219 | if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt)) |
| 220 | Diag(stmt->getSemiLoc(), diag::warn_empty_if_body); |
| 221 | } |
| 222 | |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 223 | CondVal.release(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 224 | return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt, |
| 225 | (Stmt*)ElseVal.release())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 228 | Action::OwningStmtResult |
| 229 | Sema::ActOnStartOfSwitchStmt(ExprArg cond) { |
| 230 | Expr *Cond = static_cast<Expr*>(cond.release()); |
| 231 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 232 | if (getLangOptions().CPlusPlus) { |
| 233 | // C++ 6.4.2.p2: |
| 234 | // The condition shall be of integral type, enumeration type, or of a class |
| 235 | // type for which a single conversion function to integral or enumeration |
| 236 | // type exists (12.3). If the condition is of class type, the condition is |
| 237 | // converted by calling that conversion function, and the result of the |
| 238 | // conversion is used in place of the original condition for the remainder |
| 239 | // of this section. Integral promotions are performed. |
| 240 | |
| 241 | QualType Ty = Cond->getType(); |
| 242 | |
| 243 | // FIXME: Handle class types. |
| 244 | |
| 245 | // If the type is wrong a diagnostic will be emitted later at |
| 246 | // ActOnFinishSwitchStmt. |
| 247 | if (Ty->isIntegralType() || Ty->isEnumeralType()) { |
| 248 | // Integral promotions are performed. |
| 249 | // FIXME: Integral promotions for C++ are not complete. |
| 250 | UsualUnaryConversions(Cond); |
| 251 | } |
| 252 | } else { |
| 253 | // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. |
| 254 | UsualUnaryConversions(Cond); |
| 255 | } |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 256 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 257 | SwitchStmt *SS = new (Context) SwitchStmt(Cond); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 258 | SwitchStack.push_back(SS); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 259 | return Owned(SS); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 260 | } |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 261 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 262 | /// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have |
| 263 | /// the specified width and sign. If an overflow occurs, detect it and emit |
| 264 | /// the specified diagnostic. |
| 265 | void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, |
| 266 | unsigned NewWidth, bool NewSign, |
| 267 | SourceLocation Loc, |
| 268 | unsigned DiagID) { |
| 269 | // Perform a conversion to the promoted condition type if needed. |
| 270 | if (NewWidth > Val.getBitWidth()) { |
| 271 | // If this is an extension, just do it. |
| 272 | llvm::APSInt OldVal(Val); |
| 273 | Val.extend(NewWidth); |
| 274 | |
| 275 | // If the input was signed and negative and the output is unsigned, |
| 276 | // warn. |
| 277 | if (!NewSign && OldVal.isSigned() && OldVal.isNegative()) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 278 | Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 279 | |
| 280 | Val.setIsSigned(NewSign); |
| 281 | } else if (NewWidth < Val.getBitWidth()) { |
| 282 | // If this is a truncation, check for overflow. |
| 283 | llvm::APSInt ConvVal(Val); |
| 284 | ConvVal.trunc(NewWidth); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 285 | ConvVal.setIsSigned(NewSign); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 286 | ConvVal.extend(Val.getBitWidth()); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 287 | ConvVal.setIsSigned(Val.isSigned()); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 288 | if (ConvVal != Val) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 289 | Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 290 | |
| 291 | // Regardless of whether a diagnostic was emitted, really do the |
| 292 | // truncation. |
| 293 | Val.trunc(NewWidth); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 294 | Val.setIsSigned(NewSign); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 295 | } else if (NewSign != Val.isSigned()) { |
| 296 | // Convert the sign to match the sign of the condition. This can cause |
| 297 | // overflow as well: unsigned(INTMIN) |
| 298 | llvm::APSInt OldVal(Val); |
| 299 | Val.setIsSigned(NewSign); |
| 300 | |
| 301 | if (Val.isNegative()) // Sign bit changes meaning. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 302 | Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 306 | namespace { |
| 307 | struct CaseCompareFunctor { |
| 308 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 309 | const llvm::APSInt &RHS) { |
| 310 | return LHS.first < RHS; |
| 311 | } |
Chris Lattner | 0e85a27 | 2007-09-03 18:31:57 +0000 | [diff] [blame] | 312 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 313 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 314 | return LHS.first < RHS.first; |
| 315 | } |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 316 | bool operator()(const llvm::APSInt &LHS, |
| 317 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 318 | return LHS < RHS.first; |
| 319 | } |
| 320 | }; |
| 321 | } |
| 322 | |
Chris Lattner | 764a7ce | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 323 | /// CmpCaseVals - Comparison predicate for sorting case values. |
| 324 | /// |
| 325 | static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, |
| 326 | const std::pair<llvm::APSInt, CaseStmt*>& rhs) { |
| 327 | if (lhs.first < rhs.first) |
| 328 | return true; |
| 329 | |
| 330 | if (lhs.first == rhs.first && |
| 331 | lhs.second->getCaseLoc().getRawEncoding() |
| 332 | < rhs.second->getCaseLoc().getRawEncoding()) |
| 333 | return true; |
| 334 | return false; |
| 335 | } |
| 336 | |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 337 | Action::OwningStmtResult |
| 338 | Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch, |
| 339 | StmtArg Body) { |
| 340 | Stmt *BodyStmt = (Stmt*)Body.release(); |
| 341 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 342 | SwitchStmt *SS = SwitchStack.back(); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 343 | assert(SS == (SwitchStmt*)Switch.get() && "switch stack missing push/pop!"); |
| 344 | |
Steve Naroff | 9dcbfa4 | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 345 | SS->setBody(BodyStmt, SwitchLoc); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 346 | SwitchStack.pop_back(); |
| 347 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 348 | Expr *CondExpr = SS->getCond(); |
| 349 | QualType CondType = CondExpr->getType(); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 350 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 351 | if (!CondType->isIntegerType()) { // C99 6.8.4.2p1 |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 352 | Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 353 | << CondType << CondExpr->getSourceRange(); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 354 | return StmtError(); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 355 | } |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 356 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 357 | // Get the bitwidth of the switched-on value before promotions. We must |
| 358 | // convert the integer case values to this width before comparison. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 359 | unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType)); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 360 | bool CondIsSigned = CondType->isSignedIntegerType(); |
| 361 | |
| 362 | // Accumulate all of the case values in a vector so that we can sort them |
| 363 | // and detect duplicates. This vector contains the APInt for the case after |
| 364 | // it has been converted to the condition type. |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 365 | typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; |
| 366 | CaseValsTy CaseVals; |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 367 | |
| 368 | // Keep track of any GNU case ranges we see. The APSInt is the low value. |
| 369 | std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges; |
| 370 | |
| 371 | DefaultStmt *TheDefaultStmt = 0; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 372 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 373 | bool CaseListIsErroneous = false; |
| 374 | |
| 375 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 376 | SC = SC->getNextSwitchCase()) { |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 377 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 378 | if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 379 | if (TheDefaultStmt) { |
| 380 | Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 381 | Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev); |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 382 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 383 | // FIXME: Remove the default statement from the switch block so that |
| 384 | // we'll return a valid AST. This requires recursing down the |
| 385 | // AST and finding it, not something we are set up to do right now. For |
| 386 | // now, just lop the entire switch stmt out of the AST. |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 387 | CaseListIsErroneous = true; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 388 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 389 | TheDefaultStmt = DS; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 390 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 391 | } else { |
| 392 | CaseStmt *CS = cast<CaseStmt>(SC); |
| 393 | |
| 394 | // We already verified that the expression has a i-c-e value (C99 |
| 395 | // 6.8.4.2p3) - get that value now. |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 396 | Expr *Lo = CS->getLHS(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 397 | llvm::APSInt LoVal = Lo->EvaluateAsInt(Context); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 398 | |
| 399 | // Convert the value to the same width/sign as the condition. |
| 400 | ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, |
| 401 | CS->getLHS()->getLocStart(), |
| 402 | diag::warn_case_value_overflow); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 404 | // If the LHS is not the same type as the condition, insert an implicit |
| 405 | // cast. |
| 406 | ImpCastExprToType(Lo, CondType); |
| 407 | CS->setLHS(Lo); |
| 408 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 409 | // If this is a case range, remember it in CaseRanges, otherwise CaseVals. |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 410 | if (CS->getRHS()) |
| 411 | CaseRanges.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 412 | else |
| 413 | CaseVals.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 417 | // Sort all the scalar case values so we can easily detect duplicates. |
Chris Lattner | 764a7ce | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 418 | std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 419 | |
Chris Lattner | f334850 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 420 | if (!CaseVals.empty()) { |
| 421 | for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { |
| 422 | if (CaseVals[i].first == CaseVals[i+1].first) { |
| 423 | // If we have a duplicate, report it. |
| 424 | Diag(CaseVals[i+1].second->getLHS()->getLocStart(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 425 | diag::err_duplicate_case) << CaseVals[i].first.toString(10); |
Chris Lattner | f334850 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 426 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 427 | diag::note_duplicate_case_prev); |
Chris Lattner | f334850 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 428 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 429 | // but we have no way to do this right now. |
| 430 | CaseListIsErroneous = true; |
| 431 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 434 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 435 | // Detect duplicate case ranges, which usually don't exist at all in the first |
| 436 | // place. |
| 437 | if (!CaseRanges.empty()) { |
| 438 | // Sort all the case ranges by their low value so we can easily detect |
| 439 | // overlaps between ranges. |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 440 | std::stable_sort(CaseRanges.begin(), CaseRanges.end()); |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 441 | |
| 442 | // Scan the ranges, computing the high values and removing empty ranges. |
| 443 | std::vector<llvm::APSInt> HiVals; |
Chris Lattner | 6efc4d3 | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 444 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 445 | CaseStmt *CR = CaseRanges[i].second; |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 446 | Expr *Hi = CR->getRHS(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 447 | llvm::APSInt HiVal = Hi->EvaluateAsInt(Context); |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 448 | |
| 449 | // Convert the value to the same width/sign as the condition. |
| 450 | ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, |
| 451 | CR->getRHS()->getLocStart(), |
| 452 | diag::warn_case_value_overflow); |
| 453 | |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 454 | // If the LHS is not the same type as the condition, insert an implicit |
| 455 | // cast. |
| 456 | ImpCastExprToType(Hi, CondType); |
| 457 | CR->setRHS(Hi); |
| 458 | |
Chris Lattner | 6efc4d3 | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 459 | // If the low value is bigger than the high value, the case is empty. |
| 460 | if (CaseRanges[i].first > HiVal) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 461 | Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range) |
| 462 | << SourceRange(CR->getLHS()->getLocStart(), |
| 463 | CR->getRHS()->getLocEnd()); |
Chris Lattner | 6efc4d3 | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 464 | CaseRanges.erase(CaseRanges.begin()+i); |
| 465 | --i, --e; |
| 466 | continue; |
| 467 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 468 | HiVals.push_back(HiVal); |
| 469 | } |
| 470 | |
| 471 | // Rescan the ranges, looking for overlap with singleton values and other |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 472 | // ranges. Since the range list is sorted, we only need to compare case |
| 473 | // ranges with their neighbors. |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 474 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 475 | llvm::APSInt &CRLo = CaseRanges[i].first; |
| 476 | llvm::APSInt &CRHi = HiVals[i]; |
| 477 | CaseStmt *CR = CaseRanges[i].second; |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 478 | |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 479 | // Check to see whether the case range overlaps with any singleton cases. |
| 480 | CaseStmt *OverlapStmt = 0; |
| 481 | llvm::APSInt OverlapVal(32); |
| 482 | |
| 483 | // Find the smallest value >= the lower bound. If I is in the case range, |
| 484 | // then we have overlap. |
| 485 | CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), |
| 486 | CaseVals.end(), CRLo, |
| 487 | CaseCompareFunctor()); |
| 488 | if (I != CaseVals.end() && I->first < CRHi) { |
| 489 | OverlapVal = I->first; // Found overlap with scalar. |
| 490 | OverlapStmt = I->second; |
| 491 | } |
| 492 | |
| 493 | // Find the smallest value bigger than the upper bound. |
| 494 | I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); |
| 495 | if (I != CaseVals.begin() && (I-1)->first >= CRLo) { |
| 496 | OverlapVal = (I-1)->first; // Found overlap with scalar. |
| 497 | OverlapStmt = (I-1)->second; |
| 498 | } |
| 499 | |
| 500 | // Check to see if this case stmt overlaps with the subsequent case range. |
| 501 | if (i && CRLo <= HiVals[i-1]) { |
| 502 | OverlapVal = HiVals[i-1]; // Found overlap with range. |
| 503 | OverlapStmt = CaseRanges[i-1].second; |
| 504 | } |
| 505 | |
| 506 | if (OverlapStmt) { |
| 507 | // If we have a duplicate, report it. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 508 | Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case) |
| 509 | << OverlapVal.toString(10); |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 510 | Diag(OverlapStmt->getLHS()->getLocStart(), |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 511 | diag::note_duplicate_case_prev); |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 512 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 513 | // but we have no way to do this right now. |
| 514 | CaseListIsErroneous = true; |
| 515 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 518 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 519 | // FIXME: If the case list was broken is some way, we don't have a good system |
| 520 | // to patch it up. Instead, just return the whole substmt as broken. |
| 521 | if (CaseListIsErroneous) |
Sebastian Redl | de30747 | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 522 | return StmtError(); |
| 523 | |
| 524 | Switch.release(); |
| 525 | return Owned(SS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 528 | Action::OwningStmtResult |
| 529 | Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprArg Cond, StmtArg Body) { |
| 530 | Expr *condExpr = (Expr *)Cond.release(); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 531 | assert(condExpr && "ActOnWhileStmt(): missing expression"); |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 532 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 533 | DefaultFunctionArrayConversion(condExpr); |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 534 | Cond = condExpr; |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 535 | QualType condType = condExpr->getType(); |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 536 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 537 | if (getLangOptions().CPlusPlus) { |
| 538 | if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4 |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 539 | return StmtError(); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 540 | } else if (!condType->isScalarType()) // C99 6.8.5p2 |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 541 | return StmtError(Diag(WhileLoc, |
| 542 | diag::err_typecheck_statement_requires_scalar) |
| 543 | << condType << condExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 544 | |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 545 | Cond.release(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 546 | return Owned(new (Context) WhileStmt(condExpr, (Stmt*)Body.release(), |
| 547 | WhileLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 550 | Action::OwningStmtResult |
| 551 | Sema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body, |
| 552 | SourceLocation WhileLoc, ExprArg Cond) { |
| 553 | Expr *condExpr = (Expr *)Cond.release(); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 554 | assert(condExpr && "ActOnDoStmt(): missing expression"); |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 555 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 556 | DefaultFunctionArrayConversion(condExpr); |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 557 | Cond = condExpr; |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 558 | QualType condType = condExpr->getType(); |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 559 | |
Argyrios Kyrtzidis | 6314ff2 | 2008-09-11 05:16:22 +0000 | [diff] [blame] | 560 | if (getLangOptions().CPlusPlus) { |
| 561 | if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4 |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 562 | return StmtError(); |
Argyrios Kyrtzidis | 6314ff2 | 2008-09-11 05:16:22 +0000 | [diff] [blame] | 563 | } else if (!condType->isScalarType()) // C99 6.8.5p2 |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 564 | return StmtError(Diag(DoLoc, diag::err_typecheck_statement_requires_scalar) |
| 565 | << condType << condExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 566 | |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 567 | Cond.release(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 568 | return Owned(new (Context) DoStmt((Stmt*)Body.release(), condExpr, DoLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 571 | Action::OwningStmtResult |
| 572 | Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
| 573 | StmtArg first, ExprArg second, ExprArg third, |
| 574 | SourceLocation RParenLoc, StmtArg body) { |
| 575 | Stmt *First = static_cast<Stmt*>(first.get()); |
| 576 | Expr *Second = static_cast<Expr*>(second.get()); |
| 577 | Expr *Third = static_cast<Expr*>(third.get()); |
| 578 | Stmt *Body = static_cast<Stmt*>(body.get()); |
| 579 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 580 | if (!getLangOptions().CPlusPlus) { |
| 581 | if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 582 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only |
| 583 | // declare identifiers for objects having storage class 'auto' or |
| 584 | // 'register'. |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 585 | for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); |
| 586 | DI!=DE; ++DI) { |
| 587 | VarDecl *VD = dyn_cast<VarDecl>(*DI); |
| 588 | if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage()) |
| 589 | VD = 0; |
| 590 | if (VD == 0) |
| 591 | Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for); |
| 592 | // FIXME: mark decl erroneous! |
| 593 | } |
Chris Lattner | ae3b701 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 594 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 595 | } |
| 596 | if (Second) { |
Chris Lattner | 36c4b0e | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 597 | DefaultFunctionArrayConversion(Second); |
| 598 | QualType SecondType = Second->getType(); |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 599 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 600 | if (getLangOptions().CPlusPlus) { |
| 601 | if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4 |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 602 | return StmtError(); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 603 | } else if (!SecondType->isScalarType()) // C99 6.8.5p2 |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 604 | return StmtError(Diag(ForLoc, |
| 605 | diag::err_typecheck_statement_requires_scalar) |
| 606 | << SecondType << Second->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 607 | } |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 608 | first.release(); |
| 609 | second.release(); |
| 610 | third.release(); |
| 611 | body.release(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 612 | return Owned(new (Context) ForStmt(First, Second, Third, Body, ForLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 615 | Action::OwningStmtResult |
| 616 | Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, |
| 617 | SourceLocation LParenLoc, |
| 618 | StmtArg first, ExprArg second, |
| 619 | SourceLocation RParenLoc, StmtArg body) { |
| 620 | Stmt *First = static_cast<Stmt*>(first.get()); |
| 621 | Expr *Second = static_cast<Expr*>(second.get()); |
| 622 | Stmt *Body = static_cast<Stmt*>(body.get()); |
Fariborz Jahanian | 20552d2 | 2008-01-10 20:33:58 +0000 | [diff] [blame] | 623 | if (First) { |
| 624 | QualType FirstType; |
| 625 | if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) { |
Ted Kremenek | f34afee | 2008-10-06 20:58:11 +0000 | [diff] [blame] | 626 | if (!DS->hasSolitaryDecl()) |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 627 | return StmtError(Diag((*DS->decl_begin())->getLocation(), |
| 628 | diag::err_toomany_element_decls)); |
| 629 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 630 | Decl *D = DS->getSolitaryDecl(); |
Ted Kremenek | f34afee | 2008-10-06 20:58:11 +0000 | [diff] [blame] | 631 | FirstType = cast<ValueDecl>(D)->getType(); |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 632 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only |
| 633 | // declare identifiers for objects having storage class 'auto' or |
| 634 | // 'register'. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 635 | VarDecl *VD = cast<VarDecl>(D); |
| 636 | if (VD->isBlockVarDecl() && !VD->hasLocalStorage()) |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 637 | return StmtError(Diag(VD->getLocation(), |
| 638 | diag::err_non_variable_decl_in_for)); |
Anders Carlsson | 1fe379f | 2008-08-25 18:16:36 +0000 | [diff] [blame] | 639 | } else { |
| 640 | Expr::isLvalueResult lval = cast<Expr>(First)->isLvalue(Context); |
Anders Carlsson | 1fe379f | 2008-08-25 18:16:36 +0000 | [diff] [blame] | 641 | |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 642 | if (lval != Expr::LV_Valid) |
| 643 | return StmtError(Diag(First->getLocStart(), |
| 644 | diag::err_selector_element_not_lvalue) |
| 645 | << First->getSourceRange()); |
| 646 | |
| 647 | FirstType = static_cast<Expr*>(First)->getType(); |
Anders Carlsson | 1fe379f | 2008-08-25 18:16:36 +0000 | [diff] [blame] | 648 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 649 | if (!Context.isObjCObjectPointerType(FirstType)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 650 | Diag(ForLoc, diag::err_selector_element_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 651 | << FirstType << First->getSourceRange(); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 652 | } |
| 653 | if (Second) { |
| 654 | DefaultFunctionArrayConversion(Second); |
| 655 | QualType SecondType = Second->getType(); |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 656 | if (!Context.isObjCObjectPointerType(SecondType)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 657 | Diag(ForLoc, diag::err_collection_expr_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 658 | << SecondType << Second->getSourceRange(); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 659 | } |
Sebastian Redl | f05b152 | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 660 | first.release(); |
| 661 | second.release(); |
| 662 | body.release(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 663 | return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body, |
| 664 | ForLoc, RParenLoc)); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 665 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 666 | |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 667 | Action::OwningStmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 668 | Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 669 | IdentifierInfo *LabelII) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 670 | // If we are in a block, reject all gotos for now. |
| 671 | if (CurBlock) |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 672 | return StmtError(Diag(GotoLoc, diag::err_goto_in_block)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 673 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 674 | // Look up the record for this label identifier. |
| 675 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 676 | |
| 677 | // If we haven't seen this label yet, create a forward reference. |
| 678 | if (LabelDecl == 0) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 679 | LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0); |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 680 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 681 | return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 684 | Action::OwningStmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 685 | Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 686 | ExprArg DestExp) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 687 | // FIXME: Verify that the operand is convertible to void*. |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 688 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 689 | return Owned(new (Context) IndirectGotoStmt((Expr*)DestExp.release())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 692 | Action::OwningStmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 693 | Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 694 | Scope *S = CurScope->getContinueParent(); |
| 695 | if (!S) { |
| 696 | // C99 6.8.6.2p1: A break shall appear only in or as a loop body. |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 697 | return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 698 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 699 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 700 | return Owned(new (Context) ContinueStmt(ContinueLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 703 | Action::OwningStmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 704 | Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 705 | Scope *S = CurScope->getBreakParent(); |
| 706 | if (!S) { |
| 707 | // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 708 | return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 709 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 710 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 711 | return Owned(new (Context) BreakStmt(BreakLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 714 | /// ActOnBlockReturnStmt - Utility routine to figure out block's return type. |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 715 | /// |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 716 | Action::OwningStmtResult |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 717 | Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 718 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 719 | // If this is the first return we've seen in the block, infer the type of |
| 720 | // the block from it. |
| 721 | if (CurBlock->ReturnType == 0) { |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 722 | if (RetValExp) { |
Steve Naroff | 1656442 | 2008-09-24 22:26:48 +0000 | [diff] [blame] | 723 | // Don't call UsualUnaryConversions(), since we don't want to do |
| 724 | // integer promotions here. |
| 725 | DefaultFunctionArrayConversion(RetValExp); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 726 | CurBlock->ReturnType = RetValExp->getType().getTypePtr(); |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 727 | } else |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 728 | CurBlock->ReturnType = Context.VoidTy.getTypePtr(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 729 | } |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 730 | QualType FnRetType = QualType(CurBlock->ReturnType, 0); |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 731 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 732 | // Otherwise, verify that this result type matches the previous one. We are |
| 733 | // pickier with blocks than for normal functions because we don't have GCC |
| 734 | // compatibility to worry about here. |
| 735 | if (CurBlock->ReturnType->isVoidType()) { |
| 736 | if (RetValExp) { |
| 737 | Diag(ReturnLoc, diag::err_return_block_has_expr); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 738 | RetValExp->Destroy(Context); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 739 | RetValExp = 0; |
| 740 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 741 | return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 742 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 743 | |
| 744 | if (!RetValExp) |
| 745 | return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr)); |
| 746 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 747 | if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) { |
| 748 | // we have a non-void block with an expression, continue checking |
| 749 | QualType RetValType = RetValExp->getType(); |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 750 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 751 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 752 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 753 | // function return. |
| 754 | |
| 755 | // In C++ the return statement is handled via a copy initialization. |
| 756 | // the C version of which boils down to CheckSingleAssignmentConstraints. |
| 757 | // FIXME: Leaks RetValExp. |
| 758 | if (PerformCopyInitialization(RetValExp, FnRetType, "returning")) |
| 759 | return StmtError(); |
| 760 | |
| 761 | if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 762 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 763 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 764 | return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 765 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 766 | |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 767 | Action::OwningStmtResult |
| 768 | Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) { |
| 769 | Expr *RetValExp = static_cast<Expr *>(rex.release()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 770 | if (CurBlock) |
| 771 | return ActOnBlockReturnStmt(ReturnLoc, RetValExp); |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 772 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 773 | QualType FnRetType; |
| 774 | if (FunctionDecl *FD = getCurFunctionDecl()) |
| 775 | FnRetType = FD->getResultType(); |
| 776 | else |
| 777 | FnRetType = getCurMethodDecl()->getResultType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 778 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 779 | if (FnRetType->isVoidType()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 780 | if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns) |
Chris Lattner | 65ce04b | 2008-12-18 02:01:17 +0000 | [diff] [blame] | 781 | unsigned D = diag::ext_return_has_expr; |
| 782 | if (RetValExp->getType()->isVoidType()) |
| 783 | D = diag::ext_return_has_void_expr; |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 784 | |
Chris Lattner | e878eb0 | 2008-12-18 02:03:48 +0000 | [diff] [blame] | 785 | // return (some void expression); is legal in C++. |
| 786 | if (D != diag::ext_return_has_void_expr || |
| 787 | !getLangOptions().CPlusPlus) { |
| 788 | NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); |
| 789 | Diag(ReturnLoc, D) |
| 790 | << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl) |
| 791 | << RetValExp->getSourceRange(); |
| 792 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 793 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 794 | return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 795 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 796 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 797 | if (!RetValExp) { |
| 798 | unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4 |
| 799 | // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 800 | if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr; |
| 801 | |
| 802 | if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 803 | Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/; |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 804 | else |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 805 | Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 806 | return Owned(new (Context) ReturnStmt(ReturnLoc, (Expr*)0)); |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 807 | } |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 808 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 809 | if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) { |
| 810 | // we have a non-void function with an expression, continue checking |
| 811 | QualType RetValType = RetValExp->getType(); |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 812 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 813 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 814 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 815 | // function return. |
| 816 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 817 | // In C++ the return statement is handled via a copy initialization. |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 818 | // the C version of which boils down to CheckSingleAssignmentConstraints. |
| 819 | // FIXME: Leaks RetValExp. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 820 | if (PerformCopyInitialization(RetValExp, FnRetType, "returning")) |
Sebastian Redl | 4cffe2f | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 821 | return StmtError(); |
| 822 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 823 | if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); |
| 824 | } |
| 825 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 826 | return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 829 | Sema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, |
| 830 | bool IsSimple, |
| 831 | bool IsVolatile, |
| 832 | unsigned NumOutputs, |
| 833 | unsigned NumInputs, |
| 834 | std::string *Names, |
| 835 | MultiExprArg constraints, |
| 836 | MultiExprArg exprs, |
| 837 | ExprArg asmString, |
| 838 | MultiExprArg clobbers, |
| 839 | SourceLocation RParenLoc) { |
| 840 | unsigned NumClobbers = clobbers.size(); |
| 841 | StringLiteral **Constraints = |
| 842 | reinterpret_cast<StringLiteral**>(constraints.get()); |
| 843 | Expr **Exprs = reinterpret_cast<Expr **>(exprs.get()); |
| 844 | StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString.get()); |
| 845 | StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get()); |
| 846 | |
Anders Carlsson | 03eb543 | 2009-01-27 20:38:24 +0000 | [diff] [blame] | 847 | llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
| 848 | |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 849 | // The parser verifies that there is a string literal here. |
Chris Lattner | 6bc5211 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 850 | if (AsmString->isWide()) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 851 | return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character) |
| 852 | << AsmString->getSourceRange()); |
| 853 | |
| 854 | |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 855 | for (unsigned i = 0; i != NumOutputs; i++) { |
| 856 | StringLiteral *Literal = Constraints[i]; |
Chris Lattner | 6bc5211 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 857 | if (Literal->isWide()) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 858 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 859 | << Literal->getSourceRange()); |
| 860 | |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 861 | std::string OutputConstraint(Literal->getStrData(), |
| 862 | Literal->getByteLength()); |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 863 | |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 864 | TargetInfo::ConstraintInfo info; |
Chris Lattner | 6bc5211 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 865 | if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info)) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 866 | return StmtError(Diag(Literal->getLocStart(), |
| 867 | diag::err_asm_invalid_output_constraint) << OutputConstraint); |
| 868 | |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 869 | // Check that the output exprs are valid lvalues. |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 870 | ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]); |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 871 | Expr::isLvalueResult Result = OutputExpr->isLvalue(Context); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 872 | if (Result != Expr::LV_Valid) { |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 873 | return StmtError(Diag(OutputExpr->getSubExpr()->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 874 | diag::err_asm_invalid_lvalue_in_output) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 875 | << OutputExpr->getSubExpr()->getSourceRange()); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 876 | } |
Anders Carlsson | 03eb543 | 2009-01-27 20:38:24 +0000 | [diff] [blame] | 877 | |
| 878 | OutputConstraintInfos.push_back(info); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 879 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 880 | |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 881 | for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 882 | StringLiteral *Literal = Constraints[i]; |
Chris Lattner | 6bc5211 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 883 | if (Literal->isWide()) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 884 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 885 | << Literal->getSourceRange()); |
| 886 | |
| 887 | std::string InputConstraint(Literal->getStrData(), |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 888 | Literal->getByteLength()); |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 889 | |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 890 | TargetInfo::ConstraintInfo info; |
| 891 | if (!Context.Target.validateInputConstraint(InputConstraint.c_str(), |
Anders Carlsson | 45b050e | 2009-01-17 23:36:15 +0000 | [diff] [blame] | 892 | &Names[0], |
Anders Carlsson | 03eb543 | 2009-01-27 20:38:24 +0000 | [diff] [blame] | 893 | &Names[0] + NumOutputs, |
| 894 | &OutputConstraintInfos[0], |
| 895 | info)) { |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 896 | return StmtError(Diag(Literal->getLocStart(), |
| 897 | diag::err_asm_invalid_input_constraint) << InputConstraint); |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 898 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 899 | |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 900 | ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]); |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 901 | |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 902 | // Only allow void types for memory constraints. |
Anders Carlsson | e6ea279 | 2009-01-21 06:27:20 +0000 | [diff] [blame] | 903 | if ((info & TargetInfo::CI_AllowsMemory) |
| 904 | && !(info & TargetInfo::CI_AllowsRegister)) { |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 905 | if (InputExpr->isLvalue(Context) != Expr::LV_Valid) |
| 906 | return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(), |
| 907 | diag::err_asm_invalid_lvalue_in_input) |
| 908 | << InputConstraint << InputExpr->getSubExpr()->getSourceRange()); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 909 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 910 | |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 911 | if (info & TargetInfo::CI_AllowsRegister) { |
| 912 | if (InputExpr->getType()->isVoidType()) { |
| 913 | return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(), |
| 914 | diag::err_asm_invalid_type_in_input) |
| 915 | << InputExpr->getType() << InputConstraint |
| 916 | << InputExpr->getSubExpr()->getSourceRange()); |
| 917 | } |
| 918 | |
Anders Carlsson | 562489e | 2008-12-31 07:27:38 +0000 | [diff] [blame] | 919 | DefaultFunctionArrayConversion(Exprs[i]); |
Anders Carlsson | d9fca6e | 2009-01-20 20:49:22 +0000 | [diff] [blame] | 920 | } |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 921 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 922 | |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 923 | // Check that the clobbers are valid. |
Chris Lattner | 1708b96 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 924 | for (unsigned i = 0; i != NumClobbers; i++) { |
| 925 | StringLiteral *Literal = Clobbers[i]; |
Chris Lattner | 6bc5211 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 926 | if (Literal->isWide()) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 927 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 928 | << Literal->getSourceRange()); |
| 929 | |
| 930 | llvm::SmallString<16> Clobber(Literal->getStrData(), |
| 931 | Literal->getStrData() + |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 932 | Literal->getByteLength()); |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 933 | |
Chris Lattner | 6bc5211 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 934 | if (!Context.Target.isValidGCCRegisterName(Clobber.c_str())) |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 935 | return StmtError(Diag(Literal->getLocStart(), |
| 936 | diag::err_asm_unknown_register_name) << Clobber.c_str()); |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 937 | } |
Sebastian Redl | 3037ed0 | 2009-01-18 16:53:17 +0000 | [diff] [blame] | 938 | |
| 939 | constraints.release(); |
| 940 | exprs.release(); |
| 941 | asmString.release(); |
| 942 | clobbers.release(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 943 | return Owned(new (Context) AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, |
| 944 | NumInputs, Names, Constraints, Exprs, |
| 945 | AsmString, NumClobbers, |
| 946 | Clobbers, RParenLoc)); |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 947 | } |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 948 | |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 949 | Action::OwningStmtResult |
| 950 | Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, |
| 951 | SourceLocation RParen, StmtArg Parm, |
| 952 | StmtArg Body, StmtArg catchList) { |
| 953 | Stmt *CatchList = static_cast<Stmt*>(catchList.release()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 954 | ObjCAtCatchStmt *CS = new (Context) ObjCAtCatchStmt(AtLoc, RParen, |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 955 | static_cast<Stmt*>(Parm.release()), static_cast<Stmt*>(Body.release()), |
| 956 | CatchList); |
| 957 | return Owned(CatchList ? CatchList : CS); |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 960 | Action::OwningStmtResult |
| 961 | Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 962 | return Owned(new (Context) ObjCAtFinallyStmt(AtLoc, |
| 963 | static_cast<Stmt*>(Body.release()))); |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 964 | } |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 965 | |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 966 | Action::OwningStmtResult |
| 967 | Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc, |
| 968 | StmtArg Try, StmtArg Catch, StmtArg Finally) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 969 | return Owned(new (Context) ObjCAtTryStmt(AtLoc, |
| 970 | static_cast<Stmt*>(Try.release()), |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 971 | static_cast<Stmt*>(Catch.release()), |
| 972 | static_cast<Stmt*>(Finally.release()))); |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 975 | Action::OwningStmtResult |
Steve Naroff | 3dcfe10 | 2009-02-12 15:54:59 +0000 | [diff] [blame] | 976 | Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg expr,Scope *CurScope) { |
Steve Naroff | 7151bbb | 2009-02-11 17:45:08 +0000 | [diff] [blame] | 977 | Expr *ThrowExpr = static_cast<Expr*>(expr.release()); |
| 978 | if (!ThrowExpr) { |
Steve Naroff | e21dd6f | 2009-02-11 20:05:44 +0000 | [diff] [blame] | 979 | // @throw without an expression designates a rethrow (which much occur |
| 980 | // in the context of an @catch clause). |
| 981 | Scope *AtCatchParent = CurScope; |
| 982 | while (AtCatchParent && !AtCatchParent->isAtCatchScope()) |
| 983 | AtCatchParent = AtCatchParent->getParent(); |
| 984 | if (!AtCatchParent) |
Steve Naroff | 4ab2414 | 2009-02-12 18:09:32 +0000 | [diff] [blame] | 985 | return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch)); |
Steve Naroff | 7151bbb | 2009-02-11 17:45:08 +0000 | [diff] [blame] | 986 | } else { |
| 987 | QualType ThrowType = ThrowExpr->getType(); |
| 988 | // Make sure the expression type is an ObjC pointer or "void *". |
| 989 | if (!Context.isObjCObjectPointerType(ThrowType)) { |
| 990 | const PointerType *PT = ThrowType->getAsPointerType(); |
| 991 | if (!PT || !PT->getPointeeType()->isVoidType()) |
Steve Naroff | 4ab2414 | 2009-02-12 18:09:32 +0000 | [diff] [blame] | 992 | return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object) |
| 993 | << ThrowExpr->getType() << ThrowExpr->getSourceRange()); |
Steve Naroff | 7151bbb | 2009-02-11 17:45:08 +0000 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | return Owned(new (Context) ObjCAtThrowStmt(AtLoc, ThrowExpr)); |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 997 | } |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 998 | |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 999 | Action::OwningStmtResult |
| 1000 | Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr, |
| 1001 | StmtArg SynchBody) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1002 | return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc, |
Sebastian Redl | 431e90e | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 1003 | static_cast<Stmt*>(SynchExpr.release()), |
| 1004 | static_cast<Stmt*>(SynchBody.release()))); |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1005 | } |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 1006 | |
| 1007 | /// ActOnCXXCatchBlock - Takes an exception declaration and a handler block |
| 1008 | /// and creates a proper catch handler from them. |
| 1009 | Action::OwningStmtResult |
| 1010 | Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclTy *ExDecl, |
| 1011 | StmtArg HandlerBlock) { |
| 1012 | // There's nothing to test that ActOnExceptionDecl didn't already test. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1013 | return Owned(new (Context) CXXCatchStmt(CatchLoc, |
| 1014 | static_cast<VarDecl*>(ExDecl), |
| 1015 | static_cast<Stmt*>(HandlerBlock.release()))); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 1016 | } |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 1017 | |
| 1018 | /// ActOnCXXTryBlock - Takes a try compound-statement and a number of |
| 1019 | /// handlers and creates a try statement from them. |
| 1020 | Action::OwningStmtResult |
| 1021 | Sema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock, |
| 1022 | MultiStmtArg RawHandlers) { |
| 1023 | unsigned NumHandlers = RawHandlers.size(); |
| 1024 | assert(NumHandlers > 0 && |
| 1025 | "The parser shouldn't call this if there are no handlers."); |
| 1026 | Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get()); |
| 1027 | |
| 1028 | for(unsigned i = 0; i < NumHandlers - 1; ++i) { |
| 1029 | CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]); |
| 1030 | if (!Handler->getExceptionDecl()) |
| 1031 | return StmtError(Diag(Handler->getLocStart(), diag::err_early_catch_all)); |
| 1032 | } |
| 1033 | // FIXME: We should detect handlers for the same type as an earlier one. |
| 1034 | // This one is rather easy. |
| 1035 | // FIXME: We should detect handlers that cannot catch anything because an |
| 1036 | // earlier handler catches a superclass. Need to find a method that is not |
| 1037 | // quadratic for this. |
| 1038 | // Neither of these are explicitly forbidden, but every compiler detects them |
| 1039 | // and warns. |
| 1040 | |
| 1041 | RawHandlers.release(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1042 | return Owned(new (Context) CXXTryStmt(TryLoc, |
| 1043 | static_cast<Stmt*>(TryBlock.release()), |
| 1044 | Handlers, NumHandlers)); |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 1045 | } |