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" |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 15 | #include "clang/AST/APValue.h" |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.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" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 23 | Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 24 | Expr *E = static_cast<Expr*>(expr); |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 25 | assert(E && "ActOnExprStmt(): missing expression"); |
Chris Lattner | e02e440 | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 26 | |
| 27 | // C99 6.8.3p2: The expression in an expression statement is evaluated as a |
| 28 | // void expression for its side effects. Conversion to void allows any |
| 29 | // operand, even incomplete types. |
| 30 | |
| 31 | // Same thing in for stmt first clause (when expr) and third clause. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 32 | return E; |
| 33 | } |
| 34 | |
| 35 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 36 | Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 37 | return new NullStmt(SemiLoc); |
| 38 | } |
| 39 | |
Chris Lattner | a4ff427 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 40 | Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl, SourceLocation StartLoc, |
| 41 | SourceLocation EndLoc) { |
| 42 | if (decl == 0) |
| 43 | return true; |
| 44 | |
| 45 | ScopedDecl *SD = cast<ScopedDecl>(static_cast<Decl *>(decl)); |
Ted Kremenek | 1bc18e6 | 2008-10-07 23:09:49 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | // This is a temporary hack until we are always passing around |
| 49 | // DeclGroupRefs. |
| 50 | llvm::SmallVector<Decl*, 10> decls; |
| 51 | while (SD) { |
| 52 | ScopedDecl* d = SD; |
| 53 | SD = SD->getNextDeclarator(); |
| 54 | d->setNextDeclarator(0); |
| 55 | decls.push_back(d); |
| 56 | } |
| 57 | |
| 58 | assert (!decls.empty()); |
| 59 | |
| 60 | if (decls.size() == 1) { |
| 61 | DeclGroupOwningRef DG(*decls.begin()); |
| 62 | return new DeclStmt(DG, StartLoc, EndLoc); |
| 63 | } |
| 64 | else { |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame^] | 65 | DeclGroupOwningRef DG(DeclGroup::Create(Context, decls.size(), &decls[0])); |
Ted Kremenek | 1bc18e6 | 2008-10-07 23:09:49 +0000 | [diff] [blame] | 66 | return new DeclStmt(DG, StartLoc, EndLoc); |
| 67 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 71 | Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, |
Chris Lattner | f2b0757 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 72 | StmtTy **elts, unsigned NumElts, bool isStmtExpr) { |
Chris Lattner | 3ea3b66 | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 73 | Stmt **Elts = reinterpret_cast<Stmt**>(elts); |
| 74 | // If we're in C89 mode, check that we don't have any decls after stmts. If |
| 75 | // so, emit an extension diagnostic. |
| 76 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) { |
| 77 | // Note that __extension__ can be around a decl. |
| 78 | unsigned i = 0; |
| 79 | // Skip over all declarations. |
| 80 | for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) |
| 81 | /*empty*/; |
| 82 | |
| 83 | // We found the end of the list or a statement. Scan for another declstmt. |
| 84 | for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) |
| 85 | /*empty*/; |
| 86 | |
| 87 | if (i != NumElts) { |
Ted Kremenek | c735043 | 2008-10-06 18:48:35 +0000 | [diff] [blame] | 88 | ScopedDecl *D = *cast<DeclStmt>(Elts[i])->decl_begin(); |
Chris Lattner | 3ea3b66 | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 89 | Diag(D->getLocation(), diag::ext_mixed_decls_code); |
| 90 | } |
| 91 | } |
Chris Lattner | f2b0757 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 92 | // Warn about unused expressions in statements. |
| 93 | for (unsigned i = 0; i != NumElts; ++i) { |
| 94 | Expr *E = dyn_cast<Expr>(Elts[i]); |
| 95 | if (!E) continue; |
| 96 | |
| 97 | // Warn about expressions with unused results. |
| 98 | if (E->hasLocalSideEffect() || E->getType()->isVoidType()) |
| 99 | continue; |
| 100 | |
| 101 | // The last expr in a stmt expr really is used. |
| 102 | if (isStmtExpr && i == NumElts-1) |
| 103 | continue; |
| 104 | |
| 105 | /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in |
| 106 | /// a context where the result is unused. Emit a diagnostic to warn about |
| 107 | /// this. |
| 108 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 109 | Diag(BO->getOperatorLoc(), diag::warn_unused_expr) |
| 110 | << BO->getLHS()->getSourceRange() << BO->getRHS()->getSourceRange(); |
Chris Lattner | f2b0757 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 111 | else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 112 | Diag(UO->getOperatorLoc(), diag::warn_unused_expr) |
| 113 | << UO->getSubExpr()->getSourceRange(); |
Chris Lattner | f2b0757 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 114 | else |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 115 | Diag(E->getExprLoc(), diag::warn_unused_expr) << E->getSourceRange(); |
Chris Lattner | f2b0757 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Steve Naroff | 5d2fff8 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 118 | return new CompoundStmt(Elts, NumElts, L, R); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 122 | Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 123 | SourceLocation DotDotDotLoc, ExprTy *rhsval, |
| 124 | SourceLocation ColonLoc, StmtTy *subStmt) { |
| 125 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
| 126 | Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval); |
| 127 | assert((LHSVal != 0) && "missing expression in case statement"); |
| 128 | |
| 129 | SourceLocation ExpLoc; |
| 130 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 131 | // However, GCC allows any evaluatable integer expression. |
| 132 | // FIXME: Should we warn if this is evaluatable but not an I-C-E? |
| 133 | APValue Result; |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 134 | bool isEvaluated; |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 135 | |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 136 | if (!LHSVal->Evaluate(Result, Context, &isEvaluated) || !Result.isInt() || |
| 137 | !isEvaluated) { |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 138 | // FIXME: Evaluate doesn't return the SourceLocation that it failed to |
| 139 | // evaluate. |
| 140 | ExpLoc = LHSVal->getExprLoc(); |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 141 | Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr) |
| 142 | << LHSVal->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 143 | return SubStmt; |
| 144 | } |
| 145 | |
| 146 | // GCC extension: The expression shall be an integer constant. |
Anders Carlsson | b96c206 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 147 | if (RHSVal && !RHSVal->Evaluate(Result, Context, &isEvaluated) || |
| 148 | !Result.isInt() || !isEvaluated) { |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 149 | ExpLoc = RHSVal->getExprLoc(); |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 150 | Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr) |
| 151 | << RHSVal->getSourceRange(); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 152 | RHSVal = 0; // Recover by just forgetting about it. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | if (SwitchStack.empty()) { |
| 156 | Diag(CaseLoc, diag::err_case_not_in_switch); |
| 157 | return SubStmt; |
| 158 | } |
| 159 | |
Steve Naroff | 5d2fff8 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 160 | CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 161 | SwitchStack.back()->addSwitchCase(CS); |
| 162 | return CS; |
| 163 | } |
| 164 | |
| 165 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 166 | Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 167 | StmtTy *subStmt, Scope *CurScope) { |
| 168 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
| 169 | |
| 170 | if (SwitchStack.empty()) { |
| 171 | Diag(DefaultLoc, diag::err_default_not_in_switch); |
| 172 | return SubStmt; |
| 173 | } |
| 174 | |
| 175 | DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt); |
| 176 | SwitchStack.back()->addSwitchCase(DS); |
| 177 | |
| 178 | return DS; |
| 179 | } |
| 180 | |
| 181 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 182 | Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 183 | SourceLocation ColonLoc, StmtTy *subStmt) { |
| 184 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
| 185 | // Look up the record for this label identifier. |
| 186 | LabelStmt *&LabelDecl = LabelMap[II]; |
| 187 | |
| 188 | // If not forward referenced or defined already, just create a new LabelStmt. |
| 189 | if (LabelDecl == 0) |
| 190 | return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt); |
| 191 | |
| 192 | assert(LabelDecl->getID() == II && "Label mismatch!"); |
| 193 | |
| 194 | // Otherwise, this label was either forward reference or multiply defined. If |
| 195 | // multiply defined, reject it now. |
| 196 | if (LabelDecl->getSubStmt()) { |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame^] | 197 | Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 198 | Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition); |
| 199 | return SubStmt; |
| 200 | } |
| 201 | |
| 202 | // Otherwise, this label was forward declared, and we just found its real |
| 203 | // definition. Fill in the forward definition and return it. |
| 204 | LabelDecl->setIdentLoc(IdentLoc); |
| 205 | LabelDecl->setSubStmt(SubStmt); |
| 206 | return LabelDecl; |
| 207 | } |
| 208 | |
| 209 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 210 | Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 211 | StmtTy *ThenVal, SourceLocation ElseLoc, |
| 212 | StmtTy *ElseVal) { |
| 213 | Expr *condExpr = (Expr *)CondVal; |
Anders Carlsson | 663733e | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 214 | Stmt *thenStmt = (Stmt *)ThenVal; |
| 215 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 216 | assert(condExpr && "ActOnIfStmt(): missing expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 217 | |
| 218 | DefaultFunctionArrayConversion(condExpr); |
| 219 | QualType condType = condExpr->getType(); |
| 220 | |
Argiris Kirtzidis | 810c0f7 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 221 | if (getLangOptions().CPlusPlus) { |
| 222 | if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4 |
| 223 | return true; |
| 224 | } else if (!condType->isScalarType()) // C99 6.8.4.1p1 |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 225 | return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar) |
| 226 | << condType.getAsString() << condExpr->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 227 | |
Anders Carlsson | 663733e | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 228 | // Warn if the if block has a null body without an else value. |
| 229 | // this helps prevent bugs due to typos, such as |
| 230 | // if (condition); |
| 231 | // do_stuff(); |
| 232 | if (!ElseVal) { |
| 233 | if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt)) |
| 234 | Diag(stmt->getSemiLoc(), diag::warn_empty_if_body); |
| 235 | } |
| 236 | |
| 237 | return new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 241 | Sema::ActOnStartOfSwitchStmt(ExprTy *cond) { |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 242 | Expr *Cond = static_cast<Expr*>(cond); |
| 243 | |
Argiris Kirtzidis | 810c0f7 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 244 | if (getLangOptions().CPlusPlus) { |
| 245 | // C++ 6.4.2.p2: |
| 246 | // The condition shall be of integral type, enumeration type, or of a class |
| 247 | // type for which a single conversion function to integral or enumeration |
| 248 | // type exists (12.3). If the condition is of class type, the condition is |
| 249 | // converted by calling that conversion function, and the result of the |
| 250 | // conversion is used in place of the original condition for the remainder |
| 251 | // of this section. Integral promotions are performed. |
| 252 | |
| 253 | QualType Ty = Cond->getType(); |
| 254 | |
| 255 | // FIXME: Handle class types. |
| 256 | |
| 257 | // If the type is wrong a diagnostic will be emitted later at |
| 258 | // ActOnFinishSwitchStmt. |
| 259 | if (Ty->isIntegralType() || Ty->isEnumeralType()) { |
| 260 | // Integral promotions are performed. |
| 261 | // FIXME: Integral promotions for C++ are not complete. |
| 262 | UsualUnaryConversions(Cond); |
| 263 | } |
| 264 | } else { |
| 265 | // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. |
| 266 | UsualUnaryConversions(Cond); |
| 267 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 268 | |
| 269 | SwitchStmt *SS = new SwitchStmt(Cond); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 270 | SwitchStack.push_back(SS); |
| 271 | return SS; |
| 272 | } |
| 273 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 274 | /// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have |
| 275 | /// the specified width and sign. If an overflow occurs, detect it and emit |
| 276 | /// the specified diagnostic. |
| 277 | void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, |
| 278 | unsigned NewWidth, bool NewSign, |
| 279 | SourceLocation Loc, |
| 280 | unsigned DiagID) { |
| 281 | // Perform a conversion to the promoted condition type if needed. |
| 282 | if (NewWidth > Val.getBitWidth()) { |
| 283 | // If this is an extension, just do it. |
| 284 | llvm::APSInt OldVal(Val); |
| 285 | Val.extend(NewWidth); |
| 286 | |
| 287 | // If the input was signed and negative and the output is unsigned, |
| 288 | // warn. |
| 289 | if (!NewSign && OldVal.isSigned() && OldVal.isNegative()) |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 290 | Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 291 | |
| 292 | Val.setIsSigned(NewSign); |
| 293 | } else if (NewWidth < Val.getBitWidth()) { |
| 294 | // If this is a truncation, check for overflow. |
| 295 | llvm::APSInt ConvVal(Val); |
| 296 | ConvVal.trunc(NewWidth); |
Chris Lattner | 5c03960 | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 297 | ConvVal.setIsSigned(NewSign); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 298 | ConvVal.extend(Val.getBitWidth()); |
Chris Lattner | 5c03960 | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 299 | ConvVal.setIsSigned(Val.isSigned()); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 300 | if (ConvVal != Val) |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 301 | Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 302 | |
| 303 | // Regardless of whether a diagnostic was emitted, really do the |
| 304 | // truncation. |
| 305 | Val.trunc(NewWidth); |
Chris Lattner | 5c03960 | 2007-08-23 22:08:35 +0000 | [diff] [blame] | 306 | Val.setIsSigned(NewSign); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 307 | } else if (NewSign != Val.isSigned()) { |
| 308 | // Convert the sign to match the sign of the condition. This can cause |
| 309 | // overflow as well: unsigned(INTMIN) |
| 310 | llvm::APSInt OldVal(Val); |
| 311 | Val.setIsSigned(NewSign); |
| 312 | |
| 313 | if (Val.isNegative()) // Sign bit changes meaning. |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 314 | Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 318 | namespace { |
| 319 | struct CaseCompareFunctor { |
| 320 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 321 | const llvm::APSInt &RHS) { |
| 322 | return LHS.first < RHS; |
| 323 | } |
Chris Lattner | 2157f27 | 2007-09-03 18:31:57 +0000 | [diff] [blame] | 324 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 325 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 326 | return LHS.first < RHS.first; |
| 327 | } |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 328 | bool operator()(const llvm::APSInt &LHS, |
| 329 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 330 | return LHS < RHS.first; |
| 331 | } |
| 332 | }; |
| 333 | } |
| 334 | |
Chris Lattner | 766afb8 | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 335 | /// CmpCaseVals - Comparison predicate for sorting case values. |
| 336 | /// |
| 337 | static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, |
| 338 | const std::pair<llvm::APSInt, CaseStmt*>& rhs) { |
| 339 | if (lhs.first < rhs.first) |
| 340 | return true; |
| 341 | |
| 342 | if (lhs.first == rhs.first && |
| 343 | lhs.second->getCaseLoc().getRawEncoding() |
| 344 | < rhs.second->getCaseLoc().getRawEncoding()) |
| 345 | return true; |
| 346 | return false; |
| 347 | } |
| 348 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 349 | Action::StmtResult |
Chris Lattner | 766afb8 | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 350 | Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, |
| 351 | ExprTy *Body) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 352 | Stmt *BodyStmt = (Stmt*)Body; |
| 353 | |
| 354 | SwitchStmt *SS = SwitchStack.back(); |
| 355 | assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!"); |
| 356 | |
Steve Naroff | a610eab | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 357 | SS->setBody(BodyStmt, SwitchLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 358 | SwitchStack.pop_back(); |
| 359 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 360 | Expr *CondExpr = SS->getCond(); |
| 361 | QualType CondType = CondExpr->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 363 | if (!CondType->isIntegerType()) { // C99 6.8.4.2p1 |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 364 | Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer) |
| 365 | << CondType.getAsString() << CondExpr->getSourceRange(); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 366 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 368 | |
| 369 | // Get the bitwidth of the switched-on value before promotions. We must |
| 370 | // convert the integer case values to this width before comparison. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 371 | unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType)); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 372 | bool CondIsSigned = CondType->isSignedIntegerType(); |
| 373 | |
| 374 | // Accumulate all of the case values in a vector so that we can sort them |
| 375 | // and detect duplicates. This vector contains the APInt for the case after |
| 376 | // it has been converted to the condition type. |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 377 | typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; |
| 378 | CaseValsTy CaseVals; |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 379 | |
| 380 | // Keep track of any GNU case ranges we see. The APSInt is the low value. |
| 381 | std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges; |
| 382 | |
| 383 | DefaultStmt *TheDefaultStmt = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 385 | bool CaseListIsErroneous = false; |
| 386 | |
| 387 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 388 | SC = SC->getNextSwitchCase()) { |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 389 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 390 | if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 391 | if (TheDefaultStmt) { |
| 392 | Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); |
| 393 | Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 395 | // FIXME: Remove the default statement from the switch block so that |
| 396 | // we'll return a valid AST. This requires recursing down the |
| 397 | // AST and finding it, not something we are set up to do right now. For |
| 398 | // now, just lop the entire switch stmt out of the AST. |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 399 | CaseListIsErroneous = true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 400 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 401 | TheDefaultStmt = DS; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 402 | |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 403 | } else { |
| 404 | CaseStmt *CS = cast<CaseStmt>(SC); |
| 405 | |
| 406 | // We already verified that the expression has a i-c-e value (C99 |
| 407 | // 6.8.4.2p3) - get that value now. |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 408 | Expr *Lo = CS->getLHS(); |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 409 | llvm::APSInt LoVal = Lo->EvaluateAsInt(Context); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 410 | |
| 411 | // Convert the value to the same width/sign as the condition. |
| 412 | ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, |
| 413 | CS->getLHS()->getLocStart(), |
| 414 | diag::warn_case_value_overflow); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 415 | |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 416 | // If the LHS is not the same type as the condition, insert an implicit |
| 417 | // cast. |
| 418 | ImpCastExprToType(Lo, CondType); |
| 419 | CS->setLHS(Lo); |
| 420 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 421 | // If this is a case range, remember it in CaseRanges, otherwise CaseVals. |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 422 | if (CS->getRHS()) |
| 423 | CaseRanges.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 424 | else |
| 425 | CaseVals.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 429 | // Sort all the scalar case values so we can easily detect duplicates. |
Chris Lattner | 766afb8 | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 430 | std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 2b1b9a8 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 432 | if (!CaseVals.empty()) { |
| 433 | for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { |
| 434 | if (CaseVals[i].first == CaseVals[i+1].first) { |
| 435 | // If we have a duplicate, report it. |
| 436 | Diag(CaseVals[i+1].second->getLHS()->getLocStart(), |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 437 | diag::err_duplicate_case) << CaseVals[i].first.toString(10); |
Chris Lattner | 2b1b9a8 | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 438 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
| 439 | diag::err_duplicate_case_prev); |
| 440 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 441 | // but we have no way to do this right now. |
| 442 | CaseListIsErroneous = true; |
| 443 | } |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 447 | // Detect duplicate case ranges, which usually don't exist at all in the first |
| 448 | // place. |
| 449 | if (!CaseRanges.empty()) { |
| 450 | // Sort all the case ranges by their low value so we can easily detect |
| 451 | // overlaps between ranges. |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 452 | std::stable_sort(CaseRanges.begin(), CaseRanges.end()); |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 453 | |
| 454 | // Scan the ranges, computing the high values and removing empty ranges. |
| 455 | std::vector<llvm::APSInt> HiVals; |
Chris Lattner | 7443e0f | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 456 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 457 | CaseStmt *CR = CaseRanges[i].second; |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 458 | Expr *Hi = CR->getRHS(); |
Anders Carlsson | e8bd9f2 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 459 | llvm::APSInt HiVal = Hi->EvaluateAsInt(Context); |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 460 | |
| 461 | // Convert the value to the same width/sign as the condition. |
| 462 | ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, |
| 463 | CR->getRHS()->getLocStart(), |
| 464 | diag::warn_case_value_overflow); |
| 465 | |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 466 | // If the LHS is not the same type as the condition, insert an implicit |
| 467 | // cast. |
| 468 | ImpCastExprToType(Hi, CondType); |
| 469 | CR->setRHS(Hi); |
| 470 | |
Chris Lattner | 7443e0f | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 471 | // If the low value is bigger than the high value, the case is empty. |
| 472 | if (CaseRanges[i].first > HiVal) { |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 473 | Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range) |
| 474 | << SourceRange(CR->getLHS()->getLocStart(), |
| 475 | CR->getRHS()->getLocEnd()); |
Chris Lattner | 7443e0f | 2007-08-23 17:48:14 +0000 | [diff] [blame] | 476 | CaseRanges.erase(CaseRanges.begin()+i); |
| 477 | --i, --e; |
| 478 | continue; |
| 479 | } |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 480 | HiVals.push_back(HiVal); |
| 481 | } |
| 482 | |
| 483 | // Rescan the ranges, looking for overlap with singleton values and other |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 484 | // ranges. Since the range list is sorted, we only need to compare case |
| 485 | // ranges with their neighbors. |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 486 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 487 | llvm::APSInt &CRLo = CaseRanges[i].first; |
| 488 | llvm::APSInt &CRHi = HiVals[i]; |
| 489 | CaseStmt *CR = CaseRanges[i].second; |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 490 | |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 491 | // Check to see whether the case range overlaps with any singleton cases. |
| 492 | CaseStmt *OverlapStmt = 0; |
| 493 | llvm::APSInt OverlapVal(32); |
| 494 | |
| 495 | // Find the smallest value >= the lower bound. If I is in the case range, |
| 496 | // then we have overlap. |
| 497 | CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), |
| 498 | CaseVals.end(), CRLo, |
| 499 | CaseCompareFunctor()); |
| 500 | if (I != CaseVals.end() && I->first < CRHi) { |
| 501 | OverlapVal = I->first; // Found overlap with scalar. |
| 502 | OverlapStmt = I->second; |
| 503 | } |
| 504 | |
| 505 | // Find the smallest value bigger than the upper bound. |
| 506 | I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); |
| 507 | if (I != CaseVals.begin() && (I-1)->first >= CRLo) { |
| 508 | OverlapVal = (I-1)->first; // Found overlap with scalar. |
| 509 | OverlapStmt = (I-1)->second; |
| 510 | } |
| 511 | |
| 512 | // Check to see if this case stmt overlaps with the subsequent case range. |
| 513 | if (i && CRLo <= HiVals[i-1]) { |
| 514 | OverlapVal = HiVals[i-1]; // Found overlap with range. |
| 515 | OverlapStmt = CaseRanges[i-1].second; |
| 516 | } |
| 517 | |
| 518 | if (OverlapStmt) { |
| 519 | // If we have a duplicate, report it. |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 520 | Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case) |
| 521 | << OverlapVal.toString(10); |
Chris Lattner | 0ab833c | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 522 | Diag(OverlapStmt->getLHS()->getLocStart(), |
| 523 | diag::err_duplicate_case_prev); |
| 524 | // FIXME: We really want to remove the bogus case stmt from the substmt, |
| 525 | // but we have no way to do this right now. |
| 526 | CaseListIsErroneous = true; |
| 527 | } |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 530 | |
Chris Lattner | 1a4066d | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 531 | // FIXME: If the case list was broken is some way, we don't have a good system |
| 532 | // to patch it up. Instead, just return the whole substmt as broken. |
| 533 | if (CaseListIsErroneous) |
| 534 | return true; |
Chris Lattner | 3429a81 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 535 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 536 | return SS; |
| 537 | } |
| 538 | |
| 539 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 540 | Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 541 | Expr *condExpr = (Expr *)Cond; |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 542 | assert(condExpr && "ActOnWhileStmt(): missing expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 543 | |
| 544 | DefaultFunctionArrayConversion(condExpr); |
| 545 | QualType condType = condExpr->getType(); |
| 546 | |
Argiris Kirtzidis | 810c0f7 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 547 | if (getLangOptions().CPlusPlus) { |
| 548 | if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4 |
| 549 | return true; |
| 550 | } else if (!condType->isScalarType()) // C99 6.8.5p2 |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 551 | return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar) |
| 552 | << condType.getAsString() << condExpr->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 553 | |
Steve Naroff | 5d2fff8 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 554 | return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 558 | Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 559 | SourceLocation WhileLoc, ExprTy *Cond) { |
| 560 | Expr *condExpr = (Expr *)Cond; |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 561 | assert(condExpr && "ActOnDoStmt(): missing expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 562 | |
| 563 | DefaultFunctionArrayConversion(condExpr); |
| 564 | QualType condType = condExpr->getType(); |
| 565 | |
Argiris Kirtzidis | c362d38 | 2008-09-11 05:16:22 +0000 | [diff] [blame] | 566 | if (getLangOptions().CPlusPlus) { |
| 567 | if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4 |
| 568 | return true; |
| 569 | } else if (!condType->isScalarType()) // C99 6.8.5p2 |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 570 | return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar) |
| 571 | << condType.getAsString() << condExpr->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 572 | |
Steve Naroff | 5d2fff8 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 573 | return new DoStmt((Stmt*)Body, condExpr, DoLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 577 | Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
Chris Lattner | 3332fbd | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 578 | StmtTy *first, ExprTy *second, ExprTy *third, |
| 579 | SourceLocation RParenLoc, StmtTy *body) { |
| 580 | Stmt *First = static_cast<Stmt*>(first); |
| 581 | Expr *Second = static_cast<Expr*>(second); |
| 582 | Expr *Third = static_cast<Expr*>(third); |
| 583 | Stmt *Body = static_cast<Stmt*>(body); |
| 584 | |
Argiris Kirtzidis | 810c0f7 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 585 | if (!getLangOptions().CPlusPlus) { |
| 586 | if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 587 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only |
| 588 | // declare identifiers for objects having storage class 'auto' or |
| 589 | // 'register'. |
Argiris Kirtzidis | 810c0f7 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 590 | for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); |
| 591 | DI!=DE; ++DI) { |
| 592 | VarDecl *VD = dyn_cast<VarDecl>(*DI); |
| 593 | if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage()) |
| 594 | VD = 0; |
| 595 | if (VD == 0) |
| 596 | Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for); |
| 597 | // FIXME: mark decl erroneous! |
| 598 | } |
Chris Lattner | 0661105 | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 599 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 600 | } |
| 601 | if (Second) { |
Chris Lattner | 3332fbd | 2007-08-28 04:55:47 +0000 | [diff] [blame] | 602 | DefaultFunctionArrayConversion(Second); |
| 603 | QualType SecondType = Second->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 604 | |
Argiris Kirtzidis | 810c0f7 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 605 | if (getLangOptions().CPlusPlus) { |
| 606 | if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4 |
| 607 | return true; |
| 608 | } else if (!SecondType->isScalarType()) // C99 6.8.5p2 |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 609 | return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar) |
| 610 | << SecondType.getAsString() << Second->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 611 | } |
Steve Naroff | 5d2fff8 | 2007-08-31 23:28:33 +0000 | [diff] [blame] | 612 | return new ForStmt(First, Second, Third, Body, ForLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Fariborz Jahanian | 1300bc7 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 615 | Action::StmtResult |
Fariborz Jahanian | df2b095 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 616 | Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, |
Fariborz Jahanian | 1300bc7 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 617 | SourceLocation LParenLoc, |
| 618 | StmtTy *first, ExprTy *second, |
| 619 | SourceLocation RParenLoc, StmtTy *body) { |
| 620 | Stmt *First = static_cast<Stmt*>(first); |
| 621 | Expr *Second = static_cast<Expr*>(second); |
| 622 | Stmt *Body = static_cast<Stmt*>(body); |
Fariborz Jahanian | 6833b3b | 2008-01-10 20:33:58 +0000 | [diff] [blame] | 623 | if (First) { |
| 624 | QualType FirstType; |
| 625 | if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) { |
Ted Kremenek | 779e1c2 | 2008-10-06 20:58:11 +0000 | [diff] [blame] | 626 | if (!DS->hasSolitaryDecl()) |
| 627 | return Diag((*DS->decl_begin())->getLocation(), |
| 628 | diag::err_toomany_element_decls); |
| 629 | |
| 630 | ScopedDecl *D = DS->getSolitaryDecl(); |
| 631 | FirstType = cast<ValueDecl>(D)->getType(); |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 632 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only |
| 633 | // declare identifiers for objects having storage class 'auto' or |
| 634 | // 'register'. |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 635 | VarDecl *VD = cast<VarDecl>(D); |
| 636 | if (VD->isBlockVarDecl() && !VD->hasLocalStorage()) |
| 637 | return Diag(VD->getLocation(), diag::err_non_variable_decl_in_for); |
Anders Carlsson | 8d0bcb0 | 2008-08-25 18:16:36 +0000 | [diff] [blame] | 638 | } else { |
| 639 | Expr::isLvalueResult lval = cast<Expr>(First)->isLvalue(Context); |
| 640 | |
| 641 | if (lval != Expr::LV_Valid) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 642 | return Diag(First->getLocStart(), diag::err_selector_element_not_lvalue) |
| 643 | << First->getSourceRange(); |
Anders Carlsson | 8d0bcb0 | 2008-08-25 18:16:36 +0000 | [diff] [blame] | 644 | |
| 645 | FirstType = static_cast<Expr*>(first)->getType(); |
| 646 | } |
Ted Kremenek | 118930e | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 647 | if (!Context.isObjCObjectPointerType(FirstType)) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 648 | Diag(ForLoc, diag::err_selector_element_type) |
| 649 | << FirstType.getAsString() << First->getSourceRange(); |
Fariborz Jahanian | 1300bc7 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 650 | } |
| 651 | if (Second) { |
| 652 | DefaultFunctionArrayConversion(Second); |
| 653 | QualType SecondType = Second->getType(); |
Ted Kremenek | 118930e | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 654 | if (!Context.isObjCObjectPointerType(SecondType)) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 655 | Diag(ForLoc, diag::err_collection_expr_type) |
| 656 | << SecondType.getAsString() << Second->getSourceRange(); |
Fariborz Jahanian | 1300bc7 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 657 | } |
Fariborz Jahanian | df2b095 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 658 | return new ObjCForCollectionStmt(First, Second, Body, ForLoc, RParenLoc); |
Fariborz Jahanian | 1300bc7 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 659 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 660 | |
| 661 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 662 | Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 663 | IdentifierInfo *LabelII) { |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 664 | // If we are in a block, reject all gotos for now. |
| 665 | if (CurBlock) |
| 666 | return Diag(GotoLoc, diag::err_goto_in_block); |
| 667 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 668 | // Look up the record for this label identifier. |
| 669 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 670 | |
| 671 | // If we haven't seen this label yet, create a forward reference. |
| 672 | if (LabelDecl == 0) |
| 673 | LabelDecl = new LabelStmt(LabelLoc, LabelII, 0); |
| 674 | |
Ted Kremenek | a65ad46 | 2007-09-06 17:11:52 +0000 | [diff] [blame] | 675 | return new GotoStmt(LabelDecl, GotoLoc, LabelLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 679 | Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 680 | ExprTy *DestExp) { |
| 681 | // FIXME: Verify that the operand is convertible to void*. |
| 682 | |
| 683 | return new IndirectGotoStmt((Expr*)DestExp); |
| 684 | } |
| 685 | |
| 686 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 687 | Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 688 | Scope *S = CurScope->getContinueParent(); |
| 689 | if (!S) { |
| 690 | // C99 6.8.6.2p1: A break shall appear only in or as a loop body. |
| 691 | Diag(ContinueLoc, diag::err_continue_not_in_loop); |
| 692 | return true; |
| 693 | } |
| 694 | |
Steve Naroff | c32a20d | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 695 | return new ContinueStmt(ContinueLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 699 | Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 700 | Scope *S = CurScope->getBreakParent(); |
| 701 | if (!S) { |
| 702 | // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. |
| 703 | Diag(BreakLoc, diag::err_break_not_in_loop_or_switch); |
| 704 | return true; |
| 705 | } |
| 706 | |
Steve Naroff | c32a20d | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 707 | return new BreakStmt(BreakLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 710 | /// ActOnBlockReturnStmt - Utility routine to figure out block's return type. |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 711 | /// |
| 712 | Action::StmtResult |
| 713 | Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { |
| 714 | |
| 715 | // If this is the first return we've seen in the block, infer the type of |
| 716 | // the block from it. |
| 717 | if (CurBlock->ReturnType == 0) { |
Steve Naroff | 503996b | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 718 | if (RetValExp) { |
Steve Naroff | e2b66a8 | 2008-09-24 22:26:48 +0000 | [diff] [blame] | 719 | // Don't call UsualUnaryConversions(), since we don't want to do |
| 720 | // integer promotions here. |
| 721 | DefaultFunctionArrayConversion(RetValExp); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 722 | CurBlock->ReturnType = RetValExp->getType().getTypePtr(); |
Steve Naroff | 503996b | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 723 | } else |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 724 | CurBlock->ReturnType = Context.VoidTy.getTypePtr(); |
| 725 | return new ReturnStmt(ReturnLoc, RetValExp); |
| 726 | } |
| 727 | |
| 728 | // Otherwise, verify that this result type matches the previous one. We are |
| 729 | // pickier with blocks than for normal functions because we don't have GCC |
| 730 | // compatibility to worry about here. |
| 731 | if (CurBlock->ReturnType->isVoidType()) { |
| 732 | if (RetValExp) { |
| 733 | Diag(ReturnLoc, diag::err_return_block_has_expr); |
| 734 | delete RetValExp; |
| 735 | RetValExp = 0; |
| 736 | } |
| 737 | return new ReturnStmt(ReturnLoc, RetValExp); |
| 738 | } |
| 739 | |
| 740 | if (!RetValExp) { |
| 741 | Diag(ReturnLoc, diag::err_block_return_missing_expr); |
| 742 | return true; |
| 743 | } |
| 744 | |
| 745 | // we have a non-void block with an expression, continue checking |
| 746 | QualType RetValType = RetValExp->getType(); |
| 747 | |
| 748 | // For now, restrict multiple return statements in a block to have |
| 749 | // strict compatible types only. |
| 750 | QualType BlockQT = QualType(CurBlock->ReturnType, 0); |
| 751 | if (Context.getCanonicalType(BlockQT).getTypePtr() |
| 752 | != Context.getCanonicalType(RetValType).getTypePtr()) { |
| 753 | DiagnoseAssignmentResult(Incompatible, ReturnLoc, BlockQT, |
| 754 | RetValType, RetValExp, "returning"); |
| 755 | return true; |
| 756 | } |
| 757 | |
| 758 | if (RetValExp) CheckReturnStackAddr(RetValExp, BlockQT, ReturnLoc); |
| 759 | |
| 760 | return new ReturnStmt(ReturnLoc, (Expr*)RetValExp); |
| 761 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 762 | |
| 763 | Action::StmtResult |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 764 | Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 765 | Expr *RetValExp = static_cast<Expr *>(rex); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 766 | if (CurBlock) |
| 767 | return ActOnBlockReturnStmt(ReturnLoc, RetValExp); |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 768 | QualType FnRetType = |
| 769 | getCurFunctionDecl() ? getCurFunctionDecl()->getResultType() : |
| 770 | getCurMethodDecl()->getResultType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 771 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 772 | if (FnRetType->isVoidType()) { |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 773 | if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns) |
| 774 | if (FunctionDecl *FD = getCurFunctionDecl()) |
| 775 | Diag(ReturnLoc, diag::ext_return_has_expr) |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame^] | 776 | << FD->getIdentifier() << 0/*function*/<< RetValExp->getSourceRange(); |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 777 | else |
| 778 | Diag(ReturnLoc, diag::ext_return_has_expr) |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame^] | 779 | << getCurMethodDecl()->getDeclName() << 1 /*method*/ |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 780 | << RetValExp->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 781 | } |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 782 | return new ReturnStmt(ReturnLoc, RetValExp); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 783 | } |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 784 | |
| 785 | if (!RetValExp) { |
| 786 | unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4 |
| 787 | // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 788 | if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr; |
| 789 | |
| 790 | if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame^] | 791 | Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/; |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 792 | else |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame^] | 793 | Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/; |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 794 | return new ReturnStmt(ReturnLoc, (Expr*)0); |
| 795 | } |
| 796 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 797 | // we have a non-void function with an expression, continue checking |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 798 | QualType RetValType = RetValExp->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 799 | |
| 800 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 801 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 802 | // function return. |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 803 | |
| 804 | // In C++ the return statement is handled via a copy initialization. |
| 805 | // the C version of which boils down to |
| 806 | // CheckSingleAssignmentConstraints. |
| 807 | if (PerformCopyInitialization(RetValExp, FnRetType, "returning")) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 808 | return true; |
Ted Kremenek | 45925ab | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 809 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 810 | if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); |
Ted Kremenek | 45925ab | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 811 | |
Steve Naroff | c32a20d | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 812 | return new ReturnStmt(ReturnLoc, (Expr*)RetValExp); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Anders Carlsson | 076c111 | 2007-11-20 19:21:03 +0000 | [diff] [blame] | 815 | Sema::StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame^] | 816 | bool IsSimple, |
Anders Carlsson | 759f45d | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 817 | bool IsVolatile, |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 818 | unsigned NumOutputs, |
| 819 | unsigned NumInputs, |
| 820 | std::string *Names, |
Chris Lattner | b052a83 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 821 | ExprTy **constraints, |
| 822 | ExprTy **exprs, |
Chris Lattner | 8441802 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 823 | ExprTy *asmString, |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 824 | unsigned NumClobbers, |
Chris Lattner | b052a83 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 825 | ExprTy **clobbers, |
Chris Lattner | 8a40a83 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 826 | SourceLocation RParenLoc) { |
Chris Lattner | b052a83 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 827 | StringLiteral **Constraints = reinterpret_cast<StringLiteral**>(constraints); |
| 828 | Expr **Exprs = reinterpret_cast<Expr **>(exprs); |
Chris Lattner | 8441802 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 829 | StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString); |
Chris Lattner | b052a83 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 830 | StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers); |
| 831 | |
| 832 | // The parser verifies that there is a string literal here. |
Chris Lattner | 8441802 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 833 | if (AsmString->isWide()) |
| 834 | // FIXME: We currently leak memory here. |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 835 | return Diag(AsmString->getLocStart(), diag::err_asm_wide_character) |
| 836 | << AsmString->getSourceRange(); |
Chris Lattner | 8441802 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 837 | |
| 838 | |
Chris Lattner | b052a83 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 839 | for (unsigned i = 0; i != NumOutputs; i++) { |
| 840 | StringLiteral *Literal = Constraints[i]; |
Chris Lattner | 8441802 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 841 | if (Literal->isWide()) |
| 842 | // FIXME: We currently leak memory here. |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 843 | return Diag(Literal->getLocStart(), diag::err_asm_wide_character) |
| 844 | << Literal->getSourceRange(); |
Chris Lattner | 8441802 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 845 | |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 846 | std::string OutputConstraint(Literal->getStrData(), |
| 847 | Literal->getByteLength()); |
| 848 | |
| 849 | TargetInfo::ConstraintInfo info; |
Chris Lattner | 8441802 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 850 | if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info)) |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 851 | // FIXME: We currently leak memory here. |
Chris Lattner | 8441802 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 852 | return Diag(Literal->getLocStart(), |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 853 | diag::err_asm_invalid_output_constraint) << OutputConstraint; |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 854 | |
| 855 | // Check that the output exprs are valid lvalues. |
Chris Lattner | b052a83 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 856 | ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]); |
Chris Lattner | 25168a5 | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 857 | Expr::isLvalueResult Result = OutputExpr->isLvalue(Context); |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 858 | if (Result != Expr::LV_Valid) { |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 859 | // FIXME: We currently leak memory here. |
Chris Lattner | b052a83 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 860 | return Diag(OutputExpr->getSubExpr()->getLocStart(), |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 861 | diag::err_asm_invalid_lvalue_in_output) |
| 862 | << OutputExpr->getSubExpr()->getSourceRange(); |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 866 | for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { |
Chris Lattner | b052a83 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 867 | StringLiteral *Literal = Constraints[i]; |
Chris Lattner | 8441802 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 868 | if (Literal->isWide()) |
| 869 | // FIXME: We currently leak memory here. |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 870 | return Diag(Literal->getLocStart(), diag::err_asm_wide_character) |
| 871 | << Literal->getSourceRange(); |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 872 | |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 873 | std::string InputConstraint(Literal->getStrData(), |
| 874 | Literal->getByteLength()); |
| 875 | |
| 876 | TargetInfo::ConstraintInfo info; |
| 877 | if (!Context.Target.validateInputConstraint(InputConstraint.c_str(), |
Chris Lattner | b052a83 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 878 | NumOutputs, info)) { |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 879 | // FIXME: We currently leak memory here. |
Chris Lattner | 8441802 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 880 | return Diag(Literal->getLocStart(), |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 881 | diag::err_asm_invalid_input_constraint) << InputConstraint; |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | // Check that the input exprs aren't of type void. |
Chris Lattner | b052a83 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 885 | ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]); |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 886 | if (InputExpr->getType()->isVoidType()) { |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 887 | |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 888 | // FIXME: We currently leak memory here. |
Chris Lattner | b052a83 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 889 | return Diag(InputExpr->getSubExpr()->getLocStart(), |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 890 | diag::err_asm_invalid_type_in_input) |
| 891 | << InputExpr->getType().getAsString() << InputConstraint |
| 892 | << InputExpr->getSubExpr()->getSourceRange(); |
Anders Carlsson | b4487a8 | 2007-11-23 19:43:50 +0000 | [diff] [blame] | 893 | } |
| 894 | } |
Anders Carlsson | 965d520 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 895 | |
Anders Carlsson | 49dadd6 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 896 | // Check that the clobbers are valid. |
Chris Lattner | b052a83 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 897 | for (unsigned i = 0; i != NumClobbers; i++) { |
| 898 | StringLiteral *Literal = Clobbers[i]; |
Chris Lattner | 8441802 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 899 | if (Literal->isWide()) |
| 900 | // FIXME: We currently leak memory here. |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 901 | return Diag(Literal->getLocStart(), diag::err_asm_wide_character) |
| 902 | << Literal->getSourceRange(); |
Anders Carlsson | 49dadd6 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 903 | |
| 904 | llvm::SmallString<16> Clobber(Literal->getStrData(), |
| 905 | Literal->getStrData() + |
| 906 | Literal->getByteLength()); |
| 907 | |
Chris Lattner | 8441802 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 908 | if (!Context.Target.isValidGCCRegisterName(Clobber.c_str())) |
Anders Carlsson | 49dadd6 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 909 | // FIXME: We currently leak memory here. |
Chris Lattner | 8441802 | 2008-07-23 06:46:56 +0000 | [diff] [blame] | 910 | return Diag(Literal->getLocStart(), |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 911 | diag::err_asm_unknown_register_name) << Clobber.c_str(); |
Anders Carlsson | 49dadd6 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Chris Lattner | b052a83 | 2008-08-18 19:55:17 +0000 | [diff] [blame] | 914 | return new AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs, |
| 915 | Names, Constraints, Exprs, AsmString, NumClobbers, |
| 916 | Clobbers, RParenLoc); |
Chris Lattner | 8a40a83 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 917 | } |
Fariborz Jahanian | 0679836 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 918 | |
| 919 | Action::StmtResult |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 920 | Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, |
Fariborz Jahanian | 0679836 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 921 | SourceLocation RParen, StmtTy *Parm, |
| 922 | StmtTy *Body, StmtTy *CatchList) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 923 | ObjCAtCatchStmt *CS = new ObjCAtCatchStmt(AtLoc, RParen, |
Fariborz Jahanian | 0679836 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 924 | static_cast<Stmt*>(Parm), static_cast<Stmt*>(Body), |
| 925 | static_cast<Stmt*>(CatchList)); |
| 926 | return CatchList ? CatchList : CS; |
| 927 | } |
| 928 | |
Fariborz Jahanian | de3abf8 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 929 | Action::StmtResult |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 930 | Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtTy *Body) { |
| 931 | ObjCAtFinallyStmt *FS = new ObjCAtFinallyStmt(AtLoc, |
Fariborz Jahanian | de3abf8 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 932 | static_cast<Stmt*>(Body)); |
| 933 | return FS; |
| 934 | } |
Fariborz Jahanian | b8bf607 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 935 | |
| 936 | Action::StmtResult |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 937 | Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc, |
Fariborz Jahanian | b8bf607 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 938 | StmtTy *Try, StmtTy *Catch, StmtTy *Finally) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 939 | ObjCAtTryStmt *TS = new ObjCAtTryStmt(AtLoc, static_cast<Stmt*>(Try), |
Fariborz Jahanian | b8bf607 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 940 | static_cast<Stmt*>(Catch), |
| 941 | static_cast<Stmt*>(Finally)); |
| 942 | return TS; |
| 943 | } |
| 944 | |
Fariborz Jahanian | 08df2c6 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 945 | Action::StmtResult |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 946 | Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, StmtTy *Throw) { |
| 947 | ObjCAtThrowStmt *TS = new ObjCAtThrowStmt(AtLoc, static_cast<Stmt*>(Throw)); |
Fariborz Jahanian | 08df2c6 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 948 | return TS; |
| 949 | } |
Fariborz Jahanian | b8bf607 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 950 | |
Fariborz Jahanian | c9fd4d1 | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 951 | Action::StmtResult |
| 952 | Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprTy *SynchExpr, |
| 953 | StmtTy *SynchBody) { |
| 954 | ObjCAtSynchronizedStmt *SS = new ObjCAtSynchronizedStmt(AtLoc, |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 955 | static_cast<Stmt*>(SynchExpr), static_cast<Stmt*>(SynchBody)); |
Fariborz Jahanian | c9fd4d1 | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 956 | return SS; |
| 957 | } |