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