blob: 34f9f65cd7a4d488be9b47c1e1ffa256c770f489 [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"
Anders Carlsson6fa90862007-11-25 00:25:21 +000019#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/Diagnostic.h"
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) {
37 return Owned(new NullStmt(SemiLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +000038}
39
Sebastian Redla60528c2008-12-21 12:04:03 +000040Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclTy *decl,
41 SourceLocation StartLoc,
42 SourceLocation EndLoc) {
Chris Lattner81c018d2008-03-13 06:29:04 +000043 if (decl == 0)
Sebastian Redla60528c2008-12-21 12:04:03 +000044 return StmtError();
45
Chris Lattner81c018d2008-03-13 06:29:04 +000046 ScopedDecl *SD = cast<ScopedDecl>(static_cast<Decl *>(decl));
Sebastian Redla60528c2008-12-21 12:04:03 +000047
Ted Kremenek8ffb1592008-10-07 23:09:49 +000048 // This is a temporary hack until we are always passing around
49 // DeclGroupRefs.
50 llvm::SmallVector<Decl*, 10> decls;
51 while (SD) {
52 ScopedDecl* d = SD;
53 SD = SD->getNextDeclarator();
54 d->setNextDeclarator(0);
55 decls.push_back(d);
56 }
57
58 assert (!decls.empty());
Sebastian Redla60528c2008-12-21 12:04:03 +000059
Ted Kremenek8ffb1592008-10-07 23:09:49 +000060 if (decls.size() == 1) {
61 DeclGroupOwningRef DG(*decls.begin());
Sebastian Redla60528c2008-12-21 12:04:03 +000062 return Owned(new DeclStmt(DG, StartLoc, EndLoc));
Ted Kremenek8ffb1592008-10-07 23:09:49 +000063 }
64 else {
Chris Lattner08631c52008-11-23 21:45:46 +000065 DeclGroupOwningRef DG(DeclGroup::Create(Context, decls.size(), &decls[0]));
Sebastian Redla60528c2008-12-21 12:04:03 +000066 return Owned(new DeclStmt(DG, StartLoc, EndLoc));
Ted Kremenek8ffb1592008-10-07 23:09:49 +000067 }
Reid Spencer5f016e22007-07-11 17:01:13 +000068}
69
Sebastian Redla60528c2008-12-21 12:04:03 +000070Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +000071Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Sebastian Redla60528c2008-12-21 12:04:03 +000072 MultiStmtArg elts, bool isStmtExpr) {
73 unsigned NumElts = elts.size();
74 Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000075 // If we're in C89 mode, check that we don't have any decls after stmts. If
76 // so, emit an extension diagnostic.
77 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
78 // Note that __extension__ can be around a decl.
79 unsigned i = 0;
80 // Skip over all declarations.
81 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
82 /*empty*/;
83
84 // We found the end of the list or a statement. Scan for another declstmt.
85 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
86 /*empty*/;
87
88 if (i != NumElts) {
Ted Kremenek1bddf7e2008-10-06 18:48:35 +000089 ScopedDecl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000090 Diag(D->getLocation(), diag::ext_mixed_decls_code);
91 }
92 }
Chris Lattner98414c12007-08-31 21:49:55 +000093 // Warn about unused expressions in statements.
94 for (unsigned i = 0; i != NumElts; ++i) {
95 Expr *E = dyn_cast<Expr>(Elts[i]);
96 if (!E) continue;
97
98 // Warn about expressions with unused results.
99 if (E->hasLocalSideEffect() || E->getType()->isVoidType())
100 continue;
101
102 // The last expr in a stmt expr really is used.
103 if (isStmtExpr && i == NumElts-1)
104 continue;
105
106 /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in
107 /// a context where the result is unused. Emit a diagnostic to warn about
108 /// this.
109 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000110 Diag(BO->getOperatorLoc(), diag::warn_unused_expr)
111 << BO->getLHS()->getSourceRange() << BO->getRHS()->getSourceRange();
Chris Lattner98414c12007-08-31 21:49:55 +0000112 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000113 Diag(UO->getOperatorLoc(), diag::warn_unused_expr)
114 << UO->getSubExpr()->getSourceRange();
Sebastian Redla60528c2008-12-21 12:04:03 +0000115 else
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000116 Diag(E->getExprLoc(), diag::warn_unused_expr) << E->getSourceRange();
Chris Lattner98414c12007-08-31 21:49:55 +0000117 }
Sebastian Redla60528c2008-12-21 12:04:03 +0000118
119 return Owned(new CompoundStmt(Elts, NumElts, L, R));
Reid Spencer5f016e22007-07-11 17:01:13 +0000120}
121
Sebastian Redl117054a2008-12-28 16:13:43 +0000122Action::OwningStmtResult
123Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval,
124 SourceLocation DotDotDotLoc, ExprArg rhsval,
125 SourceLocation ColonLoc, StmtArg subStmt) {
126 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
127 assert((lhsval.get() != 0) && "missing expression in case statement");
128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 // C99 6.8.4.2p3: The expression shall be an integer constant.
Anders Carlsson51fe9962008-11-22 21:04:56 +0000130 // However, GCC allows any evaluatable integer expression.
Anders Carlssond3a61d52008-12-01 02:13:02 +0000131
Sebastian Redl117054a2008-12-28 16:13:43 +0000132 Expr *LHSVal = static_cast<Expr*>(lhsval.get());
Anders Carlssond3a61d52008-12-01 02:13:02 +0000133 if (VerifyIntegerConstantExpression(LHSVal))
Sebastian Redl117054a2008-12-28 16:13:43 +0000134 return Owned(SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000135
Chris Lattner6c36be52007-07-18 02:28:47 +0000136 // GCC extension: The expression shall be an integer constant.
Sebastian Redl117054a2008-12-28 16:13:43 +0000137
138 Expr *RHSVal = static_cast<Expr*>(rhsval.get());
139 if (RHSVal && VerifyIntegerConstantExpression(RHSVal)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000140 RHSVal = 0; // Recover by just forgetting about it.
Sebastian Redl117054a2008-12-28 16:13:43 +0000141 rhsval = 0;
142 }
143
Chris Lattner8a87e572007-07-23 17:05:23 +0000144 if (SwitchStack.empty()) {
145 Diag(CaseLoc, diag::err_case_not_in_switch);
Sebastian Redl117054a2008-12-28 16:13:43 +0000146 return Owned(SubStmt);
Chris Lattner8a87e572007-07-23 17:05:23 +0000147 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000148
Sebastian Redl117054a2008-12-28 16:13:43 +0000149 // Only now release the smart pointers.
150 lhsval.release();
151 rhsval.release();
Steve Naroffb5a69582007-08-31 23:28:33 +0000152 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner8a87e572007-07-23 17:05:23 +0000153 SwitchStack.back()->addSwitchCase(CS);
Sebastian Redl117054a2008-12-28 16:13:43 +0000154 return Owned(CS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000155}
156
Sebastian Redl117054a2008-12-28 16:13:43 +0000157Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000158Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Sebastian Redl117054a2008-12-28 16:13:43 +0000159 StmtArg subStmt, Scope *CurScope) {
160 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
161
Chris Lattner8a87e572007-07-23 17:05:23 +0000162 if (SwitchStack.empty()) {
Chris Lattner0fa152e2007-07-21 03:00:26 +0000163 Diag(DefaultLoc, diag::err_default_not_in_switch);
Sebastian Redl117054a2008-12-28 16:13:43 +0000164 return Owned(SubStmt);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000165 }
Sebastian Redl117054a2008-12-28 16:13:43 +0000166
Chris Lattner0fa152e2007-07-21 03:00:26 +0000167 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner8a87e572007-07-23 17:05:23 +0000168 SwitchStack.back()->addSwitchCase(DS);
Sebastian Redl117054a2008-12-28 16:13:43 +0000169 return Owned(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000170}
171
Sebastian Redlde307472009-01-11 00:38:46 +0000172Action::OwningStmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000173Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Sebastian Redlde307472009-01-11 00:38:46 +0000174 SourceLocation ColonLoc, StmtArg subStmt) {
175 Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 // Look up the record for this label identifier.
177 LabelStmt *&LabelDecl = LabelMap[II];
Sebastian Redlde307472009-01-11 00:38:46 +0000178
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 // If not forward referenced or defined already, just create a new LabelStmt.
180 if (LabelDecl == 0)
Sebastian Redlde307472009-01-11 00:38:46 +0000181 return Owned(LabelDecl = new LabelStmt(IdentLoc, II, SubStmt));
182
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 assert(LabelDecl->getID() == II && "Label mismatch!");
Sebastian Redlde307472009-01-11 00:38:46 +0000184
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 // Otherwise, this label was either forward reference or multiply defined. If
186 // multiply defined, reject it now.
187 if (LabelDecl->getSubStmt()) {
Chris Lattner08631c52008-11-23 21:45:46 +0000188 Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000189 Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition);
Sebastian Redlde307472009-01-11 00:38:46 +0000190 return Owned(SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 }
Sebastian Redlde307472009-01-11 00:38:46 +0000192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 // Otherwise, this label was forward declared, and we just found its real
194 // definition. Fill in the forward definition and return it.
195 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000196 LabelDecl->setSubStmt(SubStmt);
Sebastian Redlde307472009-01-11 00:38:46 +0000197 return Owned(LabelDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000198}
199
Sebastian Redlde307472009-01-11 00:38:46 +0000200Action::OwningStmtResult
201Sema::ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal,
202 StmtArg ThenVal, SourceLocation ElseLoc,
203 StmtArg ElseVal) {
204 Expr *condExpr = (Expr *)CondVal.release();
205
Steve Naroff1b273c42007-09-16 14:56:35 +0000206 assert(condExpr && "ActOnIfStmt(): missing expression");
Sebastian Redlde307472009-01-11 00:38:46 +0000207
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000208 DefaultFunctionArrayConversion(condExpr);
Sebastian Redlde307472009-01-11 00:38:46 +0000209 // Take ownership again until we're past the error checking.
210 CondVal = condExpr;
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000211 QualType condType = condExpr->getType();
Sebastian Redlde307472009-01-11 00:38:46 +0000212
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000213 if (getLangOptions().CPlusPlus) {
214 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
Sebastian Redlde307472009-01-11 00:38:46 +0000215 return StmtError();
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000216 } else if (!condType->isScalarType()) // C99 6.8.4.1p1
Sebastian Redlde307472009-01-11 00:38:46 +0000217 return StmtError(Diag(IfLoc, diag::err_typecheck_statement_requires_scalar)
218 << condType << condExpr->getSourceRange());
219
220 Stmt *thenStmt = (Stmt *)ThenVal.release();
Reid Spencer5f016e22007-07-11 17:01:13 +0000221
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000222 // Warn if the if block has a null body without an else value.
223 // this helps prevent bugs due to typos, such as
224 // if (condition);
225 // do_stuff();
Sebastian Redlde307472009-01-11 00:38:46 +0000226 if (!ElseVal.get()) {
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000227 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
228 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
229 }
230
Sebastian Redlde307472009-01-11 00:38:46 +0000231 CondVal.release();
232 return Owned(new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000233}
234
Sebastian Redlde307472009-01-11 00:38:46 +0000235Action::OwningStmtResult
236Sema::ActOnStartOfSwitchStmt(ExprArg cond) {
237 Expr *Cond = static_cast<Expr*>(cond.release());
238
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000239 if (getLangOptions().CPlusPlus) {
240 // C++ 6.4.2.p2:
241 // The condition shall be of integral type, enumeration type, or of a class
242 // type for which a single conversion function to integral or enumeration
243 // type exists (12.3). If the condition is of class type, the condition is
244 // converted by calling that conversion function, and the result of the
245 // conversion is used in place of the original condition for the remainder
246 // of this section. Integral promotions are performed.
247
248 QualType Ty = Cond->getType();
249
250 // FIXME: Handle class types.
251
252 // If the type is wrong a diagnostic will be emitted later at
253 // ActOnFinishSwitchStmt.
254 if (Ty->isIntegralType() || Ty->isEnumeralType()) {
255 // Integral promotions are performed.
256 // FIXME: Integral promotions for C++ are not complete.
257 UsualUnaryConversions(Cond);
258 }
259 } else {
260 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
261 UsualUnaryConversions(Cond);
262 }
Sebastian Redlde307472009-01-11 00:38:46 +0000263
Chris Lattnerf4021e72007-08-23 05:46:52 +0000264 SwitchStmt *SS = new SwitchStmt(Cond);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000265 SwitchStack.push_back(SS);
Sebastian Redlde307472009-01-11 00:38:46 +0000266 return Owned(SS);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000267}
Chris Lattner6c36be52007-07-18 02:28:47 +0000268
Chris Lattnerf4021e72007-08-23 05:46:52 +0000269/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
270/// the specified width and sign. If an overflow occurs, detect it and emit
271/// the specified diagnostic.
272void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
273 unsigned NewWidth, bool NewSign,
274 SourceLocation Loc,
275 unsigned DiagID) {
276 // Perform a conversion to the promoted condition type if needed.
277 if (NewWidth > Val.getBitWidth()) {
278 // If this is an extension, just do it.
279 llvm::APSInt OldVal(Val);
280 Val.extend(NewWidth);
281
282 // If the input was signed and negative and the output is unsigned,
283 // warn.
284 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000285 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000286
287 Val.setIsSigned(NewSign);
288 } else if (NewWidth < Val.getBitWidth()) {
289 // If this is a truncation, check for overflow.
290 llvm::APSInt ConvVal(Val);
291 ConvVal.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000292 ConvVal.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000293 ConvVal.extend(Val.getBitWidth());
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000294 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000295 if (ConvVal != Val)
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000296 Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000297
298 // Regardless of whether a diagnostic was emitted, really do the
299 // truncation.
300 Val.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000301 Val.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000302 } else if (NewSign != Val.isSigned()) {
303 // Convert the sign to match the sign of the condition. This can cause
304 // overflow as well: unsigned(INTMIN)
305 llvm::APSInt OldVal(Val);
306 Val.setIsSigned(NewSign);
307
308 if (Val.isNegative()) // Sign bit changes meaning.
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000309 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000310 }
311}
312
Chris Lattner0471f5b2007-08-23 18:29:20 +0000313namespace {
314 struct CaseCompareFunctor {
315 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
316 const llvm::APSInt &RHS) {
317 return LHS.first < RHS;
318 }
Chris Lattner0e85a272007-09-03 18:31:57 +0000319 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
320 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
321 return LHS.first < RHS.first;
322 }
Chris Lattner0471f5b2007-08-23 18:29:20 +0000323 bool operator()(const llvm::APSInt &LHS,
324 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
325 return LHS < RHS.first;
326 }
327 };
328}
329
Chris Lattner764a7ce2007-09-21 18:15:22 +0000330/// CmpCaseVals - Comparison predicate for sorting case values.
331///
332static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
333 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
334 if (lhs.first < rhs.first)
335 return true;
336
337 if (lhs.first == rhs.first &&
338 lhs.second->getCaseLoc().getRawEncoding()
339 < rhs.second->getCaseLoc().getRawEncoding())
340 return true;
341 return false;
342}
343
Sebastian Redlde307472009-01-11 00:38:46 +0000344Action::OwningStmtResult
345Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch,
346 StmtArg Body) {
347 Stmt *BodyStmt = (Stmt*)Body.release();
348
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000349 SwitchStmt *SS = SwitchStack.back();
Sebastian Redlde307472009-01-11 00:38:46 +0000350 assert(SS == (SwitchStmt*)Switch.get() && "switch stack missing push/pop!");
351
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000352 SS->setBody(BodyStmt, SwitchLoc);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000353 SwitchStack.pop_back();
354
Chris Lattnerf4021e72007-08-23 05:46:52 +0000355 Expr *CondExpr = SS->getCond();
356 QualType CondType = CondExpr->getType();
Sebastian Redlde307472009-01-11 00:38:46 +0000357
Chris Lattnerf4021e72007-08-23 05:46:52 +0000358 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000359 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000360 << CondType << CondExpr->getSourceRange();
Sebastian Redlde307472009-01-11 00:38:46 +0000361 return StmtError();
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000362 }
Sebastian Redlde307472009-01-11 00:38:46 +0000363
Chris Lattnerf4021e72007-08-23 05:46:52 +0000364 // Get the bitwidth of the switched-on value before promotions. We must
365 // convert the integer case values to this width before comparison.
Chris Lattner98be4942008-03-05 18:54:05 +0000366 unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000367 bool CondIsSigned = CondType->isSignedIntegerType();
368
369 // Accumulate all of the case values in a vector so that we can sort them
370 // and detect duplicates. This vector contains the APInt for the case after
371 // it has been converted to the condition type.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000372 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
373 CaseValsTy CaseVals;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000374
375 // Keep track of any GNU case ranges we see. The APSInt is the low value.
376 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
377
378 DefaultStmt *TheDefaultStmt = 0;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000379
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000380 bool CaseListIsErroneous = false;
381
382 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000383 SC = SC->getNextSwitchCase()) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000384
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000385 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000386 if (TheDefaultStmt) {
387 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
Chris Lattner5f4a6822008-11-23 23:12:31 +0000388 Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
Sebastian Redlde307472009-01-11 00:38:46 +0000389
Chris Lattnerf4021e72007-08-23 05:46:52 +0000390 // FIXME: Remove the default statement from the switch block so that
391 // we'll return a valid AST. This requires recursing down the
392 // AST and finding it, not something we are set up to do right now. For
393 // now, just lop the entire switch stmt out of the AST.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000394 CaseListIsErroneous = true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000395 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000396 TheDefaultStmt = DS;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000397
Chris Lattnerf4021e72007-08-23 05:46:52 +0000398 } else {
399 CaseStmt *CS = cast<CaseStmt>(SC);
400
401 // We already verified that the expression has a i-c-e value (C99
402 // 6.8.4.2p3) - get that value now.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000403 Expr *Lo = CS->getLHS();
Anders Carlsson51fe9962008-11-22 21:04:56 +0000404 llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000405
406 // Convert the value to the same width/sign as the condition.
407 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
408 CS->getLHS()->getLocStart(),
409 diag::warn_case_value_overflow);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000410
Chris Lattner1e0a3902008-01-16 19:17:22 +0000411 // If the LHS is not the same type as the condition, insert an implicit
412 // cast.
413 ImpCastExprToType(Lo, CondType);
414 CS->setLHS(Lo);
415
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000416 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerf4021e72007-08-23 05:46:52 +0000417 if (CS->getRHS())
418 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000419 else
420 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000421 }
422 }
423
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000424 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner764a7ce2007-09-21 18:15:22 +0000425 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000426
Chris Lattnerf3348502007-08-23 14:29:07 +0000427 if (!CaseVals.empty()) {
428 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
429 if (CaseVals[i].first == CaseVals[i+1].first) {
430 // If we have a duplicate, report it.
431 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000432 diag::err_duplicate_case) << CaseVals[i].first.toString(10);
Chris Lattnerf3348502007-08-23 14:29:07 +0000433 Diag(CaseVals[i].second->getLHS()->getLocStart(),
Chris Lattner5f4a6822008-11-23 23:12:31 +0000434 diag::note_duplicate_case_prev);
Chris Lattnerf3348502007-08-23 14:29:07 +0000435 // FIXME: We really want to remove the bogus case stmt from the substmt,
436 // but we have no way to do this right now.
437 CaseListIsErroneous = true;
438 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000439 }
440 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000441
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000442 // Detect duplicate case ranges, which usually don't exist at all in the first
443 // place.
444 if (!CaseRanges.empty()) {
445 // Sort all the case ranges by their low value so we can easily detect
446 // overlaps between ranges.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000447 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000448
449 // Scan the ranges, computing the high values and removing empty ranges.
450 std::vector<llvm::APSInt> HiVals;
Chris Lattner6efc4d32007-08-23 17:48:14 +0000451 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000452 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000453 Expr *Hi = CR->getRHS();
Anders Carlsson51fe9962008-11-22 21:04:56 +0000454 llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000455
456 // Convert the value to the same width/sign as the condition.
457 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
458 CR->getRHS()->getLocStart(),
459 diag::warn_case_value_overflow);
460
Chris Lattner1e0a3902008-01-16 19:17:22 +0000461 // If the LHS is not the same type as the condition, insert an implicit
462 // cast.
463 ImpCastExprToType(Hi, CondType);
464 CR->setRHS(Hi);
465
Chris Lattner6efc4d32007-08-23 17:48:14 +0000466 // If the low value is bigger than the high value, the case is empty.
467 if (CaseRanges[i].first > HiVal) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000468 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
469 << SourceRange(CR->getLHS()->getLocStart(),
470 CR->getRHS()->getLocEnd());
Chris Lattner6efc4d32007-08-23 17:48:14 +0000471 CaseRanges.erase(CaseRanges.begin()+i);
472 --i, --e;
473 continue;
474 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000475 HiVals.push_back(HiVal);
476 }
477
478 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0471f5b2007-08-23 18:29:20 +0000479 // ranges. Since the range list is sorted, we only need to compare case
480 // ranges with their neighbors.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000481 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0471f5b2007-08-23 18:29:20 +0000482 llvm::APSInt &CRLo = CaseRanges[i].first;
483 llvm::APSInt &CRHi = HiVals[i];
484 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000485
Chris Lattner0471f5b2007-08-23 18:29:20 +0000486 // Check to see whether the case range overlaps with any singleton cases.
487 CaseStmt *OverlapStmt = 0;
488 llvm::APSInt OverlapVal(32);
489
490 // Find the smallest value >= the lower bound. If I is in the case range,
491 // then we have overlap.
492 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
493 CaseVals.end(), CRLo,
494 CaseCompareFunctor());
495 if (I != CaseVals.end() && I->first < CRHi) {
496 OverlapVal = I->first; // Found overlap with scalar.
497 OverlapStmt = I->second;
498 }
499
500 // Find the smallest value bigger than the upper bound.
501 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
502 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
503 OverlapVal = (I-1)->first; // Found overlap with scalar.
504 OverlapStmt = (I-1)->second;
505 }
506
507 // Check to see if this case stmt overlaps with the subsequent case range.
508 if (i && CRLo <= HiVals[i-1]) {
509 OverlapVal = HiVals[i-1]; // Found overlap with range.
510 OverlapStmt = CaseRanges[i-1].second;
511 }
512
513 if (OverlapStmt) {
514 // If we have a duplicate, report it.
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000515 Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
516 << OverlapVal.toString(10);
Chris Lattner0471f5b2007-08-23 18:29:20 +0000517 Diag(OverlapStmt->getLHS()->getLocStart(),
Chris Lattner5f4a6822008-11-23 23:12:31 +0000518 diag::note_duplicate_case_prev);
Chris Lattner0471f5b2007-08-23 18:29:20 +0000519 // FIXME: We really want to remove the bogus case stmt from the substmt,
520 // but we have no way to do this right now.
521 CaseListIsErroneous = true;
522 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000523 }
524 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000525
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000526 // FIXME: If the case list was broken is some way, we don't have a good system
527 // to patch it up. Instead, just return the whole substmt as broken.
528 if (CaseListIsErroneous)
Sebastian Redlde307472009-01-11 00:38:46 +0000529 return StmtError();
530
531 Switch.release();
532 return Owned(SS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000533}
534
535Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000536Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000537 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000538 assert(condExpr && "ActOnWhileStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000539
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000540 DefaultFunctionArrayConversion(condExpr);
541 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000542
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000543 if (getLangOptions().CPlusPlus) {
544 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
545 return true;
546 } else if (!condType->isScalarType()) // C99 6.8.5p2
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000547 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar)
Chris Lattnerd1625842008-11-24 06:25:27 +0000548 << condType << condExpr->getSourceRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000549
Steve Naroffb5a69582007-08-31 23:28:33 +0000550 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000551}
552
553Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000554Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 SourceLocation WhileLoc, ExprTy *Cond) {
556 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000557 assert(condExpr && "ActOnDoStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000558
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000559 DefaultFunctionArrayConversion(condExpr);
560 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000561
Argyrios Kyrtzidis6314ff22008-09-11 05:16:22 +0000562 if (getLangOptions().CPlusPlus) {
563 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
564 return true;
565 } else if (!condType->isScalarType()) // C99 6.8.5p2
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000566 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar)
Chris Lattnerd1625842008-11-24 06:25:27 +0000567 << condType << condExpr->getSourceRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000568
Steve Naroffb5a69582007-08-31 23:28:33 +0000569 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000570}
571
572Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000573Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000574 StmtTy *first, ExprTy *second, ExprTy *third,
575 SourceLocation RParenLoc, StmtTy *body) {
576 Stmt *First = static_cast<Stmt*>(first);
577 Expr *Second = static_cast<Expr*>(second);
578 Expr *Third = static_cast<Expr*>(third);
579 Stmt *Body = static_cast<Stmt*>(body);
580
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000581 if (!getLangOptions().CPlusPlus) {
582 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000583 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
584 // declare identifiers for objects having storage class 'auto' or
585 // 'register'.
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000586 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
587 DI!=DE; ++DI) {
588 VarDecl *VD = dyn_cast<VarDecl>(*DI);
589 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
590 VD = 0;
591 if (VD == 0)
592 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
593 // FIXME: mark decl erroneous!
594 }
Chris Lattnerae3b7012007-08-28 05:03:08 +0000595 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 }
597 if (Second) {
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000598 DefaultFunctionArrayConversion(Second);
599 QualType SecondType = Second->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000600
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000601 if (getLangOptions().CPlusPlus) {
602 if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4
603 return true;
604 } else if (!SecondType->isScalarType()) // C99 6.8.5p2
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000605 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar)
Chris Lattnerd1625842008-11-24 06:25:27 +0000606 << SecondType << Second->getSourceRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 }
Steve Naroffb5a69582007-08-31 23:28:33 +0000608 return new ForStmt(First, Second, Third, Body, ForLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000609}
610
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000611Action::StmtResult
Fariborz Jahanian75712282008-01-10 00:24:29 +0000612Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000613 SourceLocation LParenLoc,
614 StmtTy *first, ExprTy *second,
615 SourceLocation RParenLoc, StmtTy *body) {
616 Stmt *First = static_cast<Stmt*>(first);
617 Expr *Second = static_cast<Expr*>(second);
618 Stmt *Body = static_cast<Stmt*>(body);
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000619 if (First) {
620 QualType FirstType;
621 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
Ted Kremenekf34afee2008-10-06 20:58:11 +0000622 if (!DS->hasSolitaryDecl())
623 return Diag((*DS->decl_begin())->getLocation(),
624 diag::err_toomany_element_decls);
625
626 ScopedDecl *D = DS->getSolitaryDecl();
627 FirstType = cast<ValueDecl>(D)->getType();
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000628 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
629 // declare identifiers for objects having storage class 'auto' or
630 // 'register'.
Steve Naroff248a7532008-04-15 22:42:06 +0000631 VarDecl *VD = cast<VarDecl>(D);
632 if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
633 return Diag(VD->getLocation(), diag::err_non_variable_decl_in_for);
Anders Carlsson1fe379f2008-08-25 18:16:36 +0000634 } else {
635 Expr::isLvalueResult lval = cast<Expr>(First)->isLvalue(Context);
636
637 if (lval != Expr::LV_Valid)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000638 return Diag(First->getLocStart(), diag::err_selector_element_not_lvalue)
639 << First->getSourceRange();
Anders Carlsson1fe379f2008-08-25 18:16:36 +0000640
641 FirstType = static_cast<Expr*>(first)->getType();
642 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000643 if (!Context.isObjCObjectPointerType(FirstType))
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000644 Diag(ForLoc, diag::err_selector_element_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000645 << FirstType << First->getSourceRange();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000646 }
647 if (Second) {
648 DefaultFunctionArrayConversion(Second);
649 QualType SecondType = Second->getType();
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000650 if (!Context.isObjCObjectPointerType(SecondType))
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000651 Diag(ForLoc, diag::err_collection_expr_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000652 << SecondType << Second->getSourceRange();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000653 }
Fariborz Jahanian75712282008-01-10 00:24:29 +0000654 return new ObjCForCollectionStmt(First, Second, Body, ForLoc, RParenLoc);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000655}
Reid Spencer5f016e22007-07-11 17:01:13 +0000656
657Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000658Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 IdentifierInfo *LabelII) {
Steve Naroff4eb206b2008-09-03 18:15:37 +0000660 // If we are in a block, reject all gotos for now.
661 if (CurBlock)
662 return Diag(GotoLoc, diag::err_goto_in_block);
663
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 // Look up the record for this label identifier.
665 LabelStmt *&LabelDecl = LabelMap[LabelII];
666
667 // If we haven't seen this label yet, create a forward reference.
668 if (LabelDecl == 0)
669 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
670
Ted Kremenek61f62162007-09-06 17:11:52 +0000671 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000672}
673
674Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000675Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 ExprTy *DestExp) {
677 // FIXME: Verify that the operand is convertible to void*.
678
679 return new IndirectGotoStmt((Expr*)DestExp);
680}
681
682Action::StmtResult
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.
687 Diag(ContinueLoc, diag::err_continue_not_in_loop);
688 return true;
689 }
690
Steve Naroff507f2d52007-08-31 23:49:30 +0000691 return new ContinueStmt(ContinueLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000692}
693
694Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000695Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 Scope *S = CurScope->getBreakParent();
697 if (!S) {
698 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
699 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
700 return true;
701 }
702
Steve Naroff507f2d52007-08-31 23:49:30 +0000703 return new BreakStmt(BreakLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000704}
705
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000706/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000707///
708Action::StmtResult
709Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
710
711 // If this is the first return we've seen in the block, infer the type of
712 // the block from it.
713 if (CurBlock->ReturnType == 0) {
Steve Naroffc50a4a52008-09-16 22:25:10 +0000714 if (RetValExp) {
Steve Naroff16564422008-09-24 22:26:48 +0000715 // Don't call UsualUnaryConversions(), since we don't want to do
716 // integer promotions here.
717 DefaultFunctionArrayConversion(RetValExp);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000718 CurBlock->ReturnType = RetValExp->getType().getTypePtr();
Steve Naroffc50a4a52008-09-16 22:25:10 +0000719 } else
Steve Naroff4eb206b2008-09-03 18:15:37 +0000720 CurBlock->ReturnType = Context.VoidTy.getTypePtr();
721 return new ReturnStmt(ReturnLoc, RetValExp);
722 }
723
724 // Otherwise, verify that this result type matches the previous one. We are
725 // pickier with blocks than for normal functions because we don't have GCC
726 // compatibility to worry about here.
727 if (CurBlock->ReturnType->isVoidType()) {
728 if (RetValExp) {
729 Diag(ReturnLoc, diag::err_return_block_has_expr);
730 delete RetValExp;
731 RetValExp = 0;
732 }
733 return new ReturnStmt(ReturnLoc, RetValExp);
734 }
735
736 if (!RetValExp) {
737 Diag(ReturnLoc, diag::err_block_return_missing_expr);
738 return true;
739 }
740
741 // we have a non-void block with an expression, continue checking
742 QualType RetValType = RetValExp->getType();
743
744 // For now, restrict multiple return statements in a block to have
745 // strict compatible types only.
746 QualType BlockQT = QualType(CurBlock->ReturnType, 0);
747 if (Context.getCanonicalType(BlockQT).getTypePtr()
748 != Context.getCanonicalType(RetValType).getTypePtr()) {
749 DiagnoseAssignmentResult(Incompatible, ReturnLoc, BlockQT,
750 RetValType, RetValExp, "returning");
751 return true;
752 }
753
754 if (RetValExp) CheckReturnStackAddr(RetValExp, BlockQT, ReturnLoc);
755
756 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
757}
Reid Spencer5f016e22007-07-11 17:01:13 +0000758
759Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000760Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
Steve Naroff90045e82007-07-13 23:32:42 +0000761 Expr *RetValExp = static_cast<Expr *>(rex);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000762 if (CurBlock)
763 return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
Chris Lattner371f2582008-12-04 23:50:19 +0000764
765 QualType FnRetType;
766 if (FunctionDecl *FD = getCurFunctionDecl())
767 FnRetType = FD->getResultType();
768 else
769 FnRetType = getCurMethodDecl()->getResultType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000770
Chris Lattner5cf216b2008-01-04 18:04:52 +0000771 if (FnRetType->isVoidType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000772 if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns)
Chris Lattner65ce04b2008-12-18 02:01:17 +0000773 unsigned D = diag::ext_return_has_expr;
774 if (RetValExp->getType()->isVoidType())
775 D = diag::ext_return_has_void_expr;
Chris Lattner65ce04b2008-12-18 02:01:17 +0000776
Chris Lattnere878eb02008-12-18 02:03:48 +0000777 // return (some void expression); is legal in C++.
778 if (D != diag::ext_return_has_void_expr ||
779 !getLangOptions().CPlusPlus) {
780 NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
781 Diag(ReturnLoc, D)
782 << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
783 << RetValExp->getSourceRange();
784 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 }
Chris Lattner3c73c412008-11-19 08:23:25 +0000786 return new ReturnStmt(ReturnLoc, RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 }
Chris Lattner3c73c412008-11-19 08:23:25 +0000788
789 if (!RetValExp) {
790 unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4
791 // C99 6.8.6.4p1 (ext_ since GCC warns)
792 if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
793
794 if (FunctionDecl *FD = getCurFunctionDecl())
Chris Lattner08631c52008-11-23 21:45:46 +0000795 Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
Chris Lattner3c73c412008-11-19 08:23:25 +0000796 else
Chris Lattner08631c52008-11-23 21:45:46 +0000797 Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
Chris Lattner3c73c412008-11-19 08:23:25 +0000798 return new ReturnStmt(ReturnLoc, (Expr*)0);
799 }
800
Douglas Gregor898574e2008-12-05 23:32:09 +0000801 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
802 // we have a non-void function with an expression, continue checking
803 QualType RetValType = RetValExp->getType();
804
805 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
806 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
807 // function return.
808
809 // In C++ the return statement is handled via a copy initialization.
810 // the C version of which boils down to
811 // CheckSingleAssignmentConstraints.
812 if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
813 return true;
Ted Kremenek06de2762007-08-17 16:46:58 +0000814
Douglas Gregor898574e2008-12-05 23:32:09 +0000815 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
816 }
817
Steve Naroff507f2d52007-08-31 23:49:30 +0000818 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000819}
820
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000821Sema::StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
Chris Lattner08631c52008-11-23 21:45:46 +0000822 bool IsSimple,
Anders Carlsson39c47b52007-11-23 23:12:25 +0000823 bool IsVolatile,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000824 unsigned NumOutputs,
825 unsigned NumInputs,
826 std::string *Names,
Chris Lattner1708b962008-08-18 19:55:17 +0000827 ExprTy **constraints,
828 ExprTy **exprs,
Chris Lattner6bc52112008-07-23 06:46:56 +0000829 ExprTy *asmString,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000830 unsigned NumClobbers,
Chris Lattner1708b962008-08-18 19:55:17 +0000831 ExprTy **clobbers,
Chris Lattnerfe795952007-10-29 04:04:16 +0000832 SourceLocation RParenLoc) {
Chris Lattner1708b962008-08-18 19:55:17 +0000833 StringLiteral **Constraints = reinterpret_cast<StringLiteral**>(constraints);
834 Expr **Exprs = reinterpret_cast<Expr **>(exprs);
Chris Lattner6bc52112008-07-23 06:46:56 +0000835 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString);
Chris Lattner1708b962008-08-18 19:55:17 +0000836 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers);
837
838 // The parser verifies that there is a string literal here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000839 if (AsmString->isWide())
840 // FIXME: We currently leak memory here.
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000841 return Diag(AsmString->getLocStart(), diag::err_asm_wide_character)
842 << AsmString->getSourceRange();
Chris Lattner6bc52112008-07-23 06:46:56 +0000843
844
Chris Lattner1708b962008-08-18 19:55:17 +0000845 for (unsigned i = 0; i != NumOutputs; i++) {
846 StringLiteral *Literal = Constraints[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000847 if (Literal->isWide())
848 // FIXME: We currently leak memory here.
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000849 return Diag(Literal->getLocStart(), diag::err_asm_wide_character)
850 << Literal->getSourceRange();
Chris Lattner6bc52112008-07-23 06:46:56 +0000851
Anders Carlssond04c6e22007-11-27 04:11:28 +0000852 std::string OutputConstraint(Literal->getStrData(),
853 Literal->getByteLength());
854
855 TargetInfo::ConstraintInfo info;
Chris Lattner6bc52112008-07-23 06:46:56 +0000856 if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info))
Anders Carlssond04c6e22007-11-27 04:11:28 +0000857 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000858 return Diag(Literal->getLocStart(),
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000859 diag::err_asm_invalid_output_constraint) << OutputConstraint;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000860
861 // Check that the output exprs are valid lvalues.
Chris Lattner1708b962008-08-18 19:55:17 +0000862 ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]);
Chris Lattner28be73f2008-07-26 21:30:36 +0000863 Expr::isLvalueResult Result = OutputExpr->isLvalue(Context);
Anders Carlsson04728b72007-11-23 19:43:50 +0000864 if (Result != Expr::LV_Valid) {
Anders Carlsson04728b72007-11-23 19:43:50 +0000865 // FIXME: We currently leak memory here.
Chris Lattner1708b962008-08-18 19:55:17 +0000866 return Diag(OutputExpr->getSubExpr()->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000867 diag::err_asm_invalid_lvalue_in_output)
868 << OutputExpr->getSubExpr()->getSourceRange();
Anders Carlsson04728b72007-11-23 19:43:50 +0000869 }
870 }
871
Anders Carlsson04728b72007-11-23 19:43:50 +0000872 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
Chris Lattner1708b962008-08-18 19:55:17 +0000873 StringLiteral *Literal = Constraints[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000874 if (Literal->isWide())
875 // FIXME: We currently leak memory here.
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000876 return Diag(Literal->getLocStart(), diag::err_asm_wide_character)
877 << Literal->getSourceRange();
Anders Carlsson04728b72007-11-23 19:43:50 +0000878
Anders Carlssond04c6e22007-11-27 04:11:28 +0000879 std::string InputConstraint(Literal->getStrData(),
880 Literal->getByteLength());
881
882 TargetInfo::ConstraintInfo info;
883 if (!Context.Target.validateInputConstraint(InputConstraint.c_str(),
Chris Lattner1708b962008-08-18 19:55:17 +0000884 NumOutputs, info)) {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000885 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000886 return Diag(Literal->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000887 diag::err_asm_invalid_input_constraint) << InputConstraint;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000888 }
889
890 // Check that the input exprs aren't of type void.
Chris Lattner1708b962008-08-18 19:55:17 +0000891 ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]);
Anders Carlsson04728b72007-11-23 19:43:50 +0000892 if (InputExpr->getType()->isVoidType()) {
Anders Carlsson04728b72007-11-23 19:43:50 +0000893
Anders Carlsson04728b72007-11-23 19:43:50 +0000894 // FIXME: We currently leak memory here.
Chris Lattner1708b962008-08-18 19:55:17 +0000895 return Diag(InputExpr->getSubExpr()->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000896 diag::err_asm_invalid_type_in_input)
Chris Lattnerd1625842008-11-24 06:25:27 +0000897 << InputExpr->getType() << InputConstraint
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000898 << InputExpr->getSubExpr()->getSourceRange();
Anders Carlsson04728b72007-11-23 19:43:50 +0000899 }
Anders Carlsson562489e2008-12-31 07:27:38 +0000900
901 if (info & TargetInfo::CI_AllowsRegister)
902 DefaultFunctionArrayConversion(Exprs[i]);
Anders Carlsson04728b72007-11-23 19:43:50 +0000903 }
Anders Carlssonb235fc22007-11-22 01:36:19 +0000904
Anders Carlsson6fa90862007-11-25 00:25:21 +0000905 // Check that the clobbers are valid.
Chris Lattner1708b962008-08-18 19:55:17 +0000906 for (unsigned i = 0; i != NumClobbers; i++) {
907 StringLiteral *Literal = Clobbers[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000908 if (Literal->isWide())
909 // FIXME: We currently leak memory here.
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000910 return Diag(Literal->getLocStart(), diag::err_asm_wide_character)
911 << Literal->getSourceRange();
Anders Carlsson6fa90862007-11-25 00:25:21 +0000912
913 llvm::SmallString<16> Clobber(Literal->getStrData(),
914 Literal->getStrData() +
915 Literal->getByteLength());
916
Chris Lattner6bc52112008-07-23 06:46:56 +0000917 if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000918 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000919 return Diag(Literal->getLocStart(),
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000920 diag::err_asm_unknown_register_name) << Clobber.c_str();
Anders Carlsson6fa90862007-11-25 00:25:21 +0000921 }
922
Chris Lattner1708b962008-08-18 19:55:17 +0000923 return new AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs,
924 Names, Constraints, Exprs, AsmString, NumClobbers,
925 Clobbers, RParenLoc);
Chris Lattnerfe795952007-10-29 04:04:16 +0000926}
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000927
928Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000929Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000930 SourceLocation RParen, StmtTy *Parm,
931 StmtTy *Body, StmtTy *CatchList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000932 ObjCAtCatchStmt *CS = new ObjCAtCatchStmt(AtLoc, RParen,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000933 static_cast<Stmt*>(Parm), static_cast<Stmt*>(Body),
934 static_cast<Stmt*>(CatchList));
935 return CatchList ? CatchList : CS;
936}
937
Fariborz Jahanian161a9c52007-11-02 00:18:53 +0000938Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000939Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtTy *Body) {
940 ObjCAtFinallyStmt *FS = new ObjCAtFinallyStmt(AtLoc,
Fariborz Jahanian161a9c52007-11-02 00:18:53 +0000941 static_cast<Stmt*>(Body));
942 return FS;
943}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000944
945Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000946Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000947 StmtTy *Try, StmtTy *Catch, StmtTy *Finally) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000948 ObjCAtTryStmt *TS = new ObjCAtTryStmt(AtLoc, static_cast<Stmt*>(Try),
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000949 static_cast<Stmt*>(Catch),
950 static_cast<Stmt*>(Finally));
951 return TS;
952}
953
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000954Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000955Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, StmtTy *Throw) {
956 ObjCAtThrowStmt *TS = new ObjCAtThrowStmt(AtLoc, static_cast<Stmt*>(Throw));
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000957 return TS;
958}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000959
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000960Action::StmtResult
961Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprTy *SynchExpr,
962 StmtTy *SynchBody) {
963 ObjCAtSynchronizedStmt *SS = new ObjCAtSynchronizedStmt(AtLoc,
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000964 static_cast<Stmt*>(SynchExpr), static_cast<Stmt*>(SynchBody));
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000965 return SS;
966}
Sebastian Redl4b07b292008-12-22 19:15:10 +0000967
968/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
969/// and creates a proper catch handler from them.
970Action::OwningStmtResult
971Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclTy *ExDecl,
972 StmtArg HandlerBlock) {
973 // There's nothing to test that ActOnExceptionDecl didn't already test.
974 return Owned(new CXXCatchStmt(CatchLoc, static_cast<VarDecl*>(ExDecl),
975 static_cast<Stmt*>(HandlerBlock.release())));
976}
Sebastian Redl8351da02008-12-22 21:35:02 +0000977
978/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
979/// handlers and creates a try statement from them.
980Action::OwningStmtResult
981Sema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock,
982 MultiStmtArg RawHandlers) {
983 unsigned NumHandlers = RawHandlers.size();
984 assert(NumHandlers > 0 &&
985 "The parser shouldn't call this if there are no handlers.");
986 Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get());
987
988 for(unsigned i = 0; i < NumHandlers - 1; ++i) {
989 CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
990 if (!Handler->getExceptionDecl())
991 return StmtError(Diag(Handler->getLocStart(), diag::err_early_catch_all));
992 }
993 // FIXME: We should detect handlers for the same type as an earlier one.
994 // This one is rather easy.
995 // FIXME: We should detect handlers that cannot catch anything because an
996 // earlier handler catches a superclass. Need to find a method that is not
997 // quadratic for this.
998 // Neither of these are explicitly forbidden, but every compiler detects them
999 // and warns.
1000
1001 RawHandlers.release();
1002 return Owned(new CXXTryStmt(TryLoc, static_cast<Stmt*>(TryBlock.release()),
1003 Handlers, NumHandlers));
1004}