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