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