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