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