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" |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 17 | #include "clang/AST/Stmt.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/Parse/Scope.h" |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
| 21 | #include "clang/Basic/LangOptions.h" |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 25 | Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | Expr *E = static_cast<Expr*>(expr); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 27 | assert(E && "ActOnExprStmt(): missing expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | return E; |
| 29 | } |
| 30 | |
| 31 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 32 | Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | return new NullStmt(SemiLoc); |
| 34 | } |
| 35 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 36 | Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl) { |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 37 | if (decl) { |
| 38 | ScopedDecl *SD = dyn_cast<ScopedDecl>(static_cast<Decl *>(decl)); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 39 | assert(SD && "Sema::ActOnDeclStmt(): expected ScopedDecl"); |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 40 | return new DeclStmt(SD); |
| 41 | } else |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | return true; // error |
| 43 | } |
| 44 | |
| 45 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 46 | Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 47 | StmtTy **elts, unsigned NumElts, bool isStmtExpr) { |
Chris Lattner | c30ebfb | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 48 | Stmt **Elts = reinterpret_cast<Stmt**>(elts); |
| 49 | // If we're in C89 mode, check that we don't have any decls after stmts. If |
| 50 | // so, emit an extension diagnostic. |
| 51 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) { |
| 52 | // Note that __extension__ can be around a decl. |
| 53 | unsigned i = 0; |
| 54 | // Skip over all declarations. |
| 55 | for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) |
| 56 | /*empty*/; |
| 57 | |
| 58 | // We found the end of the list or a statement. Scan for another declstmt. |
| 59 | for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) |
| 60 | /*empty*/; |
| 61 | |
| 62 | if (i != NumElts) { |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 63 | ScopedDecl *D = cast<DeclStmt>(Elts[i])->getDecl(); |
Chris Lattner | c30ebfb | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 64 | Diag(D->getLocation(), diag::ext_mixed_decls_code); |
| 65 | } |
| 66 | } |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 67 | // Warn about unused expressions in statements. |
| 68 | for (unsigned i = 0; i != NumElts; ++i) { |
| 69 | Expr *E = dyn_cast<Expr>(Elts[i]); |
| 70 | if (!E) continue; |
| 71 | |
| 72 | // Warn about expressions with unused results. |
| 73 | if (E->hasLocalSideEffect() || E->getType()->isVoidType()) |
| 74 | continue; |
| 75 | |
| 76 | // The last expr in a stmt expr really is used. |
| 77 | if (isStmtExpr && i == NumElts-1) |
| 78 | continue; |
| 79 | |
| 80 | /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in |
| 81 | /// a context where the result is unused. Emit a diagnostic to warn about |
| 82 | /// this. |
| 83 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) |
| 84 | Diag(BO->getOperatorLoc(), diag::warn_unused_expr, |
| 85 | BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange()); |
| 86 | else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 87 | Diag(UO->getOperatorLoc(), diag::warn_unused_expr, |
| 88 | UO->getSubExpr()->getSourceRange()); |
| 89 | else |
| 90 | Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange()); |
| 91 | } |
| 92 | |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 93 | return new CompoundStmt(Elts, NumElts, L, R); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 97 | Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval, |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 98 | SourceLocation DotDotDotLoc, ExprTy *rhsval, |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 99 | SourceLocation ColonLoc, StmtTy *subStmt) { |
| 100 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 101 | Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 102 | assert((LHSVal != 0) && "missing expression in case statement"); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 103 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | SourceLocation ExpLoc; |
| 105 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 106 | if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) { |
| 107 | Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr, |
| 108 | LHSVal->getSourceRange()); |
| 109 | return SubStmt; |
| 110 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 112 | // GCC extension: The expression shall be an integer constant. |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 113 | if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) { |
| 114 | Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr, |
| 115 | RHSVal->getSourceRange()); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 116 | RHSVal = 0; // Recover by just forgetting about it. |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 117 | } |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 118 | |
| 119 | if (SwitchStack.empty()) { |
| 120 | Diag(CaseLoc, diag::err_case_not_in_switch); |
| 121 | return SubStmt; |
| 122 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 124 | CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc); |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 125 | SwitchStack.back()->addSwitchCase(CS); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 126 | return CS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 130 | Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 131 | StmtTy *subStmt, Scope *CurScope) { |
| 132 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 134 | if (SwitchStack.empty()) { |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 135 | Diag(DefaultLoc, diag::err_default_not_in_switch); |
| 136 | return SubStmt; |
| 137 | } |
| 138 | |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 139 | DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt); |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 140 | SwitchStack.back()->addSwitchCase(DS); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 142 | return DS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 146 | Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 147 | SourceLocation ColonLoc, StmtTy *subStmt) { |
| 148 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 149 | // Look up the record for this label identifier. |
| 150 | LabelStmt *&LabelDecl = LabelMap[II]; |
| 151 | |
| 152 | // If not forward referenced or defined already, just create a new LabelStmt. |
| 153 | if (LabelDecl == 0) |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 154 | return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | |
| 156 | assert(LabelDecl->getID() == II && "Label mismatch!"); |
| 157 | |
| 158 | // Otherwise, this label was either forward reference or multiply defined. If |
| 159 | // multiply defined, reject it now. |
| 160 | if (LabelDecl->getSubStmt()) { |
| 161 | Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName()); |
| 162 | Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition); |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 163 | return SubStmt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Otherwise, this label was forward declared, and we just found its real |
| 167 | // definition. Fill in the forward definition and return it. |
| 168 | LabelDecl->setIdentLoc(IdentLoc); |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 169 | LabelDecl->setSubStmt(SubStmt); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | return LabelDecl; |
| 171 | } |
| 172 | |
| 173 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 174 | Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | StmtTy *ThenVal, SourceLocation ElseLoc, |
| 176 | StmtTy *ElseVal) { |
| 177 | Expr *condExpr = (Expr *)CondVal; |
Anders Carlsson | 2d85f8b | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 178 | Stmt *thenStmt = (Stmt *)ThenVal; |
| 179 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 180 | assert(condExpr && "ActOnIfStmt(): missing expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 181 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 182 | DefaultFunctionArrayConversion(condExpr); |
| 183 | QualType condType = condExpr->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | |
| 185 | if (!condType->isScalarType()) // C99 6.8.4.1p1 |
| 186 | return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar, |
| 187 | condType.getAsString(), condExpr->getSourceRange()); |
| 188 | |
Anders Carlsson | 2d85f8b | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 189 | // Warn if the if block has a null body without an else value. |
| 190 | // this helps prevent bugs due to typos, such as |
| 191 | // if (condition); |
| 192 | // do_stuff(); |
| 193 | if (!ElseVal) { |
| 194 | if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt)) |
| 195 | Diag(stmt->getSemiLoc(), diag::warn_empty_if_body); |
| 196 | } |
| 197 | |
| 198 | return new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 202 | Sema::ActOnStartOfSwitchStmt(ExprTy *cond) { |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 203 | Expr *Cond = static_cast<Expr*>(cond); |
| 204 | |
| 205 | // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. |
| 206 | UsualUnaryConversions(Cond); |
| 207 | |
| 208 | SwitchStmt *SS = new SwitchStmt(Cond); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 209 | SwitchStack.push_back(SS); |
| 210 | return SS; |
| 211 | } |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 212 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 213 | /// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have |
| 214 | /// the specified width and sign. If an overflow occurs, detect it and emit |
| 215 | /// the specified diagnostic. |
| 216 | void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, |
| 217 | unsigned NewWidth, bool NewSign, |
| 218 | SourceLocation Loc, |
| 219 | unsigned DiagID) { |
| 220 | // Perform a conversion to the promoted condition type if needed. |
| 221 | if (NewWidth > Val.getBitWidth()) { |
| 222 | // If this is an extension, just do it. |
| 223 | llvm::APSInt OldVal(Val); |
| 224 | Val.extend(NewWidth); |
| 225 | |
| 226 | // If the input was signed and negative and the output is unsigned, |
| 227 | // warn. |
| 228 | if (!NewSign && OldVal.isSigned() && OldVal.isNegative()) |
| 229 | Diag(Loc, DiagID, OldVal.toString(), Val.toString()); |
| 230 | |
| 231 | Val.setIsSigned(NewSign); |
| 232 | } else if (NewWidth < Val.getBitWidth()) { |
| 233 | // If this is a truncation, check for overflow. |
| 234 | llvm::APSInt ConvVal(Val); |
| 235 | ConvVal.trunc(NewWidth); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 236 | ConvVal.setIsSigned(NewSign); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 237 | ConvVal.extend(Val.getBitWidth()); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 238 | ConvVal.setIsSigned(Val.isSigned()); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 239 | if (ConvVal != Val) |
| 240 | Diag(Loc, DiagID, Val.toString(), ConvVal.toString()); |
| 241 | |
| 242 | // Regardless of whether a diagnostic was emitted, really do the |
| 243 | // truncation. |
| 244 | Val.trunc(NewWidth); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 245 | Val.setIsSigned(NewSign); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 246 | } else if (NewSign != Val.isSigned()) { |
| 247 | // Convert the sign to match the sign of the condition. This can cause |
| 248 | // overflow as well: unsigned(INTMIN) |
| 249 | llvm::APSInt OldVal(Val); |
| 250 | Val.setIsSigned(NewSign); |
| 251 | |
| 252 | if (Val.isNegative()) // Sign bit changes meaning. |
| 253 | Diag(Loc, DiagID, OldVal.toString(), Val.toString()); |
| 254 | } |
| 255 | } |
| 256 | |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 257 | namespace { |
| 258 | struct CaseCompareFunctor { |
| 259 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 260 | const llvm::APSInt &RHS) { |
| 261 | return LHS.first < RHS; |
| 262 | } |
Chris Lattner | 0e85a27 | 2007-09-03 18:31:57 +0000 | [diff] [blame] | 263 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 264 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 265 | return LHS.first < RHS.first; |
| 266 | } |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 267 | bool operator()(const llvm::APSInt &LHS, |
| 268 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 269 | return LHS < RHS.first; |
| 270 | } |
| 271 | }; |
| 272 | } |
| 273 | |
Chris Lattner | 764a7ce | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 274 | /// CmpCaseVals - Comparison predicate for sorting case values. |
| 275 | /// |
| 276 | static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, |
| 277 | const std::pair<llvm::APSInt, CaseStmt*>& rhs) { |
| 278 | if (lhs.first < rhs.first) |
| 279 | return true; |
| 280 | |
| 281 | if (lhs.first == rhs.first && |
| 282 | lhs.second->getCaseLoc().getRawEncoding() |
| 283 | < rhs.second->getCaseLoc().getRawEncoding()) |
| 284 | return true; |
| 285 | return false; |
| 286 | } |
| 287 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 288 | Action::StmtResult |
Chris Lattner | 764a7ce | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 289 | Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, |
| 290 | ExprTy *Body) { |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 291 | Stmt *BodyStmt = (Stmt*)Body; |
| 292 | |
| 293 | SwitchStmt *SS = SwitchStack.back(); |
| 294 | assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!"); |
| 295 | |
Steve Naroff | 9dcbfa4 | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 296 | SS->setBody(BodyStmt, SwitchLoc); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 297 | SwitchStack.pop_back(); |
| 298 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 299 | Expr *CondExpr = SS->getCond(); |
| 300 | QualType CondType = CondExpr->getType(); |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 301 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 302 | if (!CondType->isIntegerType()) { // C99 6.8.4.2p1 |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 303 | Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer, |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 304 | CondType.getAsString(), CondExpr->getSourceRange()); |
| 305 | return true; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 306 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 307 | |
| 308 | // Get the bitwidth of the switched-on value before promotions. We must |
| 309 | // convert the integer case values to this width before comparison. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 310 | unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType)); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 311 | bool CondIsSigned = CondType->isSignedIntegerType(); |
| 312 | |
| 313 | // Accumulate all of the case values in a vector so that we can sort them |
| 314 | // and detect duplicates. This vector contains the APInt for the case after |
| 315 | // it has been converted to the condition type. |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 316 | typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; |
| 317 | CaseValsTy CaseVals; |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 318 | |
| 319 | // Keep track of any GNU case ranges we see. The APSInt is the low value. |
| 320 | std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges; |
| 321 | |
| 322 | DefaultStmt *TheDefaultStmt = 0; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 323 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 324 | bool CaseListIsErroneous = false; |
| 325 | |
| 326 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 327 | SC = SC->getNextSwitchCase()) { |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 328 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 329 | if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 330 | if (TheDefaultStmt) { |
| 331 | Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); |
| 332 | Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 333 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 334 | // FIXME: Remove the default statement from the switch block so that |
| 335 | // we'll return a valid AST. This requires recursing down the |
| 336 | // AST and finding it, not something we are set up to do right now. For |
| 337 | // now, just lop the entire switch stmt out of the AST. |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 338 | CaseListIsErroneous = true; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 339 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 340 | TheDefaultStmt = DS; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 341 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 342 | } else { |
| 343 | CaseStmt *CS = cast<CaseStmt>(SC); |
| 344 | |
| 345 | // We already verified that the expression has a i-c-e value (C99 |
| 346 | // 6.8.4.2p3) - get that value now. |
| 347 | llvm::APSInt LoVal(32); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 348 | Expr *Lo = CS->getLHS(); |
| 349 | Lo->isIntegerConstantExpr(LoVal, Context); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 350 | |
| 351 | // Convert the value to the same width/sign as the condition. |
| 352 | ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, |
| 353 | CS->getLHS()->getLocStart(), |
| 354 | diag::warn_case_value_overflow); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 355 | |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 356 | // If the LHS is not the same type as the condition, insert an implicit |
| 357 | // cast. |
| 358 | ImpCastExprToType(Lo, CondType); |
| 359 | CS->setLHS(Lo); |
| 360 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 361 | // If this is a case range, remember it in CaseRanges, otherwise CaseVals. |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 362 | if (CS->getRHS()) |
| 363 | CaseRanges.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 364 | else |
| 365 | CaseVals.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 369 | // Sort all the scalar case values so we can easily detect duplicates. |
Chris Lattner | 764a7ce | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 370 | std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 371 | |
Chris Lattner | f334850 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 372 | if (!CaseVals.empty()) { |
| 373 | for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { |
| 374 | if (CaseVals[i].first == CaseVals[i+1].first) { |
| 375 | // If we have a duplicate, report it. |
| 376 | Diag(CaseVals[i+1].second->getLHS()->getLocStart(), |
| 377 | diag::err_duplicate_case, CaseVals[i].first.toString()); |
| 378 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
| 379 | diag::err_duplicate_case_prev); |
| 380 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 381 | // but we have no way to do this right now. |
| 382 | CaseListIsErroneous = true; |
| 383 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 386 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 387 | // Detect duplicate case ranges, which usually don't exist at all in the first |
| 388 | // place. |
| 389 | if (!CaseRanges.empty()) { |
| 390 | // Sort all the case ranges by their low value so we can easily detect |
| 391 | // overlaps between ranges. |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 392 | std::stable_sort(CaseRanges.begin(), CaseRanges.end()); |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 393 | |
| 394 | // Scan the ranges, computing the high values and removing empty ranges. |
| 395 | std::vector<llvm::APSInt> HiVals; |
Chris Lattner | 6efc4d3 | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 396 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 397 | CaseStmt *CR = CaseRanges[i].second; |
| 398 | llvm::APSInt HiVal(32); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 399 | Expr *Hi = CR->getRHS(); |
| 400 | Hi->isIntegerConstantExpr(HiVal, Context); |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 401 | |
| 402 | // Convert the value to the same width/sign as the condition. |
| 403 | ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, |
| 404 | CR->getRHS()->getLocStart(), |
| 405 | diag::warn_case_value_overflow); |
| 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(Hi, CondType); |
| 410 | CR->setRHS(Hi); |
| 411 | |
Chris Lattner | 6efc4d3 | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 412 | // If the low value is bigger than the high value, the case is empty. |
| 413 | if (CaseRanges[i].first > HiVal) { |
| 414 | Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range, |
| 415 | SourceRange(CR->getLHS()->getLocStart(), |
| 416 | CR->getRHS()->getLocEnd())); |
| 417 | CaseRanges.erase(CaseRanges.begin()+i); |
| 418 | --i, --e; |
| 419 | continue; |
| 420 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 421 | HiVals.push_back(HiVal); |
| 422 | } |
| 423 | |
| 424 | // Rescan the ranges, looking for overlap with singleton values and other |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 425 | // ranges. Since the range list is sorted, we only need to compare case |
| 426 | // ranges with their neighbors. |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 427 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 428 | llvm::APSInt &CRLo = CaseRanges[i].first; |
| 429 | llvm::APSInt &CRHi = HiVals[i]; |
| 430 | CaseStmt *CR = CaseRanges[i].second; |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 432 | // Check to see whether the case range overlaps with any singleton cases. |
| 433 | CaseStmt *OverlapStmt = 0; |
| 434 | llvm::APSInt OverlapVal(32); |
| 435 | |
| 436 | // Find the smallest value >= the lower bound. If I is in the case range, |
| 437 | // then we have overlap. |
| 438 | CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), |
| 439 | CaseVals.end(), CRLo, |
| 440 | CaseCompareFunctor()); |
| 441 | if (I != CaseVals.end() && I->first < CRHi) { |
| 442 | OverlapVal = I->first; // Found overlap with scalar. |
| 443 | OverlapStmt = I->second; |
| 444 | } |
| 445 | |
| 446 | // Find the smallest value bigger than the upper bound. |
| 447 | I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); |
| 448 | if (I != CaseVals.begin() && (I-1)->first >= CRLo) { |
| 449 | OverlapVal = (I-1)->first; // Found overlap with scalar. |
| 450 | OverlapStmt = (I-1)->second; |
| 451 | } |
| 452 | |
| 453 | // Check to see if this case stmt overlaps with the subsequent case range. |
| 454 | if (i && CRLo <= HiVals[i-1]) { |
| 455 | OverlapVal = HiVals[i-1]; // Found overlap with range. |
| 456 | OverlapStmt = CaseRanges[i-1].second; |
| 457 | } |
| 458 | |
| 459 | if (OverlapStmt) { |
| 460 | // If we have a duplicate, report it. |
| 461 | Diag(CR->getLHS()->getLocStart(), |
| 462 | diag::err_duplicate_case, OverlapVal.toString()); |
| 463 | Diag(OverlapStmt->getLHS()->getLocStart(), |
| 464 | diag::err_duplicate_case_prev); |
| 465 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 466 | // but we have no way to do this right now. |
| 467 | CaseListIsErroneous = true; |
| 468 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 471 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 472 | // FIXME: If the case list was broken is some way, we don't have a good system |
| 473 | // to patch it up. Instead, just return the whole substmt as broken. |
| 474 | if (CaseListIsErroneous) |
| 475 | return true; |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 476 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 477 | return SS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 481 | Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 482 | Expr *condExpr = (Expr *)Cond; |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 483 | assert(condExpr && "ActOnWhileStmt(): missing expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 484 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 485 | DefaultFunctionArrayConversion(condExpr); |
| 486 | QualType condType = condExpr->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 487 | |
| 488 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 489 | return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar, |
| 490 | condType.getAsString(), condExpr->getSourceRange()); |
| 491 | |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 492 | return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 496 | Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 497 | SourceLocation WhileLoc, ExprTy *Cond) { |
| 498 | Expr *condExpr = (Expr *)Cond; |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 499 | assert(condExpr && "ActOnDoStmt(): missing expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 500 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 501 | DefaultFunctionArrayConversion(condExpr); |
| 502 | QualType condType = condExpr->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 503 | |
| 504 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 505 | return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar, |
| 506 | condType.getAsString(), condExpr->getSourceRange()); |
| 507 | |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 508 | return new DoStmt((Stmt*)Body, condExpr, DoLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 512 | Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
Chris Lattner | 36c4b0e | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 513 | StmtTy *first, ExprTy *second, ExprTy *third, |
| 514 | SourceLocation RParenLoc, StmtTy *body) { |
| 515 | Stmt *First = static_cast<Stmt*>(first); |
| 516 | Expr *Second = static_cast<Expr*>(second); |
| 517 | Expr *Third = static_cast<Expr*>(third); |
| 518 | Stmt *Body = static_cast<Stmt*>(body); |
| 519 | |
Chris Lattner | ae3b701 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 520 | if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { |
| 521 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare |
| 522 | // identifiers for objects having storage class 'auto' or 'register'. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 523 | for (ScopedDecl *D = DS->getDecl(); D; D = D->getNextDeclarator()) { |
Chris Lattner | ae3b701 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 524 | BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D); |
| 525 | if (BVD && !BVD->hasLocalStorage()) |
| 526 | BVD = 0; |
| 527 | if (BVD == 0) |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 528 | Diag(dyn_cast<ScopedDecl>(D)->getLocation(), |
| 529 | diag::err_non_variable_decl_in_for); |
Chris Lattner | ae3b701 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 530 | // FIXME: mark decl erroneous! |
| 531 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 532 | } |
| 533 | if (Second) { |
Chris Lattner | 36c4b0e | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 534 | DefaultFunctionArrayConversion(Second); |
| 535 | QualType SecondType = Second->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 536 | |
Chris Lattner | 36c4b0e | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 537 | if (!SecondType->isScalarType()) // C99 6.8.5p2 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 538 | return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar, |
Chris Lattner | 36c4b0e | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 539 | SecondType.getAsString(), Second->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 540 | } |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 541 | return new ForStmt(First, Second, Third, Body, ForLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 544 | Action::StmtResult |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 545 | Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 546 | SourceLocation LParenLoc, |
| 547 | StmtTy *first, ExprTy *second, |
| 548 | SourceLocation RParenLoc, StmtTy *body) { |
| 549 | Stmt *First = static_cast<Stmt*>(first); |
| 550 | Expr *Second = static_cast<Expr*>(second); |
| 551 | Stmt *Body = static_cast<Stmt*>(body); |
Fariborz Jahanian | 20552d2 | 2008-01-10 20:33:58 +0000 | [diff] [blame] | 552 | if (First) { |
| 553 | QualType FirstType; |
| 554 | if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) { |
| 555 | FirstType = cast<ValueDecl>(DS->getDecl())->getType(); |
| 556 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare |
| 557 | // identifiers for objects having storage class 'auto' or 'register'. |
| 558 | ScopedDecl *D = DS->getDecl(); |
| 559 | BlockVarDecl *BVD = cast<BlockVarDecl>(D); |
| 560 | if (!BVD->hasLocalStorage()) |
| 561 | return Diag(BVD->getLocation(), diag::err_non_variable_decl_in_for); |
| 562 | if (D->getNextDeclarator()) |
| 563 | return Diag(D->getLocation(), diag::err_toomany_element_decls); |
| 564 | } |
| 565 | else |
| 566 | FirstType = static_cast<Expr*>(first)->getType(); |
| 567 | if (!isObjCObjectPointerType(FirstType)) |
| 568 | Diag(ForLoc, diag::err_selector_element_type, |
| 569 | FirstType.getAsString(), First->getSourceRange()); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 570 | } |
| 571 | if (Second) { |
| 572 | DefaultFunctionArrayConversion(Second); |
| 573 | QualType SecondType = Second->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 574 | if (!isObjCObjectPointerType(SecondType)) |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 575 | Diag(ForLoc, diag::err_collection_expr_type, |
Fariborz Jahanian | 1f990d6 | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 576 | SecondType.getAsString(), Second->getSourceRange()); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 577 | } |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 578 | return new ObjCForCollectionStmt(First, Second, Body, ForLoc, RParenLoc); |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 579 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 580 | |
| 581 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 582 | Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 583 | IdentifierInfo *LabelII) { |
| 584 | // Look up the record for this label identifier. |
| 585 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 586 | |
| 587 | // If we haven't seen this label yet, create a forward reference. |
| 588 | if (LabelDecl == 0) |
| 589 | LabelDecl = new LabelStmt(LabelLoc, LabelII, 0); |
| 590 | |
Ted Kremenek | 61f6216 | 2007-09-06 17:11:52 +0000 | [diff] [blame] | 591 | return new GotoStmt(LabelDecl, GotoLoc, LabelLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 595 | Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 596 | ExprTy *DestExp) { |
| 597 | // FIXME: Verify that the operand is convertible to void*. |
| 598 | |
| 599 | return new IndirectGotoStmt((Expr*)DestExp); |
| 600 | } |
| 601 | |
| 602 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 603 | Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 604 | Scope *S = CurScope->getContinueParent(); |
| 605 | if (!S) { |
| 606 | // C99 6.8.6.2p1: A break shall appear only in or as a loop body. |
| 607 | Diag(ContinueLoc, diag::err_continue_not_in_loop); |
| 608 | return true; |
| 609 | } |
| 610 | |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 611 | return new ContinueStmt(ContinueLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 615 | Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 616 | Scope *S = CurScope->getBreakParent(); |
| 617 | if (!S) { |
| 618 | // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. |
| 619 | Diag(BreakLoc, diag::err_break_not_in_loop_or_switch); |
| 620 | return true; |
| 621 | } |
| 622 | |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 623 | return new BreakStmt(BreakLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | |
| 627 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 628 | Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) { |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 629 | Expr *RetValExp = static_cast<Expr *>(rex); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 630 | QualType FnRetType = CurFunctionDecl ? CurFunctionDecl->getResultType() : |
| 631 | CurMethodDecl->getResultType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 632 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 633 | if (FnRetType->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 634 | if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns) |
Fariborz Jahanian | b107ce8 | 2007-12-04 19:20:11 +0000 | [diff] [blame] | 635 | Diag(ReturnLoc, diag::ext_return_has_expr, |
| 636 | (CurFunctionDecl ? CurFunctionDecl->getIdentifier()->getName() : |
| 637 | CurMethodDecl->getSelector().getName()), |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 638 | RetValExp->getSourceRange()); |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 639 | return new ReturnStmt(ReturnLoc, RetValExp); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 640 | } else { |
| 641 | if (!RetValExp) { |
Fariborz Jahanian | b107ce8 | 2007-12-04 19:20:11 +0000 | [diff] [blame] | 642 | const char *funcName = CurFunctionDecl ? |
| 643 | CurFunctionDecl->getIdentifier()->getName() : |
| 644 | CurMethodDecl->getSelector().getName().c_str(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 645 | if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 646 | Diag(ReturnLoc, diag::ext_return_missing_expr, funcName); |
| 647 | else // C90 6.6.6.4p4 |
| 648 | Diag(ReturnLoc, diag::warn_return_missing_expr, funcName); |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 649 | return new ReturnStmt(ReturnLoc, (Expr*)0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | // we have a non-void function with an expression, continue checking |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 653 | QualType RetValType = RetValExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 654 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 655 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 656 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 657 | // function return. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 658 | AssignConvertType ConvTy = CheckSingleAssignmentConstraints(FnRetType, |
| 659 | RetValExp); |
| 660 | if (DiagnoseAssignmentResult(ConvTy, ReturnLoc, FnRetType, |
| 661 | RetValType, RetValExp, "returning")) |
| 662 | return true; |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 663 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 664 | if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 665 | |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 666 | return new ReturnStmt(ReturnLoc, (Expr*)RetValExp); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Anders Carlsson | 6a0ef4b | 2007-11-20 19:21:03 +0000 | [diff] [blame] | 669 | Sema::StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, |
Anders Carlsson | dfab34a | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 670 | bool IsSimple, |
Anders Carlsson | 39c47b5 | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 671 | bool IsVolatile, |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 672 | unsigned NumOutputs, |
| 673 | unsigned NumInputs, |
| 674 | std::string *Names, |
| 675 | ExprTy **Constraints, |
| 676 | ExprTy **Exprs, |
Anders Carlsson | 6a0ef4b | 2007-11-20 19:21:03 +0000 | [diff] [blame] | 677 | ExprTy *AsmString, |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 678 | unsigned NumClobbers, |
| 679 | ExprTy **Clobbers, |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 680 | SourceLocation RParenLoc) { |
Anders Carlsson | 6a0ef4b | 2007-11-20 19:21:03 +0000 | [diff] [blame] | 681 | Expr *E = (Expr *)AsmString; |
Anders Carlsson | 39c47b5 | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 682 | |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 683 | for (unsigned i = 0; i < NumOutputs; i++) { |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 684 | StringLiteral *Literal = cast<StringLiteral>((Expr *)Constraints[i]); |
| 685 | assert(!Literal->isWide() && |
| 686 | "Output constraint strings should not be wide!"); |
| 687 | |
| 688 | std::string OutputConstraint(Literal->getStrData(), |
| 689 | Literal->getByteLength()); |
| 690 | |
| 691 | TargetInfo::ConstraintInfo info; |
| 692 | if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(), |
| 693 | info)) { |
| 694 | // FIXME: We currently leak memory here. |
| 695 | Diag(Literal->getLocStart(), |
| 696 | diag::err_invalid_output_constraint_in_asm); |
| 697 | return true; |
| 698 | } |
| 699 | |
| 700 | // Check that the output exprs are valid lvalues. |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 701 | Expr *OutputExpr = (Expr *)Exprs[i]; |
| 702 | Expr::isLvalueResult Result = OutputExpr->isLvalue(); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 703 | if (Result != Expr::LV_Valid) { |
| 704 | ParenExpr *PE = cast<ParenExpr>(OutputExpr); |
| 705 | |
| 706 | Diag(PE->getSubExpr()->getLocStart(), |
| 707 | diag::err_invalid_lvalue_in_asm_output, |
| 708 | PE->getSubExpr()->getSourceRange()); |
| 709 | |
| 710 | // FIXME: We currently leak memory here. |
| 711 | return true; |
| 712 | } |
| 713 | } |
| 714 | |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 715 | for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 716 | StringLiteral *Literal = cast<StringLiteral>((Expr *)Constraints[i]); |
| 717 | assert(!Literal->isWide() && |
| 718 | "Output constraint strings should not be wide!"); |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 719 | |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 720 | std::string InputConstraint(Literal->getStrData(), |
| 721 | Literal->getByteLength()); |
| 722 | |
| 723 | TargetInfo::ConstraintInfo info; |
| 724 | if (!Context.Target.validateInputConstraint(InputConstraint.c_str(), |
| 725 | NumOutputs, |
| 726 | info)) { |
| 727 | // FIXME: We currently leak memory here. |
| 728 | Diag(Literal->getLocStart(), |
| 729 | diag::err_invalid_input_constraint_in_asm); |
| 730 | return true; |
| 731 | } |
| 732 | |
| 733 | // Check that the input exprs aren't of type void. |
| 734 | Expr *InputExpr = (Expr *)Exprs[i]; |
Anders Carlsson | 04728b7 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 735 | if (InputExpr->getType()->isVoidType()) { |
| 736 | ParenExpr *PE = cast<ParenExpr>(InputExpr); |
| 737 | |
| 738 | Diag(PE->getSubExpr()->getLocStart(), |
| 739 | diag::err_invalid_type_in_asm_input, |
| 740 | PE->getType().getAsString(), |
| 741 | PE->getSubExpr()->getSourceRange()); |
| 742 | |
| 743 | // FIXME: We currently leak memory here. |
| 744 | return true; |
| 745 | } |
| 746 | } |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 747 | |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 748 | // Check that the clobbers are valid. |
| 749 | for (unsigned i = 0; i < NumClobbers; i++) { |
| 750 | StringLiteral *Literal = cast<StringLiteral>((Expr *)Clobbers[i]); |
| 751 | assert(!Literal->isWide() && "Clobber strings should not be wide!"); |
| 752 | |
| 753 | llvm::SmallString<16> Clobber(Literal->getStrData(), |
| 754 | Literal->getStrData() + |
| 755 | Literal->getByteLength()); |
| 756 | |
| 757 | if (!Context.Target.isValidGCCRegisterName(Clobber.c_str())) { |
| 758 | Diag(Literal->getLocStart(), |
| 759 | diag::err_unknown_register_name_in_asm, |
| 760 | Clobber.c_str()); |
| 761 | |
| 762 | // FIXME: We currently leak memory here. |
| 763 | return true; |
| 764 | } |
| 765 | } |
| 766 | |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 767 | return new AsmStmt(AsmLoc, |
Anders Carlsson | dfab34a | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 768 | IsSimple, |
Anders Carlsson | 39c47b5 | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 769 | IsVolatile, |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 770 | NumOutputs, |
| 771 | NumInputs, |
| 772 | Names, |
| 773 | reinterpret_cast<StringLiteral**>(Constraints), |
| 774 | reinterpret_cast<Expr**>(Exprs), |
| 775 | cast<StringLiteral>(E), |
| 776 | NumClobbers, |
| 777 | reinterpret_cast<StringLiteral**>(Clobbers), |
| 778 | RParenLoc); |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 779 | } |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 780 | |
| 781 | Action::StmtResult |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 782 | Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 783 | SourceLocation RParen, StmtTy *Parm, |
| 784 | StmtTy *Body, StmtTy *CatchList) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 785 | ObjCAtCatchStmt *CS = new ObjCAtCatchStmt(AtLoc, RParen, |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 786 | static_cast<Stmt*>(Parm), static_cast<Stmt*>(Body), |
| 787 | static_cast<Stmt*>(CatchList)); |
| 788 | return CatchList ? CatchList : CS; |
| 789 | } |
| 790 | |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 791 | Action::StmtResult |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 792 | Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtTy *Body) { |
| 793 | ObjCAtFinallyStmt *FS = new ObjCAtFinallyStmt(AtLoc, |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 794 | static_cast<Stmt*>(Body)); |
| 795 | return FS; |
| 796 | } |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 797 | |
| 798 | Action::StmtResult |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 799 | Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc, |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 800 | StmtTy *Try, StmtTy *Catch, StmtTy *Finally) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 801 | ObjCAtTryStmt *TS = new ObjCAtTryStmt(AtLoc, static_cast<Stmt*>(Try), |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 802 | static_cast<Stmt*>(Catch), |
| 803 | static_cast<Stmt*>(Finally)); |
| 804 | return TS; |
| 805 | } |
| 806 | |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 807 | Action::StmtResult |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 808 | Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, StmtTy *Throw) { |
| 809 | ObjCAtThrowStmt *TS = new ObjCAtThrowStmt(AtLoc, static_cast<Stmt*>(Throw)); |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 810 | return TS; |
| 811 | } |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 812 | |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 813 | Action::StmtResult |
| 814 | Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprTy *SynchExpr, |
| 815 | StmtTy *SynchBody) { |
| 816 | ObjCAtSynchronizedStmt *SS = new ObjCAtSynchronizedStmt(AtLoc, |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 817 | static_cast<Stmt*>(SynchExpr), static_cast<Stmt*>(SynchBody)); |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 818 | return SS; |
| 819 | } |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 820 | |
| 821 | |