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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
| 19 | #include "clang/Basic/Diagnostic.h" |
| 20 | #include "clang/Basic/LangOptions.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 23 | Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | Expr *E = static_cast<Expr*>(expr); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 25 | assert(E && "ActOnExprStmt(): missing expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | return E; |
| 27 | } |
| 28 | |
| 29 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 30 | Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | return new NullStmt(SemiLoc); |
| 32 | } |
| 33 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 34 | Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl) { |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 35 | if (decl) { |
| 36 | ScopedDecl *SD = dyn_cast<ScopedDecl>(static_cast<Decl *>(decl)); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 37 | assert(SD && "Sema::ActOnDeclStmt(): expected ScopedDecl"); |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 38 | return new DeclStmt(SD); |
| 39 | } else |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | return true; // error |
| 41 | } |
| 42 | |
| 43 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 44 | Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 45 | StmtTy **elts, unsigned NumElts, bool isStmtExpr) { |
Chris Lattner | c30ebfb | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 46 | Stmt **Elts = reinterpret_cast<Stmt**>(elts); |
| 47 | // If we're in C89 mode, check that we don't have any decls after stmts. If |
| 48 | // so, emit an extension diagnostic. |
| 49 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) { |
| 50 | // Note that __extension__ can be around a decl. |
| 51 | unsigned i = 0; |
| 52 | // Skip over all declarations. |
| 53 | for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) |
| 54 | /*empty*/; |
| 55 | |
| 56 | // We found the end of the list or a statement. Scan for another declstmt. |
| 57 | for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) |
| 58 | /*empty*/; |
| 59 | |
| 60 | if (i != NumElts) { |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 61 | ScopedDecl *D = cast<DeclStmt>(Elts[i])->getDecl(); |
Chris Lattner | c30ebfb | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 62 | Diag(D->getLocation(), diag::ext_mixed_decls_code); |
| 63 | } |
| 64 | } |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 65 | // Warn about unused expressions in statements. |
| 66 | for (unsigned i = 0; i != NumElts; ++i) { |
| 67 | Expr *E = dyn_cast<Expr>(Elts[i]); |
| 68 | if (!E) continue; |
| 69 | |
| 70 | // Warn about expressions with unused results. |
| 71 | if (E->hasLocalSideEffect() || E->getType()->isVoidType()) |
| 72 | continue; |
| 73 | |
| 74 | // The last expr in a stmt expr really is used. |
| 75 | if (isStmtExpr && i == NumElts-1) |
| 76 | continue; |
| 77 | |
| 78 | /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in |
| 79 | /// a context where the result is unused. Emit a diagnostic to warn about |
| 80 | /// this. |
| 81 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) |
| 82 | Diag(BO->getOperatorLoc(), diag::warn_unused_expr, |
| 83 | BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange()); |
| 84 | else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 85 | Diag(UO->getOperatorLoc(), diag::warn_unused_expr, |
| 86 | UO->getSubExpr()->getSourceRange()); |
| 87 | else |
| 88 | Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange()); |
| 89 | } |
| 90 | |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 91 | return new CompoundStmt(Elts, NumElts, L, R); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 95 | Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval, |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 96 | SourceLocation DotDotDotLoc, ExprTy *rhsval, |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 97 | SourceLocation ColonLoc, StmtTy *subStmt) { |
| 98 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 99 | Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | assert((LHSVal != 0) && "missing expression in case statement"); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 101 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 102 | SourceLocation ExpLoc; |
| 103 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 104 | if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) { |
| 105 | Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr, |
| 106 | LHSVal->getSourceRange()); |
| 107 | return SubStmt; |
| 108 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 110 | // GCC extension: The expression shall be an integer constant. |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 111 | if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) { |
| 112 | Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr, |
| 113 | RHSVal->getSourceRange()); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 114 | RHSVal = 0; // Recover by just forgetting about it. |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 115 | } |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 116 | |
| 117 | if (SwitchStack.empty()) { |
| 118 | Diag(CaseLoc, diag::err_case_not_in_switch); |
| 119 | return SubStmt; |
| 120 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 122 | CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc); |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 123 | SwitchStack.back()->addSwitchCase(CS); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 124 | return CS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 128 | Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 129 | StmtTy *subStmt, Scope *CurScope) { |
| 130 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 132 | if (SwitchStack.empty()) { |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 133 | Diag(DefaultLoc, diag::err_default_not_in_switch); |
| 134 | return SubStmt; |
| 135 | } |
| 136 | |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 137 | DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt); |
Chris Lattner | 8a87e57 | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 138 | SwitchStack.back()->addSwitchCase(DS); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 140 | return DS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 144 | Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 145 | SourceLocation ColonLoc, StmtTy *subStmt) { |
| 146 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | // Look up the record for this label identifier. |
| 148 | LabelStmt *&LabelDecl = LabelMap[II]; |
| 149 | |
| 150 | // If not forward referenced or defined already, just create a new LabelStmt. |
| 151 | if (LabelDecl == 0) |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 152 | return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 153 | |
| 154 | assert(LabelDecl->getID() == II && "Label mismatch!"); |
| 155 | |
| 156 | // Otherwise, this label was either forward reference or multiply defined. If |
| 157 | // multiply defined, reject it now. |
| 158 | if (LabelDecl->getSubStmt()) { |
| 159 | Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName()); |
| 160 | Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition); |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 161 | return SubStmt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Otherwise, this label was forward declared, and we just found its real |
| 165 | // definition. Fill in the forward definition and return it. |
| 166 | LabelDecl->setIdentLoc(IdentLoc); |
Chris Lattner | 0fa152e | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 167 | LabelDecl->setSubStmt(SubStmt); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | return LabelDecl; |
| 169 | } |
| 170 | |
| 171 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 172 | Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 173 | StmtTy *ThenVal, SourceLocation ElseLoc, |
| 174 | StmtTy *ElseVal) { |
| 175 | Expr *condExpr = (Expr *)CondVal; |
Anders Carlsson | 2d85f8b | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 176 | Stmt *thenStmt = (Stmt *)ThenVal; |
| 177 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 178 | assert(condExpr && "ActOnIfStmt(): missing expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 180 | DefaultFunctionArrayConversion(condExpr); |
| 181 | QualType condType = condExpr->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | |
| 183 | if (!condType->isScalarType()) // C99 6.8.4.1p1 |
| 184 | return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar, |
| 185 | condType.getAsString(), condExpr->getSourceRange()); |
| 186 | |
Anders Carlsson | 2d85f8b | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 187 | // Warn if the if block has a null body without an else value. |
| 188 | // this helps prevent bugs due to typos, such as |
| 189 | // if (condition); |
| 190 | // do_stuff(); |
| 191 | if (!ElseVal) { |
| 192 | if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt)) |
| 193 | Diag(stmt->getSemiLoc(), diag::warn_empty_if_body); |
| 194 | } |
| 195 | |
| 196 | return new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 200 | Sema::ActOnStartOfSwitchStmt(ExprTy *cond) { |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 201 | Expr *Cond = static_cast<Expr*>(cond); |
| 202 | |
| 203 | // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. |
| 204 | UsualUnaryConversions(Cond); |
| 205 | |
| 206 | SwitchStmt *SS = new SwitchStmt(Cond); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 207 | SwitchStack.push_back(SS); |
| 208 | return SS; |
| 209 | } |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 210 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 211 | /// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have |
| 212 | /// the specified width and sign. If an overflow occurs, detect it and emit |
| 213 | /// the specified diagnostic. |
| 214 | void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, |
| 215 | unsigned NewWidth, bool NewSign, |
| 216 | SourceLocation Loc, |
| 217 | unsigned DiagID) { |
| 218 | // Perform a conversion to the promoted condition type if needed. |
| 219 | if (NewWidth > Val.getBitWidth()) { |
| 220 | // If this is an extension, just do it. |
| 221 | llvm::APSInt OldVal(Val); |
| 222 | Val.extend(NewWidth); |
| 223 | |
| 224 | // If the input was signed and negative and the output is unsigned, |
| 225 | // warn. |
| 226 | if (!NewSign && OldVal.isSigned() && OldVal.isNegative()) |
| 227 | Diag(Loc, DiagID, OldVal.toString(), Val.toString()); |
| 228 | |
| 229 | Val.setIsSigned(NewSign); |
| 230 | } else if (NewWidth < Val.getBitWidth()) { |
| 231 | // If this is a truncation, check for overflow. |
| 232 | llvm::APSInt ConvVal(Val); |
| 233 | ConvVal.trunc(NewWidth); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 234 | ConvVal.setIsSigned(NewSign); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 235 | ConvVal.extend(Val.getBitWidth()); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 236 | ConvVal.setIsSigned(Val.isSigned()); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 237 | if (ConvVal != Val) |
| 238 | Diag(Loc, DiagID, Val.toString(), ConvVal.toString()); |
| 239 | |
| 240 | // Regardless of whether a diagnostic was emitted, really do the |
| 241 | // truncation. |
| 242 | Val.trunc(NewWidth); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 243 | Val.setIsSigned(NewSign); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 244 | } else if (NewSign != Val.isSigned()) { |
| 245 | // Convert the sign to match the sign of the condition. This can cause |
| 246 | // overflow as well: unsigned(INTMIN) |
| 247 | llvm::APSInt OldVal(Val); |
| 248 | Val.setIsSigned(NewSign); |
| 249 | |
| 250 | if (Val.isNegative()) // Sign bit changes meaning. |
| 251 | Diag(Loc, DiagID, OldVal.toString(), Val.toString()); |
| 252 | } |
| 253 | } |
| 254 | |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 255 | namespace { |
| 256 | struct CaseCompareFunctor { |
| 257 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 258 | const llvm::APSInt &RHS) { |
| 259 | return LHS.first < RHS; |
| 260 | } |
Chris Lattner | 0e85a27 | 2007-09-03 18:31:57 +0000 | [diff] [blame] | 261 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 262 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 263 | return LHS.first < RHS.first; |
| 264 | } |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 265 | bool operator()(const llvm::APSInt &LHS, |
| 266 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 267 | return LHS < RHS.first; |
| 268 | } |
| 269 | }; |
| 270 | } |
| 271 | |
Chris Lattner | 764a7ce | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 272 | /// CmpCaseVals - Comparison predicate for sorting case values. |
| 273 | /// |
| 274 | static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, |
| 275 | const std::pair<llvm::APSInt, CaseStmt*>& rhs) { |
| 276 | if (lhs.first < rhs.first) |
| 277 | return true; |
| 278 | |
| 279 | if (lhs.first == rhs.first && |
| 280 | lhs.second->getCaseLoc().getRawEncoding() |
| 281 | < rhs.second->getCaseLoc().getRawEncoding()) |
| 282 | return true; |
| 283 | return false; |
| 284 | } |
| 285 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 286 | Action::StmtResult |
Chris Lattner | 764a7ce | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 287 | Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, |
| 288 | ExprTy *Body) { |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 289 | Stmt *BodyStmt = (Stmt*)Body; |
| 290 | |
| 291 | SwitchStmt *SS = SwitchStack.back(); |
| 292 | assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!"); |
| 293 | |
Steve Naroff | 9dcbfa4 | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 294 | SS->setBody(BodyStmt, SwitchLoc); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 295 | SwitchStack.pop_back(); |
| 296 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 297 | Expr *CondExpr = SS->getCond(); |
| 298 | QualType CondType = CondExpr->getType(); |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 299 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 300 | if (!CondType->isIntegerType()) { // C99 6.8.4.2p1 |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 301 | Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer, |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 302 | CondType.getAsString(), CondExpr->getSourceRange()); |
| 303 | return true; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 304 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 305 | |
| 306 | // Get the bitwidth of the switched-on value before promotions. We must |
| 307 | // convert the integer case values to this width before comparison. |
Hartmut Kaiser | 1ff4f0c | 2007-09-16 00:04:22 +0000 | [diff] [blame] | 308 | unsigned CondWidth = |
| 309 | static_cast<unsigned>(Context.getTypeSize(CondType, SwitchLoc)); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 310 | bool CondIsSigned = CondType->isSignedIntegerType(); |
| 311 | |
| 312 | // Accumulate all of the case values in a vector so that we can sort them |
| 313 | // and detect duplicates. This vector contains the APInt for the case after |
| 314 | // it has been converted to the condition type. |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 315 | typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; |
| 316 | CaseValsTy CaseVals; |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 317 | |
| 318 | // Keep track of any GNU case ranges we see. The APSInt is the low value. |
| 319 | std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges; |
| 320 | |
| 321 | DefaultStmt *TheDefaultStmt = 0; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 322 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 323 | bool CaseListIsErroneous = false; |
| 324 | |
| 325 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 326 | SC = SC->getNextSwitchCase()) { |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 327 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 328 | if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 329 | if (TheDefaultStmt) { |
| 330 | Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); |
| 331 | Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 332 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 333 | // FIXME: Remove the default statement from the switch block so that |
| 334 | // we'll return a valid AST. This requires recursing down the |
| 335 | // AST and finding it, not something we are set up to do right now. For |
| 336 | // now, just lop the entire switch stmt out of the AST. |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 337 | CaseListIsErroneous = true; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 338 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 339 | TheDefaultStmt = DS; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 340 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 341 | } else { |
| 342 | CaseStmt *CS = cast<CaseStmt>(SC); |
| 343 | |
| 344 | // We already verified that the expression has a i-c-e value (C99 |
| 345 | // 6.8.4.2p3) - get that value now. |
| 346 | llvm::APSInt LoVal(32); |
| 347 | CS->getLHS()->isIntegerConstantExpr(LoVal, Context); |
| 348 | |
| 349 | // Convert the value to the same width/sign as the condition. |
| 350 | ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, |
| 351 | CS->getLHS()->getLocStart(), |
| 352 | diag::warn_case_value_overflow); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 353 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 354 | // If this is a case range, remember it in CaseRanges, otherwise CaseVals. |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 355 | if (CS->getRHS()) |
| 356 | CaseRanges.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 357 | else |
| 358 | CaseVals.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 362 | // Sort all the scalar case values so we can easily detect duplicates. |
Chris Lattner | 764a7ce | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 363 | std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 364 | |
Chris Lattner | f334850 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 365 | if (!CaseVals.empty()) { |
| 366 | for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { |
| 367 | if (CaseVals[i].first == CaseVals[i+1].first) { |
| 368 | // If we have a duplicate, report it. |
| 369 | Diag(CaseVals[i+1].second->getLHS()->getLocStart(), |
| 370 | diag::err_duplicate_case, CaseVals[i].first.toString()); |
| 371 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
| 372 | diag::err_duplicate_case_prev); |
| 373 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 374 | // but we have no way to do this right now. |
| 375 | CaseListIsErroneous = true; |
| 376 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 379 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 380 | // Detect duplicate case ranges, which usually don't exist at all in the first |
| 381 | // place. |
| 382 | if (!CaseRanges.empty()) { |
| 383 | // Sort all the case ranges by their low value so we can easily detect |
| 384 | // overlaps between ranges. |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 385 | std::stable_sort(CaseRanges.begin(), CaseRanges.end()); |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 386 | |
| 387 | // Scan the ranges, computing the high values and removing empty ranges. |
| 388 | std::vector<llvm::APSInt> HiVals; |
Chris Lattner | 6efc4d3 | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 389 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 390 | CaseStmt *CR = CaseRanges[i].second; |
| 391 | llvm::APSInt HiVal(32); |
| 392 | CR->getRHS()->isIntegerConstantExpr(HiVal, Context); |
| 393 | |
| 394 | // Convert the value to the same width/sign as the condition. |
| 395 | ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, |
| 396 | CR->getRHS()->getLocStart(), |
| 397 | diag::warn_case_value_overflow); |
| 398 | |
Chris Lattner | 6efc4d3 | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 399 | // If the low value is bigger than the high value, the case is empty. |
| 400 | if (CaseRanges[i].first > HiVal) { |
| 401 | Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range, |
| 402 | SourceRange(CR->getLHS()->getLocStart(), |
| 403 | CR->getRHS()->getLocEnd())); |
| 404 | CaseRanges.erase(CaseRanges.begin()+i); |
| 405 | --i, --e; |
| 406 | continue; |
| 407 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 408 | HiVals.push_back(HiVal); |
| 409 | } |
| 410 | |
| 411 | // Rescan the ranges, looking for overlap with singleton values and other |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 412 | // ranges. Since the range list is sorted, we only need to compare case |
| 413 | // ranges with their neighbors. |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 414 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 415 | llvm::APSInt &CRLo = CaseRanges[i].first; |
| 416 | llvm::APSInt &CRHi = HiVals[i]; |
| 417 | CaseStmt *CR = CaseRanges[i].second; |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 418 | |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 419 | // Check to see whether the case range overlaps with any singleton cases. |
| 420 | CaseStmt *OverlapStmt = 0; |
| 421 | llvm::APSInt OverlapVal(32); |
| 422 | |
| 423 | // Find the smallest value >= the lower bound. If I is in the case range, |
| 424 | // then we have overlap. |
| 425 | CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), |
| 426 | CaseVals.end(), CRLo, |
| 427 | CaseCompareFunctor()); |
| 428 | if (I != CaseVals.end() && I->first < CRHi) { |
| 429 | OverlapVal = I->first; // Found overlap with scalar. |
| 430 | OverlapStmt = I->second; |
| 431 | } |
| 432 | |
| 433 | // Find the smallest value bigger than the upper bound. |
| 434 | I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); |
| 435 | if (I != CaseVals.begin() && (I-1)->first >= CRLo) { |
| 436 | OverlapVal = (I-1)->first; // Found overlap with scalar. |
| 437 | OverlapStmt = (I-1)->second; |
| 438 | } |
| 439 | |
| 440 | // Check to see if this case stmt overlaps with the subsequent case range. |
| 441 | if (i && CRLo <= HiVals[i-1]) { |
| 442 | OverlapVal = HiVals[i-1]; // Found overlap with range. |
| 443 | OverlapStmt = CaseRanges[i-1].second; |
| 444 | } |
| 445 | |
| 446 | if (OverlapStmt) { |
| 447 | // If we have a duplicate, report it. |
| 448 | Diag(CR->getLHS()->getLocStart(), |
| 449 | diag::err_duplicate_case, OverlapVal.toString()); |
| 450 | Diag(OverlapStmt->getLHS()->getLocStart(), |
| 451 | diag::err_duplicate_case_prev); |
| 452 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 453 | // but we have no way to do this right now. |
| 454 | CaseListIsErroneous = true; |
| 455 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 456 | } |
| 457 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 458 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 459 | // FIXME: If the case list was broken is some way, we don't have a good system |
| 460 | // to patch it up. Instead, just return the whole substmt as broken. |
| 461 | if (CaseListIsErroneous) |
| 462 | return true; |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 463 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 464 | return SS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 468 | Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 469 | Expr *condExpr = (Expr *)Cond; |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 470 | assert(condExpr && "ActOnWhileStmt(): missing expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 471 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 472 | DefaultFunctionArrayConversion(condExpr); |
| 473 | QualType condType = condExpr->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 474 | |
| 475 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 476 | return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar, |
| 477 | condType.getAsString(), condExpr->getSourceRange()); |
| 478 | |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 479 | return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 483 | Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 484 | SourceLocation WhileLoc, ExprTy *Cond) { |
| 485 | Expr *condExpr = (Expr *)Cond; |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 486 | assert(condExpr && "ActOnDoStmt(): missing expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 487 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 488 | DefaultFunctionArrayConversion(condExpr); |
| 489 | QualType condType = condExpr->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 490 | |
| 491 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 492 | return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar, |
| 493 | condType.getAsString(), condExpr->getSourceRange()); |
| 494 | |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 495 | return new DoStmt((Stmt*)Body, condExpr, DoLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 499 | Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
Chris Lattner | 36c4b0e | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 500 | StmtTy *first, ExprTy *second, ExprTy *third, |
| 501 | SourceLocation RParenLoc, StmtTy *body) { |
| 502 | Stmt *First = static_cast<Stmt*>(first); |
| 503 | Expr *Second = static_cast<Expr*>(second); |
| 504 | Expr *Third = static_cast<Expr*>(third); |
| 505 | Stmt *Body = static_cast<Stmt*>(body); |
| 506 | |
Chris Lattner | ae3b701 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 507 | if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { |
| 508 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare |
| 509 | // identifiers for objects having storage class 'auto' or 'register'. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 510 | for (ScopedDecl *D = DS->getDecl(); D; D = D->getNextDeclarator()) { |
Chris Lattner | ae3b701 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 511 | BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D); |
| 512 | if (BVD && !BVD->hasLocalStorage()) |
| 513 | BVD = 0; |
| 514 | if (BVD == 0) |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 515 | Diag(dyn_cast<ScopedDecl>(D)->getLocation(), |
| 516 | diag::err_non_variable_decl_in_for); |
Chris Lattner | ae3b701 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 517 | // FIXME: mark decl erroneous! |
| 518 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 519 | } |
| 520 | if (Second) { |
Chris Lattner | 36c4b0e | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 521 | DefaultFunctionArrayConversion(Second); |
| 522 | QualType SecondType = Second->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 36c4b0e | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 524 | if (!SecondType->isScalarType()) // C99 6.8.5p2 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 525 | return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar, |
Chris Lattner | 36c4b0e | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 526 | SecondType.getAsString(), Second->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 527 | } |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 528 | return new ForStmt(First, Second, Third, Body, ForLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
| 532 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 533 | Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 534 | IdentifierInfo *LabelII) { |
| 535 | // Look up the record for this label identifier. |
| 536 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 537 | |
| 538 | // If we haven't seen this label yet, create a forward reference. |
| 539 | if (LabelDecl == 0) |
| 540 | LabelDecl = new LabelStmt(LabelLoc, LabelII, 0); |
| 541 | |
Ted Kremenek | 61f6216 | 2007-09-06 17:11:52 +0000 | [diff] [blame] | 542 | return new GotoStmt(LabelDecl, GotoLoc, LabelLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 546 | Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 547 | ExprTy *DestExp) { |
| 548 | // FIXME: Verify that the operand is convertible to void*. |
| 549 | |
| 550 | return new IndirectGotoStmt((Expr*)DestExp); |
| 551 | } |
| 552 | |
| 553 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 554 | Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 555 | Scope *S = CurScope->getContinueParent(); |
| 556 | if (!S) { |
| 557 | // C99 6.8.6.2p1: A break shall appear only in or as a loop body. |
| 558 | Diag(ContinueLoc, diag::err_continue_not_in_loop); |
| 559 | return true; |
| 560 | } |
| 561 | |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 562 | return new ContinueStmt(ContinueLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 566 | Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 567 | Scope *S = CurScope->getBreakParent(); |
| 568 | if (!S) { |
| 569 | // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. |
| 570 | Diag(BreakLoc, diag::err_break_not_in_loop_or_switch); |
| 571 | return true; |
| 572 | } |
| 573 | |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 574 | return new BreakStmt(BreakLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | |
| 578 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 579 | Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) { |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 580 | Expr *RetValExp = static_cast<Expr *>(rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 581 | QualType lhsType = CurFunctionDecl->getResultType(); |
| 582 | |
| 583 | if (lhsType->isVoidType()) { |
| 584 | if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 585 | Diag(ReturnLoc, diag::ext_return_has_expr, |
| 586 | CurFunctionDecl->getIdentifier()->getName(), |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 587 | RetValExp->getSourceRange()); |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 588 | return new ReturnStmt(ReturnLoc, RetValExp); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 589 | } else { |
| 590 | if (!RetValExp) { |
| 591 | const char *funcName = CurFunctionDecl->getIdentifier()->getName(); |
| 592 | if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 593 | Diag(ReturnLoc, diag::ext_return_missing_expr, funcName); |
| 594 | else // C90 6.6.6.4p4 |
| 595 | Diag(ReturnLoc, diag::warn_return_missing_expr, funcName); |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 596 | return new ReturnStmt(ReturnLoc, (Expr*)0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | // we have a non-void function with an expression, continue checking |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 600 | QualType rhsType = RetValExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 601 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 602 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 603 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 604 | // function return. |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 605 | AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType, |
| 606 | RetValExp); |
Ted Kremenek | 6a2394c | 2007-08-14 18:14:14 +0000 | [diff] [blame] | 607 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 608 | // decode the result (notice that extensions still return a type). |
| 609 | switch (result) { |
| 610 | case Compatible: |
| 611 | break; |
| 612 | case Incompatible: |
| 613 | Diag(ReturnLoc, diag::err_typecheck_return_incompatible, |
| 614 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 615 | RetValExp->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 616 | break; |
| 617 | case PointerFromInt: |
| 618 | // check for null pointer constant (C99 6.3.2.3p3) |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 619 | if (!RetValExp->isNullPointerConstant(Context)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 620 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 621 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 622 | RetValExp->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 623 | } |
| 624 | break; |
| 625 | case IntFromPointer: |
| 626 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 627 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 628 | RetValExp->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 629 | break; |
| 630 | case IncompatiblePointer: |
| 631 | Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer, |
| 632 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 633 | RetValExp->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 634 | break; |
| 635 | case CompatiblePointerDiscardsQualifiers: |
| 636 | Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers, |
| 637 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 638 | RetValExp->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 639 | break; |
| 640 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 641 | |
| 642 | if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc); |
| 643 | |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 644 | return new ReturnStmt(ReturnLoc, (Expr*)RetValExp); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 645 | } |
| 646 | |