blob: 23d3cd5fae83789313fc555a40f668266f1ea244 [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
172Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000173Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner0fa152e2007-07-21 03:00:26 +0000174 SourceLocation ColonLoc, StmtTy *subStmt) {
175 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 // Look up the record for this label identifier.
177 LabelStmt *&LabelDecl = LabelMap[II];
178
179 // If not forward referenced or defined already, just create a new LabelStmt.
180 if (LabelDecl == 0)
Chris Lattner0fa152e2007-07-21 03:00:26 +0000181 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000182
183 assert(LabelDecl->getID() == II && "Label mismatch!");
184
185 // 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);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000190 return SubStmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 }
192
193 // 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);
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 return LabelDecl;
198}
199
200Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000201Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 StmtTy *ThenVal, SourceLocation ElseLoc,
203 StmtTy *ElseVal) {
204 Expr *condExpr = (Expr *)CondVal;
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000205 Stmt *thenStmt = (Stmt *)ThenVal;
206
Steve Naroff1b273c42007-09-16 14:56:35 +0000207 assert(condExpr && "ActOnIfStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000208
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000209 DefaultFunctionArrayConversion(condExpr);
210 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000211
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000212 if (getLangOptions().CPlusPlus) {
213 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
214 return true;
215 } else if (!condType->isScalarType()) // C99 6.8.4.1p1
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000216 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar)
Chris Lattnerd1625842008-11-24 06:25:27 +0000217 << condType << condExpr->getSourceRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000218
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000219 // Warn if the if block has a null body without an else value.
220 // this helps prevent bugs due to typos, such as
221 // if (condition);
222 // do_stuff();
223 if (!ElseVal) {
224 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
225 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
226 }
227
228 return new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000229}
230
231Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000232Sema::ActOnStartOfSwitchStmt(ExprTy *cond) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000233 Expr *Cond = static_cast<Expr*>(cond);
234
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000235 if (getLangOptions().CPlusPlus) {
236 // C++ 6.4.2.p2:
237 // The condition shall be of integral type, enumeration type, or of a class
238 // type for which a single conversion function to integral or enumeration
239 // type exists (12.3). If the condition is of class type, the condition is
240 // converted by calling that conversion function, and the result of the
241 // conversion is used in place of the original condition for the remainder
242 // of this section. Integral promotions are performed.
243
244 QualType Ty = Cond->getType();
245
246 // FIXME: Handle class types.
247
248 // If the type is wrong a diagnostic will be emitted later at
249 // ActOnFinishSwitchStmt.
250 if (Ty->isIntegralType() || Ty->isEnumeralType()) {
251 // Integral promotions are performed.
252 // FIXME: Integral promotions for C++ are not complete.
253 UsualUnaryConversions(Cond);
254 }
255 } else {
256 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
257 UsualUnaryConversions(Cond);
258 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000259
260 SwitchStmt *SS = new SwitchStmt(Cond);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000261 SwitchStack.push_back(SS);
262 return SS;
263}
Chris Lattner6c36be52007-07-18 02:28:47 +0000264
Chris Lattnerf4021e72007-08-23 05:46:52 +0000265/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
266/// the specified width and sign. If an overflow occurs, detect it and emit
267/// the specified diagnostic.
268void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
269 unsigned NewWidth, bool NewSign,
270 SourceLocation Loc,
271 unsigned DiagID) {
272 // Perform a conversion to the promoted condition type if needed.
273 if (NewWidth > Val.getBitWidth()) {
274 // If this is an extension, just do it.
275 llvm::APSInt OldVal(Val);
276 Val.extend(NewWidth);
277
278 // If the input was signed and negative and the output is unsigned,
279 // warn.
280 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000281 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000282
283 Val.setIsSigned(NewSign);
284 } else if (NewWidth < Val.getBitWidth()) {
285 // If this is a truncation, check for overflow.
286 llvm::APSInt ConvVal(Val);
287 ConvVal.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000288 ConvVal.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000289 ConvVal.extend(Val.getBitWidth());
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000290 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000291 if (ConvVal != Val)
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000292 Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000293
294 // Regardless of whether a diagnostic was emitted, really do the
295 // truncation.
296 Val.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000297 Val.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000298 } else if (NewSign != Val.isSigned()) {
299 // Convert the sign to match the sign of the condition. This can cause
300 // overflow as well: unsigned(INTMIN)
301 llvm::APSInt OldVal(Val);
302 Val.setIsSigned(NewSign);
303
304 if (Val.isNegative()) // Sign bit changes meaning.
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000305 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000306 }
307}
308
Chris Lattner0471f5b2007-08-23 18:29:20 +0000309namespace {
310 struct CaseCompareFunctor {
311 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
312 const llvm::APSInt &RHS) {
313 return LHS.first < RHS;
314 }
Chris Lattner0e85a272007-09-03 18:31:57 +0000315 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
316 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
317 return LHS.first < RHS.first;
318 }
Chris Lattner0471f5b2007-08-23 18:29:20 +0000319 bool operator()(const llvm::APSInt &LHS,
320 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
321 return LHS < RHS.first;
322 }
323 };
324}
325
Chris Lattner764a7ce2007-09-21 18:15:22 +0000326/// CmpCaseVals - Comparison predicate for sorting case values.
327///
328static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
329 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
330 if (lhs.first < rhs.first)
331 return true;
332
333 if (lhs.first == rhs.first &&
334 lhs.second->getCaseLoc().getRawEncoding()
335 < rhs.second->getCaseLoc().getRawEncoding())
336 return true;
337 return false;
338}
339
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000340Action::StmtResult
Chris Lattner764a7ce2007-09-21 18:15:22 +0000341Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
342 ExprTy *Body) {
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000343 Stmt *BodyStmt = (Stmt*)Body;
344
345 SwitchStmt *SS = SwitchStack.back();
346 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
347
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000348 SS->setBody(BodyStmt, SwitchLoc);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000349 SwitchStack.pop_back();
350
Chris Lattnerf4021e72007-08-23 05:46:52 +0000351 Expr *CondExpr = SS->getCond();
352 QualType CondType = CondExpr->getType();
Chris Lattner6c36be52007-07-18 02:28:47 +0000353
Chris Lattnerf4021e72007-08-23 05:46:52 +0000354 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000355 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000356 << CondType << CondExpr->getSourceRange();
Chris Lattnerf4021e72007-08-23 05:46:52 +0000357 return true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000358 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000359
360 // Get the bitwidth of the switched-on value before promotions. We must
361 // convert the integer case values to this width before comparison.
Chris Lattner98be4942008-03-05 18:54:05 +0000362 unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000363 bool CondIsSigned = CondType->isSignedIntegerType();
364
365 // Accumulate all of the case values in a vector so that we can sort them
366 // and detect duplicates. This vector contains the APInt for the case after
367 // it has been converted to the condition type.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000368 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
369 CaseValsTy CaseVals;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000370
371 // Keep track of any GNU case ranges we see. The APSInt is the low value.
372 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
373
374 DefaultStmt *TheDefaultStmt = 0;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000375
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000376 bool CaseListIsErroneous = false;
377
378 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000379 SC = SC->getNextSwitchCase()) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000380
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000381 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000382 if (TheDefaultStmt) {
383 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
Chris Lattner5f4a6822008-11-23 23:12:31 +0000384 Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000385
Chris Lattnerf4021e72007-08-23 05:46:52 +0000386 // FIXME: Remove the default statement from the switch block so that
387 // we'll return a valid AST. This requires recursing down the
388 // AST and finding it, not something we are set up to do right now. For
389 // now, just lop the entire switch stmt out of the AST.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000390 CaseListIsErroneous = true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000391 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000392 TheDefaultStmt = DS;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000393
Chris Lattnerf4021e72007-08-23 05:46:52 +0000394 } else {
395 CaseStmt *CS = cast<CaseStmt>(SC);
396
397 // We already verified that the expression has a i-c-e value (C99
398 // 6.8.4.2p3) - get that value now.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000399 Expr *Lo = CS->getLHS();
Anders Carlsson51fe9962008-11-22 21:04:56 +0000400 llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000401
402 // Convert the value to the same width/sign as the condition.
403 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
404 CS->getLHS()->getLocStart(),
405 diag::warn_case_value_overflow);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000406
Chris Lattner1e0a3902008-01-16 19:17:22 +0000407 // If the LHS is not the same type as the condition, insert an implicit
408 // cast.
409 ImpCastExprToType(Lo, CondType);
410 CS->setLHS(Lo);
411
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000412 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerf4021e72007-08-23 05:46:52 +0000413 if (CS->getRHS())
414 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000415 else
416 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000417 }
418 }
419
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000420 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner764a7ce2007-09-21 18:15:22 +0000421 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000422
Chris Lattnerf3348502007-08-23 14:29:07 +0000423 if (!CaseVals.empty()) {
424 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
425 if (CaseVals[i].first == CaseVals[i+1].first) {
426 // If we have a duplicate, report it.
427 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000428 diag::err_duplicate_case) << CaseVals[i].first.toString(10);
Chris Lattnerf3348502007-08-23 14:29:07 +0000429 Diag(CaseVals[i].second->getLHS()->getLocStart(),
Chris Lattner5f4a6822008-11-23 23:12:31 +0000430 diag::note_duplicate_case_prev);
Chris Lattnerf3348502007-08-23 14:29:07 +0000431 // FIXME: We really want to remove the bogus case stmt from the substmt,
432 // but we have no way to do this right now.
433 CaseListIsErroneous = true;
434 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000435 }
436 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000437
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000438 // Detect duplicate case ranges, which usually don't exist at all in the first
439 // place.
440 if (!CaseRanges.empty()) {
441 // Sort all the case ranges by their low value so we can easily detect
442 // overlaps between ranges.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000443 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000444
445 // Scan the ranges, computing the high values and removing empty ranges.
446 std::vector<llvm::APSInt> HiVals;
Chris Lattner6efc4d32007-08-23 17:48:14 +0000447 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000448 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000449 Expr *Hi = CR->getRHS();
Anders Carlsson51fe9962008-11-22 21:04:56 +0000450 llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000451
452 // Convert the value to the same width/sign as the condition.
453 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
454 CR->getRHS()->getLocStart(),
455 diag::warn_case_value_overflow);
456
Chris Lattner1e0a3902008-01-16 19:17:22 +0000457 // If the LHS is not the same type as the condition, insert an implicit
458 // cast.
459 ImpCastExprToType(Hi, CondType);
460 CR->setRHS(Hi);
461
Chris Lattner6efc4d32007-08-23 17:48:14 +0000462 // If the low value is bigger than the high value, the case is empty.
463 if (CaseRanges[i].first > HiVal) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000464 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
465 << SourceRange(CR->getLHS()->getLocStart(),
466 CR->getRHS()->getLocEnd());
Chris Lattner6efc4d32007-08-23 17:48:14 +0000467 CaseRanges.erase(CaseRanges.begin()+i);
468 --i, --e;
469 continue;
470 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000471 HiVals.push_back(HiVal);
472 }
473
474 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0471f5b2007-08-23 18:29:20 +0000475 // ranges. Since the range list is sorted, we only need to compare case
476 // ranges with their neighbors.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000477 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0471f5b2007-08-23 18:29:20 +0000478 llvm::APSInt &CRLo = CaseRanges[i].first;
479 llvm::APSInt &CRHi = HiVals[i];
480 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000481
Chris Lattner0471f5b2007-08-23 18:29:20 +0000482 // Check to see whether the case range overlaps with any singleton cases.
483 CaseStmt *OverlapStmt = 0;
484 llvm::APSInt OverlapVal(32);
485
486 // Find the smallest value >= the lower bound. If I is in the case range,
487 // then we have overlap.
488 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
489 CaseVals.end(), CRLo,
490 CaseCompareFunctor());
491 if (I != CaseVals.end() && I->first < CRHi) {
492 OverlapVal = I->first; // Found overlap with scalar.
493 OverlapStmt = I->second;
494 }
495
496 // Find the smallest value bigger than the upper bound.
497 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
498 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
499 OverlapVal = (I-1)->first; // Found overlap with scalar.
500 OverlapStmt = (I-1)->second;
501 }
502
503 // Check to see if this case stmt overlaps with the subsequent case range.
504 if (i && CRLo <= HiVals[i-1]) {
505 OverlapVal = HiVals[i-1]; // Found overlap with range.
506 OverlapStmt = CaseRanges[i-1].second;
507 }
508
509 if (OverlapStmt) {
510 // If we have a duplicate, report it.
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000511 Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
512 << OverlapVal.toString(10);
Chris Lattner0471f5b2007-08-23 18:29:20 +0000513 Diag(OverlapStmt->getLHS()->getLocStart(),
Chris Lattner5f4a6822008-11-23 23:12:31 +0000514 diag::note_duplicate_case_prev);
Chris Lattner0471f5b2007-08-23 18:29:20 +0000515 // FIXME: We really want to remove the bogus case stmt from the substmt,
516 // but we have no way to do this right now.
517 CaseListIsErroneous = true;
518 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000519 }
520 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000521
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000522 // FIXME: If the case list was broken is some way, we don't have a good system
523 // to patch it up. Instead, just return the whole substmt as broken.
524 if (CaseListIsErroneous)
525 return true;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000526
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000527 return SS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000528}
529
530Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000531Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000533 assert(condExpr && "ActOnWhileStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000534
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000535 DefaultFunctionArrayConversion(condExpr);
536 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000537
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000538 if (getLangOptions().CPlusPlus) {
539 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
540 return true;
541 } else if (!condType->isScalarType()) // C99 6.8.5p2
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000542 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar)
Chris Lattnerd1625842008-11-24 06:25:27 +0000543 << condType << condExpr->getSourceRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000544
Steve Naroffb5a69582007-08-31 23:28:33 +0000545 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000546}
547
548Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000549Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 SourceLocation WhileLoc, ExprTy *Cond) {
551 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000552 assert(condExpr && "ActOnDoStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000553
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000554 DefaultFunctionArrayConversion(condExpr);
555 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000556
Argyrios Kyrtzidis6314ff22008-09-11 05:16:22 +0000557 if (getLangOptions().CPlusPlus) {
558 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
559 return true;
560 } else if (!condType->isScalarType()) // C99 6.8.5p2
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000561 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar)
Chris Lattnerd1625842008-11-24 06:25:27 +0000562 << condType << condExpr->getSourceRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000563
Steve Naroffb5a69582007-08-31 23:28:33 +0000564 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000565}
566
567Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000568Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000569 StmtTy *first, ExprTy *second, ExprTy *third,
570 SourceLocation RParenLoc, StmtTy *body) {
571 Stmt *First = static_cast<Stmt*>(first);
572 Expr *Second = static_cast<Expr*>(second);
573 Expr *Third = static_cast<Expr*>(third);
574 Stmt *Body = static_cast<Stmt*>(body);
575
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000576 if (!getLangOptions().CPlusPlus) {
577 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000578 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
579 // declare identifiers for objects having storage class 'auto' or
580 // 'register'.
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000581 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
582 DI!=DE; ++DI) {
583 VarDecl *VD = dyn_cast<VarDecl>(*DI);
584 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
585 VD = 0;
586 if (VD == 0)
587 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
588 // FIXME: mark decl erroneous!
589 }
Chris Lattnerae3b7012007-08-28 05:03:08 +0000590 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 }
592 if (Second) {
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000593 DefaultFunctionArrayConversion(Second);
594 QualType SecondType = Second->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000595
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000596 if (getLangOptions().CPlusPlus) {
597 if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4
598 return true;
599 } else if (!SecondType->isScalarType()) // C99 6.8.5p2
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000600 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar)
Chris Lattnerd1625842008-11-24 06:25:27 +0000601 << SecondType << Second->getSourceRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 }
Steve Naroffb5a69582007-08-31 23:28:33 +0000603 return new ForStmt(First, Second, Third, Body, ForLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000604}
605
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000606Action::StmtResult
Fariborz Jahanian75712282008-01-10 00:24:29 +0000607Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000608 SourceLocation LParenLoc,
609 StmtTy *first, ExprTy *second,
610 SourceLocation RParenLoc, StmtTy *body) {
611 Stmt *First = static_cast<Stmt*>(first);
612 Expr *Second = static_cast<Expr*>(second);
613 Stmt *Body = static_cast<Stmt*>(body);
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000614 if (First) {
615 QualType FirstType;
616 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
Ted Kremenekf34afee2008-10-06 20:58:11 +0000617 if (!DS->hasSolitaryDecl())
618 return Diag((*DS->decl_begin())->getLocation(),
619 diag::err_toomany_element_decls);
620
621 ScopedDecl *D = DS->getSolitaryDecl();
622 FirstType = cast<ValueDecl>(D)->getType();
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000623 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
624 // declare identifiers for objects having storage class 'auto' or
625 // 'register'.
Steve Naroff248a7532008-04-15 22:42:06 +0000626 VarDecl *VD = cast<VarDecl>(D);
627 if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
628 return Diag(VD->getLocation(), diag::err_non_variable_decl_in_for);
Anders Carlsson1fe379f2008-08-25 18:16:36 +0000629 } else {
630 Expr::isLvalueResult lval = cast<Expr>(First)->isLvalue(Context);
631
632 if (lval != Expr::LV_Valid)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000633 return Diag(First->getLocStart(), diag::err_selector_element_not_lvalue)
634 << First->getSourceRange();
Anders Carlsson1fe379f2008-08-25 18:16:36 +0000635
636 FirstType = static_cast<Expr*>(first)->getType();
637 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000638 if (!Context.isObjCObjectPointerType(FirstType))
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000639 Diag(ForLoc, diag::err_selector_element_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000640 << FirstType << First->getSourceRange();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000641 }
642 if (Second) {
643 DefaultFunctionArrayConversion(Second);
644 QualType SecondType = Second->getType();
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000645 if (!Context.isObjCObjectPointerType(SecondType))
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000646 Diag(ForLoc, diag::err_collection_expr_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000647 << SecondType << Second->getSourceRange();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000648 }
Fariborz Jahanian75712282008-01-10 00:24:29 +0000649 return new ObjCForCollectionStmt(First, Second, Body, ForLoc, RParenLoc);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000650}
Reid Spencer5f016e22007-07-11 17:01:13 +0000651
652Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000653Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 IdentifierInfo *LabelII) {
Steve Naroff4eb206b2008-09-03 18:15:37 +0000655 // If we are in a block, reject all gotos for now.
656 if (CurBlock)
657 return Diag(GotoLoc, diag::err_goto_in_block);
658
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 // Look up the record for this label identifier.
660 LabelStmt *&LabelDecl = LabelMap[LabelII];
661
662 // If we haven't seen this label yet, create a forward reference.
663 if (LabelDecl == 0)
664 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
665
Ted Kremenek61f62162007-09-06 17:11:52 +0000666 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000667}
668
669Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000670Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 ExprTy *DestExp) {
672 // FIXME: Verify that the operand is convertible to void*.
673
674 return new IndirectGotoStmt((Expr*)DestExp);
675}
676
677Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000678Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 Scope *S = CurScope->getContinueParent();
680 if (!S) {
681 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
682 Diag(ContinueLoc, diag::err_continue_not_in_loop);
683 return true;
684 }
685
Steve Naroff507f2d52007-08-31 23:49:30 +0000686 return new ContinueStmt(ContinueLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000687}
688
689Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000690Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 Scope *S = CurScope->getBreakParent();
692 if (!S) {
693 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
694 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
695 return true;
696 }
697
Steve Naroff507f2d52007-08-31 23:49:30 +0000698 return new BreakStmt(BreakLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000699}
700
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000701/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000702///
703Action::StmtResult
704Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
705
706 // If this is the first return we've seen in the block, infer the type of
707 // the block from it.
708 if (CurBlock->ReturnType == 0) {
Steve Naroffc50a4a52008-09-16 22:25:10 +0000709 if (RetValExp) {
Steve Naroff16564422008-09-24 22:26:48 +0000710 // Don't call UsualUnaryConversions(), since we don't want to do
711 // integer promotions here.
712 DefaultFunctionArrayConversion(RetValExp);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000713 CurBlock->ReturnType = RetValExp->getType().getTypePtr();
Steve Naroffc50a4a52008-09-16 22:25:10 +0000714 } else
Steve Naroff4eb206b2008-09-03 18:15:37 +0000715 CurBlock->ReturnType = Context.VoidTy.getTypePtr();
716 return new ReturnStmt(ReturnLoc, RetValExp);
717 }
718
719 // Otherwise, verify that this result type matches the previous one. We are
720 // pickier with blocks than for normal functions because we don't have GCC
721 // compatibility to worry about here.
722 if (CurBlock->ReturnType->isVoidType()) {
723 if (RetValExp) {
724 Diag(ReturnLoc, diag::err_return_block_has_expr);
725 delete RetValExp;
726 RetValExp = 0;
727 }
728 return new ReturnStmt(ReturnLoc, RetValExp);
729 }
730
731 if (!RetValExp) {
732 Diag(ReturnLoc, diag::err_block_return_missing_expr);
733 return true;
734 }
735
736 // we have a non-void block with an expression, continue checking
737 QualType RetValType = RetValExp->getType();
738
739 // For now, restrict multiple return statements in a block to have
740 // strict compatible types only.
741 QualType BlockQT = QualType(CurBlock->ReturnType, 0);
742 if (Context.getCanonicalType(BlockQT).getTypePtr()
743 != Context.getCanonicalType(RetValType).getTypePtr()) {
744 DiagnoseAssignmentResult(Incompatible, ReturnLoc, BlockQT,
745 RetValType, RetValExp, "returning");
746 return true;
747 }
748
749 if (RetValExp) CheckReturnStackAddr(RetValExp, BlockQT, ReturnLoc);
750
751 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
752}
Reid Spencer5f016e22007-07-11 17:01:13 +0000753
754Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000755Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
Steve Naroff90045e82007-07-13 23:32:42 +0000756 Expr *RetValExp = static_cast<Expr *>(rex);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000757 if (CurBlock)
758 return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
Chris Lattner371f2582008-12-04 23:50:19 +0000759
760 QualType FnRetType;
761 if (FunctionDecl *FD = getCurFunctionDecl())
762 FnRetType = FD->getResultType();
763 else
764 FnRetType = getCurMethodDecl()->getResultType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000765
Chris Lattner5cf216b2008-01-04 18:04:52 +0000766 if (FnRetType->isVoidType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000767 if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns)
Chris Lattner65ce04b2008-12-18 02:01:17 +0000768 unsigned D = diag::ext_return_has_expr;
769 if (RetValExp->getType()->isVoidType())
770 D = diag::ext_return_has_void_expr;
Chris Lattner65ce04b2008-12-18 02:01:17 +0000771
Chris Lattnere878eb02008-12-18 02:03:48 +0000772 // return (some void expression); is legal in C++.
773 if (D != diag::ext_return_has_void_expr ||
774 !getLangOptions().CPlusPlus) {
775 NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
776 Diag(ReturnLoc, D)
777 << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
778 << RetValExp->getSourceRange();
779 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 }
Chris Lattner3c73c412008-11-19 08:23:25 +0000781 return new ReturnStmt(ReturnLoc, RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 }
Chris Lattner3c73c412008-11-19 08:23:25 +0000783
784 if (!RetValExp) {
785 unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4
786 // C99 6.8.6.4p1 (ext_ since GCC warns)
787 if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
788
789 if (FunctionDecl *FD = getCurFunctionDecl())
Chris Lattner08631c52008-11-23 21:45:46 +0000790 Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
Chris Lattner3c73c412008-11-19 08:23:25 +0000791 else
Chris Lattner08631c52008-11-23 21:45:46 +0000792 Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
Chris Lattner3c73c412008-11-19 08:23:25 +0000793 return new ReturnStmt(ReturnLoc, (Expr*)0);
794 }
795
Douglas Gregor898574e2008-12-05 23:32:09 +0000796 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
797 // we have a non-void function with an expression, continue checking
798 QualType RetValType = RetValExp->getType();
799
800 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
801 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
802 // function return.
803
804 // In C++ the return statement is handled via a copy initialization.
805 // the C version of which boils down to
806 // CheckSingleAssignmentConstraints.
807 if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
808 return true;
Ted Kremenek06de2762007-08-17 16:46:58 +0000809
Douglas Gregor898574e2008-12-05 23:32:09 +0000810 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
811 }
812
Steve Naroff507f2d52007-08-31 23:49:30 +0000813 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000814}
815
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000816Sema::StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
Chris Lattner08631c52008-11-23 21:45:46 +0000817 bool IsSimple,
Anders Carlsson39c47b52007-11-23 23:12:25 +0000818 bool IsVolatile,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000819 unsigned NumOutputs,
820 unsigned NumInputs,
821 std::string *Names,
Chris Lattner1708b962008-08-18 19:55:17 +0000822 ExprTy **constraints,
823 ExprTy **exprs,
Chris Lattner6bc52112008-07-23 06:46:56 +0000824 ExprTy *asmString,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000825 unsigned NumClobbers,
Chris Lattner1708b962008-08-18 19:55:17 +0000826 ExprTy **clobbers,
Chris Lattnerfe795952007-10-29 04:04:16 +0000827 SourceLocation RParenLoc) {
Chris Lattner1708b962008-08-18 19:55:17 +0000828 StringLiteral **Constraints = reinterpret_cast<StringLiteral**>(constraints);
829 Expr **Exprs = reinterpret_cast<Expr **>(exprs);
Chris Lattner6bc52112008-07-23 06:46:56 +0000830 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString);
Chris Lattner1708b962008-08-18 19:55:17 +0000831 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers);
832
833 // The parser verifies that there is a string literal here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000834 if (AsmString->isWide())
835 // FIXME: We currently leak memory here.
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000836 return Diag(AsmString->getLocStart(), diag::err_asm_wide_character)
837 << AsmString->getSourceRange();
Chris Lattner6bc52112008-07-23 06:46:56 +0000838
839
Chris Lattner1708b962008-08-18 19:55:17 +0000840 for (unsigned i = 0; i != NumOutputs; i++) {
841 StringLiteral *Literal = Constraints[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000842 if (Literal->isWide())
843 // FIXME: We currently leak memory here.
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000844 return Diag(Literal->getLocStart(), diag::err_asm_wide_character)
845 << Literal->getSourceRange();
Chris Lattner6bc52112008-07-23 06:46:56 +0000846
Anders Carlssond04c6e22007-11-27 04:11:28 +0000847 std::string OutputConstraint(Literal->getStrData(),
848 Literal->getByteLength());
849
850 TargetInfo::ConstraintInfo info;
Chris Lattner6bc52112008-07-23 06:46:56 +0000851 if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info))
Anders Carlssond04c6e22007-11-27 04:11:28 +0000852 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000853 return Diag(Literal->getLocStart(),
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000854 diag::err_asm_invalid_output_constraint) << OutputConstraint;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000855
856 // Check that the output exprs are valid lvalues.
Chris Lattner1708b962008-08-18 19:55:17 +0000857 ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]);
Chris Lattner28be73f2008-07-26 21:30:36 +0000858 Expr::isLvalueResult Result = OutputExpr->isLvalue(Context);
Anders Carlsson04728b72007-11-23 19:43:50 +0000859 if (Result != Expr::LV_Valid) {
Anders Carlsson04728b72007-11-23 19:43:50 +0000860 // FIXME: We currently leak memory here.
Chris Lattner1708b962008-08-18 19:55:17 +0000861 return Diag(OutputExpr->getSubExpr()->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000862 diag::err_asm_invalid_lvalue_in_output)
863 << OutputExpr->getSubExpr()->getSourceRange();
Anders Carlsson04728b72007-11-23 19:43:50 +0000864 }
865 }
866
Anders Carlsson04728b72007-11-23 19:43:50 +0000867 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
Chris Lattner1708b962008-08-18 19:55:17 +0000868 StringLiteral *Literal = Constraints[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000869 if (Literal->isWide())
870 // FIXME: We currently leak memory here.
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000871 return Diag(Literal->getLocStart(), diag::err_asm_wide_character)
872 << Literal->getSourceRange();
Anders Carlsson04728b72007-11-23 19:43:50 +0000873
Anders Carlssond04c6e22007-11-27 04:11:28 +0000874 std::string InputConstraint(Literal->getStrData(),
875 Literal->getByteLength());
876
877 TargetInfo::ConstraintInfo info;
878 if (!Context.Target.validateInputConstraint(InputConstraint.c_str(),
Chris Lattner1708b962008-08-18 19:55:17 +0000879 NumOutputs, info)) {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000880 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000881 return Diag(Literal->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000882 diag::err_asm_invalid_input_constraint) << InputConstraint;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000883 }
884
885 // Check that the input exprs aren't of type void.
Chris Lattner1708b962008-08-18 19:55:17 +0000886 ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]);
Anders Carlsson04728b72007-11-23 19:43:50 +0000887 if (InputExpr->getType()->isVoidType()) {
Anders Carlsson04728b72007-11-23 19:43:50 +0000888
Anders Carlsson04728b72007-11-23 19:43:50 +0000889 // FIXME: We currently leak memory here.
Chris Lattner1708b962008-08-18 19:55:17 +0000890 return Diag(InputExpr->getSubExpr()->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000891 diag::err_asm_invalid_type_in_input)
Chris Lattnerd1625842008-11-24 06:25:27 +0000892 << InputExpr->getType() << InputConstraint
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000893 << InputExpr->getSubExpr()->getSourceRange();
Anders Carlsson04728b72007-11-23 19:43:50 +0000894 }
Anders Carlsson562489e2008-12-31 07:27:38 +0000895
896 if (info & TargetInfo::CI_AllowsRegister)
897 DefaultFunctionArrayConversion(Exprs[i]);
Anders Carlsson04728b72007-11-23 19:43:50 +0000898 }
Anders Carlssonb235fc22007-11-22 01:36:19 +0000899
Anders Carlsson6fa90862007-11-25 00:25:21 +0000900 // Check that the clobbers are valid.
Chris Lattner1708b962008-08-18 19:55:17 +0000901 for (unsigned i = 0; i != NumClobbers; i++) {
902 StringLiteral *Literal = Clobbers[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000903 if (Literal->isWide())
904 // FIXME: We currently leak memory here.
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000905 return Diag(Literal->getLocStart(), diag::err_asm_wide_character)
906 << Literal->getSourceRange();
Anders Carlsson6fa90862007-11-25 00:25:21 +0000907
908 llvm::SmallString<16> Clobber(Literal->getStrData(),
909 Literal->getStrData() +
910 Literal->getByteLength());
911
Chris Lattner6bc52112008-07-23 06:46:56 +0000912 if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000913 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000914 return Diag(Literal->getLocStart(),
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000915 diag::err_asm_unknown_register_name) << Clobber.c_str();
Anders Carlsson6fa90862007-11-25 00:25:21 +0000916 }
917
Chris Lattner1708b962008-08-18 19:55:17 +0000918 return new AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs,
919 Names, Constraints, Exprs, AsmString, NumClobbers,
920 Clobbers, RParenLoc);
Chris Lattnerfe795952007-10-29 04:04:16 +0000921}
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000922
923Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000924Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000925 SourceLocation RParen, StmtTy *Parm,
926 StmtTy *Body, StmtTy *CatchList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000927 ObjCAtCatchStmt *CS = new ObjCAtCatchStmt(AtLoc, RParen,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000928 static_cast<Stmt*>(Parm), static_cast<Stmt*>(Body),
929 static_cast<Stmt*>(CatchList));
930 return CatchList ? CatchList : CS;
931}
932
Fariborz Jahanian161a9c52007-11-02 00:18:53 +0000933Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000934Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtTy *Body) {
935 ObjCAtFinallyStmt *FS = new ObjCAtFinallyStmt(AtLoc,
Fariborz Jahanian161a9c52007-11-02 00:18:53 +0000936 static_cast<Stmt*>(Body));
937 return FS;
938}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000939
940Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000941Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000942 StmtTy *Try, StmtTy *Catch, StmtTy *Finally) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000943 ObjCAtTryStmt *TS = new ObjCAtTryStmt(AtLoc, static_cast<Stmt*>(Try),
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000944 static_cast<Stmt*>(Catch),
945 static_cast<Stmt*>(Finally));
946 return TS;
947}
948
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000949Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000950Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, StmtTy *Throw) {
951 ObjCAtThrowStmt *TS = new ObjCAtThrowStmt(AtLoc, static_cast<Stmt*>(Throw));
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000952 return TS;
953}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000954
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000955Action::StmtResult
956Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprTy *SynchExpr,
957 StmtTy *SynchBody) {
958 ObjCAtSynchronizedStmt *SS = new ObjCAtSynchronizedStmt(AtLoc,
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000959 static_cast<Stmt*>(SynchExpr), static_cast<Stmt*>(SynchBody));
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000960 return SS;
961}
Sebastian Redl4b07b292008-12-22 19:15:10 +0000962
963/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
964/// and creates a proper catch handler from them.
965Action::OwningStmtResult
966Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclTy *ExDecl,
967 StmtArg HandlerBlock) {
968 // There's nothing to test that ActOnExceptionDecl didn't already test.
969 return Owned(new CXXCatchStmt(CatchLoc, static_cast<VarDecl*>(ExDecl),
970 static_cast<Stmt*>(HandlerBlock.release())));
971}
Sebastian Redl8351da02008-12-22 21:35:02 +0000972
973/// ActOnCXXTryBlock - Takes a try compound-statement and a number of
974/// handlers and creates a try statement from them.
975Action::OwningStmtResult
976Sema::ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock,
977 MultiStmtArg RawHandlers) {
978 unsigned NumHandlers = RawHandlers.size();
979 assert(NumHandlers > 0 &&
980 "The parser shouldn't call this if there are no handlers.");
981 Stmt **Handlers = reinterpret_cast<Stmt**>(RawHandlers.get());
982
983 for(unsigned i = 0; i < NumHandlers - 1; ++i) {
984 CXXCatchStmt *Handler = llvm::cast<CXXCatchStmt>(Handlers[i]);
985 if (!Handler->getExceptionDecl())
986 return StmtError(Diag(Handler->getLocStart(), diag::err_early_catch_all));
987 }
988 // FIXME: We should detect handlers for the same type as an earlier one.
989 // This one is rather easy.
990 // FIXME: We should detect handlers that cannot catch anything because an
991 // earlier handler catches a superclass. Need to find a method that is not
992 // quadratic for this.
993 // Neither of these are explicitly forbidden, but every compiler detects them
994 // and warns.
995
996 RawHandlers.release();
997 return Owned(new CXXTryStmt(TryLoc, static_cast<Stmt*>(TryBlock.release()),
998 Handlers, NumHandlers));
999}