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 | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 201 | Action::StmtResult |
| 202 | Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) { |
| 203 | Stmt *BodyStmt = (Stmt*)Body; |
| 204 | |
| 205 | SwitchStmt *SS = SwitchStack.back(); |
| 206 | assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!"); |
| 207 | |
| 208 | SS->setBody(BodyStmt); |
| 209 | SwitchStack.pop_back(); |
| 210 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 211 | Expr *CondExpr = SS->getCond(); |
| 212 | QualType CondType = CondExpr->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 214 | if (!CondType->isIntegerType()) { // C99 6.8.4.2p1 |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 215 | Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer, |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 216 | CondType.getAsString(), CondExpr->getSourceRange()); |
| 217 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 218 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 219 | |
| 220 | // Get the bitwidth of the switched-on value before promotions. We must |
| 221 | // convert the integer case values to this width before comparison. |
| 222 | unsigned CondWidth = Context.getTypeSize(CondType, SwitchLoc); |
| 223 | bool CondIsSigned = CondType->isSignedIntegerType(); |
| 224 | |
| 225 | // Accumulate all of the case values in a vector so that we can sort them |
| 226 | // and detect duplicates. This vector contains the APInt for the case after |
| 227 | // it has been converted to the condition type. |
| 228 | llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseVals; |
| 229 | |
| 230 | // Keep track of any GNU case ranges we see. The APSInt is the low value. |
| 231 | std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges; |
| 232 | |
| 233 | DefaultStmt *TheDefaultStmt = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame^] | 235 | bool CaseListIsErroneous = false; |
| 236 | |
| 237 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 238 | SC = SC->getNextSwitchCase()) { |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame^] | 239 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 240 | if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 241 | if (TheDefaultStmt) { |
| 242 | Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); |
| 243 | Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 245 | // FIXME: Remove the default statement from the switch block so that |
| 246 | // we'll return a valid AST. This requires recursing down the |
| 247 | // AST and finding it, not something we are set up to do right now. For |
| 248 | // now, just lop the entire switch stmt out of the AST. |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame^] | 249 | CaseListIsErroneous = true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 250 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 251 | TheDefaultStmt = DS; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 253 | } else { |
| 254 | CaseStmt *CS = cast<CaseStmt>(SC); |
| 255 | |
| 256 | // We already verified that the expression has a i-c-e value (C99 |
| 257 | // 6.8.4.2p3) - get that value now. |
| 258 | llvm::APSInt LoVal(32); |
| 259 | CS->getLHS()->isIntegerConstantExpr(LoVal, Context); |
| 260 | |
| 261 | // Convert the value to the same width/sign as the condition. |
| 262 | ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, |
| 263 | CS->getLHS()->getLocStart(), |
| 264 | diag::warn_case_value_overflow); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame^] | 266 | // If this is a case range, remember it in CaseRanges, otherwise CaseVals. |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 267 | if (CS->getRHS()) |
| 268 | CaseRanges.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame^] | 269 | else |
| 270 | CaseVals.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame^] | 274 | // Sort all the scalar case values so we can easily detect duplicates. |
| 275 | std::stable_sort(CaseVals.begin(), CaseVals.end()); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame^] | 277 | for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { |
| 278 | if (CaseVals[i].first == CaseVals[i+1].first) { |
| 279 | // If we have a duplicate, report it. |
| 280 | Diag(CaseVals[i+1].second->getLHS()->getLocStart(), |
| 281 | diag::err_duplicate_case, CaseVals[i].first.toString()); |
| 282 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
| 283 | diag::err_duplicate_case_prev); |
| 284 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 285 | // but we have no way to do this right now. |
| 286 | CaseListIsErroneous = true; |
| 287 | } |
| 288 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame^] | 290 | // Detect duplicate case ranges, which usually don't exist at all in the first |
| 291 | // place. |
| 292 | if (!CaseRanges.empty()) { |
| 293 | // Sort all the case ranges by their low value so we can easily detect |
| 294 | // overlaps between ranges. |
| 295 | std::stable_sort(CaseVals.begin(), CaseVals.end()); |
| 296 | |
| 297 | // Scan the ranges, computing the high values and removing empty ranges. |
| 298 | std::vector<llvm::APSInt> HiVals; |
| 299 | for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) { |
| 300 | CaseStmt *CR = CaseRanges[i].second; |
| 301 | llvm::APSInt HiVal(32); |
| 302 | CR->getRHS()->isIntegerConstantExpr(HiVal, Context); |
| 303 | |
| 304 | // Convert the value to the same width/sign as the condition. |
| 305 | ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, |
| 306 | CR->getRHS()->getLocStart(), |
| 307 | diag::warn_case_value_overflow); |
| 308 | |
| 309 | // FIXME: if the low value is bigger than the high value, the case is |
| 310 | // empty: emit "empty range specified" warning and drop it. |
| 311 | |
| 312 | HiVals.push_back(HiVal); |
| 313 | } |
| 314 | |
| 315 | // Rescan the ranges, looking for overlap with singleton values and other |
| 316 | // ranges. |
| 317 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
| 318 | //llvm::APSInt &CRLow = CaseRanges[i].first; |
| 319 | //CaseStmt *CR = CaseRanges[i].second; |
| 320 | |
| 321 | // FIXME: TODO. |
| 322 | } |
| 323 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame^] | 325 | // FIXME: If the case list was broken is some way, we don't have a good system |
| 326 | // to patch it up. Instead, just return the whole substmt as broken. |
| 327 | if (CaseListIsErroneous) |
| 328 | return true; |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 330 | return SS; |
| 331 | } |
| 332 | |
| 333 | Action::StmtResult |
| 334 | Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) { |
| 335 | Expr *condExpr = (Expr *)Cond; |
| 336 | assert(condExpr && "ParseWhileStmt(): missing expression"); |
| 337 | |
| 338 | DefaultFunctionArrayConversion(condExpr); |
| 339 | QualType condType = condExpr->getType(); |
| 340 | |
| 341 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 342 | return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar, |
| 343 | condType.getAsString(), condExpr->getSourceRange()); |
| 344 | |
| 345 | return new WhileStmt(condExpr, (Stmt*)Body); |
| 346 | } |
| 347 | |
| 348 | Action::StmtResult |
| 349 | Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body, |
| 350 | SourceLocation WhileLoc, ExprTy *Cond) { |
| 351 | Expr *condExpr = (Expr *)Cond; |
| 352 | assert(condExpr && "ParseDoStmt(): missing expression"); |
| 353 | |
| 354 | DefaultFunctionArrayConversion(condExpr); |
| 355 | QualType condType = condExpr->getType(); |
| 356 | |
| 357 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 358 | return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar, |
| 359 | condType.getAsString(), condExpr->getSourceRange()); |
| 360 | |
| 361 | return new DoStmt((Stmt*)Body, condExpr); |
| 362 | } |
| 363 | |
| 364 | Action::StmtResult |
| 365 | Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
| 366 | StmtTy *First, ExprTy *Second, ExprTy *Third, |
| 367 | SourceLocation RParenLoc, StmtTy *Body) { |
| 368 | if (First) { |
| 369 | // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and |
| 370 | // declaration support to create a DeclStmt node. Once this is done, |
| 371 | // we can test for DeclStmt vs. Expr (already a sub-class of Stmt). |
| 372 | } |
| 373 | if (Second) { |
| 374 | Expr *testExpr = (Expr *)Second; |
| 375 | DefaultFunctionArrayConversion(testExpr); |
| 376 | QualType testType = testExpr->getType(); |
| 377 | |
| 378 | if (!testType->isScalarType()) // C99 6.8.5p2 |
| 379 | return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar, |
| 380 | testType.getAsString(), testExpr->getSourceRange()); |
| 381 | } |
| 382 | return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | Action::StmtResult |
| 387 | Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
| 388 | IdentifierInfo *LabelII) { |
| 389 | // Look up the record for this label identifier. |
| 390 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 391 | |
| 392 | // If we haven't seen this label yet, create a forward reference. |
| 393 | if (LabelDecl == 0) |
| 394 | LabelDecl = new LabelStmt(LabelLoc, LabelII, 0); |
| 395 | |
| 396 | return new GotoStmt(LabelDecl); |
| 397 | } |
| 398 | |
| 399 | Action::StmtResult |
| 400 | Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
| 401 | ExprTy *DestExp) { |
| 402 | // FIXME: Verify that the operand is convertible to void*. |
| 403 | |
| 404 | return new IndirectGotoStmt((Expr*)DestExp); |
| 405 | } |
| 406 | |
| 407 | Action::StmtResult |
| 408 | Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
| 409 | Scope *S = CurScope->getContinueParent(); |
| 410 | if (!S) { |
| 411 | // C99 6.8.6.2p1: A break shall appear only in or as a loop body. |
| 412 | Diag(ContinueLoc, diag::err_continue_not_in_loop); |
| 413 | return true; |
| 414 | } |
| 415 | |
| 416 | return new ContinueStmt(); |
| 417 | } |
| 418 | |
| 419 | Action::StmtResult |
| 420 | Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
| 421 | Scope *S = CurScope->getBreakParent(); |
| 422 | if (!S) { |
| 423 | // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. |
| 424 | Diag(BreakLoc, diag::err_break_not_in_loop_or_switch); |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | return new BreakStmt(); |
| 429 | } |
| 430 | |
| 431 | |
| 432 | Action::StmtResult |
| 433 | Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) { |
| 434 | Expr *RetValExp = static_cast<Expr *>(rex); |
| 435 | QualType lhsType = CurFunctionDecl->getResultType(); |
| 436 | |
| 437 | if (lhsType->isVoidType()) { |
| 438 | if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 439 | Diag(ReturnLoc, diag::ext_return_has_expr, |
| 440 | CurFunctionDecl->getIdentifier()->getName(), |
| 441 | RetValExp->getSourceRange()); |
| 442 | return new ReturnStmt(RetValExp); |
| 443 | } else { |
| 444 | if (!RetValExp) { |
| 445 | const char *funcName = CurFunctionDecl->getIdentifier()->getName(); |
| 446 | if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 447 | Diag(ReturnLoc, diag::ext_return_missing_expr, funcName); |
| 448 | else // C90 6.6.6.4p4 |
| 449 | Diag(ReturnLoc, diag::warn_return_missing_expr, funcName); |
| 450 | return new ReturnStmt((Expr*)0); |
| 451 | } |
| 452 | } |
| 453 | // we have a non-void function with an expression, continue checking |
| 454 | QualType rhsType = RetValExp->getType(); |
| 455 | |
| 456 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 457 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 458 | // function return. |
| 459 | AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType, |
| 460 | RetValExp); |
Ted Kremenek | 235e1ad | 2007-08-14 18:14:14 +0000 | [diff] [blame] | 461 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 462 | // decode the result (notice that extensions still return a type). |
| 463 | switch (result) { |
| 464 | case Compatible: |
| 465 | break; |
| 466 | case Incompatible: |
| 467 | Diag(ReturnLoc, diag::err_typecheck_return_incompatible, |
| 468 | lhsType.getAsString(), rhsType.getAsString(), |
| 469 | RetValExp->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 470 | break; |
| 471 | case PointerFromInt: |
| 472 | // check for null pointer constant (C99 6.3.2.3p3) |
| 473 | if (!RetValExp->isNullPointerConstant(Context)) { |
| 474 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 475 | lhsType.getAsString(), rhsType.getAsString(), |
| 476 | RetValExp->getSourceRange()); |
| 477 | } |
| 478 | break; |
| 479 | case IntFromPointer: |
| 480 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 481 | lhsType.getAsString(), rhsType.getAsString(), |
| 482 | RetValExp->getSourceRange()); |
| 483 | break; |
| 484 | case IncompatiblePointer: |
| 485 | Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer, |
| 486 | lhsType.getAsString(), rhsType.getAsString(), |
| 487 | RetValExp->getSourceRange()); |
| 488 | break; |
| 489 | case CompatiblePointerDiscardsQualifiers: |
| 490 | Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers, |
| 491 | lhsType.getAsString(), rhsType.getAsString(), |
| 492 | RetValExp->getSourceRange()); |
| 493 | break; |
| 494 | } |
Ted Kremenek | 45925ab | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 495 | |
| 496 | if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc); |
| 497 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 498 | return new ReturnStmt((Expr*)RetValExp); |
| 499 | } |
| 500 | |