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