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 | // |
| 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 | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 17 | #include "clang/AST/Stmt.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 18 | #include "clang/Parse/Scope.h" |
| 19 | #include "clang/Basic/Diagnostic.h" |
| 20 | #include "clang/Basic/LangOptions.h" |
| 21 | #include "clang/Lex/IdentifierTable.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
| 24 | Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) { |
| 25 | Expr *E = static_cast<Expr*>(expr); |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 26 | assert(E && "ParseExprStmt(): missing expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 27 | return E; |
| 28 | } |
| 29 | |
| 30 | |
| 31 | Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) { |
| 32 | return new NullStmt(SemiLoc); |
| 33 | } |
| 34 | |
| 35 | Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) { |
| 36 | if (decl) |
| 37 | return new DeclStmt(static_cast<Decl *>(decl)); |
| 38 | else |
| 39 | return true; // error |
| 40 | } |
| 41 | |
| 42 | Action::StmtResult |
| 43 | Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R, |
Chris Lattner | f2b0757 | 2007-08-31 21:49:55 +0000 | [diff] [blame^] | 44 | StmtTy **elts, unsigned NumElts, bool isStmtExpr) { |
Chris Lattner | 3ea3b66 | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 45 | Stmt **Elts = reinterpret_cast<Stmt**>(elts); |
| 46 | // If we're in C89 mode, check that we don't have any decls after stmts. If |
| 47 | // so, emit an extension diagnostic. |
| 48 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) { |
| 49 | // Note that __extension__ can be around a decl. |
| 50 | unsigned i = 0; |
| 51 | // Skip over all declarations. |
| 52 | for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) |
| 53 | /*empty*/; |
| 54 | |
| 55 | // We found the end of the list or a statement. Scan for another declstmt. |
| 56 | for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) |
| 57 | /*empty*/; |
| 58 | |
| 59 | if (i != NumElts) { |
| 60 | Decl *D = cast<DeclStmt>(Elts[i])->getDecl(); |
| 61 | Diag(D->getLocation(), diag::ext_mixed_decls_code); |
| 62 | } |
| 63 | } |
Chris Lattner | 8141772 | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 64 | |
Chris Lattner | f2b0757 | 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 | |
Chris Lattner | 3ea3b66 | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 91 | return new CompoundStmt(Elts, NumElts); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | Action::StmtResult |
| 95 | Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval, |
| 96 | SourceLocation DotDotDotLoc, ExprTy *rhsval, |
| 97 | SourceLocation ColonLoc, StmtTy *subStmt) { |
| 98 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
| 99 | Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval); |
| 100 | assert((LHSVal != 0) && "missing expression in case statement"); |
| 101 | |
| 102 | SourceLocation ExpLoc; |
| 103 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
| 104 | if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) { |
| 105 | Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr, |
| 106 | LHSVal->getSourceRange()); |
| 107 | return SubStmt; |
| 108 | } |
| 109 | |
| 110 | // GCC extension: The expression shall be an integer constant. |
| 111 | if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) { |
| 112 | Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr, |
| 113 | RHSVal->getSourceRange()); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 114 | RHSVal = 0; // Recover by just forgetting about it. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | if (SwitchStack.empty()) { |
| 118 | Diag(CaseLoc, diag::err_case_not_in_switch); |
| 119 | return SubStmt; |
| 120 | } |
| 121 | |
| 122 | CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt); |
| 123 | SwitchStack.back()->addSwitchCase(CS); |
| 124 | return CS; |
| 125 | } |
| 126 | |
| 127 | Action::StmtResult |
| 128 | Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, |
| 129 | StmtTy *subStmt, Scope *CurScope) { |
| 130 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
| 131 | |
| 132 | if (SwitchStack.empty()) { |
| 133 | Diag(DefaultLoc, diag::err_default_not_in_switch); |
| 134 | return SubStmt; |
| 135 | } |
| 136 | |
| 137 | DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt); |
| 138 | SwitchStack.back()->addSwitchCase(DS); |
| 139 | |
| 140 | return DS; |
| 141 | } |
| 142 | |
| 143 | Action::StmtResult |
| 144 | Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, |
| 145 | SourceLocation ColonLoc, StmtTy *subStmt) { |
| 146 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
| 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) |
| 152 | return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt); |
| 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); |
| 161 | return SubStmt; |
| 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); |
| 167 | LabelDecl->setSubStmt(SubStmt); |
| 168 | return LabelDecl; |
| 169 | } |
| 170 | |
| 171 | Action::StmtResult |
| 172 | Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal, |
| 173 | StmtTy *ThenVal, SourceLocation ElseLoc, |
| 174 | StmtTy *ElseVal) { |
| 175 | Expr *condExpr = (Expr *)CondVal; |
| 176 | assert(condExpr && "ParseIfStmt(): missing expression"); |
| 177 | |
| 178 | DefaultFunctionArrayConversion(condExpr); |
| 179 | QualType condType = condExpr->getType(); |
| 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 | |
| 185 | return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal); |
| 186 | } |
| 187 | |
| 188 | Action::StmtResult |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 189 | Sema::StartSwitchStmt(ExprTy *cond) { |
| 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); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 196 | SwitchStack.push_back(SS); |
| 197 | return SS; |
| 198 | } |
| 199 | |
Chris Lattner | 3429a81 | 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 | 5c03960 | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 223 | ConvVal.setIsSigned(NewSign); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 224 | ConvVal.extend(Val.getBitWidth()); |
Chris Lattner | 5c03960 | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 225 | ConvVal.setIsSigned(Val.isSigned()); |
Chris Lattner | 3429a81 | 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 | 5c03960 | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 232 | Val.setIsSigned(NewSign); |
Chris Lattner | 3429a81 | 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 | 0ab833c | 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 | } |
| 250 | bool operator()(const llvm::APSInt &LHS, |
| 251 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 252 | return LHS < RHS.first; |
| 253 | } |
| 254 | }; |
| 255 | } |
| 256 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 257 | Action::StmtResult |
| 258 | Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) { |
| 259 | Stmt *BodyStmt = (Stmt*)Body; |
| 260 | |
| 261 | SwitchStmt *SS = SwitchStack.back(); |
| 262 | assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!"); |
| 263 | |
| 264 | SS->setBody(BodyStmt); |
| 265 | SwitchStack.pop_back(); |
| 266 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 267 | Expr *CondExpr = SS->getCond(); |
| 268 | QualType CondType = CondExpr->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 270 | if (!CondType->isIntegerType()) { // C99 6.8.4.2p1 |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 271 | Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer, |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 272 | CondType.getAsString(), CondExpr->getSourceRange()); |
| 273 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 274 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 275 | |
| 276 | // Get the bitwidth of the switched-on value before promotions. We must |
| 277 | // convert the integer case values to this width before comparison. |
| 278 | unsigned CondWidth = Context.getTypeSize(CondType, SwitchLoc); |
| 279 | bool CondIsSigned = CondType->isSignedIntegerType(); |
| 280 | |
| 281 | // Accumulate all of the case values in a vector so that we can sort them |
| 282 | // and detect duplicates. This vector contains the APInt for the case after |
| 283 | // it has been converted to the condition type. |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 284 | typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; |
| 285 | CaseValsTy CaseVals; |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 286 | |
| 287 | // Keep track of any GNU case ranges we see. The APSInt is the low value. |
| 288 | std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges; |
| 289 | |
| 290 | DefaultStmt *TheDefaultStmt = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 291 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 292 | bool CaseListIsErroneous = false; |
| 293 | |
| 294 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 295 | SC = SC->getNextSwitchCase()) { |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 297 | if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 298 | if (TheDefaultStmt) { |
| 299 | Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); |
| 300 | Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 301 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 302 | // FIXME: Remove the default statement from the switch block so that |
| 303 | // we'll return a valid AST. This requires recursing down the |
| 304 | // AST and finding it, not something we are set up to do right now. For |
| 305 | // now, just lop the entire switch stmt out of the AST. |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 306 | CaseListIsErroneous = true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 307 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 308 | TheDefaultStmt = DS; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 310 | } else { |
| 311 | CaseStmt *CS = cast<CaseStmt>(SC); |
| 312 | |
| 313 | // We already verified that the expression has a i-c-e value (C99 |
| 314 | // 6.8.4.2p3) - get that value now. |
| 315 | llvm::APSInt LoVal(32); |
| 316 | CS->getLHS()->isIntegerConstantExpr(LoVal, Context); |
| 317 | |
| 318 | // Convert the value to the same width/sign as the condition. |
| 319 | ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, |
| 320 | CS->getLHS()->getLocStart(), |
| 321 | diag::warn_case_value_overflow); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 323 | // If this is a case range, remember it in CaseRanges, otherwise CaseVals. |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 324 | if (CS->getRHS()) |
| 325 | CaseRanges.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 326 | else |
| 327 | CaseVals.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 331 | // Sort all the scalar case values so we can easily detect duplicates. |
| 332 | std::stable_sort(CaseVals.begin(), CaseVals.end()); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 2b1b9a8 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 334 | if (!CaseVals.empty()) { |
| 335 | for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { |
| 336 | if (CaseVals[i].first == CaseVals[i+1].first) { |
| 337 | // If we have a duplicate, report it. |
| 338 | Diag(CaseVals[i+1].second->getLHS()->getLocStart(), |
| 339 | diag::err_duplicate_case, CaseVals[i].first.toString()); |
| 340 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
| 341 | diag::err_duplicate_case_prev); |
| 342 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 343 | // but we have no way to do this right now. |
| 344 | CaseListIsErroneous = true; |
| 345 | } |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 349 | // Detect duplicate case ranges, which usually don't exist at all in the first |
| 350 | // place. |
| 351 | if (!CaseRanges.empty()) { |
| 352 | // Sort all the case ranges by their low value so we can easily detect |
| 353 | // overlaps between ranges. |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 354 | std::stable_sort(CaseRanges.begin(), CaseRanges.end()); |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 355 | |
| 356 | // Scan the ranges, computing the high values and removing empty ranges. |
| 357 | std::vector<llvm::APSInt> HiVals; |
Chris Lattner | 7443e0f | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 358 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 359 | CaseStmt *CR = CaseRanges[i].second; |
| 360 | llvm::APSInt HiVal(32); |
| 361 | CR->getRHS()->isIntegerConstantExpr(HiVal, Context); |
| 362 | |
| 363 | // Convert the value to the same width/sign as the condition. |
| 364 | ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, |
| 365 | CR->getRHS()->getLocStart(), |
| 366 | diag::warn_case_value_overflow); |
| 367 | |
Chris Lattner | 7443e0f | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 368 | // If the low value is bigger than the high value, the case is empty. |
| 369 | if (CaseRanges[i].first > HiVal) { |
| 370 | Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range, |
| 371 | SourceRange(CR->getLHS()->getLocStart(), |
| 372 | CR->getRHS()->getLocEnd())); |
| 373 | CaseRanges.erase(CaseRanges.begin()+i); |
| 374 | --i, --e; |
| 375 | continue; |
| 376 | } |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 377 | HiVals.push_back(HiVal); |
| 378 | } |
| 379 | |
| 380 | // Rescan the ranges, looking for overlap with singleton values and other |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 381 | // ranges. Since the range list is sorted, we only need to compare case |
| 382 | // ranges with their neighbors. |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 383 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 384 | llvm::APSInt &CRLo = CaseRanges[i].first; |
| 385 | llvm::APSInt &CRHi = HiVals[i]; |
| 386 | CaseStmt *CR = CaseRanges[i].second; |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 387 | |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 388 | // Check to see whether the case range overlaps with any singleton cases. |
| 389 | CaseStmt *OverlapStmt = 0; |
| 390 | llvm::APSInt OverlapVal(32); |
| 391 | |
| 392 | // Find the smallest value >= the lower bound. If I is in the case range, |
| 393 | // then we have overlap. |
| 394 | CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), |
| 395 | CaseVals.end(), CRLo, |
| 396 | CaseCompareFunctor()); |
| 397 | if (I != CaseVals.end() && I->first < CRHi) { |
| 398 | OverlapVal = I->first; // Found overlap with scalar. |
| 399 | OverlapStmt = I->second; |
| 400 | } |
| 401 | |
| 402 | // Find the smallest value bigger than the upper bound. |
| 403 | I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); |
| 404 | if (I != CaseVals.begin() && (I-1)->first >= CRLo) { |
| 405 | OverlapVal = (I-1)->first; // Found overlap with scalar. |
| 406 | OverlapStmt = (I-1)->second; |
| 407 | } |
| 408 | |
| 409 | // Check to see if this case stmt overlaps with the subsequent case range. |
| 410 | if (i && CRLo <= HiVals[i-1]) { |
| 411 | OverlapVal = HiVals[i-1]; // Found overlap with range. |
| 412 | OverlapStmt = CaseRanges[i-1].second; |
| 413 | } |
| 414 | |
| 415 | if (OverlapStmt) { |
| 416 | // If we have a duplicate, report it. |
| 417 | Diag(CR->getLHS()->getLocStart(), |
| 418 | diag::err_duplicate_case, OverlapVal.toString()); |
| 419 | Diag(OverlapStmt->getLHS()->getLocStart(), |
| 420 | diag::err_duplicate_case_prev); |
| 421 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 422 | // but we have no way to do this right now. |
| 423 | CaseListIsErroneous = true; |
| 424 | } |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 428 | // FIXME: If the case list was broken is some way, we don't have a good system |
| 429 | // to patch it up. Instead, just return the whole substmt as broken. |
| 430 | if (CaseListIsErroneous) |
| 431 | return true; |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 433 | return SS; |
| 434 | } |
| 435 | |
| 436 | Action::StmtResult |
| 437 | Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) { |
| 438 | Expr *condExpr = (Expr *)Cond; |
| 439 | assert(condExpr && "ParseWhileStmt(): missing expression"); |
| 440 | |
| 441 | DefaultFunctionArrayConversion(condExpr); |
| 442 | QualType condType = condExpr->getType(); |
| 443 | |
| 444 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 445 | return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar, |
| 446 | condType.getAsString(), condExpr->getSourceRange()); |
| 447 | |
| 448 | return new WhileStmt(condExpr, (Stmt*)Body); |
| 449 | } |
| 450 | |
| 451 | Action::StmtResult |
| 452 | Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body, |
| 453 | SourceLocation WhileLoc, ExprTy *Cond) { |
| 454 | Expr *condExpr = (Expr *)Cond; |
| 455 | assert(condExpr && "ParseDoStmt(): missing expression"); |
| 456 | |
| 457 | DefaultFunctionArrayConversion(condExpr); |
| 458 | QualType condType = condExpr->getType(); |
| 459 | |
| 460 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 461 | return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar, |
| 462 | condType.getAsString(), condExpr->getSourceRange()); |
| 463 | |
| 464 | return new DoStmt((Stmt*)Body, condExpr); |
| 465 | } |
| 466 | |
| 467 | Action::StmtResult |
| 468 | Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
Chris Lattner | 3332fbd | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 469 | StmtTy *first, ExprTy *second, ExprTy *third, |
| 470 | SourceLocation RParenLoc, StmtTy *body) { |
| 471 | Stmt *First = static_cast<Stmt*>(first); |
| 472 | Expr *Second = static_cast<Expr*>(second); |
| 473 | Expr *Third = static_cast<Expr*>(third); |
| 474 | Stmt *Body = static_cast<Stmt*>(body); |
| 475 | |
Chris Lattner | 0661105 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 476 | if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { |
| 477 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare |
| 478 | // identifiers for objects having storage class 'auto' or 'register'. |
| 479 | for (Decl *D = DS->getDecl(); D; D = D->getNextDeclarator()) { |
| 480 | BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D); |
| 481 | if (BVD && !BVD->hasLocalStorage()) |
| 482 | BVD = 0; |
| 483 | if (BVD == 0) |
| 484 | Diag(D->getLocation(), diag::err_non_variable_decl_in_for); |
| 485 | // FIXME: mark decl erroneous! |
| 486 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 487 | } |
| 488 | if (Second) { |
Chris Lattner | 3332fbd | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 489 | DefaultFunctionArrayConversion(Second); |
| 490 | QualType SecondType = Second->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 3332fbd | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 492 | if (!SecondType->isScalarType()) // C99 6.8.5p2 |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 493 | return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar, |
Chris Lattner | 3332fbd | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 494 | SecondType.getAsString(), Second->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 495 | } |
Chris Lattner | 3332fbd | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 496 | return new ForStmt(First, Second, Third, Body); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | |
| 500 | Action::StmtResult |
| 501 | Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
| 502 | IdentifierInfo *LabelII) { |
| 503 | // Look up the record for this label identifier. |
| 504 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 505 | |
| 506 | // If we haven't seen this label yet, create a forward reference. |
| 507 | if (LabelDecl == 0) |
| 508 | LabelDecl = new LabelStmt(LabelLoc, LabelII, 0); |
| 509 | |
| 510 | return new GotoStmt(LabelDecl); |
| 511 | } |
| 512 | |
| 513 | Action::StmtResult |
| 514 | Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
| 515 | ExprTy *DestExp) { |
| 516 | // FIXME: Verify that the operand is convertible to void*. |
| 517 | |
| 518 | return new IndirectGotoStmt((Expr*)DestExp); |
| 519 | } |
| 520 | |
| 521 | Action::StmtResult |
| 522 | Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
| 523 | Scope *S = CurScope->getContinueParent(); |
| 524 | if (!S) { |
| 525 | // C99 6.8.6.2p1: A break shall appear only in or as a loop body. |
| 526 | Diag(ContinueLoc, diag::err_continue_not_in_loop); |
| 527 | return true; |
| 528 | } |
| 529 | |
| 530 | return new ContinueStmt(); |
| 531 | } |
| 532 | |
| 533 | Action::StmtResult |
| 534 | Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
| 535 | Scope *S = CurScope->getBreakParent(); |
| 536 | if (!S) { |
| 537 | // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. |
| 538 | Diag(BreakLoc, diag::err_break_not_in_loop_or_switch); |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | return new BreakStmt(); |
| 543 | } |
| 544 | |
| 545 | |
| 546 | Action::StmtResult |
| 547 | Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) { |
| 548 | Expr *RetValExp = static_cast<Expr *>(rex); |
| 549 | QualType lhsType = CurFunctionDecl->getResultType(); |
| 550 | |
| 551 | if (lhsType->isVoidType()) { |
| 552 | if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 553 | Diag(ReturnLoc, diag::ext_return_has_expr, |
| 554 | CurFunctionDecl->getIdentifier()->getName(), |
| 555 | RetValExp->getSourceRange()); |
| 556 | return new ReturnStmt(RetValExp); |
| 557 | } else { |
| 558 | if (!RetValExp) { |
| 559 | const char *funcName = CurFunctionDecl->getIdentifier()->getName(); |
| 560 | if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 561 | Diag(ReturnLoc, diag::ext_return_missing_expr, funcName); |
| 562 | else // C90 6.6.6.4p4 |
| 563 | Diag(ReturnLoc, diag::warn_return_missing_expr, funcName); |
| 564 | return new ReturnStmt((Expr*)0); |
| 565 | } |
| 566 | } |
| 567 | // we have a non-void function with an expression, continue checking |
| 568 | QualType rhsType = RetValExp->getType(); |
| 569 | |
| 570 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 571 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 572 | // function return. |
| 573 | AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType, |
| 574 | RetValExp); |
Ted Kremenek | 235e1ad | 2007-08-14 18:14:14 +0000 | [diff] [blame] | 575 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 576 | // decode the result (notice that extensions still return a type). |
| 577 | switch (result) { |
| 578 | case Compatible: |
| 579 | break; |
| 580 | case Incompatible: |
| 581 | Diag(ReturnLoc, diag::err_typecheck_return_incompatible, |
| 582 | lhsType.getAsString(), rhsType.getAsString(), |
| 583 | RetValExp->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 584 | break; |
| 585 | case PointerFromInt: |
| 586 | // check for null pointer constant (C99 6.3.2.3p3) |
| 587 | if (!RetValExp->isNullPointerConstant(Context)) { |
| 588 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 589 | lhsType.getAsString(), rhsType.getAsString(), |
| 590 | RetValExp->getSourceRange()); |
| 591 | } |
| 592 | break; |
| 593 | case IntFromPointer: |
| 594 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 595 | lhsType.getAsString(), rhsType.getAsString(), |
| 596 | RetValExp->getSourceRange()); |
| 597 | break; |
| 598 | case IncompatiblePointer: |
| 599 | Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer, |
| 600 | lhsType.getAsString(), rhsType.getAsString(), |
| 601 | RetValExp->getSourceRange()); |
| 602 | break; |
| 603 | case CompatiblePointerDiscardsQualifiers: |
| 604 | Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers, |
| 605 | lhsType.getAsString(), rhsType.getAsString(), |
| 606 | RetValExp->getSourceRange()); |
| 607 | break; |
| 608 | } |
Ted Kremenek | 45925ab | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 609 | |
| 610 | if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc); |
| 611 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 612 | return new ReturnStmt((Expr*)RetValExp); |
| 613 | } |
| 614 | |