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; |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 176 | assert(condExpr && "ActOnIfStmt(): missing expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 177 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 178 | DefaultFunctionArrayConversion(condExpr); |
| 179 | QualType condType = condExpr->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | |
| 181 | if (!condType->isScalarType()) // C99 6.8.4.1p1 |
| 182 | return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar, |
| 183 | condType.getAsString(), condExpr->getSourceRange()); |
| 184 | |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 185 | return new IfStmt(IfLoc, condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 189 | Sema::ActOnStartOfSwitchStmt(ExprTy *cond) { |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 190 | Expr *Cond = static_cast<Expr*>(cond); |
| 191 | |
| 192 | // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. |
| 193 | UsualUnaryConversions(Cond); |
| 194 | |
| 195 | SwitchStmt *SS = new SwitchStmt(Cond); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 196 | SwitchStack.push_back(SS); |
| 197 | return SS; |
| 198 | } |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 199 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 200 | /// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have |
| 201 | /// the specified width and sign. If an overflow occurs, detect it and emit |
| 202 | /// the specified diagnostic. |
| 203 | void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, |
| 204 | unsigned NewWidth, bool NewSign, |
| 205 | SourceLocation Loc, |
| 206 | unsigned DiagID) { |
| 207 | // Perform a conversion to the promoted condition type if needed. |
| 208 | if (NewWidth > Val.getBitWidth()) { |
| 209 | // If this is an extension, just do it. |
| 210 | llvm::APSInt OldVal(Val); |
| 211 | Val.extend(NewWidth); |
| 212 | |
| 213 | // If the input was signed and negative and the output is unsigned, |
| 214 | // warn. |
| 215 | if (!NewSign && OldVal.isSigned() && OldVal.isNegative()) |
| 216 | Diag(Loc, DiagID, OldVal.toString(), Val.toString()); |
| 217 | |
| 218 | Val.setIsSigned(NewSign); |
| 219 | } else if (NewWidth < Val.getBitWidth()) { |
| 220 | // If this is a truncation, check for overflow. |
| 221 | llvm::APSInt ConvVal(Val); |
| 222 | ConvVal.trunc(NewWidth); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 223 | ConvVal.setIsSigned(NewSign); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 224 | ConvVal.extend(Val.getBitWidth()); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 225 | ConvVal.setIsSigned(Val.isSigned()); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 226 | if (ConvVal != Val) |
| 227 | Diag(Loc, DiagID, Val.toString(), ConvVal.toString()); |
| 228 | |
| 229 | // Regardless of whether a diagnostic was emitted, really do the |
| 230 | // truncation. |
| 231 | Val.trunc(NewWidth); |
Chris Lattner | b2137ae | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 232 | Val.setIsSigned(NewSign); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 233 | } else if (NewSign != Val.isSigned()) { |
| 234 | // Convert the sign to match the sign of the condition. This can cause |
| 235 | // overflow as well: unsigned(INTMIN) |
| 236 | llvm::APSInt OldVal(Val); |
| 237 | Val.setIsSigned(NewSign); |
| 238 | |
| 239 | if (Val.isNegative()) // Sign bit changes meaning. |
| 240 | Diag(Loc, DiagID, OldVal.toString(), Val.toString()); |
| 241 | } |
| 242 | } |
| 243 | |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 244 | namespace { |
| 245 | struct CaseCompareFunctor { |
| 246 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 247 | const llvm::APSInt &RHS) { |
| 248 | return LHS.first < RHS; |
| 249 | } |
Chris Lattner | 0e85a27 | 2007-09-03 18:31:57 +0000 | [diff] [blame] | 250 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 251 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 252 | return LHS.first < RHS.first; |
| 253 | } |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 254 | bool operator()(const llvm::APSInt &LHS, |
| 255 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 256 | return LHS < RHS.first; |
| 257 | } |
| 258 | }; |
| 259 | } |
| 260 | |
Chris Lattner | 764a7ce | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 261 | /// CmpCaseVals - Comparison predicate for sorting case values. |
| 262 | /// |
| 263 | static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, |
| 264 | const std::pair<llvm::APSInt, CaseStmt*>& rhs) { |
| 265 | if (lhs.first < rhs.first) |
| 266 | return true; |
| 267 | |
| 268 | if (lhs.first == rhs.first && |
| 269 | lhs.second->getCaseLoc().getRawEncoding() |
| 270 | < rhs.second->getCaseLoc().getRawEncoding()) |
| 271 | return true; |
| 272 | return false; |
| 273 | } |
| 274 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 275 | Action::StmtResult |
Chris Lattner | 764a7ce | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 276 | Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, |
| 277 | ExprTy *Body) { |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 278 | Stmt *BodyStmt = (Stmt*)Body; |
| 279 | |
| 280 | SwitchStmt *SS = SwitchStack.back(); |
| 281 | assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!"); |
| 282 | |
Steve Naroff | 9dcbfa4 | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 283 | SS->setBody(BodyStmt, SwitchLoc); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 284 | SwitchStack.pop_back(); |
| 285 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 286 | Expr *CondExpr = SS->getCond(); |
| 287 | QualType CondType = CondExpr->getType(); |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 288 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 289 | if (!CondType->isIntegerType()) { // C99 6.8.4.2p1 |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 290 | Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer, |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 291 | CondType.getAsString(), CondExpr->getSourceRange()); |
| 292 | return true; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 293 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 294 | |
| 295 | // Get the bitwidth of the switched-on value before promotions. We must |
| 296 | // convert the integer case values to this width before comparison. |
Hartmut Kaiser | 1ff4f0c | 2007-09-16 00:04:22 +0000 | [diff] [blame] | 297 | unsigned CondWidth = |
| 298 | static_cast<unsigned>(Context.getTypeSize(CondType, SwitchLoc)); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 299 | bool CondIsSigned = CondType->isSignedIntegerType(); |
| 300 | |
| 301 | // Accumulate all of the case values in a vector so that we can sort them |
| 302 | // and detect duplicates. This vector contains the APInt for the case after |
| 303 | // it has been converted to the condition type. |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 304 | typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; |
| 305 | CaseValsTy CaseVals; |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 306 | |
| 307 | // Keep track of any GNU case ranges we see. The APSInt is the low value. |
| 308 | std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges; |
| 309 | |
| 310 | DefaultStmt *TheDefaultStmt = 0; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 311 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 312 | bool CaseListIsErroneous = false; |
| 313 | |
| 314 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 315 | SC = SC->getNextSwitchCase()) { |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 316 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 317 | if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 318 | if (TheDefaultStmt) { |
| 319 | Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); |
| 320 | Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 321 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 322 | // FIXME: Remove the default statement from the switch block so that |
| 323 | // we'll return a valid AST. This requires recursing down the |
| 324 | // AST and finding it, not something we are set up to do right now. For |
| 325 | // now, just lop the entire switch stmt out of the AST. |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 326 | CaseListIsErroneous = true; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 327 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 328 | TheDefaultStmt = DS; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 329 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 330 | } else { |
| 331 | CaseStmt *CS = cast<CaseStmt>(SC); |
| 332 | |
| 333 | // We already verified that the expression has a i-c-e value (C99 |
| 334 | // 6.8.4.2p3) - get that value now. |
| 335 | llvm::APSInt LoVal(32); |
| 336 | CS->getLHS()->isIntegerConstantExpr(LoVal, Context); |
| 337 | |
| 338 | // Convert the value to the same width/sign as the condition. |
| 339 | ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, |
| 340 | CS->getLHS()->getLocStart(), |
| 341 | diag::warn_case_value_overflow); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 342 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 343 | // If this is a case range, remember it in CaseRanges, otherwise CaseVals. |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 344 | if (CS->getRHS()) |
| 345 | CaseRanges.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 346 | else |
| 347 | CaseVals.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 351 | // Sort all the scalar case values so we can easily detect duplicates. |
Chris Lattner | 764a7ce | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 352 | std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 353 | |
Chris Lattner | f334850 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 354 | if (!CaseVals.empty()) { |
| 355 | for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { |
| 356 | if (CaseVals[i].first == CaseVals[i+1].first) { |
| 357 | // If we have a duplicate, report it. |
| 358 | Diag(CaseVals[i+1].second->getLHS()->getLocStart(), |
| 359 | diag::err_duplicate_case, CaseVals[i].first.toString()); |
| 360 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
| 361 | diag::err_duplicate_case_prev); |
| 362 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 363 | // but we have no way to do this right now. |
| 364 | CaseListIsErroneous = true; |
| 365 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 368 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 369 | // Detect duplicate case ranges, which usually don't exist at all in the first |
| 370 | // place. |
| 371 | if (!CaseRanges.empty()) { |
| 372 | // Sort all the case ranges by their low value so we can easily detect |
| 373 | // overlaps between ranges. |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 374 | std::stable_sort(CaseRanges.begin(), CaseRanges.end()); |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 375 | |
| 376 | // Scan the ranges, computing the high values and removing empty ranges. |
| 377 | std::vector<llvm::APSInt> HiVals; |
Chris Lattner | 6efc4d3 | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 378 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 379 | CaseStmt *CR = CaseRanges[i].second; |
| 380 | llvm::APSInt HiVal(32); |
| 381 | CR->getRHS()->isIntegerConstantExpr(HiVal, Context); |
| 382 | |
| 383 | // Convert the value to the same width/sign as the condition. |
| 384 | ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, |
| 385 | CR->getRHS()->getLocStart(), |
| 386 | diag::warn_case_value_overflow); |
| 387 | |
Chris Lattner | 6efc4d3 | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 388 | // If the low value is bigger than the high value, the case is empty. |
| 389 | if (CaseRanges[i].first > HiVal) { |
| 390 | Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range, |
| 391 | SourceRange(CR->getLHS()->getLocStart(), |
| 392 | CR->getRHS()->getLocEnd())); |
| 393 | CaseRanges.erase(CaseRanges.begin()+i); |
| 394 | --i, --e; |
| 395 | continue; |
| 396 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 397 | HiVals.push_back(HiVal); |
| 398 | } |
| 399 | |
| 400 | // Rescan the ranges, looking for overlap with singleton values and other |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 401 | // ranges. Since the range list is sorted, we only need to compare case |
| 402 | // ranges with their neighbors. |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 403 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 404 | llvm::APSInt &CRLo = CaseRanges[i].first; |
| 405 | llvm::APSInt &CRHi = HiVals[i]; |
| 406 | CaseStmt *CR = CaseRanges[i].second; |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 407 | |
Chris Lattner | 0471f5b | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 408 | // Check to see whether the case range overlaps with any singleton cases. |
| 409 | CaseStmt *OverlapStmt = 0; |
| 410 | llvm::APSInt OverlapVal(32); |
| 411 | |
| 412 | // Find the smallest value >= the lower bound. If I is in the case range, |
| 413 | // then we have overlap. |
| 414 | CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), |
| 415 | CaseVals.end(), CRLo, |
| 416 | CaseCompareFunctor()); |
| 417 | if (I != CaseVals.end() && I->first < CRHi) { |
| 418 | OverlapVal = I->first; // Found overlap with scalar. |
| 419 | OverlapStmt = I->second; |
| 420 | } |
| 421 | |
| 422 | // Find the smallest value bigger than the upper bound. |
| 423 | I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); |
| 424 | if (I != CaseVals.begin() && (I-1)->first >= CRLo) { |
| 425 | OverlapVal = (I-1)->first; // Found overlap with scalar. |
| 426 | OverlapStmt = (I-1)->second; |
| 427 | } |
| 428 | |
| 429 | // Check to see if this case stmt overlaps with the subsequent case range. |
| 430 | if (i && CRLo <= HiVals[i-1]) { |
| 431 | OverlapVal = HiVals[i-1]; // Found overlap with range. |
| 432 | OverlapStmt = CaseRanges[i-1].second; |
| 433 | } |
| 434 | |
| 435 | if (OverlapStmt) { |
| 436 | // If we have a duplicate, report it. |
| 437 | Diag(CR->getLHS()->getLocStart(), |
| 438 | diag::err_duplicate_case, OverlapVal.toString()); |
| 439 | Diag(OverlapStmt->getLHS()->getLocStart(), |
| 440 | diag::err_duplicate_case_prev); |
| 441 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 442 | // but we have no way to do this right now. |
| 443 | CaseListIsErroneous = true; |
| 444 | } |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 447 | |
Chris Lattner | b2ec9d6 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 448 | // FIXME: If the case list was broken is some way, we don't have a good system |
| 449 | // to patch it up. Instead, just return the whole substmt as broken. |
| 450 | if (CaseListIsErroneous) |
| 451 | return true; |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 452 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 453 | return SS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 457 | Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 458 | Expr *condExpr = (Expr *)Cond; |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 459 | assert(condExpr && "ActOnWhileStmt(): missing expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 460 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 461 | DefaultFunctionArrayConversion(condExpr); |
| 462 | QualType condType = condExpr->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 463 | |
| 464 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 465 | return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar, |
| 466 | condType.getAsString(), condExpr->getSourceRange()); |
| 467 | |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 468 | return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 472 | Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 473 | SourceLocation WhileLoc, ExprTy *Cond) { |
| 474 | Expr *condExpr = (Expr *)Cond; |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 475 | assert(condExpr && "ActOnDoStmt(): missing expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 476 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 477 | DefaultFunctionArrayConversion(condExpr); |
| 478 | QualType condType = condExpr->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 479 | |
| 480 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 481 | return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar, |
| 482 | condType.getAsString(), condExpr->getSourceRange()); |
| 483 | |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 484 | return new DoStmt((Stmt*)Body, condExpr, DoLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 488 | Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
Chris Lattner | 36c4b0e | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 489 | StmtTy *first, ExprTy *second, ExprTy *third, |
| 490 | SourceLocation RParenLoc, StmtTy *body) { |
| 491 | Stmt *First = static_cast<Stmt*>(first); |
| 492 | Expr *Second = static_cast<Expr*>(second); |
| 493 | Expr *Third = static_cast<Expr*>(third); |
| 494 | Stmt *Body = static_cast<Stmt*>(body); |
| 495 | |
Chris Lattner | ae3b701 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 496 | if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { |
| 497 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare |
| 498 | // identifiers for objects having storage class 'auto' or 'register'. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 499 | for (ScopedDecl *D = DS->getDecl(); D; D = D->getNextDeclarator()) { |
Chris Lattner | ae3b701 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 500 | BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D); |
| 501 | if (BVD && !BVD->hasLocalStorage()) |
| 502 | BVD = 0; |
| 503 | if (BVD == 0) |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 504 | Diag(dyn_cast<ScopedDecl>(D)->getLocation(), |
| 505 | diag::err_non_variable_decl_in_for); |
Chris Lattner | ae3b701 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 506 | // FIXME: mark decl erroneous! |
| 507 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 508 | } |
| 509 | if (Second) { |
Chris Lattner | 36c4b0e | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 510 | DefaultFunctionArrayConversion(Second); |
| 511 | QualType SecondType = Second->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 512 | |
Chris Lattner | 36c4b0e | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 513 | if (!SecondType->isScalarType()) // C99 6.8.5p2 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 514 | return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar, |
Chris Lattner | 36c4b0e | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 515 | SecondType.getAsString(), Second->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 516 | } |
Steve Naroff | b5a6958 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 517 | return new ForStmt(First, Second, Third, Body, ForLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | |
| 521 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 522 | Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 523 | IdentifierInfo *LabelII) { |
| 524 | // Look up the record for this label identifier. |
| 525 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 526 | |
| 527 | // If we haven't seen this label yet, create a forward reference. |
| 528 | if (LabelDecl == 0) |
| 529 | LabelDecl = new LabelStmt(LabelLoc, LabelII, 0); |
| 530 | |
Ted Kremenek | 61f6216 | 2007-09-06 17:11:52 +0000 | [diff] [blame] | 531 | return new GotoStmt(LabelDecl, GotoLoc, LabelLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 535 | Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 536 | ExprTy *DestExp) { |
| 537 | // FIXME: Verify that the operand is convertible to void*. |
| 538 | |
| 539 | return new IndirectGotoStmt((Expr*)DestExp); |
| 540 | } |
| 541 | |
| 542 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 543 | Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 544 | Scope *S = CurScope->getContinueParent(); |
| 545 | if (!S) { |
| 546 | // C99 6.8.6.2p1: A break shall appear only in or as a loop body. |
| 547 | Diag(ContinueLoc, diag::err_continue_not_in_loop); |
| 548 | return true; |
| 549 | } |
| 550 | |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 551 | return new ContinueStmt(ContinueLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 555 | Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 556 | Scope *S = CurScope->getBreakParent(); |
| 557 | if (!S) { |
| 558 | // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. |
| 559 | Diag(BreakLoc, diag::err_break_not_in_loop_or_switch); |
| 560 | return true; |
| 561 | } |
| 562 | |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 563 | return new BreakStmt(BreakLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | |
| 567 | Action::StmtResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 568 | Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) { |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 569 | Expr *RetValExp = static_cast<Expr *>(rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 570 | QualType lhsType = CurFunctionDecl->getResultType(); |
| 571 | |
| 572 | if (lhsType->isVoidType()) { |
| 573 | if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 574 | Diag(ReturnLoc, diag::ext_return_has_expr, |
| 575 | CurFunctionDecl->getIdentifier()->getName(), |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 576 | RetValExp->getSourceRange()); |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 577 | return new ReturnStmt(ReturnLoc, RetValExp); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 578 | } else { |
| 579 | if (!RetValExp) { |
| 580 | const char *funcName = CurFunctionDecl->getIdentifier()->getName(); |
| 581 | if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 582 | Diag(ReturnLoc, diag::ext_return_missing_expr, funcName); |
| 583 | else // C90 6.6.6.4p4 |
| 584 | Diag(ReturnLoc, diag::warn_return_missing_expr, funcName); |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 585 | return new ReturnStmt(ReturnLoc, (Expr*)0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | // we have a non-void function with an expression, continue checking |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 589 | QualType rhsType = RetValExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 590 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 591 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 592 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 593 | // function return. |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 594 | AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType, |
| 595 | RetValExp); |
Ted Kremenek | 6a2394c | 2007-08-14 18:14:14 +0000 | [diff] [blame] | 596 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 597 | // decode the result (notice that extensions still return a type). |
| 598 | switch (result) { |
| 599 | case Compatible: |
| 600 | break; |
| 601 | case Incompatible: |
| 602 | Diag(ReturnLoc, diag::err_typecheck_return_incompatible, |
| 603 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 604 | RetValExp->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 605 | break; |
| 606 | case PointerFromInt: |
| 607 | // check for null pointer constant (C99 6.3.2.3p3) |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 608 | if (!RetValExp->isNullPointerConstant(Context)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 609 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 610 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 611 | RetValExp->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 612 | } |
| 613 | break; |
| 614 | case IntFromPointer: |
| 615 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 616 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 617 | RetValExp->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 618 | break; |
| 619 | case IncompatiblePointer: |
| 620 | Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer, |
| 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 | break; |
| 624 | case CompatiblePointerDiscardsQualifiers: |
| 625 | Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers, |
| 626 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 627 | RetValExp->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 628 | break; |
| 629 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 630 | |
| 631 | if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc); |
| 632 | |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 633 | return new ReturnStmt(ReturnLoc, (Expr*)RetValExp); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 634 | } |
| 635 | |