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