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 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 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" |
Anders Carlsson | 49dadd6 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
| 21 | #include "clang/Basic/LangOptions.h" |
Anders Carlsson | 49dadd6 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 25 | Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 26 | Expr *E = static_cast<Expr*>(expr); |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 27 | assert(E && "ActOnExprStmt(): missing expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 28 | return E; |
| 29 | } |
| 30 | |
| 31 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 32 | Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 33 | return new NullStmt(SemiLoc); |
| 34 | } |
| 35 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 36 | Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl) { |
Steve Naroff | cb59747 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 37 | if (decl) { |
| 38 | ScopedDecl *SD = dyn_cast<ScopedDecl>(static_cast<Decl *>(decl)); |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 39 | assert(SD && "Sema::ActOnDeclStmt(): expected ScopedDecl"); |
Steve Naroff | cb59747 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 40 | return new DeclStmt(SD); |
| 41 | } else |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 42 | return true; // error |
| 43 | } |
| 44 | |
| 45 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 46 | Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, |
Chris Lattner | f2b0757 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 47 | StmtTy **elts, unsigned NumElts, bool isStmtExpr) { |
Chris Lattner | 3ea3b66 | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 48 | Stmt **Elts = reinterpret_cast<Stmt**>(elts); |
| 49 | // If we're in C89 mode, check that we don't have any decls after stmts. If |
| 50 | // so, emit an extension diagnostic. |
| 51 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) { |
| 52 | // Note that __extension__ can be around a decl. |
| 53 | unsigned i = 0; |
| 54 | // Skip over all declarations. |
| 55 | for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) |
| 56 | /*empty*/; |
| 57 | |
| 58 | // We found the end of the list or a statement. Scan for another declstmt. |
| 59 | for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) |
| 60 | /*empty*/; |
| 61 | |
| 62 | if (i != NumElts) { |
Steve Naroff | cb59747 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 63 | ScopedDecl *D = cast<DeclStmt>(Elts[i])->getDecl(); |
Chris Lattner | 3ea3b66 | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 64 | Diag(D->getLocation(), diag::ext_mixed_decls_code); |
| 65 | } |
| 66 | } |
Chris Lattner | f2b0757 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 67 | // Warn about unused expressions in statements. |
| 68 | for (unsigned i = 0; i != NumElts; ++i) { |
| 69 | Expr *E = dyn_cast<Expr>(Elts[i]); |
| 70 | if (!E) continue; |
| 71 | |
| 72 | // Warn about expressions with unused results. |
| 73 | if (E->hasLocalSideEffect() || E->getType()->isVoidType()) |
| 74 | continue; |
| 75 | |
| 76 | // The last expr in a stmt expr really is used. |
| 77 | if (isStmtExpr && i == NumElts-1) |
| 78 | continue; |
| 79 | |
| 80 | /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in |
| 81 | /// a context where the result is unused. Emit a diagnostic to warn about |
| 82 | /// this. |
| 83 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) |
| 84 | Diag(BO->getOperatorLoc(), diag::warn_unused_expr, |
| 85 | BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange()); |
| 86 | else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 87 | Diag(UO->getOperatorLoc(), diag::warn_unused_expr, |
| 88 | UO->getSubExpr()->getSourceRange()); |
| 89 | else |
| 90 | Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange()); |
| 91 | } |
| 92 | |
Steve Naroff | 5d2fff8 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 93 | return new CompoundStmt(Elts, NumElts, L, R); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 97 | Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 98 | SourceLocation DotDotDotLoc, ExprTy *rhsval, |
| 99 | SourceLocation ColonLoc, StmtTy *subStmt) { |
| 100 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
| 101 | Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval); |
| 102 | assert((LHSVal != 0) && "missing expression in case statement"); |
| 103 | |
| 104 | SourceLocation ExpLoc; |
| 105 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
| 106 | if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) { |
| 107 | Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr, |
| 108 | LHSVal->getSourceRange()); |
| 109 | return SubStmt; |
| 110 | } |
| 111 | |
| 112 | // GCC extension: The expression shall be an integer constant. |
| 113 | if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) { |
| 114 | Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr, |
| 115 | RHSVal->getSourceRange()); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 116 | RHSVal = 0; // Recover by just forgetting about it. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if (SwitchStack.empty()) { |
| 120 | Diag(CaseLoc, diag::err_case_not_in_switch); |
| 121 | return SubStmt; |
| 122 | } |
| 123 | |
Steve Naroff | 5d2fff8 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 124 | CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 125 | SwitchStack.back()->addSwitchCase(CS); |
| 126 | return CS; |
| 127 | } |
| 128 | |
| 129 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 130 | Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 131 | StmtTy *subStmt, Scope *CurScope) { |
| 132 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
| 133 | |
| 134 | if (SwitchStack.empty()) { |
| 135 | Diag(DefaultLoc, diag::err_default_not_in_switch); |
| 136 | return SubStmt; |
| 137 | } |
| 138 | |
| 139 | DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt); |
| 140 | SwitchStack.back()->addSwitchCase(DS); |
| 141 | |
| 142 | return DS; |
| 143 | } |
| 144 | |
| 145 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 146 | Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 147 | SourceLocation ColonLoc, StmtTy *subStmt) { |
| 148 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
| 149 | // Look up the record for this label identifier. |
| 150 | LabelStmt *&LabelDecl = LabelMap[II]; |
| 151 | |
| 152 | // If not forward referenced or defined already, just create a new LabelStmt. |
| 153 | if (LabelDecl == 0) |
| 154 | return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt); |
| 155 | |
| 156 | assert(LabelDecl->getID() == II && "Label mismatch!"); |
| 157 | |
| 158 | // Otherwise, this label was either forward reference or multiply defined. If |
| 159 | // multiply defined, reject it now. |
| 160 | if (LabelDecl->getSubStmt()) { |
| 161 | Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName()); |
| 162 | Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition); |
| 163 | return SubStmt; |
| 164 | } |
| 165 | |
| 166 | // Otherwise, this label was forward declared, and we just found its real |
| 167 | // definition. Fill in the forward definition and return it. |
| 168 | LabelDecl->setIdentLoc(IdentLoc); |
| 169 | LabelDecl->setSubStmt(SubStmt); |
| 170 | return LabelDecl; |
| 171 | } |
| 172 | |
| 173 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 174 | Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 175 | StmtTy *ThenVal, SourceLocation ElseLoc, |
| 176 | StmtTy *ElseVal) { |
| 177 | Expr *condExpr = (Expr *)CondVal; |
Anders Carlsson | 663733e | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 178 | Stmt *thenStmt = (Stmt *)ThenVal; |
| 179 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 180 | assert(condExpr && "ActOnIfStmt(): missing expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 181 | |
| 182 | DefaultFunctionArrayConversion(condExpr); |
| 183 | QualType condType = condExpr->getType(); |
| 184 | |
| 185 | if (!condType->isScalarType()) // C99 6.8.4.1p1 |
| 186 | return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar, |
| 187 | condType.getAsString(), condExpr->getSourceRange()); |
| 188 | |
Anders Carlsson | 663733e | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 189 | // Warn if the if block has a null body without an else value. |
| 190 | // this helps prevent bugs due to typos, such as |
| 191 | // if (condition); |
| 192 | // do_stuff(); |
| 193 | if (!ElseVal) { |
| 194 | if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt)) |
| 195 | Diag(stmt->getSemiLoc(), diag::warn_empty_if_body); |
| 196 | } |
| 197 | |
| 198 | return new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 202 | Sema::ActOnStartOfSwitchStmt(ExprTy *cond) { |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 203 | Expr *Cond = static_cast<Expr*>(cond); |
| 204 | |
| 205 | // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. |
| 206 | UsualUnaryConversions(Cond); |
| 207 | |
| 208 | SwitchStmt *SS = new SwitchStmt(Cond); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 209 | SwitchStack.push_back(SS); |
| 210 | return SS; |
| 211 | } |
| 212 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 213 | /// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have |
| 214 | /// the specified width and sign. If an overflow occurs, detect it and emit |
| 215 | /// the specified diagnostic. |
| 216 | void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, |
| 217 | unsigned NewWidth, bool NewSign, |
| 218 | SourceLocation Loc, |
| 219 | unsigned DiagID) { |
| 220 | // Perform a conversion to the promoted condition type if needed. |
| 221 | if (NewWidth > Val.getBitWidth()) { |
| 222 | // If this is an extension, just do it. |
| 223 | llvm::APSInt OldVal(Val); |
| 224 | Val.extend(NewWidth); |
| 225 | |
| 226 | // If the input was signed and negative and the output is unsigned, |
| 227 | // warn. |
| 228 | if (!NewSign && OldVal.isSigned() && OldVal.isNegative()) |
| 229 | Diag(Loc, DiagID, OldVal.toString(), Val.toString()); |
| 230 | |
| 231 | Val.setIsSigned(NewSign); |
| 232 | } else if (NewWidth < Val.getBitWidth()) { |
| 233 | // If this is a truncation, check for overflow. |
| 234 | llvm::APSInt ConvVal(Val); |
| 235 | ConvVal.trunc(NewWidth); |
Chris Lattner | 5c03960 | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 236 | ConvVal.setIsSigned(NewSign); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 237 | ConvVal.extend(Val.getBitWidth()); |
Chris Lattner | 5c03960 | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 238 | ConvVal.setIsSigned(Val.isSigned()); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 239 | if (ConvVal != Val) |
| 240 | Diag(Loc, DiagID, Val.toString(), ConvVal.toString()); |
| 241 | |
| 242 | // Regardless of whether a diagnostic was emitted, really do the |
| 243 | // truncation. |
| 244 | Val.trunc(NewWidth); |
Chris Lattner | 5c03960 | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 245 | Val.setIsSigned(NewSign); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 246 | } else if (NewSign != Val.isSigned()) { |
| 247 | // Convert the sign to match the sign of the condition. This can cause |
| 248 | // overflow as well: unsigned(INTMIN) |
| 249 | llvm::APSInt OldVal(Val); |
| 250 | Val.setIsSigned(NewSign); |
| 251 | |
| 252 | if (Val.isNegative()) // Sign bit changes meaning. |
| 253 | Diag(Loc, DiagID, OldVal.toString(), Val.toString()); |
| 254 | } |
| 255 | } |
| 256 | |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 257 | namespace { |
| 258 | struct CaseCompareFunctor { |
| 259 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 260 | const llvm::APSInt &RHS) { |
| 261 | return LHS.first < RHS; |
| 262 | } |
Chris Lattner | 2157f27 | 2007-09-03 18:31:57 +0000 | [diff] [blame] | 263 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 264 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 265 | return LHS.first < RHS.first; |
| 266 | } |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 267 | bool operator()(const llvm::APSInt &LHS, |
| 268 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 269 | return LHS < RHS.first; |
| 270 | } |
| 271 | }; |
| 272 | } |
| 273 | |
Chris Lattner | 766afb8 | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 274 | /// CmpCaseVals - Comparison predicate for sorting case values. |
| 275 | /// |
| 276 | static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, |
| 277 | const std::pair<llvm::APSInt, CaseStmt*>& rhs) { |
| 278 | if (lhs.first < rhs.first) |
| 279 | return true; |
| 280 | |
| 281 | if (lhs.first == rhs.first && |
| 282 | lhs.second->getCaseLoc().getRawEncoding() |
| 283 | < rhs.second->getCaseLoc().getRawEncoding()) |
| 284 | return true; |
| 285 | return false; |
| 286 | } |
| 287 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 288 | Action::StmtResult |
Chris Lattner | 766afb8 | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 289 | Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, |
| 290 | ExprTy *Body) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 291 | Stmt *BodyStmt = (Stmt*)Body; |
| 292 | |
| 293 | SwitchStmt *SS = SwitchStack.back(); |
| 294 | assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!"); |
| 295 | |
Steve Naroff | a610eab | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 296 | SS->setBody(BodyStmt, SwitchLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 297 | SwitchStack.pop_back(); |
| 298 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 299 | Expr *CondExpr = SS->getCond(); |
| 300 | QualType CondType = CondExpr->getType(); |
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 | if (!CondType->isIntegerType()) { // C99 6.8.4.2p1 |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 303 | Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer, |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 304 | CondType.getAsString(), CondExpr->getSourceRange()); |
| 305 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 306 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 307 | |
| 308 | // Get the bitwidth of the switched-on value before promotions. We must |
| 309 | // convert the integer case values to this width before comparison. |
Hartmut Kaiser | 612f174 | 2007-09-16 00:04:22 +0000 | [diff] [blame] | 310 | unsigned CondWidth = |
| 311 | static_cast<unsigned>(Context.getTypeSize(CondType, SwitchLoc)); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 312 | bool CondIsSigned = CondType->isSignedIntegerType(); |
| 313 | |
| 314 | // Accumulate all of the case values in a vector so that we can sort them |
| 315 | // and detect duplicates. This vector contains the APInt for the case after |
| 316 | // it has been converted to the condition type. |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 317 | typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; |
| 318 | CaseValsTy CaseVals; |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 319 | |
| 320 | // Keep track of any GNU case ranges we see. The APSInt is the low value. |
| 321 | std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges; |
| 322 | |
| 323 | DefaultStmt *TheDefaultStmt = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 325 | bool CaseListIsErroneous = false; |
| 326 | |
| 327 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 328 | SC = SC->getNextSwitchCase()) { |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 330 | if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 331 | if (TheDefaultStmt) { |
| 332 | Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); |
| 333 | Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 334 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 335 | // FIXME: Remove the default statement from the switch block so that |
| 336 | // we'll return a valid AST. This requires recursing down the |
| 337 | // AST and finding it, not something we are set up to do right now. For |
| 338 | // now, just lop the entire switch stmt out of the AST. |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 339 | CaseListIsErroneous = true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 340 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 341 | TheDefaultStmt = DS; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 343 | } else { |
| 344 | CaseStmt *CS = cast<CaseStmt>(SC); |
| 345 | |
| 346 | // We already verified that the expression has a i-c-e value (C99 |
| 347 | // 6.8.4.2p3) - get that value now. |
| 348 | llvm::APSInt LoVal(32); |
| 349 | CS->getLHS()->isIntegerConstantExpr(LoVal, Context); |
| 350 | |
| 351 | // Convert the value to the same width/sign as the condition. |
| 352 | ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, |
| 353 | CS->getLHS()->getLocStart(), |
| 354 | diag::warn_case_value_overflow); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 355 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 356 | // If this is a case range, remember it in CaseRanges, otherwise CaseVals. |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 357 | if (CS->getRHS()) |
| 358 | CaseRanges.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 359 | else |
| 360 | CaseVals.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 364 | // Sort all the scalar case values so we can easily detect duplicates. |
Chris Lattner | 766afb8 | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 365 | std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 366 | |
Chris Lattner | 2b1b9a8 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 367 | if (!CaseVals.empty()) { |
| 368 | for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { |
| 369 | if (CaseVals[i].first == CaseVals[i+1].first) { |
| 370 | // If we have a duplicate, report it. |
| 371 | Diag(CaseVals[i+1].second->getLHS()->getLocStart(), |
| 372 | diag::err_duplicate_case, CaseVals[i].first.toString()); |
| 373 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
| 374 | diag::err_duplicate_case_prev); |
| 375 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 376 | // but we have no way to do this right now. |
| 377 | CaseListIsErroneous = true; |
| 378 | } |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 382 | // Detect duplicate case ranges, which usually don't exist at all in the first |
| 383 | // place. |
| 384 | if (!CaseRanges.empty()) { |
| 385 | // Sort all the case ranges by their low value so we can easily detect |
| 386 | // overlaps between ranges. |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 387 | std::stable_sort(CaseRanges.begin(), CaseRanges.end()); |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 388 | |
| 389 | // Scan the ranges, computing the high values and removing empty ranges. |
| 390 | std::vector<llvm::APSInt> HiVals; |
Chris Lattner | 7443e0f | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 391 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 392 | CaseStmt *CR = CaseRanges[i].second; |
| 393 | llvm::APSInt HiVal(32); |
| 394 | CR->getRHS()->isIntegerConstantExpr(HiVal, Context); |
| 395 | |
| 396 | // Convert the value to the same width/sign as the condition. |
| 397 | ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, |
| 398 | CR->getRHS()->getLocStart(), |
| 399 | diag::warn_case_value_overflow); |
| 400 | |
Chris Lattner | 7443e0f | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 401 | // If the low value is bigger than the high value, the case is empty. |
| 402 | if (CaseRanges[i].first > HiVal) { |
| 403 | Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range, |
| 404 | SourceRange(CR->getLHS()->getLocStart(), |
| 405 | CR->getRHS()->getLocEnd())); |
| 406 | CaseRanges.erase(CaseRanges.begin()+i); |
| 407 | --i, --e; |
| 408 | continue; |
| 409 | } |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 410 | HiVals.push_back(HiVal); |
| 411 | } |
| 412 | |
| 413 | // Rescan the ranges, looking for overlap with singleton values and other |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 414 | // ranges. Since the range list is sorted, we only need to compare case |
| 415 | // ranges with their neighbors. |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 416 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 417 | llvm::APSInt &CRLo = CaseRanges[i].first; |
| 418 | llvm::APSInt &CRHi = HiVals[i]; |
| 419 | CaseStmt *CR = CaseRanges[i].second; |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 420 | |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 421 | // Check to see whether the case range overlaps with any singleton cases. |
| 422 | CaseStmt *OverlapStmt = 0; |
| 423 | llvm::APSInt OverlapVal(32); |
| 424 | |
| 425 | // Find the smallest value >= the lower bound. If I is in the case range, |
| 426 | // then we have overlap. |
| 427 | CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), |
| 428 | CaseVals.end(), CRLo, |
| 429 | CaseCompareFunctor()); |
| 430 | if (I != CaseVals.end() && I->first < CRHi) { |
| 431 | OverlapVal = I->first; // Found overlap with scalar. |
| 432 | OverlapStmt = I->second; |
| 433 | } |
| 434 | |
| 435 | // Find the smallest value bigger than the upper bound. |
| 436 | I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); |
| 437 | if (I != CaseVals.begin() && (I-1)->first >= CRLo) { |
| 438 | OverlapVal = (I-1)->first; // Found overlap with scalar. |
| 439 | OverlapStmt = (I-1)->second; |
| 440 | } |
| 441 | |
| 442 | // Check to see if this case stmt overlaps with the subsequent case range. |
| 443 | if (i && CRLo <= HiVals[i-1]) { |
| 444 | OverlapVal = HiVals[i-1]; // Found overlap with range. |
| 445 | OverlapStmt = CaseRanges[i-1].second; |
| 446 | } |
| 447 | |
| 448 | if (OverlapStmt) { |
| 449 | // If we have a duplicate, report it. |
| 450 | Diag(CR->getLHS()->getLocStart(), |
| 451 | diag::err_duplicate_case, OverlapVal.toString()); |
| 452 | Diag(OverlapStmt->getLHS()->getLocStart(), |
| 453 | diag::err_duplicate_case_prev); |
| 454 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 455 | // but we have no way to do this right now. |
| 456 | CaseListIsErroneous = true; |
| 457 | } |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 460 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 461 | // FIXME: If the case list was broken is some way, we don't have a good system |
| 462 | // to patch it up. Instead, just return the whole substmt as broken. |
| 463 | if (CaseListIsErroneous) |
| 464 | return true; |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 465 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 466 | return SS; |
| 467 | } |
| 468 | |
| 469 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 470 | Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 471 | Expr *condExpr = (Expr *)Cond; |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 472 | assert(condExpr && "ActOnWhileStmt(): missing expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 473 | |
| 474 | DefaultFunctionArrayConversion(condExpr); |
| 475 | QualType condType = condExpr->getType(); |
| 476 | |
| 477 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 478 | return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar, |
| 479 | condType.getAsString(), condExpr->getSourceRange()); |
| 480 | |
Steve Naroff | 5d2fff8 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 481 | return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 485 | Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 486 | SourceLocation WhileLoc, ExprTy *Cond) { |
| 487 | Expr *condExpr = (Expr *)Cond; |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 488 | assert(condExpr && "ActOnDoStmt(): missing expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 489 | |
| 490 | DefaultFunctionArrayConversion(condExpr); |
| 491 | QualType condType = condExpr->getType(); |
| 492 | |
| 493 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 494 | return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar, |
| 495 | condType.getAsString(), condExpr->getSourceRange()); |
| 496 | |
Steve Naroff | 5d2fff8 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 497 | return new DoStmt((Stmt*)Body, condExpr, DoLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 501 | Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
Chris Lattner | 3332fbd | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 502 | StmtTy *first, ExprTy *second, ExprTy *third, |
| 503 | SourceLocation RParenLoc, StmtTy *body) { |
| 504 | Stmt *First = static_cast<Stmt*>(first); |
| 505 | Expr *Second = static_cast<Expr*>(second); |
| 506 | Expr *Third = static_cast<Expr*>(third); |
| 507 | Stmt *Body = static_cast<Stmt*>(body); |
| 508 | |
Chris Lattner | 0661105 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 509 | if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { |
| 510 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare |
| 511 | // identifiers for objects having storage class 'auto' or 'register'. |
Steve Naroff | 2591e1b | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 512 | for (ScopedDecl *D = DS->getDecl(); D; D = D->getNextDeclarator()) { |
Chris Lattner | 0661105 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 513 | BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D); |
| 514 | if (BVD && !BVD->hasLocalStorage()) |
| 515 | BVD = 0; |
| 516 | if (BVD == 0) |
Steve Naroff | cb59747 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 517 | Diag(dyn_cast<ScopedDecl>(D)->getLocation(), |
| 518 | diag::err_non_variable_decl_in_for); |
Chris Lattner | 0661105 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 519 | // FIXME: mark decl erroneous! |
| 520 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 521 | } |
| 522 | if (Second) { |
Chris Lattner | 3332fbd | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 523 | DefaultFunctionArrayConversion(Second); |
| 524 | QualType SecondType = Second->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 525 | |
Chris Lattner | 3332fbd | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 526 | if (!SecondType->isScalarType()) // C99 6.8.5p2 |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 527 | return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar, |
Chris Lattner | 3332fbd | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 528 | SecondType.getAsString(), Second->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 529 | } |
Steve Naroff | 5d2fff8 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 530 | return new ForStmt(First, Second, Third, Body, ForLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | |
| 534 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 535 | Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 536 | IdentifierInfo *LabelII) { |
| 537 | // Look up the record for this label identifier. |
| 538 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 539 | |
| 540 | // If we haven't seen this label yet, create a forward reference. |
| 541 | if (LabelDecl == 0) |
| 542 | LabelDecl = new LabelStmt(LabelLoc, LabelII, 0); |
| 543 | |
Ted Kremenek | a65ad46 | 2007-09-06 17:11:52 +0000 | [diff] [blame] | 544 | return new GotoStmt(LabelDecl, GotoLoc, LabelLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 548 | Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 549 | ExprTy *DestExp) { |
| 550 | // FIXME: Verify that the operand is convertible to void*. |
| 551 | |
| 552 | return new IndirectGotoStmt((Expr*)DestExp); |
| 553 | } |
| 554 | |
| 555 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 556 | Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 557 | Scope *S = CurScope->getContinueParent(); |
| 558 | if (!S) { |
| 559 | // C99 6.8.6.2p1: A break shall appear only in or as a loop body. |
| 560 | Diag(ContinueLoc, diag::err_continue_not_in_loop); |
| 561 | return true; |
| 562 | } |
| 563 | |
Steve Naroff | c32a20d | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 564 | return new ContinueStmt(ContinueLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 568 | Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 569 | Scope *S = CurScope->getBreakParent(); |
| 570 | if (!S) { |
| 571 | // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. |
| 572 | Diag(BreakLoc, diag::err_break_not_in_loop_or_switch); |
| 573 | return true; |
| 574 | } |
| 575 | |
Steve Naroff | c32a20d | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 576 | return new BreakStmt(BreakLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | |
| 580 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 581 | Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 582 | Expr *RetValExp = static_cast<Expr *>(rex); |
Fariborz Jahanian | 336b2e8 | 2007-12-04 19:20:11 +0000 | [diff] [blame] | 583 | QualType lhsType = CurFunctionDecl ? CurFunctionDecl->getResultType() : |
| 584 | CurMethodDecl->getResultType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 585 | |
| 586 | if (lhsType->isVoidType()) { |
| 587 | if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns) |
Fariborz Jahanian | 336b2e8 | 2007-12-04 19:20:11 +0000 | [diff] [blame] | 588 | Diag(ReturnLoc, diag::ext_return_has_expr, |
| 589 | (CurFunctionDecl ? CurFunctionDecl->getIdentifier()->getName() : |
| 590 | CurMethodDecl->getSelector().getName()), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 591 | RetValExp->getSourceRange()); |
Steve Naroff | c32a20d | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 592 | return new ReturnStmt(ReturnLoc, RetValExp); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 593 | } else { |
| 594 | if (!RetValExp) { |
Fariborz Jahanian | 336b2e8 | 2007-12-04 19:20:11 +0000 | [diff] [blame] | 595 | const char *funcName = CurFunctionDecl ? |
| 596 | CurFunctionDecl->getIdentifier()->getName() : |
| 597 | CurMethodDecl->getSelector().getName().c_str(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 598 | if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 599 | Diag(ReturnLoc, diag::ext_return_missing_expr, funcName); |
| 600 | else // C90 6.6.6.4p4 |
| 601 | Diag(ReturnLoc, diag::warn_return_missing_expr, funcName); |
Steve Naroff | c32a20d | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 602 | return new ReturnStmt(ReturnLoc, (Expr*)0); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | // we have a non-void function with an expression, continue checking |
| 606 | QualType rhsType = RetValExp->getType(); |
| 607 | |
| 608 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 609 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 610 | // function return. |
| 611 | AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType, |
| 612 | RetValExp); |
Ted Kremenek | 235e1ad | 2007-08-14 18:14:14 +0000 | [diff] [blame] | 613 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 614 | // decode the result (notice that extensions still return a type). |
| 615 | switch (result) { |
| 616 | case Compatible: |
| 617 | break; |
| 618 | case Incompatible: |
| 619 | Diag(ReturnLoc, diag::err_typecheck_return_incompatible, |
| 620 | lhsType.getAsString(), rhsType.getAsString(), |
| 621 | RetValExp->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 622 | break; |
| 623 | case PointerFromInt: |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 624 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 625 | lhsType.getAsString(), rhsType.getAsString(), |
| 626 | RetValExp->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 627 | break; |
| 628 | case IntFromPointer: |
| 629 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 630 | lhsType.getAsString(), rhsType.getAsString(), |
| 631 | RetValExp->getSourceRange()); |
| 632 | break; |
| 633 | case IncompatiblePointer: |
| 634 | Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer, |
| 635 | lhsType.getAsString(), rhsType.getAsString(), |
| 636 | RetValExp->getSourceRange()); |
| 637 | break; |
| 638 | case CompatiblePointerDiscardsQualifiers: |
| 639 | Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers, |
| 640 | lhsType.getAsString(), rhsType.getAsString(), |
| 641 | RetValExp->getSourceRange()); |
| 642 | break; |
| 643 | } |
Ted Kremenek | 45925ab | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 644 | |
| 645 | if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc); |
| 646 | |
Steve Naroff | c32a20d | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 647 | return new ReturnStmt(ReturnLoc, (Expr*)RetValExp); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Anders Carlsson | 076c111 | 2007-11-20 19:21:03 +0000 | [diff] [blame] | 650 | Sema::StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, |
Anders Carlsson | 759f45d | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 651 | bool IsVolatile, |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 652 | unsigned NumOutputs, |
| 653 | unsigned NumInputs, |
| 654 | std::string *Names, |
| 655 | ExprTy **Constraints, |
| 656 | ExprTy **Exprs, |
Anders Carlsson | 076c111 | 2007-11-20 19:21:03 +0000 | [diff] [blame] | 657 | ExprTy *AsmString, |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 658 | unsigned NumClobbers, |
| 659 | ExprTy **Clobbers, |
Chris Lattner | 8a40a83 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 660 | SourceLocation RParenLoc) { |
Anders Carlsson | 076c111 | 2007-11-20 19:21:03 +0000 | [diff] [blame] | 661 | Expr *E = (Expr *)AsmString; |
Anders Carlsson | 759f45d | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 662 | |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 663 | for (unsigned i = 0; i < NumOutputs; i++) { |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 664 | StringLiteral *Literal = cast<StringLiteral>((Expr *)Constraints[i]); |
| 665 | assert(!Literal->isWide() && |
| 666 | "Output constraint strings should not be wide!"); |
| 667 | |
| 668 | std::string OutputConstraint(Literal->getStrData(), |
| 669 | Literal->getByteLength()); |
| 670 | |
| 671 | TargetInfo::ConstraintInfo info; |
| 672 | if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(), |
| 673 | info)) { |
| 674 | // FIXME: We currently leak memory here. |
| 675 | Diag(Literal->getLocStart(), |
| 676 | diag::err_invalid_output_constraint_in_asm); |
| 677 | return true; |
| 678 | } |
| 679 | |
| 680 | // Check that the output exprs are valid lvalues. |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 681 | Expr *OutputExpr = (Expr *)Exprs[i]; |
| 682 | Expr::isLvalueResult Result = OutputExpr->isLvalue(); |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 683 | if (Result != Expr::LV_Valid) { |
| 684 | ParenExpr *PE = cast<ParenExpr>(OutputExpr); |
| 685 | |
| 686 | Diag(PE->getSubExpr()->getLocStart(), |
| 687 | diag::err_invalid_lvalue_in_asm_output, |
| 688 | PE->getSubExpr()->getSourceRange()); |
| 689 | |
| 690 | // FIXME: We currently leak memory here. |
| 691 | return true; |
| 692 | } |
| 693 | } |
| 694 | |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 695 | for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 696 | StringLiteral *Literal = cast<StringLiteral>((Expr *)Constraints[i]); |
| 697 | assert(!Literal->isWide() && |
| 698 | "Output constraint strings should not be wide!"); |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 699 | |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 700 | std::string InputConstraint(Literal->getStrData(), |
| 701 | Literal->getByteLength()); |
| 702 | |
| 703 | TargetInfo::ConstraintInfo info; |
| 704 | if (!Context.Target.validateInputConstraint(InputConstraint.c_str(), |
| 705 | NumOutputs, |
| 706 | info)) { |
| 707 | // FIXME: We currently leak memory here. |
| 708 | Diag(Literal->getLocStart(), |
| 709 | diag::err_invalid_input_constraint_in_asm); |
| 710 | return true; |
| 711 | } |
| 712 | |
| 713 | // Check that the input exprs aren't of type void. |
| 714 | Expr *InputExpr = (Expr *)Exprs[i]; |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 715 | if (InputExpr->getType()->isVoidType()) { |
| 716 | ParenExpr *PE = cast<ParenExpr>(InputExpr); |
| 717 | |
| 718 | Diag(PE->getSubExpr()->getLocStart(), |
| 719 | diag::err_invalid_type_in_asm_input, |
| 720 | PE->getType().getAsString(), |
| 721 | PE->getSubExpr()->getSourceRange()); |
| 722 | |
| 723 | // FIXME: We currently leak memory here. |
| 724 | return true; |
| 725 | } |
| 726 | } |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 727 | |
Anders Carlsson | 49dadd6 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 728 | // Check that the clobbers are valid. |
| 729 | for (unsigned i = 0; i < NumClobbers; i++) { |
| 730 | StringLiteral *Literal = cast<StringLiteral>((Expr *)Clobbers[i]); |
| 731 | assert(!Literal->isWide() && "Clobber strings should not be wide!"); |
| 732 | |
| 733 | llvm::SmallString<16> Clobber(Literal->getStrData(), |
| 734 | Literal->getStrData() + |
| 735 | Literal->getByteLength()); |
| 736 | |
| 737 | if (!Context.Target.isValidGCCRegisterName(Clobber.c_str())) { |
| 738 | Diag(Literal->getLocStart(), |
| 739 | diag::err_unknown_register_name_in_asm, |
| 740 | Clobber.c_str()); |
| 741 | |
| 742 | // FIXME: We currently leak memory here. |
| 743 | return true; |
| 744 | } |
| 745 | } |
| 746 | |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 747 | return new AsmStmt(AsmLoc, |
Anders Carlsson | 759f45d | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 748 | IsVolatile, |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 749 | NumOutputs, |
| 750 | NumInputs, |
| 751 | Names, |
| 752 | reinterpret_cast<StringLiteral**>(Constraints), |
| 753 | reinterpret_cast<Expr**>(Exprs), |
| 754 | cast<StringLiteral>(E), |
| 755 | NumClobbers, |
| 756 | reinterpret_cast<StringLiteral**>(Clobbers), |
| 757 | RParenLoc); |
Chris Lattner | 8a40a83 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 758 | } |
Fariborz Jahanian | 0679836 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 759 | |
| 760 | Action::StmtResult |
| 761 | Sema::ActOnObjcAtCatchStmt(SourceLocation AtLoc, |
| 762 | SourceLocation RParen, StmtTy *Parm, |
| 763 | StmtTy *Body, StmtTy *CatchList) { |
| 764 | ObjcAtCatchStmt *CS = new ObjcAtCatchStmt(AtLoc, RParen, |
| 765 | static_cast<Stmt*>(Parm), static_cast<Stmt*>(Body), |
| 766 | static_cast<Stmt*>(CatchList)); |
| 767 | return CatchList ? CatchList : CS; |
| 768 | } |
| 769 | |
Fariborz Jahanian | de3abf8 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 770 | Action::StmtResult |
| 771 | Sema::ActOnObjcAtFinallyStmt(SourceLocation AtLoc, StmtTy *Body) { |
| 772 | ObjcAtFinallyStmt *FS = new ObjcAtFinallyStmt(AtLoc, |
| 773 | static_cast<Stmt*>(Body)); |
| 774 | return FS; |
| 775 | } |
Fariborz Jahanian | b8bf607 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 776 | |
| 777 | Action::StmtResult |
| 778 | Sema::ActOnObjcAtTryStmt(SourceLocation AtLoc, |
| 779 | StmtTy *Try, StmtTy *Catch, StmtTy *Finally) { |
| 780 | ObjcAtTryStmt *TS = new ObjcAtTryStmt(AtLoc, static_cast<Stmt*>(Try), |
| 781 | static_cast<Stmt*>(Catch), |
| 782 | static_cast<Stmt*>(Finally)); |
| 783 | return TS; |
| 784 | } |
| 785 | |
Fariborz Jahanian | 08df2c6 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 786 | Action::StmtResult |
| 787 | Sema::ActOnObjcAtThrowStmt(SourceLocation AtLoc, StmtTy *Throw) { |
| 788 | ObjcAtThrowStmt *TS = new ObjcAtThrowStmt(AtLoc, static_cast<Stmt*>(Throw)); |
| 789 | return TS; |
| 790 | } |
Fariborz Jahanian | b8bf607 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 791 | |
| 792 | |
| 793 | |