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