blob: 4a5facb39ccec9540e5e682c7786a5b35f88b417 [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"
Anders Carlsson51fe9962008-11-22 21:04:56 +000015#include "clang/AST/APValue.h"
Chris Lattnerf4021e72007-08-23 05:46:52 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/Expr.h"
Ted Kremenek8189cde2009-02-07 01:47:29 +000019#include "clang/AST/ASTContext.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000020#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Sebastian Redla60528c2008-12-21 12:04:03 +000023Sema::OwningStmtResult Sema::ActOnExprStmt(ExprArg expr) {
24 Expr *E = static_cast<Expr*>(expr.release());
Steve Naroff1b273c42007-09-16 14:56:35 +000025 assert(E && "ActOnExprStmt(): missing expression");
Sebastian Redla60528c2008-12-21 12:04:03 +000026
Chris Lattner834a72a2008-07-25 23:18:17 +000027 // 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.
Sebastian Redla60528c2008-12-21 12:04:03 +000030
Chris Lattner834a72a2008-07-25 23:18:17 +000031 // Same thing in for stmt first clause (when expr) and third clause.
Sebastian Redla60528c2008-12-21 12:04:03 +000032 return Owned(static_cast<Stmt*>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +000033}
34
35
Sebastian Redla60528c2008-12-21 12:04:03 +000036Sema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
Ted Kremenek8189cde2009-02-07 01:47:29 +000037 return Owned(new (Context) NullStmt(SemiLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +000038}
39
Chris Lattner682bf922009-03-29 16:50:03 +000040Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg,
Sebastian Redla60528c2008-12-21 12:04:03 +000041 SourceLocation StartLoc,
42 SourceLocation EndLoc) {
Chris Lattner682bf922009-03-29 16:50:03 +000043 DeclGroupRef DG = dg.getAsVal<DeclGroupRef>();
Chris Lattner20401692009-04-12 20:13:14 +000044
45 // If we have an invalid decl, just return an error.
46 if (DG.isNull()) return StmtError();
47
Chris Lattner24e1e702009-03-04 04:23:07 +000048 return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +000049}
50
Sebastian Redla60528c2008-12-21 12:04:03 +000051Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +000052Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Sebastian Redla60528c2008-12-21 12:04:03 +000053 MultiStmtArg elts, bool isStmtExpr) {
54 unsigned NumElts = elts.size();
55 Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000056 // If we're in C89 mode, check that we don't have any decls after stmts. If
57 // so, emit an extension diagnostic.
58 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
59 // Note that __extension__ can be around a decl.
60 unsigned i = 0;
61 // Skip over all declarations.
62 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
63 /*empty*/;
64
65 // We found the end of the list or a statement. Scan for another declstmt.
66 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
67 /*empty*/;
68
69 if (i != NumElts) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +000070 Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000071 Diag(D->getLocation(), diag::ext_mixed_decls_code);
72 }
73 }
Chris Lattner98414c12007-08-31 21:49:55 +000074 // Warn about unused expressions in statements.
75 for (unsigned i = 0; i != NumElts; ++i) {
76 Expr *E = dyn_cast<Expr>(Elts[i]);
77 if (!E) continue;
78
Chris Lattner026dc962009-02-14 07:37:35 +000079 // Warn about expressions with unused results if they are non-void and if
80 // this not the last stmt in a stmt expr.
81 if (E->getType()->isVoidType() || (isStmtExpr && i == NumElts-1))
Chris Lattner98414c12007-08-31 21:49:55 +000082 continue;
83
Chris Lattner026dc962009-02-14 07:37:35 +000084 SourceLocation Loc;
85 SourceRange R1, R2;
86 if (!E->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner98414c12007-08-31 21:49:55 +000087 continue;
Chris Lattner026dc962009-02-14 07:37:35 +000088
89 Diag(Loc, diag::warn_unused_expr) << R1 << R2;
Chris Lattner98414c12007-08-31 21:49:55 +000090 }
Sebastian Redla60528c2008-12-21 12:04:03 +000091
Ted Kremenek8189cde2009-02-07 01:47:29 +000092 return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R));
Reid Spencer5f016e22007-07-11 17:01:13 +000093}
94
Sebastian Redl117054a2008-12-28 16:13:43 +000095Action::OwningStmtResult
96Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval,
97 SourceLocation DotDotDotLoc, ExprArg rhsval,
Chris Lattner24e1e702009-03-04 04:23:07 +000098 SourceLocation ColonLoc) {
Sebastian Redl117054a2008-12-28 16:13:43 +000099 assert((lhsval.get() != 0) && "missing expression in case statement");
100
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 // C99 6.8.4.2p3: The expression shall be an integer constant.
Anders Carlsson51fe9962008-11-22 21:04:56 +0000102 // However, GCC allows any evaluatable integer expression.
Sebastian Redl117054a2008-12-28 16:13:43 +0000103 Expr *LHSVal = static_cast<Expr*>(lhsval.get());
Anders Carlssond3a61d52008-12-01 02:13:02 +0000104 if (VerifyIntegerConstantExpression(LHSVal))
Chris Lattner24e1e702009-03-04 04:23:07 +0000105 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000106
Chris Lattner6c36be52007-07-18 02:28:47 +0000107 // GCC extension: The expression shall be an integer constant.
Sebastian Redl117054a2008-12-28 16:13:43 +0000108
109 Expr *RHSVal = static_cast<Expr*>(rhsval.get());
110 if (RHSVal && VerifyIntegerConstantExpression(RHSVal)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000111 RHSVal = 0; // Recover by just forgetting about it.
Sebastian Redl117054a2008-12-28 16:13:43 +0000112 rhsval = 0;
113 }
114
Chris Lattnerbcfce662009-04-18 20:10:59 +0000115 if (getSwitchStack().empty()) {
Chris Lattner8a87e572007-07-23 17:05:23 +0000116 Diag(CaseLoc, diag::err_case_not_in_switch);
Chris Lattner24e1e702009-03-04 04:23:07 +0000117 return StmtError();
Chris Lattner8a87e572007-07-23 17:05:23 +0000118 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000119
Sebastian Redl117054a2008-12-28 16:13:43 +0000120 // Only now release the smart pointers.
121 lhsval.release();
122 rhsval.release();
Chris Lattner24e1e702009-03-04 04:23:07 +0000123 CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc);
Chris Lattnerbcfce662009-04-18 20:10:59 +0000124 getSwitchStack().back()->addSwitchCase(CS);
Sebastian Redl117054a2008-12-28 16:13:43 +0000125 return Owned(CS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000126}
127
Chris Lattner24e1e702009-03-04 04:23:07 +0000128/// ActOnCaseStmtBody - This installs a statement as the body of a case.
129void Sema::ActOnCaseStmtBody(StmtTy *caseStmt, StmtArg subStmt) {
130 CaseStmt *CS = static_cast<CaseStmt*>(caseStmt);
131 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
132 CS->setSubStmt(SubStmt);
133}
134
Sebastian Redl117054a2008-12-28 16:13:43 +0000135Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000136Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Sebastian Redl117054a2008-12-28 16:13:43 +0000137 StmtArg subStmt, Scope *CurScope) {
138 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
139
Chris Lattnerbcfce662009-04-18 20:10:59 +0000140 if (getSwitchStack().empty()) {
Chris Lattner0fa152e2007-07-21 03:00:26 +0000141 Diag(DefaultLoc, diag::err_default_not_in_switch);
Sebastian Redl117054a2008-12-28 16:13:43 +0000142 return Owned(SubStmt);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000143 }
Sebastian Redl117054a2008-12-28 16:13:43 +0000144
Ted Kremenek8189cde2009-02-07 01:47:29 +0000145 DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, SubStmt);
Chris Lattnerbcfce662009-04-18 20:10:59 +0000146 getSwitchStack().back()->addSwitchCase(DS);
Sebastian Redl117054a2008-12-28 16:13:43 +0000147 return Owned(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148}
149
Sebastian Redlde307472009-01-11 00:38:46 +0000150Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000151Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Sebastian Redlde307472009-01-11 00:38:46 +0000152 SourceLocation ColonLoc, StmtArg subStmt) {
153 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
Steve Narofff3cf8972009-02-28 16:48:43 +0000154 // Look up the record for this label identifier.
Chris Lattnerea29a3a2009-04-18 20:01:55 +0000155 LabelStmt *&LabelDecl = getLabelMap()[II];
Steve Narofff3cf8972009-02-28 16:48:43 +0000156
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // If not forward referenced or defined already, just create a new LabelStmt.
Steve Naroffcaaacec2009-03-13 15:38:40 +0000158 if (LabelDecl == 0)
159 return Owned(LabelDecl = new (Context) LabelStmt(IdentLoc, II, SubStmt));
Sebastian Redlde307472009-01-11 00:38:46 +0000160
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 assert(LabelDecl->getID() == II && "Label mismatch!");
Sebastian Redlde307472009-01-11 00:38:46 +0000162
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 // Otherwise, this label was either forward reference or multiply defined. If
164 // multiply defined, reject it now.
165 if (LabelDecl->getSubStmt()) {
Chris Lattner08631c52008-11-23 21:45:46 +0000166 Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000167 Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition);
Sebastian Redlde307472009-01-11 00:38:46 +0000168 return Owned(SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 }
Sebastian Redlde307472009-01-11 00:38:46 +0000170
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 // Otherwise, this label was forward declared, and we just found its real
172 // definition. Fill in the forward definition and return it.
173 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000174 LabelDecl->setSubStmt(SubStmt);
Sebastian Redlde307472009-01-11 00:38:46 +0000175 return Owned(LabelDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000176}
177
Sebastian Redlde307472009-01-11 00:38:46 +0000178Action::OwningStmtResult
179Sema::ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal,
180 StmtArg ThenVal, SourceLocation ElseLoc,
181 StmtArg ElseVal) {
182 Expr *condExpr = (Expr *)CondVal.release();
183
Steve Naroff1b273c42007-09-16 14:56:35 +0000184 assert(condExpr && "ActOnIfStmt(): missing expression");
Sebastian Redlde307472009-01-11 00:38:46 +0000185
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000186 DefaultFunctionArrayConversion(condExpr);
Sebastian Redlde307472009-01-11 00:38:46 +0000187 // Take ownership again until we're past the error checking.
188 CondVal = condExpr;
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000189 QualType condType = condExpr->getType();
Sebastian Redlde307472009-01-11 00:38:46 +0000190
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000191 if (getLangOptions().CPlusPlus) {
192 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redlde307472009-01-11 00:38:46 +0000193 return StmtError();
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000194 } else if (!condType->isScalarType()) // C99 6.8.4.1p1
Sebastian Redlde307472009-01-11 00:38:46 +0000195 return StmtError(Diag(IfLoc, diag::err_typecheck_statement_requires_scalar)
196 << condType << condExpr->getSourceRange());
197
198 Stmt *thenStmt = (Stmt *)ThenVal.release();
Reid Spencer5f016e22007-07-11 17:01:13 +0000199
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000200 // Warn if the if block has a null body without an else value.
201 // this helps prevent bugs due to typos, such as
202 // if (condition);
203 // do_stuff();
Sebastian Redlde307472009-01-11 00:38:46 +0000204 if (!ElseVal.get()) {
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000205 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
206 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
207 }
208
Sebastian Redlde307472009-01-11 00:38:46 +0000209 CondVal.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000210 return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt,
211 (Stmt*)ElseVal.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000212}
213
Sebastian Redlde307472009-01-11 00:38:46 +0000214Action::OwningStmtResult
215Sema::ActOnStartOfSwitchStmt(ExprArg cond) {
216 Expr *Cond = static_cast<Expr*>(cond.release());
217
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000218 if (getLangOptions().CPlusPlus) {
219 // C++ 6.4.2.p2:
220 // The condition shall be of integral type, enumeration type, or of a class
221 // type for which a single conversion function to integral or enumeration
222 // type exists (12.3). If the condition is of class type, the condition is
223 // converted by calling that conversion function, and the result of the
224 // conversion is used in place of the original condition for the remainder
225 // of this section. Integral promotions are performed.
226
227 QualType Ty = Cond->getType();
228
229 // FIXME: Handle class types.
230
231 // If the type is wrong a diagnostic will be emitted later at
232 // ActOnFinishSwitchStmt.
233 if (Ty->isIntegralType() || Ty->isEnumeralType()) {
234 // Integral promotions are performed.
235 // FIXME: Integral promotions for C++ are not complete.
236 UsualUnaryConversions(Cond);
237 }
238 } else {
239 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
240 UsualUnaryConversions(Cond);
241 }
Sebastian Redlde307472009-01-11 00:38:46 +0000242
Ted Kremenek8189cde2009-02-07 01:47:29 +0000243 SwitchStmt *SS = new (Context) SwitchStmt(Cond);
Chris Lattnerbcfce662009-04-18 20:10:59 +0000244 getSwitchStack().push_back(SS);
Sebastian Redlde307472009-01-11 00:38:46 +0000245 return Owned(SS);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000246}
Chris Lattner6c36be52007-07-18 02:28:47 +0000247
Chris Lattnerf4021e72007-08-23 05:46:52 +0000248/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
249/// the specified width and sign. If an overflow occurs, detect it and emit
250/// the specified diagnostic.
251void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
252 unsigned NewWidth, bool NewSign,
253 SourceLocation Loc,
254 unsigned DiagID) {
255 // Perform a conversion to the promoted condition type if needed.
256 if (NewWidth > Val.getBitWidth()) {
257 // If this is an extension, just do it.
258 llvm::APSInt OldVal(Val);
259 Val.extend(NewWidth);
260
261 // If the input was signed and negative and the output is unsigned,
262 // warn.
263 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000264 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000265
266 Val.setIsSigned(NewSign);
267 } else if (NewWidth < Val.getBitWidth()) {
268 // If this is a truncation, check for overflow.
269 llvm::APSInt ConvVal(Val);
270 ConvVal.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000271 ConvVal.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000272 ConvVal.extend(Val.getBitWidth());
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000273 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000274 if (ConvVal != Val)
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000275 Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000276
277 // Regardless of whether a diagnostic was emitted, really do the
278 // truncation.
279 Val.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000280 Val.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000281 } else if (NewSign != Val.isSigned()) {
282 // Convert the sign to match the sign of the condition. This can cause
283 // overflow as well: unsigned(INTMIN)
284 llvm::APSInt OldVal(Val);
285 Val.setIsSigned(NewSign);
286
287 if (Val.isNegative()) // Sign bit changes meaning.
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000288 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000289 }
290}
291
Chris Lattner0471f5b2007-08-23 18:29:20 +0000292namespace {
293 struct CaseCompareFunctor {
294 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
295 const llvm::APSInt &RHS) {
296 return LHS.first < RHS;
297 }
Chris Lattner0e85a272007-09-03 18:31:57 +0000298 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
299 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
300 return LHS.first < RHS.first;
301 }
Chris Lattner0471f5b2007-08-23 18:29:20 +0000302 bool operator()(const llvm::APSInt &LHS,
303 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
304 return LHS < RHS.first;
305 }
306 };
307}
308
Chris Lattner764a7ce2007-09-21 18:15:22 +0000309/// CmpCaseVals - Comparison predicate for sorting case values.
310///
311static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
312 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
313 if (lhs.first < rhs.first)
314 return true;
315
316 if (lhs.first == rhs.first &&
317 lhs.second->getCaseLoc().getRawEncoding()
318 < rhs.second->getCaseLoc().getRawEncoding())
319 return true;
320 return false;
321}
322
Sebastian Redlde307472009-01-11 00:38:46 +0000323Action::OwningStmtResult
324Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch,
325 StmtArg Body) {
326 Stmt *BodyStmt = (Stmt*)Body.release();
327
Chris Lattnerbcfce662009-04-18 20:10:59 +0000328 SwitchStmt *SS = getSwitchStack().back();
Sebastian Redlde307472009-01-11 00:38:46 +0000329 assert(SS == (SwitchStmt*)Switch.get() && "switch stack missing push/pop!");
330
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000331 SS->setBody(BodyStmt, SwitchLoc);
Chris Lattnerbcfce662009-04-18 20:10:59 +0000332 getSwitchStack().pop_back();
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000333
Chris Lattnerf4021e72007-08-23 05:46:52 +0000334 Expr *CondExpr = SS->getCond();
335 QualType CondType = CondExpr->getType();
Sebastian Redlde307472009-01-11 00:38:46 +0000336
Chris Lattnerf4021e72007-08-23 05:46:52 +0000337 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000338 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000339 << CondType << CondExpr->getSourceRange();
Sebastian Redlde307472009-01-11 00:38:46 +0000340 return StmtError();
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000341 }
Sebastian Redlde307472009-01-11 00:38:46 +0000342
Chris Lattnerf4021e72007-08-23 05:46:52 +0000343 // Get the bitwidth of the switched-on value before promotions. We must
344 // convert the integer case values to this width before comparison.
Chris Lattner98be4942008-03-05 18:54:05 +0000345 unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000346 bool CondIsSigned = CondType->isSignedIntegerType();
347
348 // Accumulate all of the case values in a vector so that we can sort them
349 // and detect duplicates. This vector contains the APInt for the case after
350 // it has been converted to the condition type.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000351 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
352 CaseValsTy CaseVals;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000353
354 // Keep track of any GNU case ranges we see. The APSInt is the low value.
355 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
356
357 DefaultStmt *TheDefaultStmt = 0;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000358
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000359 bool CaseListIsErroneous = false;
360
361 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000362 SC = SC->getNextSwitchCase()) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000363
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000364 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000365 if (TheDefaultStmt) {
366 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
Chris Lattner5f4a6822008-11-23 23:12:31 +0000367 Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
Sebastian Redlde307472009-01-11 00:38:46 +0000368
Chris Lattnerf4021e72007-08-23 05:46:52 +0000369 // FIXME: Remove the default statement from the switch block so that
370 // we'll return a valid AST. This requires recursing down the
371 // AST and finding it, not something we are set up to do right now. For
372 // now, just lop the entire switch stmt out of the AST.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000373 CaseListIsErroneous = true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000374 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000375 TheDefaultStmt = DS;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000376
Chris Lattnerf4021e72007-08-23 05:46:52 +0000377 } else {
378 CaseStmt *CS = cast<CaseStmt>(SC);
379
380 // We already verified that the expression has a i-c-e value (C99
381 // 6.8.4.2p3) - get that value now.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000382 Expr *Lo = CS->getLHS();
Anders Carlsson51fe9962008-11-22 21:04:56 +0000383 llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000384
385 // Convert the value to the same width/sign as the condition.
386 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
387 CS->getLHS()->getLocStart(),
388 diag::warn_case_value_overflow);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000389
Chris Lattner1e0a3902008-01-16 19:17:22 +0000390 // If the LHS is not the same type as the condition, insert an implicit
391 // cast.
392 ImpCastExprToType(Lo, CondType);
393 CS->setLHS(Lo);
394
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000395 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerf4021e72007-08-23 05:46:52 +0000396 if (CS->getRHS())
397 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000398 else
399 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000400 }
401 }
402
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000403 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner764a7ce2007-09-21 18:15:22 +0000404 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000405
Chris Lattnerf3348502007-08-23 14:29:07 +0000406 if (!CaseVals.empty()) {
407 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
408 if (CaseVals[i].first == CaseVals[i+1].first) {
409 // If we have a duplicate, report it.
410 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000411 diag::err_duplicate_case) << CaseVals[i].first.toString(10);
Chris Lattnerf3348502007-08-23 14:29:07 +0000412 Diag(CaseVals[i].second->getLHS()->getLocStart(),
Chris Lattner5f4a6822008-11-23 23:12:31 +0000413 diag::note_duplicate_case_prev);
Chris Lattnerf3348502007-08-23 14:29:07 +0000414 // FIXME: We really want to remove the bogus case stmt from the substmt,
415 // but we have no way to do this right now.
416 CaseListIsErroneous = true;
417 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000418 }
419 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000420
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000421 // Detect duplicate case ranges, which usually don't exist at all in the first
422 // place.
423 if (!CaseRanges.empty()) {
424 // Sort all the case ranges by their low value so we can easily detect
425 // overlaps between ranges.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000426 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000427
428 // Scan the ranges, computing the high values and removing empty ranges.
429 std::vector<llvm::APSInt> HiVals;
Chris Lattner6efc4d32007-08-23 17:48:14 +0000430 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000431 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000432 Expr *Hi = CR->getRHS();
Anders Carlsson51fe9962008-11-22 21:04:56 +0000433 llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000434
435 // Convert the value to the same width/sign as the condition.
436 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
437 CR->getRHS()->getLocStart(),
438 diag::warn_case_value_overflow);
439
Chris Lattner1e0a3902008-01-16 19:17:22 +0000440 // If the LHS is not the same type as the condition, insert an implicit
441 // cast.
442 ImpCastExprToType(Hi, CondType);
443 CR->setRHS(Hi);
444
Chris Lattner6efc4d32007-08-23 17:48:14 +0000445 // If the low value is bigger than the high value, the case is empty.
446 if (CaseRanges[i].first > HiVal) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000447 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
448 << SourceRange(CR->getLHS()->getLocStart(),
449 CR->getRHS()->getLocEnd());
Chris Lattner6efc4d32007-08-23 17:48:14 +0000450 CaseRanges.erase(CaseRanges.begin()+i);
451 --i, --e;
452 continue;
453 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000454 HiVals.push_back(HiVal);
455 }
456
457 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0471f5b2007-08-23 18:29:20 +0000458 // ranges. Since the range list is sorted, we only need to compare case
459 // ranges with their neighbors.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000460 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0471f5b2007-08-23 18:29:20 +0000461 llvm::APSInt &CRLo = CaseRanges[i].first;
462 llvm::APSInt &CRHi = HiVals[i];
463 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000464
Chris Lattner0471f5b2007-08-23 18:29:20 +0000465 // Check to see whether the case range overlaps with any singleton cases.
466 CaseStmt *OverlapStmt = 0;
467 llvm::APSInt OverlapVal(32);
468
469 // Find the smallest value >= the lower bound. If I is in the case range,
470 // then we have overlap.
471 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
472 CaseVals.end(), CRLo,
473 CaseCompareFunctor());
474 if (I != CaseVals.end() && I->first < CRHi) {
475 OverlapVal = I->first; // Found overlap with scalar.
476 OverlapStmt = I->second;
477 }
478
479 // Find the smallest value bigger than the upper bound.
480 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
481 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
482 OverlapVal = (I-1)->first; // Found overlap with scalar.
483 OverlapStmt = (I-1)->second;
484 }
485
486 // Check to see if this case stmt overlaps with the subsequent case range.
487 if (i && CRLo <= HiVals[i-1]) {
488 OverlapVal = HiVals[i-1]; // Found overlap with range.
489 OverlapStmt = CaseRanges[i-1].second;
490 }
491
492 if (OverlapStmt) {
493 // If we have a duplicate, report it.
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000494 Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
495 << OverlapVal.toString(10);
Chris Lattner0471f5b2007-08-23 18:29:20 +0000496 Diag(OverlapStmt->getLHS()->getLocStart(),
Chris Lattner5f4a6822008-11-23 23:12:31 +0000497 diag::note_duplicate_case_prev);
Chris Lattner0471f5b2007-08-23 18:29:20 +0000498 // FIXME: We really want to remove the bogus case stmt from the substmt,
499 // but we have no way to do this right now.
500 CaseListIsErroneous = true;
501 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000502 }
503 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000504
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000505 // FIXME: If the case list was broken is some way, we don't have a good system
506 // to patch it up. Instead, just return the whole substmt as broken.
507 if (CaseListIsErroneous)
Sebastian Redlde307472009-01-11 00:38:46 +0000508 return StmtError();
509
510 Switch.release();
511 return Owned(SS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000512}
513
Sebastian Redlf05b1522009-01-16 23:28:06 +0000514Action::OwningStmtResult
515Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprArg Cond, StmtArg Body) {
516 Expr *condExpr = (Expr *)Cond.release();
Steve Naroff1b273c42007-09-16 14:56:35 +0000517 assert(condExpr && "ActOnWhileStmt(): missing expression");
Sebastian Redlf05b1522009-01-16 23:28:06 +0000518
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000519 DefaultFunctionArrayConversion(condExpr);
Sebastian Redlf05b1522009-01-16 23:28:06 +0000520 Cond = condExpr;
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000521 QualType condType = condExpr->getType();
Sebastian Redlf05b1522009-01-16 23:28:06 +0000522
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000523 if (getLangOptions().CPlusPlus) {
524 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redlf05b1522009-01-16 23:28:06 +0000525 return StmtError();
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000526 } else if (!condType->isScalarType()) // C99 6.8.5p2
Sebastian Redlf05b1522009-01-16 23:28:06 +0000527 return StmtError(Diag(WhileLoc,
528 diag::err_typecheck_statement_requires_scalar)
529 << condType << condExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000530
Sebastian Redlf05b1522009-01-16 23:28:06 +0000531 Cond.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000532 return Owned(new (Context) WhileStmt(condExpr, (Stmt*)Body.release(),
533 WhileLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000534}
535
Sebastian Redlf05b1522009-01-16 23:28:06 +0000536Action::OwningStmtResult
537Sema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body,
538 SourceLocation WhileLoc, ExprArg Cond) {
539 Expr *condExpr = (Expr *)Cond.release();
Steve Naroff1b273c42007-09-16 14:56:35 +0000540 assert(condExpr && "ActOnDoStmt(): missing expression");
Sebastian Redlf05b1522009-01-16 23:28:06 +0000541
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000542 DefaultFunctionArrayConversion(condExpr);
Sebastian Redlf05b1522009-01-16 23:28:06 +0000543 Cond = condExpr;
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000544 QualType condType = condExpr->getType();
Sebastian Redlf05b1522009-01-16 23:28:06 +0000545
Argyrios Kyrtzidis6314ff22008-09-11 05:16:22 +0000546 if (getLangOptions().CPlusPlus) {
547 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redlf05b1522009-01-16 23:28:06 +0000548 return StmtError();
Argyrios Kyrtzidis6314ff22008-09-11 05:16:22 +0000549 } else if (!condType->isScalarType()) // C99 6.8.5p2
Sebastian Redlf05b1522009-01-16 23:28:06 +0000550 return StmtError(Diag(DoLoc, diag::err_typecheck_statement_requires_scalar)
551 << condType << condExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000552
Sebastian Redlf05b1522009-01-16 23:28:06 +0000553 Cond.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000554 return Owned(new (Context) DoStmt((Stmt*)Body.release(), condExpr, DoLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000555}
556
Sebastian Redlf05b1522009-01-16 23:28:06 +0000557Action::OwningStmtResult
558Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
559 StmtArg first, ExprArg second, ExprArg third,
560 SourceLocation RParenLoc, StmtArg body) {
561 Stmt *First = static_cast<Stmt*>(first.get());
562 Expr *Second = static_cast<Expr*>(second.get());
563 Expr *Third = static_cast<Expr*>(third.get());
564 Stmt *Body = static_cast<Stmt*>(body.get());
565
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000566 if (!getLangOptions().CPlusPlus) {
567 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000568 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
569 // declare identifiers for objects having storage class 'auto' or
570 // 'register'.
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000571 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
572 DI!=DE; ++DI) {
573 VarDecl *VD = dyn_cast<VarDecl>(*DI);
574 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
575 VD = 0;
576 if (VD == 0)
577 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
578 // FIXME: mark decl erroneous!
579 }
Chris Lattnerae3b7012007-08-28 05:03:08 +0000580 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 }
582 if (Second) {
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000583 DefaultFunctionArrayConversion(Second);
584 QualType SecondType = Second->getType();
Sebastian Redlf05b1522009-01-16 23:28:06 +0000585
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000586 if (getLangOptions().CPlusPlus) {
587 if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4
Sebastian Redlf05b1522009-01-16 23:28:06 +0000588 return StmtError();
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000589 } else if (!SecondType->isScalarType()) // C99 6.8.5p2
Sebastian Redlf05b1522009-01-16 23:28:06 +0000590 return StmtError(Diag(ForLoc,
591 diag::err_typecheck_statement_requires_scalar)
592 << SecondType << Second->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 }
Sebastian Redlf05b1522009-01-16 23:28:06 +0000594 first.release();
595 second.release();
596 third.release();
597 body.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000598 return Owned(new (Context) ForStmt(First, Second, Third, Body, ForLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000599}
600
Sebastian Redlf05b1522009-01-16 23:28:06 +0000601Action::OwningStmtResult
602Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
603 SourceLocation LParenLoc,
604 StmtArg first, ExprArg second,
605 SourceLocation RParenLoc, StmtArg body) {
606 Stmt *First = static_cast<Stmt*>(first.get());
607 Expr *Second = static_cast<Expr*>(second.get());
608 Stmt *Body = static_cast<Stmt*>(body.get());
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000609 if (First) {
610 QualType FirstType;
611 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
Chris Lattner7e24e822009-03-28 06:33:19 +0000612 if (!DS->isSingleDecl())
Sebastian Redlf05b1522009-01-16 23:28:06 +0000613 return StmtError(Diag((*DS->decl_begin())->getLocation(),
614 diag::err_toomany_element_decls));
615
Chris Lattner7e24e822009-03-28 06:33:19 +0000616 Decl *D = DS->getSingleDecl();
Ted Kremenekf34afee2008-10-06 20:58:11 +0000617 FirstType = cast<ValueDecl>(D)->getType();
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000618 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
619 // declare identifiers for objects having storage class 'auto' or
620 // 'register'.
Steve Naroff248a7532008-04-15 22:42:06 +0000621 VarDecl *VD = cast<VarDecl>(D);
622 if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
Sebastian Redlf05b1522009-01-16 23:28:06 +0000623 return StmtError(Diag(VD->getLocation(),
624 diag::err_non_variable_decl_in_for));
Anders Carlsson1fe379f2008-08-25 18:16:36 +0000625 } else {
Chris Lattner810f6d52009-03-13 17:38:01 +0000626 if (cast<Expr>(First)->isLvalue(Context) != Expr::LV_Valid)
Sebastian Redlf05b1522009-01-16 23:28:06 +0000627 return StmtError(Diag(First->getLocStart(),
628 diag::err_selector_element_not_lvalue)
629 << First->getSourceRange());
630
631 FirstType = static_cast<Expr*>(First)->getType();
Anders Carlsson1fe379f2008-08-25 18:16:36 +0000632 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000633 if (!Context.isObjCObjectPointerType(FirstType))
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000634 Diag(ForLoc, diag::err_selector_element_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000635 << FirstType << First->getSourceRange();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000636 }
637 if (Second) {
638 DefaultFunctionArrayConversion(Second);
639 QualType SecondType = Second->getType();
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000640 if (!Context.isObjCObjectPointerType(SecondType))
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000641 Diag(ForLoc, diag::err_collection_expr_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000642 << SecondType << Second->getSourceRange();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000643 }
Sebastian Redlf05b1522009-01-16 23:28:06 +0000644 first.release();
645 second.release();
646 body.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000647 return Owned(new (Context) ObjCForCollectionStmt(First, Second, Body,
648 ForLoc, RParenLoc));
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000649}
Reid Spencer5f016e22007-07-11 17:01:13 +0000650
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000651Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000652Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 IdentifierInfo *LabelII) {
Steve Naroff4eb206b2008-09-03 18:15:37 +0000654 // If we are in a block, reject all gotos for now.
655 if (CurBlock)
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000656 return StmtError(Diag(GotoLoc, diag::err_goto_in_block));
Steve Naroff4eb206b2008-09-03 18:15:37 +0000657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 // Look up the record for this label identifier.
Chris Lattnerea29a3a2009-04-18 20:01:55 +0000659 LabelStmt *&LabelDecl = getLabelMap()[LabelII];
Reid Spencer5f016e22007-07-11 17:01:13 +0000660
Steve Naroffcaaacec2009-03-13 15:38:40 +0000661 // If we haven't seen this label yet, create a forward reference.
662 if (LabelDecl == 0)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000663 LabelDecl = new (Context) LabelStmt(LabelLoc, LabelII, 0);
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000664
Ted Kremenek8189cde2009-02-07 01:47:29 +0000665 return Owned(new (Context) GotoStmt(LabelDecl, GotoLoc, LabelLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000666}
667
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000668Action::OwningStmtResult
Chris Lattnerad56d682009-04-19 01:04:21 +0000669Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc,
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000670 ExprArg DestExp) {
Eli Friedmanbbf46232009-03-26 00:18:06 +0000671 // Convert operand to void*
Eli Friedman33083822009-03-26 07:32:37 +0000672 Expr* E = DestExp.takeAs<Expr>();
673 QualType ETy = E->getType();
674 AssignConvertType ConvTy =
675 CheckSingleAssignmentConstraints(Context.VoidPtrTy, E);
676 if (DiagnoseAssignmentResult(ConvTy, StarLoc, Context.VoidPtrTy, ETy,
677 E, "passing"))
678 return StmtError();
Chris Lattnerad56d682009-04-19 01:04:21 +0000679 return Owned(new (Context) IndirectGotoStmt(GotoLoc, E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000680}
681
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000682Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000683Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 Scope *S = CurScope->getContinueParent();
685 if (!S) {
686 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000687 return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000689
Ted Kremenek8189cde2009-02-07 01:47:29 +0000690 return Owned(new (Context) ContinueStmt(ContinueLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000691}
692
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000693Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000694Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 Scope *S = CurScope->getBreakParent();
696 if (!S) {
697 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000698 return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000700
Ted Kremenek8189cde2009-02-07 01:47:29 +0000701 return Owned(new (Context) BreakStmt(BreakLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000702}
703
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000704/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000705///
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000706Action::OwningStmtResult
Steve Naroff4eb206b2008-09-03 18:15:37 +0000707Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000708
Steve Naroff4eb206b2008-09-03 18:15:37 +0000709 // If this is the first return we've seen in the block, infer the type of
710 // the block from it.
711 if (CurBlock->ReturnType == 0) {
Steve Naroffc50a4a52008-09-16 22:25:10 +0000712 if (RetValExp) {
Steve Naroff16564422008-09-24 22:26:48 +0000713 // Don't call UsualUnaryConversions(), since we don't want to do
714 // integer promotions here.
715 DefaultFunctionArrayConversion(RetValExp);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000716 CurBlock->ReturnType = RetValExp->getType().getTypePtr();
Steve Naroffc50a4a52008-09-16 22:25:10 +0000717 } else
Steve Naroff4eb206b2008-09-03 18:15:37 +0000718 CurBlock->ReturnType = Context.VoidTy.getTypePtr();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000719 }
Mike Stump98eb8a72009-02-04 22:31:32 +0000720 QualType FnRetType = QualType(CurBlock->ReturnType, 0);
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000721
Steve Naroff4eb206b2008-09-03 18:15:37 +0000722 // Otherwise, verify that this result type matches the previous one. We are
723 // pickier with blocks than for normal functions because we don't have GCC
724 // compatibility to worry about here.
725 if (CurBlock->ReturnType->isVoidType()) {
726 if (RetValExp) {
727 Diag(ReturnLoc, diag::err_return_block_has_expr);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000728 RetValExp->Destroy(Context);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000729 RetValExp = 0;
730 }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000731 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Steve Naroff4eb206b2008-09-03 18:15:37 +0000732 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000733
734 if (!RetValExp)
735 return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr));
736
Mike Stump98eb8a72009-02-04 22:31:32 +0000737 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
738 // we have a non-void block with an expression, continue checking
739 QualType RetValType = RetValExp->getType();
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000740
Mike Stump98eb8a72009-02-04 22:31:32 +0000741 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
742 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
743 // function return.
744
745 // In C++ the return statement is handled via a copy initialization.
746 // the C version of which boils down to CheckSingleAssignmentConstraints.
747 // FIXME: Leaks RetValExp.
748 if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
749 return StmtError();
750
751 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000752 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000753
Ted Kremenek8189cde2009-02-07 01:47:29 +0000754 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Steve Naroff4eb206b2008-09-03 18:15:37 +0000755}
Reid Spencer5f016e22007-07-11 17:01:13 +0000756
Sebastian Redle2b68332009-04-12 17:16:29 +0000757/// IsReturnCopyElidable - Whether returning @p RetExpr from a function that
758/// returns a @p RetType fulfills the criteria for copy elision (C++0x 12.8p15).
759static bool IsReturnCopyElidable(ASTContext &Ctx, QualType RetType,
760 Expr *RetExpr) {
761 QualType ExprType = RetExpr->getType();
762 // - in a return statement in a function with ...
763 // ... a class return type ...
764 if (!RetType->isRecordType())
765 return false;
766 // ... the same cv-unqualified type as the function return type ...
767 if (Ctx.getCanonicalType(RetType).getUnqualifiedType() !=
768 Ctx.getCanonicalType(ExprType).getUnqualifiedType())
769 return false;
770 // ... the expression is the name of a non-volatile automatic object ...
771 // We ignore parentheses here.
772 // FIXME: Is this compliant?
773 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetExpr->IgnoreParens());
774 if (!DR)
775 return false;
776 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
777 if (!VD)
778 return false;
779 return VD->hasLocalStorage() && !VD->getType()->isReferenceType()
780 && !VD->getType().isVolatileQualified();
781}
782
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000783Action::OwningStmtResult
784Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) {
785 Expr *RetValExp = static_cast<Expr *>(rex.release());
Steve Naroff4eb206b2008-09-03 18:15:37 +0000786 if (CurBlock)
787 return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000788
Chris Lattner371f2582008-12-04 23:50:19 +0000789 QualType FnRetType;
790 if (FunctionDecl *FD = getCurFunctionDecl())
791 FnRetType = FD->getResultType();
Steve Naroffc97fb9a2009-03-03 00:45:38 +0000792 else if (ObjCMethodDecl *MD = getCurMethodDecl())
793 FnRetType = MD->getResultType();
794 else // If we don't have a function/method context, bail.
795 return StmtError();
796
Chris Lattner5cf216b2008-01-04 18:04:52 +0000797 if (FnRetType->isVoidType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000798 if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns)
Chris Lattner65ce04b2008-12-18 02:01:17 +0000799 unsigned D = diag::ext_return_has_expr;
800 if (RetValExp->getType()->isVoidType())
801 D = diag::ext_return_has_void_expr;
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000802
Chris Lattnere878eb02008-12-18 02:03:48 +0000803 // return (some void expression); is legal in C++.
804 if (D != diag::ext_return_has_void_expr ||
805 !getLangOptions().CPlusPlus) {
806 NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
807 Diag(ReturnLoc, D)
808 << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
809 << RetValExp->getSourceRange();
810 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000812 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000814
Chris Lattner3c73c412008-11-19 08:23:25 +0000815 if (!RetValExp) {
816 unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4
817 // C99 6.8.6.4p1 (ext_ since GCC warns)
818 if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
819
820 if (FunctionDecl *FD = getCurFunctionDecl())
Chris Lattner08631c52008-11-23 21:45:46 +0000821 Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
Chris Lattner3c73c412008-11-19 08:23:25 +0000822 else
Chris Lattner08631c52008-11-23 21:45:46 +0000823 Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000824 return Owned(new (Context) ReturnStmt(ReturnLoc, (Expr*)0));
Chris Lattner3c73c412008-11-19 08:23:25 +0000825 }
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000826
Douglas Gregor898574e2008-12-05 23:32:09 +0000827 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
828 // we have a non-void function with an expression, continue checking
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000829
Douglas Gregor898574e2008-12-05 23:32:09 +0000830 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
831 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000832 // function return.
833
Sebastian Redle2b68332009-04-12 17:16:29 +0000834 // C++0x 12.8p15: When certain criteria are met, an implementation is
835 // allowed to omit the copy construction of a class object, [...]
836 // - in a return statement in a function with a class return type, when
837 // the expression is the name of a non-volatile automatic object with
838 // the same cv-unqualified type as the function return type, the copy
839 // operation can be omitted [...]
840 // C++0x 12.8p16: When the criteria for elision of a copy operation are met
841 // and the object to be copied is designated by an lvalue, overload
842 // resolution to select the constructor for the copy is first performed
843 // as if the object were designated by an rvalue.
844 // Note that we only compute Elidable if we're in C++0x, since we don't
845 // care otherwise.
846 bool Elidable = getLangOptions().CPlusPlus0x ?
847 IsReturnCopyElidable(Context, FnRetType, RetValExp) :
848 false;
849
Douglas Gregor898574e2008-12-05 23:32:09 +0000850 // In C++ the return statement is handled via a copy initialization.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000851 // the C version of which boils down to CheckSingleAssignmentConstraints.
Sebastian Redle2b68332009-04-12 17:16:29 +0000852 // FIXME: Leaks RetValExp on error.
853 if (PerformCopyInitialization(RetValExp, FnRetType, "returning", Elidable))
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000854 return StmtError();
855
Douglas Gregor898574e2008-12-05 23:32:09 +0000856 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
857 }
858
Ted Kremenek8189cde2009-02-07 01:47:29 +0000859 return Owned(new (Context) ReturnStmt(ReturnLoc, RetValExp));
Reid Spencer5f016e22007-07-11 17:01:13 +0000860}
861
Chris Lattner810f6d52009-03-13 17:38:01 +0000862/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently
863/// ignore "noop" casts in places where an lvalue is required by an inline asm.
864/// We emulate this behavior when -fheinous-gnu-extensions is specified, but
865/// provide a strong guidance to not use it.
866///
867/// This method checks to see if the argument is an acceptable l-value and
868/// returns false if it is a case we can handle.
869static bool CheckAsmLValue(const Expr *E, Sema &S) {
870 if (E->isLvalue(S.Context) == Expr::LV_Valid)
871 return false; // Cool, this is an lvalue.
872
873 // Okay, this is not an lvalue, but perhaps it is the result of a cast that we
874 // are supposed to allow.
875 const Expr *E2 = E->IgnoreParenNoopCasts(S.Context);
876 if (E != E2 && E2->isLvalue(S.Context) == Expr::LV_Valid) {
877 if (!S.getLangOptions().HeinousExtensions)
878 S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue)
879 << E->getSourceRange();
880 else
881 S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue)
882 << E->getSourceRange();
883 // Accept, even if we emitted an error diagnostic.
884 return false;
885 }
886
887 // None of the above, just randomly invalid non-lvalue.
888 return true;
889}
890
891
Sebastian Redl3037ed02009-01-18 16:53:17 +0000892Sema::OwningStmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
893 bool IsSimple,
894 bool IsVolatile,
895 unsigned NumOutputs,
896 unsigned NumInputs,
897 std::string *Names,
898 MultiExprArg constraints,
899 MultiExprArg exprs,
900 ExprArg asmString,
901 MultiExprArg clobbers,
902 SourceLocation RParenLoc) {
903 unsigned NumClobbers = clobbers.size();
904 StringLiteral **Constraints =
905 reinterpret_cast<StringLiteral**>(constraints.get());
906 Expr **Exprs = reinterpret_cast<Expr **>(exprs.get());
907 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString.get());
908 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get());
909
Anders Carlsson03eb5432009-01-27 20:38:24 +0000910 llvm::SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
911
Chris Lattner1708b962008-08-18 19:55:17 +0000912 // The parser verifies that there is a string literal here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000913 if (AsmString->isWide())
Sebastian Redl3037ed02009-01-18 16:53:17 +0000914 return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
915 << AsmString->getSourceRange());
916
917
Chris Lattner1708b962008-08-18 19:55:17 +0000918 for (unsigned i = 0; i != NumOutputs; i++) {
919 StringLiteral *Literal = Constraints[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000920 if (Literal->isWide())
Sebastian Redl3037ed02009-01-18 16:53:17 +0000921 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
922 << Literal->getSourceRange());
923
Anders Carlssond04c6e22007-11-27 04:11:28 +0000924 std::string OutputConstraint(Literal->getStrData(),
925 Literal->getByteLength());
Sebastian Redl3037ed02009-01-18 16:53:17 +0000926
Anders Carlssond04c6e22007-11-27 04:11:28 +0000927 TargetInfo::ConstraintInfo info;
Chris Lattner6bc52112008-07-23 06:46:56 +0000928 if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info))
Sebastian Redl3037ed02009-01-18 16:53:17 +0000929 return StmtError(Diag(Literal->getLocStart(),
930 diag::err_asm_invalid_output_constraint) << OutputConstraint);
931
Anders Carlssond04c6e22007-11-27 04:11:28 +0000932 // Check that the output exprs are valid lvalues.
Chris Lattner1708b962008-08-18 19:55:17 +0000933 ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]);
Chris Lattner810f6d52009-03-13 17:38:01 +0000934 if (CheckAsmLValue(OutputExpr, *this)) {
Sebastian Redl3037ed02009-01-18 16:53:17 +0000935 return StmtError(Diag(OutputExpr->getSubExpr()->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000936 diag::err_asm_invalid_lvalue_in_output)
Sebastian Redl3037ed02009-01-18 16:53:17 +0000937 << OutputExpr->getSubExpr()->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000938 }
Anders Carlsson03eb5432009-01-27 20:38:24 +0000939
940 OutputConstraintInfos.push_back(info);
Anders Carlsson04728b72007-11-23 19:43:50 +0000941 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000942
Anders Carlsson04728b72007-11-23 19:43:50 +0000943 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
Chris Lattner1708b962008-08-18 19:55:17 +0000944 StringLiteral *Literal = Constraints[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000945 if (Literal->isWide())
Sebastian Redl3037ed02009-01-18 16:53:17 +0000946 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
947 << Literal->getSourceRange());
948
949 std::string InputConstraint(Literal->getStrData(),
Anders Carlssond04c6e22007-11-27 04:11:28 +0000950 Literal->getByteLength());
Sebastian Redl3037ed02009-01-18 16:53:17 +0000951
Anders Carlssond04c6e22007-11-27 04:11:28 +0000952 TargetInfo::ConstraintInfo info;
953 if (!Context.Target.validateInputConstraint(InputConstraint.c_str(),
Anders Carlsson45b050e2009-01-17 23:36:15 +0000954 &Names[0],
Anders Carlsson03eb5432009-01-27 20:38:24 +0000955 &Names[0] + NumOutputs,
956 &OutputConstraintInfos[0],
957 info)) {
Sebastian Redl3037ed02009-01-18 16:53:17 +0000958 return StmtError(Diag(Literal->getLocStart(),
959 diag::err_asm_invalid_input_constraint) << InputConstraint);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000960 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000961
Chris Lattner1708b962008-08-18 19:55:17 +0000962 ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]);
Sebastian Redl3037ed02009-01-18 16:53:17 +0000963
Anders Carlssond9fca6e2009-01-20 20:49:22 +0000964 // Only allow void types for memory constraints.
Anders Carlssone6ea2792009-01-21 06:27:20 +0000965 if ((info & TargetInfo::CI_AllowsMemory)
966 && !(info & TargetInfo::CI_AllowsRegister)) {
Chris Lattner810f6d52009-03-13 17:38:01 +0000967 if (CheckAsmLValue(InputExpr, *this))
Anders Carlssond9fca6e2009-01-20 20:49:22 +0000968 return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(),
969 diag::err_asm_invalid_lvalue_in_input)
970 << InputConstraint << InputExpr->getSubExpr()->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000971 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000972
Anders Carlssond9fca6e2009-01-20 20:49:22 +0000973 if (info & TargetInfo::CI_AllowsRegister) {
974 if (InputExpr->getType()->isVoidType()) {
975 return StmtError(Diag(InputExpr->getSubExpr()->getLocStart(),
976 diag::err_asm_invalid_type_in_input)
977 << InputExpr->getType() << InputConstraint
978 << InputExpr->getSubExpr()->getSourceRange());
979 }
Anders Carlssond9fca6e2009-01-20 20:49:22 +0000980 }
Anders Carlsson60329792009-02-22 02:11:23 +0000981
982 DefaultFunctionArrayConversion(Exprs[i]);
Anders Carlsson04728b72007-11-23 19:43:50 +0000983 }
Sebastian Redl3037ed02009-01-18 16:53:17 +0000984
Anders Carlsson6fa90862007-11-25 00:25:21 +0000985 // Check that the clobbers are valid.
Chris Lattner1708b962008-08-18 19:55:17 +0000986 for (unsigned i = 0; i != NumClobbers; i++) {
987 StringLiteral *Literal = Clobbers[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000988 if (Literal->isWide())
Sebastian Redl3037ed02009-01-18 16:53:17 +0000989 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
990 << Literal->getSourceRange());
991
992 llvm::SmallString<16> Clobber(Literal->getStrData(),
993 Literal->getStrData() +
Anders Carlsson6fa90862007-11-25 00:25:21 +0000994 Literal->getByteLength());
Sebastian Redl3037ed02009-01-18 16:53:17 +0000995
Chris Lattner6bc52112008-07-23 06:46:56 +0000996 if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
Sebastian Redl3037ed02009-01-18 16:53:17 +0000997 return StmtError(Diag(Literal->getLocStart(),
998 diag::err_asm_unknown_register_name) << Clobber.c_str());
Anders Carlsson6fa90862007-11-25 00:25:21 +0000999 }
Sebastian Redl3037ed02009-01-18 16:53:17 +00001000
1001 constraints.release();
1002 exprs.release();
1003 asmString.release();
1004 clobbers.release();
Chris Lattnerfb5058e2009-03-10 23:41:04 +00001005 AsmStmt *NS =
1006 new (Context) AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs,
1007 Names, Constraints, Exprs, AsmString, NumClobbers,
1008 Clobbers, RParenLoc);
1009 // Validate the asm string, ensuring it makes sense given the operands we
1010 // have.
1011 llvm::SmallVector<AsmStmt::AsmStringPiece, 8> Pieces;
1012 unsigned DiagOffs;
1013 if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
Chris Lattner2ff0f422009-03-10 23:57:07 +00001014 Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
1015 << AsmString->getSourceRange();
Chris Lattnerfb5058e2009-03-10 23:41:04 +00001016 DeleteStmt(NS);
1017 return StmtError();
1018 }
1019
1020
1021 return Owned(NS);
Chris Lattnerfe795952007-10-29 04:04:16 +00001022}
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001023
Sebastian Redl431e90e2009-01-18 17:43:11 +00001024Action::OwningStmtResult
1025Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001026 SourceLocation RParen, DeclPtrTy Parm,
Sebastian Redl431e90e2009-01-18 17:43:11 +00001027 StmtArg Body, StmtArg catchList) {
1028 Stmt *CatchList = static_cast<Stmt*>(catchList.release());
Chris Lattnerb28317a2009-03-28 19:18:32 +00001029 ParmVarDecl *PVD = cast_or_null<ParmVarDecl>(Parm.getAs<Decl>());
Steve Narofff50cb362009-03-03 20:59:06 +00001030
1031 // PVD == 0 implies @catch(...).
Steve Naroff9d40ee52009-03-03 21:16:54 +00001032 if (PVD) {
Chris Lattner93c49452009-04-12 23:26:56 +00001033 // If we already know the decl is invalid, reject it.
1034 if (PVD->isInvalidDecl())
1035 return StmtError();
1036
Steve Naroff9d40ee52009-03-03 21:16:54 +00001037 if (!Context.isObjCObjectPointerType(PVD->getType()))
1038 return StmtError(Diag(PVD->getLocation(),
1039 diag::err_catch_param_not_objc_type));
1040 if (PVD->getType()->isObjCQualifiedIdType())
1041 return StmtError(Diag(PVD->getLocation(),
Steve Naroffd198aba2009-03-03 23:13:51 +00001042 diag::err_illegal_qualifiers_on_catch_parm));
Steve Naroff9d40ee52009-03-03 21:16:54 +00001043 }
Chris Lattner93c49452009-04-12 23:26:56 +00001044
Ted Kremenek8189cde2009-02-07 01:47:29 +00001045 ObjCAtCatchStmt *CS = new (Context) ObjCAtCatchStmt(AtLoc, RParen,
Steve Narofff50cb362009-03-03 20:59:06 +00001046 PVD, static_cast<Stmt*>(Body.release()), CatchList);
Sebastian Redl431e90e2009-01-18 17:43:11 +00001047 return Owned(CatchList ? CatchList : CS);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001048}
1049
Sebastian Redl431e90e2009-01-18 17:43:11 +00001050Action::OwningStmtResult
1051Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) {
Ted Kremenek8189cde2009-02-07 01:47:29 +00001052 return Owned(new (Context) ObjCAtFinallyStmt(AtLoc,
1053 static_cast<Stmt*>(Body.release())));
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001054}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001055
Sebastian Redl431e90e2009-01-18 17:43:11 +00001056Action::OwningStmtResult
1057Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
1058 StmtArg Try, StmtArg Catch, StmtArg Finally) {
Ted Kremenek8189cde2009-02-07 01:47:29 +00001059 return Owned(new (Context) ObjCAtTryStmt(AtLoc,
1060 static_cast<Stmt*>(Try.release()),
Sebastian Redl431e90e2009-01-18 17:43:11 +00001061 static_cast<Stmt*>(Catch.release()),
1062 static_cast<Stmt*>(Finally.release())));
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001063}
1064
Sebastian Redl431e90e2009-01-18 17:43:11 +00001065Action::OwningStmtResult
Steve Naroff3dcfe102009-02-12 15:54:59 +00001066Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg expr,Scope *CurScope) {
Steve Naroff7151bbb2009-02-11 17:45:08 +00001067 Expr *ThrowExpr = static_cast<Expr*>(expr.release());
1068 if (!ThrowExpr) {
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001069 // @throw without an expression designates a rethrow (which much occur
1070 // in the context of an @catch clause).
1071 Scope *AtCatchParent = CurScope;
1072 while (AtCatchParent && !AtCatchParent->isAtCatchScope())
1073 AtCatchParent = AtCatchParent->getParent();
1074 if (!AtCatchParent)
Steve Naroff4ab24142009-02-12 18:09:32 +00001075 return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch));
Steve Naroff7151bbb2009-02-11 17:45:08 +00001076 } else {
1077 QualType ThrowType = ThrowExpr->getType();
1078 // Make sure the expression type is an ObjC pointer or "void *".
1079 if (!Context.isObjCObjectPointerType(ThrowType)) {
1080 const PointerType *PT = ThrowType->getAsPointerType();
1081 if (!PT || !PT->getPointeeType()->isVoidType())
Steve Naroff4ab24142009-02-12 18:09:32 +00001082 return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object)
1083 << ThrowExpr->getType() << ThrowExpr->getSourceRange());
Steve Naroff7151bbb2009-02-11 17:45:08 +00001084 }
1085 }
1086 return Owned(new (Context) ObjCAtThrowStmt(AtLoc, ThrowExpr));
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001087}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001088
Sebastian Redl431e90e2009-01-18 17:43:11 +00001089Action::OwningStmtResult
1090Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr,
1091 StmtArg SynchBody) {
Ted Kremenek8189cde2009-02-07 01:47:29 +00001092 return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc,
Sebastian Redl431e90e2009-01-18 17:43:11 +00001093 static_cast<Stmt*>(SynchExpr.release()),
1094 static_cast<Stmt*>(SynchBody.release())));
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001095}
Sebastian Redl4b07b292008-12-22 19:15:10 +00001096
1097/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
1098/// and creates a proper catch handler from them.
1099Action::OwningStmtResult
Chris Lattnerb28317a2009-03-28 19:18:32 +00001100Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclPtrTy ExDecl,
Sebastian Redl4b07b292008-12-22 19:15:10 +00001101 StmtArg HandlerBlock) {
1102 // There's nothing to test that ActOnExceptionDecl didn't already test.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001103 return Owned(new (Context) CXXCatchStmt(CatchLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001104 cast_or_null<VarDecl>(ExDecl.getAs<Decl>()),
Ted Kremenek8189cde2009-02-07 01:47:29 +00001105 static_cast<Stmt*>(HandlerBlock.release())));
Sebastian Redl4b07b292008-12-22 19:15:10 +00001106}
Sebastian Redl8351da02008-12-22 21:35:02 +00001107
1108/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
1109/// handlers and creates a try statement from them.
1110Action::OwningStmtResult
1111Sema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock,
1112 MultiStmtArg RawHandlers) {
1113 unsigned NumHandlers = RawHandlers.size();
1114 assert(NumHandlers > 0 &&
1115 "The parser shouldn't call this if there are no handlers.");
1116 Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get());
1117
1118 for(unsigned i = 0; i < NumHandlers - 1; ++i) {
1119 CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
1120 if (!Handler->getExceptionDecl())
1121 return StmtError(Diag(Handler->getLocStart(), diag::err_early_catch_all));
1122 }
1123 // FIXME: We should detect handlers for the same type as an earlier one.
1124 // This one is rather easy.
1125 // FIXME: We should detect handlers that cannot catch anything because an
1126 // earlier handler catches a superclass. Need to find a method that is not
1127 // quadratic for this.
1128 // Neither of these are explicitly forbidden, but every compiler detects them
1129 // and warns.
1130
1131 RawHandlers.release();
Ted Kremenek8189cde2009-02-07 01:47:29 +00001132 return Owned(new (Context) CXXTryStmt(TryLoc,
1133 static_cast<Stmt*>(TryBlock.release()),
1134 Handlers, NumHandlers));
Sebastian Redl8351da02008-12-22 21:35:02 +00001135}