blob: abc7f8865aaf12da9214ca161bc7450996d8e8c6 [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"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000016#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/AST/Expr.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000018#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Basic/Diagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Steve Naroff1b273c42007-09-16 14:56:35 +000022Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) {
Reid Spencer5f016e22007-07-11 17:01:13 +000023 Expr *E = static_cast<Expr*>(expr);
Steve Naroff1b273c42007-09-16 14:56:35 +000024 assert(E && "ActOnExprStmt(): missing expression");
Chris Lattner834a72a2008-07-25 23:18:17 +000025
26 // C99 6.8.3p2: The expression in an expression statement is evaluated as a
27 // void expression for its side effects. Conversion to void allows any
28 // operand, even incomplete types.
29
30 // Same thing in for stmt first clause (when expr) and third clause.
Reid Spencer5f016e22007-07-11 17:01:13 +000031 return E;
32}
33
34
Steve Naroff1b273c42007-09-16 14:56:35 +000035Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000036 return new NullStmt(SemiLoc);
37}
38
Chris Lattner81c018d2008-03-13 06:29:04 +000039Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl, SourceLocation StartLoc,
40 SourceLocation EndLoc) {
41 if (decl == 0)
42 return true;
43
44 ScopedDecl *SD = cast<ScopedDecl>(static_cast<Decl *>(decl));
45 return new DeclStmt(SD, StartLoc, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000046}
47
48Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +000049Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattner98414c12007-08-31 21:49:55 +000050 StmtTy **elts, unsigned NumElts, bool isStmtExpr) {
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000051 Stmt **Elts = reinterpret_cast<Stmt**>(elts);
52 // If we're in C89 mode, check that we don't have any decls after stmts. If
53 // so, emit an extension diagnostic.
54 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
55 // Note that __extension__ can be around a decl.
56 unsigned i = 0;
57 // Skip over all declarations.
58 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
59 /*empty*/;
60
61 // We found the end of the list or a statement. Scan for another declstmt.
62 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
63 /*empty*/;
64
65 if (i != NumElts) {
Steve Naroff8e74c932007-09-13 21:41:19 +000066 ScopedDecl *D = cast<DeclStmt>(Elts[i])->getDecl();
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000067 Diag(D->getLocation(), diag::ext_mixed_decls_code);
68 }
69 }
Chris Lattner98414c12007-08-31 21:49:55 +000070 // Warn about unused expressions in statements.
71 for (unsigned i = 0; i != NumElts; ++i) {
72 Expr *E = dyn_cast<Expr>(Elts[i]);
73 if (!E) continue;
74
75 // Warn about expressions with unused results.
76 if (E->hasLocalSideEffect() || E->getType()->isVoidType())
77 continue;
78
79 // The last expr in a stmt expr really is used.
80 if (isStmtExpr && i == NumElts-1)
81 continue;
82
83 /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in
84 /// a context where the result is unused. Emit a diagnostic to warn about
85 /// this.
86 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
87 Diag(BO->getOperatorLoc(), diag::warn_unused_expr,
88 BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange());
89 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
90 Diag(UO->getOperatorLoc(), diag::warn_unused_expr,
91 UO->getSubExpr()->getSourceRange());
92 else
93 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
94 }
95
Steve Naroffb5a69582007-08-31 23:28:33 +000096 return new CompoundStmt(Elts, NumElts, L, R);
Reid Spencer5f016e22007-07-11 17:01:13 +000097}
98
99Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000100Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner6c36be52007-07-18 02:28:47 +0000101 SourceLocation DotDotDotLoc, ExprTy *rhsval,
Chris Lattner0fa152e2007-07-21 03:00:26 +0000102 SourceLocation ColonLoc, StmtTy *subStmt) {
103 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner8a87e572007-07-23 17:05:23 +0000104 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 assert((LHSVal != 0) && "missing expression in case statement");
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 SourceLocation ExpLoc;
108 // C99 6.8.4.2p3: The expression shall be an integer constant.
Chris Lattner0fa152e2007-07-21 03:00:26 +0000109 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
110 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
111 LHSVal->getSourceRange());
112 return SubStmt;
113 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000114
Chris Lattner6c36be52007-07-18 02:28:47 +0000115 // GCC extension: The expression shall be an integer constant.
Chris Lattner0fa152e2007-07-21 03:00:26 +0000116 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
117 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
118 RHSVal->getSourceRange());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000119 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner6c36be52007-07-18 02:28:47 +0000120 }
Chris Lattner8a87e572007-07-23 17:05:23 +0000121
122 if (SwitchStack.empty()) {
123 Diag(CaseLoc, diag::err_case_not_in_switch);
124 return SubStmt;
125 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000126
Steve Naroffb5a69582007-08-31 23:28:33 +0000127 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner8a87e572007-07-23 17:05:23 +0000128 SwitchStack.back()->addSwitchCase(CS);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000129 return CS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000130}
131
132Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000133Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner0fa152e2007-07-21 03:00:26 +0000134 StmtTy *subStmt, Scope *CurScope) {
135 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner6c36be52007-07-18 02:28:47 +0000136
Chris Lattner8a87e572007-07-23 17:05:23 +0000137 if (SwitchStack.empty()) {
Chris Lattner0fa152e2007-07-21 03:00:26 +0000138 Diag(DefaultLoc, diag::err_default_not_in_switch);
139 return SubStmt;
140 }
141
Chris Lattner0fa152e2007-07-21 03:00:26 +0000142 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner8a87e572007-07-23 17:05:23 +0000143 SwitchStack.back()->addSwitchCase(DS);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000144
Chris Lattner6c36be52007-07-18 02:28:47 +0000145 return DS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000146}
147
148Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000149Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner0fa152e2007-07-21 03:00:26 +0000150 SourceLocation ColonLoc, StmtTy *subStmt) {
151 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 // Look up the record for this label identifier.
153 LabelStmt *&LabelDecl = LabelMap[II];
154
155 // If not forward referenced or defined already, just create a new LabelStmt.
156 if (LabelDecl == 0)
Chris Lattner0fa152e2007-07-21 03:00:26 +0000157 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000158
159 assert(LabelDecl->getID() == II && "Label mismatch!");
160
161 // Otherwise, this label was either forward reference or multiply defined. If
162 // multiply defined, reject it now.
163 if (LabelDecl->getSubStmt()) {
164 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
165 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000166 return SubStmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 }
168
169 // Otherwise, this label was forward declared, and we just found its real
170 // definition. Fill in the forward definition and return it.
171 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000172 LabelDecl->setSubStmt(SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 return LabelDecl;
174}
175
176Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000177Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 StmtTy *ThenVal, SourceLocation ElseLoc,
179 StmtTy *ElseVal) {
180 Expr *condExpr = (Expr *)CondVal;
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000181 Stmt *thenStmt = (Stmt *)ThenVal;
182
Steve Naroff1b273c42007-09-16 14:56:35 +0000183 assert(condExpr && "ActOnIfStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000184
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000185 DefaultFunctionArrayConversion(condExpr);
186 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000187
188 if (!condType->isScalarType()) // C99 6.8.4.1p1
189 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
190 condType.getAsString(), condExpr->getSourceRange());
191
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000192 // Warn if the if block has a null body without an else value.
193 // this helps prevent bugs due to typos, such as
194 // if (condition);
195 // do_stuff();
196 if (!ElseVal) {
197 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
198 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
199 }
200
201 return new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000202}
203
204Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000205Sema::ActOnStartOfSwitchStmt(ExprTy *cond) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000206 Expr *Cond = static_cast<Expr*>(cond);
207
208 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
209 UsualUnaryConversions(Cond);
210
211 SwitchStmt *SS = new SwitchStmt(Cond);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000212 SwitchStack.push_back(SS);
213 return SS;
214}
Chris Lattner6c36be52007-07-18 02:28:47 +0000215
Chris Lattnerf4021e72007-08-23 05:46:52 +0000216/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
217/// the specified width and sign. If an overflow occurs, detect it and emit
218/// the specified diagnostic.
219void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
220 unsigned NewWidth, bool NewSign,
221 SourceLocation Loc,
222 unsigned DiagID) {
223 // Perform a conversion to the promoted condition type if needed.
224 if (NewWidth > Val.getBitWidth()) {
225 // If this is an extension, just do it.
226 llvm::APSInt OldVal(Val);
227 Val.extend(NewWidth);
228
229 // If the input was signed and negative and the output is unsigned,
230 // warn.
231 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
232 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
233
234 Val.setIsSigned(NewSign);
235 } else if (NewWidth < Val.getBitWidth()) {
236 // If this is a truncation, check for overflow.
237 llvm::APSInt ConvVal(Val);
238 ConvVal.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000239 ConvVal.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000240 ConvVal.extend(Val.getBitWidth());
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000241 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000242 if (ConvVal != Val)
243 Diag(Loc, DiagID, Val.toString(), ConvVal.toString());
244
245 // Regardless of whether a diagnostic was emitted, really do the
246 // truncation.
247 Val.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000248 Val.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000249 } else if (NewSign != Val.isSigned()) {
250 // Convert the sign to match the sign of the condition. This can cause
251 // overflow as well: unsigned(INTMIN)
252 llvm::APSInt OldVal(Val);
253 Val.setIsSigned(NewSign);
254
255 if (Val.isNegative()) // Sign bit changes meaning.
256 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
257 }
258}
259
Chris Lattner0471f5b2007-08-23 18:29:20 +0000260namespace {
261 struct CaseCompareFunctor {
262 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
263 const llvm::APSInt &RHS) {
264 return LHS.first < RHS;
265 }
Chris Lattner0e85a272007-09-03 18:31:57 +0000266 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
267 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
268 return LHS.first < RHS.first;
269 }
Chris Lattner0471f5b2007-08-23 18:29:20 +0000270 bool operator()(const llvm::APSInt &LHS,
271 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
272 return LHS < RHS.first;
273 }
274 };
275}
276
Chris Lattner764a7ce2007-09-21 18:15:22 +0000277/// CmpCaseVals - Comparison predicate for sorting case values.
278///
279static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
280 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
281 if (lhs.first < rhs.first)
282 return true;
283
284 if (lhs.first == rhs.first &&
285 lhs.second->getCaseLoc().getRawEncoding()
286 < rhs.second->getCaseLoc().getRawEncoding())
287 return true;
288 return false;
289}
290
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000291Action::StmtResult
Chris Lattner764a7ce2007-09-21 18:15:22 +0000292Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
293 ExprTy *Body) {
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000294 Stmt *BodyStmt = (Stmt*)Body;
295
296 SwitchStmt *SS = SwitchStack.back();
297 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
298
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000299 SS->setBody(BodyStmt, SwitchLoc);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000300 SwitchStack.pop_back();
301
Chris Lattnerf4021e72007-08-23 05:46:52 +0000302 Expr *CondExpr = SS->getCond();
303 QualType CondType = CondExpr->getType();
Chris Lattner6c36be52007-07-18 02:28:47 +0000304
Chris Lattnerf4021e72007-08-23 05:46:52 +0000305 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000306 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattnerf4021e72007-08-23 05:46:52 +0000307 CondType.getAsString(), CondExpr->getSourceRange());
308 return true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000309 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000310
311 // Get the bitwidth of the switched-on value before promotions. We must
312 // convert the integer case values to this width before comparison.
Chris Lattner98be4942008-03-05 18:54:05 +0000313 unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000314 bool CondIsSigned = CondType->isSignedIntegerType();
315
316 // Accumulate all of the case values in a vector so that we can sort them
317 // and detect duplicates. This vector contains the APInt for the case after
318 // it has been converted to the condition type.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000319 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
320 CaseValsTy CaseVals;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000321
322 // Keep track of any GNU case ranges we see. The APSInt is the low value.
323 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
324
325 DefaultStmt *TheDefaultStmt = 0;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000326
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000327 bool CaseListIsErroneous = false;
328
329 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000330 SC = SC->getNextSwitchCase()) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000331
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000332 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000333 if (TheDefaultStmt) {
334 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
335 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000336
Chris Lattnerf4021e72007-08-23 05:46:52 +0000337 // FIXME: Remove the default statement from the switch block so that
338 // we'll return a valid AST. This requires recursing down the
339 // AST and finding it, not something we are set up to do right now. For
340 // now, just lop the entire switch stmt out of the AST.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000341 CaseListIsErroneous = true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000342 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000343 TheDefaultStmt = DS;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000344
Chris Lattnerf4021e72007-08-23 05:46:52 +0000345 } else {
346 CaseStmt *CS = cast<CaseStmt>(SC);
347
348 // We already verified that the expression has a i-c-e value (C99
349 // 6.8.4.2p3) - get that value now.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000350 Expr *Lo = CS->getLHS();
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000351 llvm::APSInt LoVal = Lo->getIntegerConstantExprValue(Context);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000352
353 // Convert the value to the same width/sign as the condition.
354 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
355 CS->getLHS()->getLocStart(),
356 diag::warn_case_value_overflow);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000357
Chris Lattner1e0a3902008-01-16 19:17:22 +0000358 // If the LHS is not the same type as the condition, insert an implicit
359 // cast.
360 ImpCastExprToType(Lo, CondType);
361 CS->setLHS(Lo);
362
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000363 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerf4021e72007-08-23 05:46:52 +0000364 if (CS->getRHS())
365 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000366 else
367 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000368 }
369 }
370
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000371 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner764a7ce2007-09-21 18:15:22 +0000372 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000373
Chris Lattnerf3348502007-08-23 14:29:07 +0000374 if (!CaseVals.empty()) {
375 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
376 if (CaseVals[i].first == CaseVals[i+1].first) {
377 // If we have a duplicate, report it.
378 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
379 diag::err_duplicate_case, CaseVals[i].first.toString());
380 Diag(CaseVals[i].second->getLHS()->getLocStart(),
381 diag::err_duplicate_case_prev);
382 // FIXME: We really want to remove the bogus case stmt from the substmt,
383 // but we have no way to do this right now.
384 CaseListIsErroneous = true;
385 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000386 }
387 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000388
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000389 // Detect duplicate case ranges, which usually don't exist at all in the first
390 // place.
391 if (!CaseRanges.empty()) {
392 // Sort all the case ranges by their low value so we can easily detect
393 // overlaps between ranges.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000394 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000395
396 // Scan the ranges, computing the high values and removing empty ranges.
397 std::vector<llvm::APSInt> HiVals;
Chris Lattner6efc4d32007-08-23 17:48:14 +0000398 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000399 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000400 Expr *Hi = CR->getRHS();
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000401 llvm::APSInt HiVal = Hi->getIntegerConstantExprValue(Context);
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000402
403 // Convert the value to the same width/sign as the condition.
404 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
405 CR->getRHS()->getLocStart(),
406 diag::warn_case_value_overflow);
407
Chris Lattner1e0a3902008-01-16 19:17:22 +0000408 // If the LHS is not the same type as the condition, insert an implicit
409 // cast.
410 ImpCastExprToType(Hi, CondType);
411 CR->setRHS(Hi);
412
Chris Lattner6efc4d32007-08-23 17:48:14 +0000413 // If the low value is bigger than the high value, the case is empty.
414 if (CaseRanges[i].first > HiVal) {
415 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
416 SourceRange(CR->getLHS()->getLocStart(),
417 CR->getRHS()->getLocEnd()));
418 CaseRanges.erase(CaseRanges.begin()+i);
419 --i, --e;
420 continue;
421 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000422 HiVals.push_back(HiVal);
423 }
424
425 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0471f5b2007-08-23 18:29:20 +0000426 // ranges. Since the range list is sorted, we only need to compare case
427 // ranges with their neighbors.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000428 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0471f5b2007-08-23 18:29:20 +0000429 llvm::APSInt &CRLo = CaseRanges[i].first;
430 llvm::APSInt &CRHi = HiVals[i];
431 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000432
Chris Lattner0471f5b2007-08-23 18:29:20 +0000433 // Check to see whether the case range overlaps with any singleton cases.
434 CaseStmt *OverlapStmt = 0;
435 llvm::APSInt OverlapVal(32);
436
437 // Find the smallest value >= the lower bound. If I is in the case range,
438 // then we have overlap.
439 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
440 CaseVals.end(), CRLo,
441 CaseCompareFunctor());
442 if (I != CaseVals.end() && I->first < CRHi) {
443 OverlapVal = I->first; // Found overlap with scalar.
444 OverlapStmt = I->second;
445 }
446
447 // Find the smallest value bigger than the upper bound.
448 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
449 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
450 OverlapVal = (I-1)->first; // Found overlap with scalar.
451 OverlapStmt = (I-1)->second;
452 }
453
454 // Check to see if this case stmt overlaps with the subsequent case range.
455 if (i && CRLo <= HiVals[i-1]) {
456 OverlapVal = HiVals[i-1]; // Found overlap with range.
457 OverlapStmt = CaseRanges[i-1].second;
458 }
459
460 if (OverlapStmt) {
461 // If we have a duplicate, report it.
462 Diag(CR->getLHS()->getLocStart(),
463 diag::err_duplicate_case, OverlapVal.toString());
464 Diag(OverlapStmt->getLHS()->getLocStart(),
465 diag::err_duplicate_case_prev);
466 // FIXME: We really want to remove the bogus case stmt from the substmt,
467 // but we have no way to do this right now.
468 CaseListIsErroneous = true;
469 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000470 }
471 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000472
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000473 // FIXME: If the case list was broken is some way, we don't have a good system
474 // to patch it up. Instead, just return the whole substmt as broken.
475 if (CaseListIsErroneous)
476 return true;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000477
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000478 return SS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000479}
480
481Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000482Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000484 assert(condExpr && "ActOnWhileStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000485
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000486 DefaultFunctionArrayConversion(condExpr);
487 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000488
489 if (!condType->isScalarType()) // C99 6.8.5p2
490 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
491 condType.getAsString(), condExpr->getSourceRange());
492
Steve Naroffb5a69582007-08-31 23:28:33 +0000493 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000494}
495
496Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000497Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 SourceLocation WhileLoc, ExprTy *Cond) {
499 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000500 assert(condExpr && "ActOnDoStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000501
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000502 DefaultFunctionArrayConversion(condExpr);
503 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000504
505 if (!condType->isScalarType()) // C99 6.8.5p2
506 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
507 condType.getAsString(), condExpr->getSourceRange());
508
Steve Naroffb5a69582007-08-31 23:28:33 +0000509 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000510}
511
512Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000513Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000514 StmtTy *first, ExprTy *second, ExprTy *third,
515 SourceLocation RParenLoc, StmtTy *body) {
516 Stmt *First = static_cast<Stmt*>(first);
517 Expr *Second = static_cast<Expr*>(second);
518 Expr *Third = static_cast<Expr*>(third);
519 Stmt *Body = static_cast<Stmt*>(body);
520
Chris Lattnerae3b7012007-08-28 05:03:08 +0000521 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
522 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
523 // identifiers for objects having storage class 'auto' or 'register'.
Ted Kremenek909cd262008-08-08 02:45:18 +0000524 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
525 DI!=DE; ++DI) {
526 VarDecl *VD = dyn_cast<VarDecl>(*DI);
Steve Naroff248a7532008-04-15 22:42:06 +0000527 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
528 VD = 0;
529 if (VD == 0)
Ted Kremenek909cd262008-08-08 02:45:18 +0000530 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
Chris Lattnerae3b7012007-08-28 05:03:08 +0000531 // FIXME: mark decl erroneous!
532 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000533 }
534 if (Second) {
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000535 DefaultFunctionArrayConversion(Second);
536 QualType SecondType = Second->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000537
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000538 if (!SecondType->isScalarType()) // C99 6.8.5p2
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner834a72a2008-07-25 23:18:17 +0000540 SecondType.getAsString(), Second->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 }
Steve Naroffb5a69582007-08-31 23:28:33 +0000542 return new ForStmt(First, Second, Third, Body, ForLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000543}
544
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000545Action::StmtResult
Fariborz Jahanian75712282008-01-10 00:24:29 +0000546Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000547 SourceLocation LParenLoc,
548 StmtTy *first, ExprTy *second,
549 SourceLocation RParenLoc, StmtTy *body) {
550 Stmt *First = static_cast<Stmt*>(first);
551 Expr *Second = static_cast<Expr*>(second);
552 Stmt *Body = static_cast<Stmt*>(body);
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000553 if (First) {
554 QualType FirstType;
555 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
556 FirstType = cast<ValueDecl>(DS->getDecl())->getType();
557 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
558 // identifiers for objects having storage class 'auto' or 'register'.
559 ScopedDecl *D = DS->getDecl();
Steve Naroff248a7532008-04-15 22:42:06 +0000560 VarDecl *VD = cast<VarDecl>(D);
561 if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
562 return Diag(VD->getLocation(), diag::err_non_variable_decl_in_for);
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000563 if (D->getNextDeclarator())
564 return Diag(D->getLocation(), diag::err_toomany_element_decls);
Steve Naroff248a7532008-04-15 22:42:06 +0000565 } else
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000566 FirstType = static_cast<Expr*>(first)->getType();
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000567 if (!Context.isObjCObjectPointerType(FirstType))
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000568 Diag(ForLoc, diag::err_selector_element_type,
569 FirstType.getAsString(), First->getSourceRange());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000570 }
571 if (Second) {
572 DefaultFunctionArrayConversion(Second);
573 QualType SecondType = Second->getType();
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000574 if (!Context.isObjCObjectPointerType(SecondType))
Fariborz Jahanian75712282008-01-10 00:24:29 +0000575 Diag(ForLoc, diag::err_collection_expr_type,
Fariborz Jahanian1f990d62008-01-04 00:27:46 +0000576 SecondType.getAsString(), Second->getSourceRange());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000577 }
Fariborz Jahanian75712282008-01-10 00:24:29 +0000578 return new ObjCForCollectionStmt(First, Second, Body, ForLoc, RParenLoc);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000579}
Reid Spencer5f016e22007-07-11 17:01:13 +0000580
581Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000582Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 IdentifierInfo *LabelII) {
584 // Look up the record for this label identifier.
585 LabelStmt *&LabelDecl = LabelMap[LabelII];
586
587 // If we haven't seen this label yet, create a forward reference.
588 if (LabelDecl == 0)
589 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
590
Ted Kremenek61f62162007-09-06 17:11:52 +0000591 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000592}
593
594Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000595Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 ExprTy *DestExp) {
597 // FIXME: Verify that the operand is convertible to void*.
598
599 return new IndirectGotoStmt((Expr*)DestExp);
600}
601
602Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000603Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 Scope *S = CurScope->getContinueParent();
605 if (!S) {
606 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
607 Diag(ContinueLoc, diag::err_continue_not_in_loop);
608 return true;
609 }
610
Steve Naroff507f2d52007-08-31 23:49:30 +0000611 return new ContinueStmt(ContinueLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000612}
613
614Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000615Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 Scope *S = CurScope->getBreakParent();
617 if (!S) {
618 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
619 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
620 return true;
621 }
622
Steve Naroff507f2d52007-08-31 23:49:30 +0000623 return new BreakStmt(BreakLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000624}
625
626
627Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000628Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
Steve Naroff90045e82007-07-13 23:32:42 +0000629 Expr *RetValExp = static_cast<Expr *>(rex);
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000630 QualType FnRetType =
631 getCurFunctionDecl() ? getCurFunctionDecl()->getResultType() :
632 getCurMethodDecl()->getResultType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000633
Chris Lattner5cf216b2008-01-04 18:04:52 +0000634 if (FnRetType->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
Fariborz Jahanianb107ce82007-12-04 19:20:11 +0000636 Diag(ReturnLoc, diag::ext_return_has_expr,
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000637 ( getCurFunctionDecl() ?
638 getCurFunctionDecl()->getIdentifier()->getName() :
639 getCurMethodDecl()->getSelector().getName() ),
Steve Naroff90045e82007-07-13 23:32:42 +0000640 RetValExp->getSourceRange());
Steve Naroff507f2d52007-08-31 23:49:30 +0000641 return new ReturnStmt(ReturnLoc, RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 } else {
643 if (!RetValExp) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000644 const char *funcName =
645 getCurFunctionDecl() ?
646 getCurFunctionDecl()->getIdentifier()->getName() :
647 getCurMethodDecl()->getSelector().getName().c_str();
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
649 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
650 else // C90 6.6.6.4p4
651 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
Steve Naroff507f2d52007-08-31 23:49:30 +0000652 return new ReturnStmt(ReturnLoc, (Expr*)0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 }
654 }
655 // we have a non-void function with an expression, continue checking
Chris Lattner5cf216b2008-01-04 18:04:52 +0000656 QualType RetValType = RetValExp->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
659 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
660 // function return.
Chris Lattner5cf216b2008-01-04 18:04:52 +0000661 AssignConvertType ConvTy = CheckSingleAssignmentConstraints(FnRetType,
662 RetValExp);
663 if (DiagnoseAssignmentResult(ConvTy, ReturnLoc, FnRetType,
664 RetValType, RetValExp, "returning"))
665 return true;
Ted Kremenek06de2762007-08-17 16:46:58 +0000666
Chris Lattner5cf216b2008-01-04 18:04:52 +0000667 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
Ted Kremenek06de2762007-08-17 16:46:58 +0000668
Steve Naroff507f2d52007-08-31 23:49:30 +0000669 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000670}
671
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000672Sema::StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
Anders Carlssondfab34a2008-02-05 23:03:50 +0000673 bool IsSimple,
Anders Carlsson39c47b52007-11-23 23:12:25 +0000674 bool IsVolatile,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000675 unsigned NumOutputs,
676 unsigned NumInputs,
677 std::string *Names,
678 ExprTy **Constraints,
679 ExprTy **Exprs,
Chris Lattner6bc52112008-07-23 06:46:56 +0000680 ExprTy *asmString,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000681 unsigned NumClobbers,
682 ExprTy **Clobbers,
Chris Lattnerfe795952007-10-29 04:04:16 +0000683 SourceLocation RParenLoc) {
Chris Lattner6bc52112008-07-23 06:46:56 +0000684 // The parser verifies that there is a string literal here.
685 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString);
686 if (AsmString->isWide())
687 // FIXME: We currently leak memory here.
688 return Diag(AsmString->getLocStart(), diag::err_asm_wide_character,
689 AsmString->getSourceRange());
690
691
Anders Carlsson04728b72007-11-23 19:43:50 +0000692 for (unsigned i = 0; i < NumOutputs; i++) {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000693 StringLiteral *Literal = cast<StringLiteral>((Expr *)Constraints[i]);
Chris Lattner6bc52112008-07-23 06:46:56 +0000694 if (Literal->isWide())
695 // FIXME: We currently leak memory here.
696 return Diag(Literal->getLocStart(), diag::err_asm_wide_character,
697 Literal->getSourceRange());
698
Anders Carlssond04c6e22007-11-27 04:11:28 +0000699 std::string OutputConstraint(Literal->getStrData(),
700 Literal->getByteLength());
701
702 TargetInfo::ConstraintInfo info;
Chris Lattner6bc52112008-07-23 06:46:56 +0000703 if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info))
Anders Carlssond04c6e22007-11-27 04:11:28 +0000704 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000705 return Diag(Literal->getLocStart(),
706 diag::err_invalid_output_constraint_in_asm);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000707
708 // Check that the output exprs are valid lvalues.
Anders Carlsson04728b72007-11-23 19:43:50 +0000709 Expr *OutputExpr = (Expr *)Exprs[i];
Chris Lattner28be73f2008-07-26 21:30:36 +0000710 Expr::isLvalueResult Result = OutputExpr->isLvalue(Context);
Anders Carlsson04728b72007-11-23 19:43:50 +0000711 if (Result != Expr::LV_Valid) {
712 ParenExpr *PE = cast<ParenExpr>(OutputExpr);
713
Anders Carlsson04728b72007-11-23 19:43:50 +0000714 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000715 return Diag(PE->getSubExpr()->getLocStart(),
716 diag::err_invalid_lvalue_in_asm_output,
717 PE->getSubExpr()->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000718 }
719 }
720
Anders Carlsson04728b72007-11-23 19:43:50 +0000721 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000722 StringLiteral *Literal = cast<StringLiteral>((Expr *)Constraints[i]);
Chris Lattner6bc52112008-07-23 06:46:56 +0000723 if (Literal->isWide())
724 // FIXME: We currently leak memory here.
725 return Diag(Literal->getLocStart(), diag::err_asm_wide_character,
726 Literal->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000727
Anders Carlssond04c6e22007-11-27 04:11:28 +0000728 std::string InputConstraint(Literal->getStrData(),
729 Literal->getByteLength());
730
731 TargetInfo::ConstraintInfo info;
732 if (!Context.Target.validateInputConstraint(InputConstraint.c_str(),
733 NumOutputs,
734 info)) {
735 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000736 return Diag(Literal->getLocStart(),
737 diag::err_invalid_input_constraint_in_asm);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000738 }
739
740 // Check that the input exprs aren't of type void.
741 Expr *InputExpr = (Expr *)Exprs[i];
Anders Carlsson04728b72007-11-23 19:43:50 +0000742 if (InputExpr->getType()->isVoidType()) {
743 ParenExpr *PE = cast<ParenExpr>(InputExpr);
744
Anders Carlsson04728b72007-11-23 19:43:50 +0000745 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000746 return Diag(PE->getSubExpr()->getLocStart(),
747 diag::err_invalid_type_in_asm_input,
748 PE->getType().getAsString(),
749 PE->getSubExpr()->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000750 }
751 }
Anders Carlssonb235fc22007-11-22 01:36:19 +0000752
Anders Carlsson6fa90862007-11-25 00:25:21 +0000753 // Check that the clobbers are valid.
754 for (unsigned i = 0; i < NumClobbers; i++) {
755 StringLiteral *Literal = cast<StringLiteral>((Expr *)Clobbers[i]);
Chris Lattner6bc52112008-07-23 06:46:56 +0000756 if (Literal->isWide())
757 // FIXME: We currently leak memory here.
758 return Diag(Literal->getLocStart(), diag::err_asm_wide_character,
759 Literal->getSourceRange());
Anders Carlsson6fa90862007-11-25 00:25:21 +0000760
761 llvm::SmallString<16> Clobber(Literal->getStrData(),
762 Literal->getStrData() +
763 Literal->getByteLength());
764
Chris Lattner6bc52112008-07-23 06:46:56 +0000765 if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000766 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000767 return Diag(Literal->getLocStart(),
768 diag::err_unknown_register_name_in_asm, Clobber.c_str());
Anders Carlsson6fa90862007-11-25 00:25:21 +0000769 }
770
Anders Carlssonb235fc22007-11-22 01:36:19 +0000771 return new AsmStmt(AsmLoc,
Anders Carlssondfab34a2008-02-05 23:03:50 +0000772 IsSimple,
Anders Carlsson39c47b52007-11-23 23:12:25 +0000773 IsVolatile,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000774 NumOutputs,
775 NumInputs,
776 Names,
777 reinterpret_cast<StringLiteral**>(Constraints),
778 reinterpret_cast<Expr**>(Exprs),
Chris Lattner6bc52112008-07-23 06:46:56 +0000779 AsmString, NumClobbers,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000780 reinterpret_cast<StringLiteral**>(Clobbers),
781 RParenLoc);
Chris Lattnerfe795952007-10-29 04:04:16 +0000782}
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000783
784Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000785Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000786 SourceLocation RParen, StmtTy *Parm,
787 StmtTy *Body, StmtTy *CatchList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000788 ObjCAtCatchStmt *CS = new ObjCAtCatchStmt(AtLoc, RParen,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000789 static_cast<Stmt*>(Parm), static_cast<Stmt*>(Body),
790 static_cast<Stmt*>(CatchList));
791 return CatchList ? CatchList : CS;
792}
793
Fariborz Jahanian161a9c52007-11-02 00:18:53 +0000794Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000795Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtTy *Body) {
796 ObjCAtFinallyStmt *FS = new ObjCAtFinallyStmt(AtLoc,
Fariborz Jahanian161a9c52007-11-02 00:18:53 +0000797 static_cast<Stmt*>(Body));
798 return FS;
799}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000800
801Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000802Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000803 StmtTy *Try, StmtTy *Catch, StmtTy *Finally) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000804 ObjCAtTryStmt *TS = new ObjCAtTryStmt(AtLoc, static_cast<Stmt*>(Try),
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000805 static_cast<Stmt*>(Catch),
806 static_cast<Stmt*>(Finally));
807 return TS;
808}
809
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000810Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000811Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, StmtTy *Throw) {
812 ObjCAtThrowStmt *TS = new ObjCAtThrowStmt(AtLoc, static_cast<Stmt*>(Throw));
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000813 return TS;
814}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000815
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000816Action::StmtResult
817Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprTy *SynchExpr,
818 StmtTy *SynchBody) {
819 ObjCAtSynchronizedStmt *SS = new ObjCAtSynchronizedStmt(AtLoc,
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000820 static_cast<Stmt*>(SynchExpr), static_cast<Stmt*>(SynchBody));
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000821 return SS;
822}