blob: 5065bb9bc6de609c1d9606397ba33873796855f0 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for statements.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Chris Lattnerf4021e72007-08-23 05:46:52 +000015#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/Expr.h"
Chris Lattnerf4021e72007-08-23 05:46:52 +000017#include "clang/AST/Stmt.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Parse/Scope.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000019#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/LangOptions.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000022#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Steve Naroff1b273c42007-09-16 14:56:35 +000025Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) {
Reid Spencer5f016e22007-07-11 17:01:13 +000026 Expr *E = static_cast<Expr*>(expr);
Steve Naroff1b273c42007-09-16 14:56:35 +000027 assert(E && "ActOnExprStmt(): missing expression");
Chris Lattner834a72a2008-07-25 23:18:17 +000028
29 // C99 6.8.3p2: The expression in an expression statement is evaluated as a
30 // void expression for its side effects. Conversion to void allows any
31 // operand, even incomplete types.
32
33 // Same thing in for stmt first clause (when expr) and third clause.
Reid Spencer5f016e22007-07-11 17:01:13 +000034 return E;
35}
36
37
Steve Naroff1b273c42007-09-16 14:56:35 +000038Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000039 return new NullStmt(SemiLoc);
40}
41
Chris Lattner81c018d2008-03-13 06:29:04 +000042Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl, SourceLocation StartLoc,
43 SourceLocation EndLoc) {
44 if (decl == 0)
45 return true;
46
47 ScopedDecl *SD = cast<ScopedDecl>(static_cast<Decl *>(decl));
48 return new DeclStmt(SD, StartLoc, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000049}
50
51Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +000052Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattner98414c12007-08-31 21:49:55 +000053 StmtTy **elts, unsigned NumElts, bool isStmtExpr) {
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000054 Stmt **Elts = reinterpret_cast<Stmt**>(elts);
55 // If we're in C89 mode, check that we don't have any decls after stmts. If
56 // so, emit an extension diagnostic.
57 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
58 // Note that __extension__ can be around a decl.
59 unsigned i = 0;
60 // Skip over all declarations.
61 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
62 /*empty*/;
63
64 // We found the end of the list or a statement. Scan for another declstmt.
65 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
66 /*empty*/;
67
68 if (i != NumElts) {
Steve Naroff8e74c932007-09-13 21:41:19 +000069 ScopedDecl *D = cast<DeclStmt>(Elts[i])->getDecl();
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000070 Diag(D->getLocation(), diag::ext_mixed_decls_code);
71 }
72 }
Chris Lattner98414c12007-08-31 21:49:55 +000073 // Warn about unused expressions in statements.
74 for (unsigned i = 0; i != NumElts; ++i) {
75 Expr *E = dyn_cast<Expr>(Elts[i]);
76 if (!E) continue;
77
78 // Warn about expressions with unused results.
79 if (E->hasLocalSideEffect() || E->getType()->isVoidType())
80 continue;
81
82 // The last expr in a stmt expr really is used.
83 if (isStmtExpr && i == NumElts-1)
84 continue;
85
86 /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in
87 /// a context where the result is unused. Emit a diagnostic to warn about
88 /// this.
89 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
90 Diag(BO->getOperatorLoc(), diag::warn_unused_expr,
91 BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange());
92 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
93 Diag(UO->getOperatorLoc(), diag::warn_unused_expr,
94 UO->getSubExpr()->getSourceRange());
95 else
96 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
97 }
98
Steve Naroffb5a69582007-08-31 23:28:33 +000099 return new CompoundStmt(Elts, NumElts, L, R);
Reid Spencer5f016e22007-07-11 17:01:13 +0000100}
101
102Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000103Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner6c36be52007-07-18 02:28:47 +0000104 SourceLocation DotDotDotLoc, ExprTy *rhsval,
Chris Lattner0fa152e2007-07-21 03:00:26 +0000105 SourceLocation ColonLoc, StmtTy *subStmt) {
106 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner8a87e572007-07-23 17:05:23 +0000107 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 assert((LHSVal != 0) && "missing expression in case statement");
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 SourceLocation ExpLoc;
111 // C99 6.8.4.2p3: The expression shall be an integer constant.
Chris Lattner0fa152e2007-07-21 03:00:26 +0000112 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
113 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
114 LHSVal->getSourceRange());
115 return SubStmt;
116 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000117
Chris Lattner6c36be52007-07-18 02:28:47 +0000118 // GCC extension: The expression shall be an integer constant.
Chris Lattner0fa152e2007-07-21 03:00:26 +0000119 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
120 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
121 RHSVal->getSourceRange());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000122 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner6c36be52007-07-18 02:28:47 +0000123 }
Chris Lattner8a87e572007-07-23 17:05:23 +0000124
125 if (SwitchStack.empty()) {
126 Diag(CaseLoc, diag::err_case_not_in_switch);
127 return SubStmt;
128 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000129
Steve Naroffb5a69582007-08-31 23:28:33 +0000130 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner8a87e572007-07-23 17:05:23 +0000131 SwitchStack.back()->addSwitchCase(CS);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000132 return CS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000133}
134
135Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000136Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner0fa152e2007-07-21 03:00:26 +0000137 StmtTy *subStmt, Scope *CurScope) {
138 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner6c36be52007-07-18 02:28:47 +0000139
Chris Lattner8a87e572007-07-23 17:05:23 +0000140 if (SwitchStack.empty()) {
Chris Lattner0fa152e2007-07-21 03:00:26 +0000141 Diag(DefaultLoc, diag::err_default_not_in_switch);
142 return SubStmt;
143 }
144
Chris Lattner0fa152e2007-07-21 03:00:26 +0000145 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner8a87e572007-07-23 17:05:23 +0000146 SwitchStack.back()->addSwitchCase(DS);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000147
Chris Lattner6c36be52007-07-18 02:28:47 +0000148 return DS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000149}
150
151Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000152Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner0fa152e2007-07-21 03:00:26 +0000153 SourceLocation ColonLoc, StmtTy *subStmt) {
154 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 // Look up the record for this label identifier.
156 LabelStmt *&LabelDecl = LabelMap[II];
157
158 // If not forward referenced or defined already, just create a new LabelStmt.
159 if (LabelDecl == 0)
Chris Lattner0fa152e2007-07-21 03:00:26 +0000160 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161
162 assert(LabelDecl->getID() == II && "Label mismatch!");
163
164 // Otherwise, this label was either forward reference or multiply defined. If
165 // multiply defined, reject it now.
166 if (LabelDecl->getSubStmt()) {
167 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
168 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000169 return SubStmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 }
171
172 // Otherwise, this label was forward declared, and we just found its real
173 // definition. Fill in the forward definition and return it.
174 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000175 LabelDecl->setSubStmt(SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 return LabelDecl;
177}
178
179Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000180Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 StmtTy *ThenVal, SourceLocation ElseLoc,
182 StmtTy *ElseVal) {
183 Expr *condExpr = (Expr *)CondVal;
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000184 Stmt *thenStmt = (Stmt *)ThenVal;
185
Steve Naroff1b273c42007-09-16 14:56:35 +0000186 assert(condExpr && "ActOnIfStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000187
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000188 DefaultFunctionArrayConversion(condExpr);
189 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000190
191 if (!condType->isScalarType()) // C99 6.8.4.1p1
192 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
193 condType.getAsString(), condExpr->getSourceRange());
194
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000195 // 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 Spencer5f016e22007-07-11 17:01:13 +0000205}
206
207Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000208Sema::ActOnStartOfSwitchStmt(ExprTy *cond) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000209 Expr *Cond = static_cast<Expr*>(cond);
210
211 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
212 UsualUnaryConversions(Cond);
213
214 SwitchStmt *SS = new SwitchStmt(Cond);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000215 SwitchStack.push_back(SS);
216 return SS;
217}
Chris Lattner6c36be52007-07-18 02:28:47 +0000218
Chris Lattnerf4021e72007-08-23 05:46:52 +0000219/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
220/// the specified width and sign. If an overflow occurs, detect it and emit
221/// the specified diagnostic.
222void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
223 unsigned NewWidth, bool NewSign,
224 SourceLocation Loc,
225 unsigned DiagID) {
226 // Perform a conversion to the promoted condition type if needed.
227 if (NewWidth > Val.getBitWidth()) {
228 // If this is an extension, just do it.
229 llvm::APSInt OldVal(Val);
230 Val.extend(NewWidth);
231
232 // If the input was signed and negative and the output is unsigned,
233 // warn.
234 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
235 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
236
237 Val.setIsSigned(NewSign);
238 } else if (NewWidth < Val.getBitWidth()) {
239 // If this is a truncation, check for overflow.
240 llvm::APSInt ConvVal(Val);
241 ConvVal.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000242 ConvVal.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000243 ConvVal.extend(Val.getBitWidth());
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000244 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000245 if (ConvVal != Val)
246 Diag(Loc, DiagID, Val.toString(), ConvVal.toString());
247
248 // Regardless of whether a diagnostic was emitted, really do the
249 // truncation.
250 Val.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000251 Val.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000252 } else if (NewSign != Val.isSigned()) {
253 // Convert the sign to match the sign of the condition. This can cause
254 // overflow as well: unsigned(INTMIN)
255 llvm::APSInt OldVal(Val);
256 Val.setIsSigned(NewSign);
257
258 if (Val.isNegative()) // Sign bit changes meaning.
259 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
260 }
261}
262
Chris Lattner0471f5b2007-08-23 18:29:20 +0000263namespace {
264 struct CaseCompareFunctor {
265 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
266 const llvm::APSInt &RHS) {
267 return LHS.first < RHS;
268 }
Chris Lattner0e85a272007-09-03 18:31:57 +0000269 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
270 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
271 return LHS.first < RHS.first;
272 }
Chris Lattner0471f5b2007-08-23 18:29:20 +0000273 bool operator()(const llvm::APSInt &LHS,
274 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
275 return LHS < RHS.first;
276 }
277 };
278}
279
Chris Lattner764a7ce2007-09-21 18:15:22 +0000280/// CmpCaseVals - Comparison predicate for sorting case values.
281///
282static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
283 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
284 if (lhs.first < rhs.first)
285 return true;
286
287 if (lhs.first == rhs.first &&
288 lhs.second->getCaseLoc().getRawEncoding()
289 < rhs.second->getCaseLoc().getRawEncoding())
290 return true;
291 return false;
292}
293
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000294Action::StmtResult
Chris Lattner764a7ce2007-09-21 18:15:22 +0000295Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
296 ExprTy *Body) {
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000297 Stmt *BodyStmt = (Stmt*)Body;
298
299 SwitchStmt *SS = SwitchStack.back();
300 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
301
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000302 SS->setBody(BodyStmt, SwitchLoc);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000303 SwitchStack.pop_back();
304
Chris Lattnerf4021e72007-08-23 05:46:52 +0000305 Expr *CondExpr = SS->getCond();
306 QualType CondType = CondExpr->getType();
Chris Lattner6c36be52007-07-18 02:28:47 +0000307
Chris Lattnerf4021e72007-08-23 05:46:52 +0000308 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000309 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattnerf4021e72007-08-23 05:46:52 +0000310 CondType.getAsString(), CondExpr->getSourceRange());
311 return true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000312 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000313
314 // Get the bitwidth of the switched-on value before promotions. We must
315 // convert the integer case values to this width before comparison.
Chris Lattner98be4942008-03-05 18:54:05 +0000316 unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000317 bool CondIsSigned = CondType->isSignedIntegerType();
318
319 // Accumulate all of the case values in a vector so that we can sort them
320 // and detect duplicates. This vector contains the APInt for the case after
321 // it has been converted to the condition type.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000322 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
323 CaseValsTy CaseVals;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000324
325 // Keep track of any GNU case ranges we see. The APSInt is the low value.
326 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
327
328 DefaultStmt *TheDefaultStmt = 0;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000329
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000330 bool CaseListIsErroneous = false;
331
332 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000333 SC = SC->getNextSwitchCase()) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000334
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000335 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000336 if (TheDefaultStmt) {
337 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
338 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000339
Chris Lattnerf4021e72007-08-23 05:46:52 +0000340 // FIXME: Remove the default statement from the switch block so that
341 // we'll return a valid AST. This requires recursing down the
342 // AST and finding it, not something we are set up to do right now. For
343 // now, just lop the entire switch stmt out of the AST.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000344 CaseListIsErroneous = true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000345 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000346 TheDefaultStmt = DS;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000347
Chris Lattnerf4021e72007-08-23 05:46:52 +0000348 } else {
349 CaseStmt *CS = cast<CaseStmt>(SC);
350
351 // We already verified that the expression has a i-c-e value (C99
352 // 6.8.4.2p3) - get that value now.
353 llvm::APSInt LoVal(32);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000354 Expr *Lo = CS->getLHS();
355 Lo->isIntegerConstantExpr(LoVal, Context);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000356
357 // Convert the value to the same width/sign as the condition.
358 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
359 CS->getLHS()->getLocStart(),
360 diag::warn_case_value_overflow);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000361
Chris Lattner1e0a3902008-01-16 19:17:22 +0000362 // If the LHS is not the same type as the condition, insert an implicit
363 // cast.
364 ImpCastExprToType(Lo, CondType);
365 CS->setLHS(Lo);
366
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000367 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerf4021e72007-08-23 05:46:52 +0000368 if (CS->getRHS())
369 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000370 else
371 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000372 }
373 }
374
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000375 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner764a7ce2007-09-21 18:15:22 +0000376 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000377
Chris Lattnerf3348502007-08-23 14:29:07 +0000378 if (!CaseVals.empty()) {
379 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
380 if (CaseVals[i].first == CaseVals[i+1].first) {
381 // If we have a duplicate, report it.
382 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
383 diag::err_duplicate_case, CaseVals[i].first.toString());
384 Diag(CaseVals[i].second->getLHS()->getLocStart(),
385 diag::err_duplicate_case_prev);
386 // FIXME: We really want to remove the bogus case stmt from the substmt,
387 // but we have no way to do this right now.
388 CaseListIsErroneous = true;
389 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000390 }
391 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000392
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000393 // Detect duplicate case ranges, which usually don't exist at all in the first
394 // place.
395 if (!CaseRanges.empty()) {
396 // Sort all the case ranges by their low value so we can easily detect
397 // overlaps between ranges.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000398 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000399
400 // Scan the ranges, computing the high values and removing empty ranges.
401 std::vector<llvm::APSInt> HiVals;
Chris Lattner6efc4d32007-08-23 17:48:14 +0000402 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000403 CaseStmt *CR = CaseRanges[i].second;
404 llvm::APSInt HiVal(32);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000405 Expr *Hi = CR->getRHS();
406 Hi->isIntegerConstantExpr(HiVal, Context);
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000407
408 // Convert the value to the same width/sign as the condition.
409 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
410 CR->getRHS()->getLocStart(),
411 diag::warn_case_value_overflow);
412
Chris Lattner1e0a3902008-01-16 19:17:22 +0000413 // If the LHS is not the same type as the condition, insert an implicit
414 // cast.
415 ImpCastExprToType(Hi, CondType);
416 CR->setRHS(Hi);
417
Chris Lattner6efc4d32007-08-23 17:48:14 +0000418 // If the low value is bigger than the high value, the case is empty.
419 if (CaseRanges[i].first > HiVal) {
420 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
421 SourceRange(CR->getLHS()->getLocStart(),
422 CR->getRHS()->getLocEnd()));
423 CaseRanges.erase(CaseRanges.begin()+i);
424 --i, --e;
425 continue;
426 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000427 HiVals.push_back(HiVal);
428 }
429
430 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0471f5b2007-08-23 18:29:20 +0000431 // ranges. Since the range list is sorted, we only need to compare case
432 // ranges with their neighbors.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000433 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0471f5b2007-08-23 18:29:20 +0000434 llvm::APSInt &CRLo = CaseRanges[i].first;
435 llvm::APSInt &CRHi = HiVals[i];
436 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000437
Chris Lattner0471f5b2007-08-23 18:29:20 +0000438 // Check to see whether the case range overlaps with any singleton cases.
439 CaseStmt *OverlapStmt = 0;
440 llvm::APSInt OverlapVal(32);
441
442 // Find the smallest value >= the lower bound. If I is in the case range,
443 // then we have overlap.
444 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
445 CaseVals.end(), CRLo,
446 CaseCompareFunctor());
447 if (I != CaseVals.end() && I->first < CRHi) {
448 OverlapVal = I->first; // Found overlap with scalar.
449 OverlapStmt = I->second;
450 }
451
452 // Find the smallest value bigger than the upper bound.
453 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
454 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
455 OverlapVal = (I-1)->first; // Found overlap with scalar.
456 OverlapStmt = (I-1)->second;
457 }
458
459 // Check to see if this case stmt overlaps with the subsequent case range.
460 if (i && CRLo <= HiVals[i-1]) {
461 OverlapVal = HiVals[i-1]; // Found overlap with range.
462 OverlapStmt = CaseRanges[i-1].second;
463 }
464
465 if (OverlapStmt) {
466 // If we have a duplicate, report it.
467 Diag(CR->getLHS()->getLocStart(),
468 diag::err_duplicate_case, OverlapVal.toString());
469 Diag(OverlapStmt->getLHS()->getLocStart(),
470 diag::err_duplicate_case_prev);
471 // FIXME: We really want to remove the bogus case stmt from the substmt,
472 // but we have no way to do this right now.
473 CaseListIsErroneous = true;
474 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000475 }
476 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000477
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000478 // FIXME: If the case list was broken is some way, we don't have a good system
479 // to patch it up. Instead, just return the whole substmt as broken.
480 if (CaseListIsErroneous)
481 return true;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000482
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000483 return SS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000484}
485
486Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000487Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000488 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000489 assert(condExpr && "ActOnWhileStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000490
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000491 DefaultFunctionArrayConversion(condExpr);
492 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000493
494 if (!condType->isScalarType()) // C99 6.8.5p2
495 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
496 condType.getAsString(), condExpr->getSourceRange());
497
Steve Naroffb5a69582007-08-31 23:28:33 +0000498 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000499}
500
501Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000502Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 SourceLocation WhileLoc, ExprTy *Cond) {
504 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000505 assert(condExpr && "ActOnDoStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000506
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000507 DefaultFunctionArrayConversion(condExpr);
508 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000509
510 if (!condType->isScalarType()) // C99 6.8.5p2
511 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
512 condType.getAsString(), condExpr->getSourceRange());
513
Steve Naroffb5a69582007-08-31 23:28:33 +0000514 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000515}
516
517Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000518Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000519 StmtTy *first, ExprTy *second, ExprTy *third,
520 SourceLocation RParenLoc, StmtTy *body) {
521 Stmt *First = static_cast<Stmt*>(first);
522 Expr *Second = static_cast<Expr*>(second);
523 Expr *Third = static_cast<Expr*>(third);
524 Stmt *Body = static_cast<Stmt*>(body);
525
Chris Lattnerae3b7012007-08-28 05:03:08 +0000526 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
527 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
528 // identifiers for objects having storage class 'auto' or 'register'.
Ted Kremenek909cd262008-08-08 02:45:18 +0000529 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
530 DI!=DE; ++DI) {
531 VarDecl *VD = dyn_cast<VarDecl>(*DI);
Steve Naroff248a7532008-04-15 22:42:06 +0000532 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
533 VD = 0;
534 if (VD == 0)
Ted Kremenek909cd262008-08-08 02:45:18 +0000535 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
Chris Lattnerae3b7012007-08-28 05:03:08 +0000536 // FIXME: mark decl erroneous!
537 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 }
539 if (Second) {
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000540 DefaultFunctionArrayConversion(Second);
541 QualType SecondType = Second->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000542
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000543 if (!SecondType->isScalarType()) // C99 6.8.5p2
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner834a72a2008-07-25 23:18:17 +0000545 SecondType.getAsString(), Second->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 }
Steve Naroffb5a69582007-08-31 23:28:33 +0000547 return new ForStmt(First, Second, Third, Body, ForLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000548}
549
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000550Action::StmtResult
Fariborz Jahanian75712282008-01-10 00:24:29 +0000551Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000552 SourceLocation LParenLoc,
553 StmtTy *first, ExprTy *second,
554 SourceLocation RParenLoc, StmtTy *body) {
555 Stmt *First = static_cast<Stmt*>(first);
556 Expr *Second = static_cast<Expr*>(second);
557 Stmt *Body = static_cast<Stmt*>(body);
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000558 if (First) {
559 QualType FirstType;
560 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
561 FirstType = cast<ValueDecl>(DS->getDecl())->getType();
562 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
563 // identifiers for objects having storage class 'auto' or 'register'.
564 ScopedDecl *D = DS->getDecl();
Steve Naroff248a7532008-04-15 22:42:06 +0000565 VarDecl *VD = cast<VarDecl>(D);
566 if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
567 return Diag(VD->getLocation(), diag::err_non_variable_decl_in_for);
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000568 if (D->getNextDeclarator())
569 return Diag(D->getLocation(), diag::err_toomany_element_decls);
Steve Naroff248a7532008-04-15 22:42:06 +0000570 } else
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000571 FirstType = static_cast<Expr*>(first)->getType();
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000572 if (!Context.isObjCObjectPointerType(FirstType))
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000573 Diag(ForLoc, diag::err_selector_element_type,
574 FirstType.getAsString(), First->getSourceRange());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000575 }
576 if (Second) {
577 DefaultFunctionArrayConversion(Second);
578 QualType SecondType = Second->getType();
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000579 if (!Context.isObjCObjectPointerType(SecondType))
Fariborz Jahanian75712282008-01-10 00:24:29 +0000580 Diag(ForLoc, diag::err_collection_expr_type,
Fariborz Jahanian1f990d62008-01-04 00:27:46 +0000581 SecondType.getAsString(), Second->getSourceRange());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000582 }
Fariborz Jahanian75712282008-01-10 00:24:29 +0000583 return new ObjCForCollectionStmt(First, Second, Body, ForLoc, RParenLoc);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000584}
Reid Spencer5f016e22007-07-11 17:01:13 +0000585
586Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000587Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 IdentifierInfo *LabelII) {
589 // Look up the record for this label identifier.
590 LabelStmt *&LabelDecl = LabelMap[LabelII];
591
592 // If we haven't seen this label yet, create a forward reference.
593 if (LabelDecl == 0)
594 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
595
Ted Kremenek61f62162007-09-06 17:11:52 +0000596 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000597}
598
599Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000600Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 ExprTy *DestExp) {
602 // FIXME: Verify that the operand is convertible to void*.
603
604 return new IndirectGotoStmt((Expr*)DestExp);
605}
606
607Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000608Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 Scope *S = CurScope->getContinueParent();
610 if (!S) {
611 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
612 Diag(ContinueLoc, diag::err_continue_not_in_loop);
613 return true;
614 }
615
Steve Naroff507f2d52007-08-31 23:49:30 +0000616 return new ContinueStmt(ContinueLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000617}
618
619Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000620Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 Scope *S = CurScope->getBreakParent();
622 if (!S) {
623 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
624 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
625 return true;
626 }
627
Steve Naroff507f2d52007-08-31 23:49:30 +0000628 return new BreakStmt(BreakLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000629}
630
631
632Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000633Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
Steve Naroff90045e82007-07-13 23:32:42 +0000634 Expr *RetValExp = static_cast<Expr *>(rex);
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000635 QualType FnRetType =
636 getCurFunctionDecl() ? getCurFunctionDecl()->getResultType() :
637 getCurMethodDecl()->getResultType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000638
Chris Lattner5cf216b2008-01-04 18:04:52 +0000639 if (FnRetType->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
Fariborz Jahanianb107ce82007-12-04 19:20:11 +0000641 Diag(ReturnLoc, diag::ext_return_has_expr,
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000642 ( getCurFunctionDecl() ?
643 getCurFunctionDecl()->getIdentifier()->getName() :
644 getCurMethodDecl()->getSelector().getName() ),
Steve Naroff90045e82007-07-13 23:32:42 +0000645 RetValExp->getSourceRange());
Steve Naroff507f2d52007-08-31 23:49:30 +0000646 return new ReturnStmt(ReturnLoc, RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 } else {
648 if (!RetValExp) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000649 const char *funcName =
650 getCurFunctionDecl() ?
651 getCurFunctionDecl()->getIdentifier()->getName() :
652 getCurMethodDecl()->getSelector().getName().c_str();
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
654 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
655 else // C90 6.6.6.4p4
656 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
Steve Naroff507f2d52007-08-31 23:49:30 +0000657 return new ReturnStmt(ReturnLoc, (Expr*)0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 }
659 }
660 // we have a non-void function with an expression, continue checking
Chris Lattner5cf216b2008-01-04 18:04:52 +0000661 QualType RetValType = RetValExp->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000662
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
664 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
665 // function return.
Chris Lattner5cf216b2008-01-04 18:04:52 +0000666 AssignConvertType ConvTy = CheckSingleAssignmentConstraints(FnRetType,
667 RetValExp);
668 if (DiagnoseAssignmentResult(ConvTy, ReturnLoc, FnRetType,
669 RetValType, RetValExp, "returning"))
670 return true;
Ted Kremenek06de2762007-08-17 16:46:58 +0000671
Chris Lattner5cf216b2008-01-04 18:04:52 +0000672 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
Ted Kremenek06de2762007-08-17 16:46:58 +0000673
Steve Naroff507f2d52007-08-31 23:49:30 +0000674 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000675}
676
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000677Sema::StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
Anders Carlssondfab34a2008-02-05 23:03:50 +0000678 bool IsSimple,
Anders Carlsson39c47b52007-11-23 23:12:25 +0000679 bool IsVolatile,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000680 unsigned NumOutputs,
681 unsigned NumInputs,
682 std::string *Names,
683 ExprTy **Constraints,
684 ExprTy **Exprs,
Chris Lattner6bc52112008-07-23 06:46:56 +0000685 ExprTy *asmString,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000686 unsigned NumClobbers,
687 ExprTy **Clobbers,
Chris Lattnerfe795952007-10-29 04:04:16 +0000688 SourceLocation RParenLoc) {
Chris Lattner6bc52112008-07-23 06:46:56 +0000689 // The parser verifies that there is a string literal here.
690 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString);
691 if (AsmString->isWide())
692 // FIXME: We currently leak memory here.
693 return Diag(AsmString->getLocStart(), diag::err_asm_wide_character,
694 AsmString->getSourceRange());
695
696
Anders Carlsson04728b72007-11-23 19:43:50 +0000697 for (unsigned i = 0; i < NumOutputs; i++) {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000698 StringLiteral *Literal = cast<StringLiteral>((Expr *)Constraints[i]);
Chris Lattner6bc52112008-07-23 06:46:56 +0000699 if (Literal->isWide())
700 // FIXME: We currently leak memory here.
701 return Diag(Literal->getLocStart(), diag::err_asm_wide_character,
702 Literal->getSourceRange());
703
Anders Carlssond04c6e22007-11-27 04:11:28 +0000704 std::string OutputConstraint(Literal->getStrData(),
705 Literal->getByteLength());
706
707 TargetInfo::ConstraintInfo info;
Chris Lattner6bc52112008-07-23 06:46:56 +0000708 if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info))
Anders Carlssond04c6e22007-11-27 04:11:28 +0000709 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000710 return Diag(Literal->getLocStart(),
711 diag::err_invalid_output_constraint_in_asm);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000712
713 // Check that the output exprs are valid lvalues.
Anders Carlsson04728b72007-11-23 19:43:50 +0000714 Expr *OutputExpr = (Expr *)Exprs[i];
Chris Lattner28be73f2008-07-26 21:30:36 +0000715 Expr::isLvalueResult Result = OutputExpr->isLvalue(Context);
Anders Carlsson04728b72007-11-23 19:43:50 +0000716 if (Result != Expr::LV_Valid) {
717 ParenExpr *PE = cast<ParenExpr>(OutputExpr);
718
Anders Carlsson04728b72007-11-23 19:43:50 +0000719 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000720 return Diag(PE->getSubExpr()->getLocStart(),
721 diag::err_invalid_lvalue_in_asm_output,
722 PE->getSubExpr()->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000723 }
724 }
725
Anders Carlsson04728b72007-11-23 19:43:50 +0000726 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000727 StringLiteral *Literal = cast<StringLiteral>((Expr *)Constraints[i]);
Chris Lattner6bc52112008-07-23 06:46:56 +0000728 if (Literal->isWide())
729 // FIXME: We currently leak memory here.
730 return Diag(Literal->getLocStart(), diag::err_asm_wide_character,
731 Literal->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000732
Anders Carlssond04c6e22007-11-27 04:11:28 +0000733 std::string InputConstraint(Literal->getStrData(),
734 Literal->getByteLength());
735
736 TargetInfo::ConstraintInfo info;
737 if (!Context.Target.validateInputConstraint(InputConstraint.c_str(),
738 NumOutputs,
739 info)) {
740 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000741 return Diag(Literal->getLocStart(),
742 diag::err_invalid_input_constraint_in_asm);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000743 }
744
745 // Check that the input exprs aren't of type void.
746 Expr *InputExpr = (Expr *)Exprs[i];
Anders Carlsson04728b72007-11-23 19:43:50 +0000747 if (InputExpr->getType()->isVoidType()) {
748 ParenExpr *PE = cast<ParenExpr>(InputExpr);
749
Anders Carlsson04728b72007-11-23 19:43:50 +0000750 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000751 return Diag(PE->getSubExpr()->getLocStart(),
752 diag::err_invalid_type_in_asm_input,
753 PE->getType().getAsString(),
754 PE->getSubExpr()->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000755 }
756 }
Anders Carlssonb235fc22007-11-22 01:36:19 +0000757
Anders Carlsson6fa90862007-11-25 00:25:21 +0000758 // Check that the clobbers are valid.
759 for (unsigned i = 0; i < NumClobbers; i++) {
760 StringLiteral *Literal = cast<StringLiteral>((Expr *)Clobbers[i]);
Chris Lattner6bc52112008-07-23 06:46:56 +0000761 if (Literal->isWide())
762 // FIXME: We currently leak memory here.
763 return Diag(Literal->getLocStart(), diag::err_asm_wide_character,
764 Literal->getSourceRange());
Anders Carlsson6fa90862007-11-25 00:25:21 +0000765
766 llvm::SmallString<16> Clobber(Literal->getStrData(),
767 Literal->getStrData() +
768 Literal->getByteLength());
769
Chris Lattner6bc52112008-07-23 06:46:56 +0000770 if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000771 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000772 return Diag(Literal->getLocStart(),
773 diag::err_unknown_register_name_in_asm, Clobber.c_str());
Anders Carlsson6fa90862007-11-25 00:25:21 +0000774 }
775
Anders Carlssonb235fc22007-11-22 01:36:19 +0000776 return new AsmStmt(AsmLoc,
Anders Carlssondfab34a2008-02-05 23:03:50 +0000777 IsSimple,
Anders Carlsson39c47b52007-11-23 23:12:25 +0000778 IsVolatile,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000779 NumOutputs,
780 NumInputs,
781 Names,
782 reinterpret_cast<StringLiteral**>(Constraints),
783 reinterpret_cast<Expr**>(Exprs),
Chris Lattner6bc52112008-07-23 06:46:56 +0000784 AsmString, NumClobbers,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000785 reinterpret_cast<StringLiteral**>(Clobbers),
786 RParenLoc);
Chris Lattnerfe795952007-10-29 04:04:16 +0000787}
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000788
789Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000790Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000791 SourceLocation RParen, StmtTy *Parm,
792 StmtTy *Body, StmtTy *CatchList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000793 ObjCAtCatchStmt *CS = new ObjCAtCatchStmt(AtLoc, RParen,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000794 static_cast<Stmt*>(Parm), static_cast<Stmt*>(Body),
795 static_cast<Stmt*>(CatchList));
796 return CatchList ? CatchList : CS;
797}
798
Fariborz Jahanian161a9c52007-11-02 00:18:53 +0000799Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000800Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtTy *Body) {
801 ObjCAtFinallyStmt *FS = new ObjCAtFinallyStmt(AtLoc,
Fariborz Jahanian161a9c52007-11-02 00:18:53 +0000802 static_cast<Stmt*>(Body));
803 return FS;
804}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000805
806Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000807Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000808 StmtTy *Try, StmtTy *Catch, StmtTy *Finally) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000809 ObjCAtTryStmt *TS = new ObjCAtTryStmt(AtLoc, static_cast<Stmt*>(Try),
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000810 static_cast<Stmt*>(Catch),
811 static_cast<Stmt*>(Finally));
812 return TS;
813}
814
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000815Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000816Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, StmtTy *Throw) {
817 ObjCAtThrowStmt *TS = new ObjCAtThrowStmt(AtLoc, static_cast<Stmt*>(Throw));
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000818 return TS;
819}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000820
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000821Action::StmtResult
822Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprTy *SynchExpr,
823 StmtTy *SynchBody) {
824 ObjCAtSynchronizedStmt *SS = new ObjCAtSynchronizedStmt(AtLoc,
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000825 static_cast<Stmt*>(SynchExpr), static_cast<Stmt*>(SynchBody));
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000826 return SS;
827}