blob: 22a9617eb7f5b4565ea9c74fae9d6db5b25cfef0 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for statements.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Anders Carlssone8bd9f22008-11-22 21:04:56 +000015#include "clang/AST/APValue.h"
Chris Lattner3429a812007-08-23 05:46:52 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/AST/Expr.h"
Anders Carlsson49dadd62007-11-25 00:25:21 +000019#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/Basic/Diagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000023Sema::OwningStmtResult Sema::ActOnExprStmt(ExprArg expr) {
24 Expr *E = static_cast<Expr*>(expr.release());
Steve Naroff5cbb02f2007-09-16 14:56:35 +000025 assert(E && "ActOnExprStmt(): missing expression");
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000026
Chris Lattnere02e4402008-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 Redl76b9ddb2008-12-21 12:04:03 +000030
Chris Lattnere02e4402008-07-25 23:18:17 +000031 // Same thing in for stmt first clause (when expr) and third clause.
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000032 return Owned(static_cast<Stmt*>(E));
Chris Lattner4b009652007-07-25 00:24:17 +000033}
34
35
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000036Sema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
37 return Owned(new NullStmt(SemiLoc));
Chris Lattner4b009652007-07-25 00:24:17 +000038}
39
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000040Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclTy *decl,
41 SourceLocation StartLoc,
42 SourceLocation EndLoc) {
Chris Lattnera4ff4272008-03-13 06:29:04 +000043 if (decl == 0)
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000044 return StmtError();
45
Chris Lattnera4ff4272008-03-13 06:29:04 +000046 ScopedDecl *SD = cast<ScopedDecl>(static_cast<Decl *>(decl));
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000047
Ted Kremenek1bc18e62008-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 Redl76b9ddb2008-12-21 12:04:03 +000059
Ted Kremenek1bc18e62008-10-07 23:09:49 +000060 if (decls.size() == 1) {
61 DeclGroupOwningRef DG(*decls.begin());
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000062 return Owned(new DeclStmt(DG, StartLoc, EndLoc));
Ted Kremenek1bc18e62008-10-07 23:09:49 +000063 }
64 else {
Chris Lattnerb1753422008-11-23 21:45:46 +000065 DeclGroupOwningRef DG(DeclGroup::Create(Context, decls.size(), &decls[0]));
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000066 return Owned(new DeclStmt(DG, StartLoc, EndLoc));
Ted Kremenek1bc18e62008-10-07 23:09:49 +000067 }
Chris Lattner4b009652007-07-25 00:24:17 +000068}
69
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000070Action::OwningStmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +000071Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Sebastian Redl76b9ddb2008-12-21 12:04:03 +000072 MultiStmtArg elts, bool isStmtExpr) {
73 unsigned NumElts = elts.size();
74 Stmt **Elts = reinterpret_cast<Stmt**>(elts.release());
Chris Lattner3ea3b662007-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 Kremenekc7350432008-10-06 18:48:35 +000089 ScopedDecl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
Chris Lattner3ea3b662007-08-27 04:29:41 +000090 Diag(D->getLocation(), diag::ext_mixed_decls_code);
91 }
92 }
Chris Lattnerf2b07572007-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 Lattner8ba580c2008-11-19 05:08:23 +0000110 Diag(BO->getOperatorLoc(), diag::warn_unused_expr)
111 << BO->getLHS()->getSourceRange() << BO->getRHS()->getSourceRange();
Chris Lattnerf2b07572007-08-31 21:49:55 +0000112 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000113 Diag(UO->getOperatorLoc(), diag::warn_unused_expr)
114 << UO->getSubExpr()->getSourceRange();
Sebastian Redl76b9ddb2008-12-21 12:04:03 +0000115 else
Chris Lattner8ba580c2008-11-19 05:08:23 +0000116 Diag(E->getExprLoc(), diag::warn_unused_expr) << E->getSourceRange();
Chris Lattnerf2b07572007-08-31 21:49:55 +0000117 }
Sebastian Redl76b9ddb2008-12-21 12:04:03 +0000118
119 return Owned(new CompoundStmt(Elts, NumElts, L, R));
Chris Lattner4b009652007-07-25 00:24:17 +0000120}
121
122Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000123Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner4b009652007-07-25 00:24:17 +0000124 SourceLocation DotDotDotLoc, ExprTy *rhsval,
125 SourceLocation ColonLoc, StmtTy *subStmt) {
126 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
127 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
128 assert((LHSVal != 0) && "missing expression in case statement");
129
Chris Lattner4b009652007-07-25 00:24:17 +0000130 // C99 6.8.4.2p3: The expression shall be an integer constant.
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000131 // However, GCC allows any evaluatable integer expression.
Anders Carlsson613314d2008-12-01 02:13:02 +0000132
133 if (VerifyIntegerConstantExpression(LHSVal))
Chris Lattner4b009652007-07-25 00:24:17 +0000134 return SubStmt;
Chris Lattner4b009652007-07-25 00:24:17 +0000135
136 // GCC extension: The expression shall be an integer constant.
Anders Carlsson613314d2008-12-01 02:13:02 +0000137
138 if (RHSVal && VerifyIntegerConstantExpression(RHSVal))
Chris Lattner3429a812007-08-23 05:46:52 +0000139 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner4b009652007-07-25 00:24:17 +0000140
141 if (SwitchStack.empty()) {
142 Diag(CaseLoc, diag::err_case_not_in_switch);
143 return SubStmt;
144 }
145
Steve Naroff5d2fff82007-08-31 23:28:33 +0000146 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000147 SwitchStack.back()->addSwitchCase(CS);
148 return CS;
149}
150
151Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000152Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000153 StmtTy *subStmt, Scope *CurScope) {
154 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
155
156 if (SwitchStack.empty()) {
157 Diag(DefaultLoc, diag::err_default_not_in_switch);
158 return SubStmt;
159 }
160
161 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
162 SwitchStack.back()->addSwitchCase(DS);
163
164 return DS;
165}
166
167Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000168Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner4b009652007-07-25 00:24:17 +0000169 SourceLocation ColonLoc, StmtTy *subStmt) {
170 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
171 // Look up the record for this label identifier.
172 LabelStmt *&LabelDecl = LabelMap[II];
173
174 // If not forward referenced or defined already, just create a new LabelStmt.
175 if (LabelDecl == 0)
176 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
177
178 assert(LabelDecl->getID() == II && "Label mismatch!");
179
180 // Otherwise, this label was either forward reference or multiply defined. If
181 // multiply defined, reject it now.
182 if (LabelDecl->getSubStmt()) {
Chris Lattnerb1753422008-11-23 21:45:46 +0000183 Diag(IdentLoc, diag::err_redefinition_of_label) << LabelDecl->getID();
Chris Lattner1336cab2008-11-23 23:12:31 +0000184 Diag(LabelDecl->getIdentLoc(), diag::note_previous_definition);
Chris Lattner4b009652007-07-25 00:24:17 +0000185 return SubStmt;
186 }
187
188 // Otherwise, this label was forward declared, and we just found its real
189 // definition. Fill in the forward definition and return it.
190 LabelDecl->setIdentLoc(IdentLoc);
191 LabelDecl->setSubStmt(SubStmt);
192 return LabelDecl;
193}
194
195Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000196Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
Chris Lattner4b009652007-07-25 00:24:17 +0000197 StmtTy *ThenVal, SourceLocation ElseLoc,
198 StmtTy *ElseVal) {
199 Expr *condExpr = (Expr *)CondVal;
Anders Carlsson663733e2007-10-10 20:50:11 +0000200 Stmt *thenStmt = (Stmt *)ThenVal;
201
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000202 assert(condExpr && "ActOnIfStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000203
204 DefaultFunctionArrayConversion(condExpr);
205 QualType condType = condExpr->getType();
206
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000207 if (getLangOptions().CPlusPlus) {
208 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
209 return true;
210 } else if (!condType->isScalarType()) // C99 6.8.4.1p1
Chris Lattner77d52da2008-11-20 06:06:08 +0000211 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000212 << condType << condExpr->getSourceRange();
Chris Lattner4b009652007-07-25 00:24:17 +0000213
Anders Carlsson663733e2007-10-10 20:50:11 +0000214 // Warn if the if block has a null body without an else value.
215 // this helps prevent bugs due to typos, such as
216 // if (condition);
217 // do_stuff();
218 if (!ElseVal) {
219 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
220 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
221 }
222
223 return new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal);
Chris Lattner4b009652007-07-25 00:24:17 +0000224}
225
226Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000227Sema::ActOnStartOfSwitchStmt(ExprTy *cond) {
Chris Lattner3429a812007-08-23 05:46:52 +0000228 Expr *Cond = static_cast<Expr*>(cond);
229
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000230 if (getLangOptions().CPlusPlus) {
231 // C++ 6.4.2.p2:
232 // The condition shall be of integral type, enumeration type, or of a class
233 // type for which a single conversion function to integral or enumeration
234 // type exists (12.3). If the condition is of class type, the condition is
235 // converted by calling that conversion function, and the result of the
236 // conversion is used in place of the original condition for the remainder
237 // of this section. Integral promotions are performed.
238
239 QualType Ty = Cond->getType();
240
241 // FIXME: Handle class types.
242
243 // If the type is wrong a diagnostic will be emitted later at
244 // ActOnFinishSwitchStmt.
245 if (Ty->isIntegralType() || Ty->isEnumeralType()) {
246 // Integral promotions are performed.
247 // FIXME: Integral promotions for C++ are not complete.
248 UsualUnaryConversions(Cond);
249 }
250 } else {
251 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
252 UsualUnaryConversions(Cond);
253 }
Chris Lattner3429a812007-08-23 05:46:52 +0000254
255 SwitchStmt *SS = new SwitchStmt(Cond);
Chris Lattner4b009652007-07-25 00:24:17 +0000256 SwitchStack.push_back(SS);
257 return SS;
258}
259
Chris Lattner3429a812007-08-23 05:46:52 +0000260/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
261/// the specified width and sign. If an overflow occurs, detect it and emit
262/// the specified diagnostic.
263void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
264 unsigned NewWidth, bool NewSign,
265 SourceLocation Loc,
266 unsigned DiagID) {
267 // Perform a conversion to the promoted condition type if needed.
268 if (NewWidth > Val.getBitWidth()) {
269 // If this is an extension, just do it.
270 llvm::APSInt OldVal(Val);
271 Val.extend(NewWidth);
272
273 // If the input was signed and negative and the output is unsigned,
274 // warn.
275 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
Chris Lattner77d52da2008-11-20 06:06:08 +0000276 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattner3429a812007-08-23 05:46:52 +0000277
278 Val.setIsSigned(NewSign);
279 } else if (NewWidth < Val.getBitWidth()) {
280 // If this is a truncation, check for overflow.
281 llvm::APSInt ConvVal(Val);
282 ConvVal.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000283 ConvVal.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000284 ConvVal.extend(Val.getBitWidth());
Chris Lattner5c039602007-08-23 22:08:35 +0000285 ConvVal.setIsSigned(Val.isSigned());
Chris Lattner3429a812007-08-23 05:46:52 +0000286 if (ConvVal != Val)
Chris Lattner77d52da2008-11-20 06:06:08 +0000287 Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10);
Chris Lattner3429a812007-08-23 05:46:52 +0000288
289 // Regardless of whether a diagnostic was emitted, really do the
290 // truncation.
291 Val.trunc(NewWidth);
Chris Lattner5c039602007-08-23 22:08:35 +0000292 Val.setIsSigned(NewSign);
Chris Lattner3429a812007-08-23 05:46:52 +0000293 } else if (NewSign != Val.isSigned()) {
294 // Convert the sign to match the sign of the condition. This can cause
295 // overflow as well: unsigned(INTMIN)
296 llvm::APSInt OldVal(Val);
297 Val.setIsSigned(NewSign);
298
299 if (Val.isNegative()) // Sign bit changes meaning.
Chris Lattner77d52da2008-11-20 06:06:08 +0000300 Diag(Loc, DiagID) << OldVal.toString(10) << Val.toString(10);
Chris Lattner3429a812007-08-23 05:46:52 +0000301 }
302}
303
Chris Lattner0ab833c2007-08-23 18:29:20 +0000304namespace {
305 struct CaseCompareFunctor {
306 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
307 const llvm::APSInt &RHS) {
308 return LHS.first < RHS;
309 }
Chris Lattner2157f272007-09-03 18:31:57 +0000310 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
311 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
312 return LHS.first < RHS.first;
313 }
Chris Lattner0ab833c2007-08-23 18:29:20 +0000314 bool operator()(const llvm::APSInt &LHS,
315 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
316 return LHS < RHS.first;
317 }
318 };
319}
320
Chris Lattner766afb82007-09-21 18:15:22 +0000321/// CmpCaseVals - Comparison predicate for sorting case values.
322///
323static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
324 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
325 if (lhs.first < rhs.first)
326 return true;
327
328 if (lhs.first == rhs.first &&
329 lhs.second->getCaseLoc().getRawEncoding()
330 < rhs.second->getCaseLoc().getRawEncoding())
331 return true;
332 return false;
333}
334
Chris Lattner4b009652007-07-25 00:24:17 +0000335Action::StmtResult
Chris Lattner766afb82007-09-21 18:15:22 +0000336Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
337 ExprTy *Body) {
Chris Lattner4b009652007-07-25 00:24:17 +0000338 Stmt *BodyStmt = (Stmt*)Body;
339
340 SwitchStmt *SS = SwitchStack.back();
341 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
342
Steve Naroffa610eab2007-09-01 21:08:38 +0000343 SS->setBody(BodyStmt, SwitchLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000344 SwitchStack.pop_back();
345
Chris Lattner3429a812007-08-23 05:46:52 +0000346 Expr *CondExpr = SS->getCond();
347 QualType CondType = CondExpr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000348
Chris Lattner3429a812007-08-23 05:46:52 +0000349 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Chris Lattner77d52da2008-11-20 06:06:08 +0000350 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000351 << CondType << CondExpr->getSourceRange();
Chris Lattner3429a812007-08-23 05:46:52 +0000352 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000353 }
Chris Lattner3429a812007-08-23 05:46:52 +0000354
355 // Get the bitwidth of the switched-on value before promotions. We must
356 // convert the integer case values to this width before comparison.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000357 unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType));
Chris Lattner3429a812007-08-23 05:46:52 +0000358 bool CondIsSigned = CondType->isSignedIntegerType();
359
360 // Accumulate all of the case values in a vector so that we can sort them
361 // and detect duplicates. This vector contains the APInt for the case after
362 // it has been converted to the condition type.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000363 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
364 CaseValsTy CaseVals;
Chris Lattner3429a812007-08-23 05:46:52 +0000365
366 // Keep track of any GNU case ranges we see. The APSInt is the low value.
367 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
368
369 DefaultStmt *TheDefaultStmt = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000370
Chris Lattner1a4066d2007-08-23 06:23:56 +0000371 bool CaseListIsErroneous = false;
372
373 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Chris Lattner4b009652007-07-25 00:24:17 +0000374 SC = SC->getNextSwitchCase()) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000375
Chris Lattner4b009652007-07-25 00:24:17 +0000376 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattner3429a812007-08-23 05:46:52 +0000377 if (TheDefaultStmt) {
378 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
Chris Lattner1336cab2008-11-23 23:12:31 +0000379 Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev);
Chris Lattner4b009652007-07-25 00:24:17 +0000380
Chris Lattner3429a812007-08-23 05:46:52 +0000381 // FIXME: Remove the default statement from the switch block so that
382 // we'll return a valid AST. This requires recursing down the
383 // AST and finding it, not something we are set up to do right now. For
384 // now, just lop the entire switch stmt out of the AST.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000385 CaseListIsErroneous = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000386 }
Chris Lattner3429a812007-08-23 05:46:52 +0000387 TheDefaultStmt = DS;
Chris Lattner4b009652007-07-25 00:24:17 +0000388
Chris Lattner3429a812007-08-23 05:46:52 +0000389 } else {
390 CaseStmt *CS = cast<CaseStmt>(SC);
391
392 // We already verified that the expression has a i-c-e value (C99
393 // 6.8.4.2p3) - get that value now.
Chris Lattnere992d6c2008-01-16 19:17:22 +0000394 Expr *Lo = CS->getLHS();
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000395 llvm::APSInt LoVal = Lo->EvaluateAsInt(Context);
Chris Lattner3429a812007-08-23 05:46:52 +0000396
397 // Convert the value to the same width/sign as the condition.
398 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
399 CS->getLHS()->getLocStart(),
400 diag::warn_case_value_overflow);
Chris Lattner4b009652007-07-25 00:24:17 +0000401
Chris Lattnere992d6c2008-01-16 19:17:22 +0000402 // If the LHS is not the same type as the condition, insert an implicit
403 // cast.
404 ImpCastExprToType(Lo, CondType);
405 CS->setLHS(Lo);
406
Chris Lattner1a4066d2007-08-23 06:23:56 +0000407 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattner3429a812007-08-23 05:46:52 +0000408 if (CS->getRHS())
409 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattner1a4066d2007-08-23 06:23:56 +0000410 else
411 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattner3429a812007-08-23 05:46:52 +0000412 }
413 }
414
Chris Lattner1a4066d2007-08-23 06:23:56 +0000415 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner766afb82007-09-21 18:15:22 +0000416 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattner3429a812007-08-23 05:46:52 +0000417
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000418 if (!CaseVals.empty()) {
419 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
420 if (CaseVals[i].first == CaseVals[i+1].first) {
421 // If we have a duplicate, report it.
422 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
Chris Lattner77d52da2008-11-20 06:06:08 +0000423 diag::err_duplicate_case) << CaseVals[i].first.toString(10);
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000424 Diag(CaseVals[i].second->getLHS()->getLocStart(),
Chris Lattner1336cab2008-11-23 23:12:31 +0000425 diag::note_duplicate_case_prev);
Chris Lattner2b1b9a82007-08-23 14:29:07 +0000426 // FIXME: We really want to remove the bogus case stmt from the substmt,
427 // but we have no way to do this right now.
428 CaseListIsErroneous = true;
429 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000430 }
431 }
Chris Lattner3429a812007-08-23 05:46:52 +0000432
Chris Lattner1a4066d2007-08-23 06:23:56 +0000433 // Detect duplicate case ranges, which usually don't exist at all in the first
434 // place.
435 if (!CaseRanges.empty()) {
436 // Sort all the case ranges by their low value so we can easily detect
437 // overlaps between ranges.
Chris Lattner0ab833c2007-08-23 18:29:20 +0000438 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattner1a4066d2007-08-23 06:23:56 +0000439
440 // Scan the ranges, computing the high values and removing empty ranges.
441 std::vector<llvm::APSInt> HiVals;
Chris Lattner7443e0f2007-08-23 17:48:14 +0000442 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner1a4066d2007-08-23 06:23:56 +0000443 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnere992d6c2008-01-16 19:17:22 +0000444 Expr *Hi = CR->getRHS();
Anders Carlssone8bd9f22008-11-22 21:04:56 +0000445 llvm::APSInt HiVal = Hi->EvaluateAsInt(Context);
Chris Lattner1a4066d2007-08-23 06:23:56 +0000446
447 // Convert the value to the same width/sign as the condition.
448 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
449 CR->getRHS()->getLocStart(),
450 diag::warn_case_value_overflow);
451
Chris Lattnere992d6c2008-01-16 19:17:22 +0000452 // If the LHS is not the same type as the condition, insert an implicit
453 // cast.
454 ImpCastExprToType(Hi, CondType);
455 CR->setRHS(Hi);
456
Chris Lattner7443e0f2007-08-23 17:48:14 +0000457 // If the low value is bigger than the high value, the case is empty.
458 if (CaseRanges[i].first > HiVal) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000459 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range)
460 << SourceRange(CR->getLHS()->getLocStart(),
461 CR->getRHS()->getLocEnd());
Chris Lattner7443e0f2007-08-23 17:48:14 +0000462 CaseRanges.erase(CaseRanges.begin()+i);
463 --i, --e;
464 continue;
465 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000466 HiVals.push_back(HiVal);
467 }
468
469 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0ab833c2007-08-23 18:29:20 +0000470 // ranges. Since the range list is sorted, we only need to compare case
471 // ranges with their neighbors.
Chris Lattner1a4066d2007-08-23 06:23:56 +0000472 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0ab833c2007-08-23 18:29:20 +0000473 llvm::APSInt &CRLo = CaseRanges[i].first;
474 llvm::APSInt &CRHi = HiVals[i];
475 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1a4066d2007-08-23 06:23:56 +0000476
Chris Lattner0ab833c2007-08-23 18:29:20 +0000477 // Check to see whether the case range overlaps with any singleton cases.
478 CaseStmt *OverlapStmt = 0;
479 llvm::APSInt OverlapVal(32);
480
481 // Find the smallest value >= the lower bound. If I is in the case range,
482 // then we have overlap.
483 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
484 CaseVals.end(), CRLo,
485 CaseCompareFunctor());
486 if (I != CaseVals.end() && I->first < CRHi) {
487 OverlapVal = I->first; // Found overlap with scalar.
488 OverlapStmt = I->second;
489 }
490
491 // Find the smallest value bigger than the upper bound.
492 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
493 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
494 OverlapVal = (I-1)->first; // Found overlap with scalar.
495 OverlapStmt = (I-1)->second;
496 }
497
498 // Check to see if this case stmt overlaps with the subsequent case range.
499 if (i && CRLo <= HiVals[i-1]) {
500 OverlapVal = HiVals[i-1]; // Found overlap with range.
501 OverlapStmt = CaseRanges[i-1].second;
502 }
503
504 if (OverlapStmt) {
505 // If we have a duplicate, report it.
Chris Lattner77d52da2008-11-20 06:06:08 +0000506 Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case)
507 << OverlapVal.toString(10);
Chris Lattner0ab833c2007-08-23 18:29:20 +0000508 Diag(OverlapStmt->getLHS()->getLocStart(),
Chris Lattner1336cab2008-11-23 23:12:31 +0000509 diag::note_duplicate_case_prev);
Chris Lattner0ab833c2007-08-23 18:29:20 +0000510 // FIXME: We really want to remove the bogus case stmt from the substmt,
511 // but we have no way to do this right now.
512 CaseListIsErroneous = true;
513 }
Chris Lattner1a4066d2007-08-23 06:23:56 +0000514 }
515 }
Chris Lattner3429a812007-08-23 05:46:52 +0000516
Chris Lattner1a4066d2007-08-23 06:23:56 +0000517 // FIXME: If the case list was broken is some way, we don't have a good system
518 // to patch it up. Instead, just return the whole substmt as broken.
519 if (CaseListIsErroneous)
520 return true;
Chris Lattner3429a812007-08-23 05:46:52 +0000521
Chris Lattner4b009652007-07-25 00:24:17 +0000522 return SS;
523}
524
525Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000526Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
Chris Lattner4b009652007-07-25 00:24:17 +0000527 Expr *condExpr = (Expr *)Cond;
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000528 assert(condExpr && "ActOnWhileStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000529
530 DefaultFunctionArrayConversion(condExpr);
531 QualType condType = condExpr->getType();
532
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000533 if (getLangOptions().CPlusPlus) {
534 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
535 return true;
536 } else if (!condType->isScalarType()) // C99 6.8.5p2
Chris Lattner77d52da2008-11-20 06:06:08 +0000537 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000538 << condType << condExpr->getSourceRange();
Chris Lattner4b009652007-07-25 00:24:17 +0000539
Steve Naroff5d2fff82007-08-31 23:28:33 +0000540 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000541}
542
543Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000544Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Chris Lattner4b009652007-07-25 00:24:17 +0000545 SourceLocation WhileLoc, ExprTy *Cond) {
546 Expr *condExpr = (Expr *)Cond;
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000547 assert(condExpr && "ActOnDoStmt(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000548
549 DefaultFunctionArrayConversion(condExpr);
550 QualType condType = condExpr->getType();
551
Argiris Kirtzidisc362d382008-09-11 05:16:22 +0000552 if (getLangOptions().CPlusPlus) {
553 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
554 return true;
555 } else if (!condType->isScalarType()) // C99 6.8.5p2
Chris Lattner77d52da2008-11-20 06:06:08 +0000556 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000557 << condType << condExpr->getSourceRange();
Chris Lattner4b009652007-07-25 00:24:17 +0000558
Steve Naroff5d2fff82007-08-31 23:28:33 +0000559 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000560}
561
562Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000563Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner3332fbd2007-08-28 04:55:47 +0000564 StmtTy *first, ExprTy *second, ExprTy *third,
565 SourceLocation RParenLoc, StmtTy *body) {
566 Stmt *First = static_cast<Stmt*>(first);
567 Expr *Second = static_cast<Expr*>(second);
568 Expr *Third = static_cast<Expr*>(third);
569 Stmt *Body = static_cast<Stmt*>(body);
570
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000571 if (!getLangOptions().CPlusPlus) {
572 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000573 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
574 // declare identifiers for objects having storage class 'auto' or
575 // 'register'.
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000576 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
577 DI!=DE; ++DI) {
578 VarDecl *VD = dyn_cast<VarDecl>(*DI);
579 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
580 VD = 0;
581 if (VD == 0)
582 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
583 // FIXME: mark decl erroneous!
584 }
Chris Lattner06611052007-08-28 05:03:08 +0000585 }
Chris Lattner4b009652007-07-25 00:24:17 +0000586 }
587 if (Second) {
Chris Lattner3332fbd2007-08-28 04:55:47 +0000588 DefaultFunctionArrayConversion(Second);
589 QualType SecondType = Second->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000590
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000591 if (getLangOptions().CPlusPlus) {
592 if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4
593 return true;
594 } else if (!SecondType->isScalarType()) // C99 6.8.5p2
Chris Lattner77d52da2008-11-20 06:06:08 +0000595 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000596 << SecondType << Second->getSourceRange();
Chris Lattner4b009652007-07-25 00:24:17 +0000597 }
Steve Naroff5d2fff82007-08-31 23:28:33 +0000598 return new ForStmt(First, Second, Third, Body, ForLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000599}
600
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000601Action::StmtResult
Fariborz Jahaniandf2b0952008-01-10 00:24:29 +0000602Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000603 SourceLocation LParenLoc,
604 StmtTy *first, ExprTy *second,
605 SourceLocation RParenLoc, StmtTy *body) {
606 Stmt *First = static_cast<Stmt*>(first);
607 Expr *Second = static_cast<Expr*>(second);
608 Stmt *Body = static_cast<Stmt*>(body);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +0000609 if (First) {
610 QualType FirstType;
611 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
Ted Kremenek779e1c22008-10-06 20:58:11 +0000612 if (!DS->hasSolitaryDecl())
613 return Diag((*DS->decl_begin())->getLocation(),
614 diag::err_toomany_element_decls);
615
616 ScopedDecl *D = DS->getSolitaryDecl();
617 FirstType = cast<ValueDecl>(D)->getType();
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000618 // C99 6.8.5p3: The declaration part of a 'for' statement shall only
619 // declare identifiers for objects having storage class 'auto' or
620 // 'register'.
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000621 VarDecl *VD = cast<VarDecl>(D);
622 if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
623 return Diag(VD->getLocation(), diag::err_non_variable_decl_in_for);
Anders Carlsson8d0bcb02008-08-25 18:16:36 +0000624 } else {
625 Expr::isLvalueResult lval = cast<Expr>(First)->isLvalue(Context);
626
627 if (lval != Expr::LV_Valid)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000628 return Diag(First->getLocStart(), diag::err_selector_element_not_lvalue)
629 << First->getSourceRange();
Anders Carlsson8d0bcb02008-08-25 18:16:36 +0000630
631 FirstType = static_cast<Expr*>(first)->getType();
632 }
Ted Kremenek118930e2008-07-24 23:58:27 +0000633 if (!Context.isObjCObjectPointerType(FirstType))
Chris Lattner9d2cf082008-11-19 05:27:50 +0000634 Diag(ForLoc, diag::err_selector_element_type)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000635 << FirstType << First->getSourceRange();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000636 }
637 if (Second) {
638 DefaultFunctionArrayConversion(Second);
639 QualType SecondType = Second->getType();
Ted Kremenek118930e2008-07-24 23:58:27 +0000640 if (!Context.isObjCObjectPointerType(SecondType))
Chris Lattner9d2cf082008-11-19 05:27:50 +0000641 Diag(ForLoc, diag::err_collection_expr_type)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000642 << SecondType << Second->getSourceRange();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000643 }
Fariborz Jahaniandf2b0952008-01-10 00:24:29 +0000644 return new ObjCForCollectionStmt(First, Second, Body, ForLoc, RParenLoc);
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000645}
Chris Lattner4b009652007-07-25 00:24:17 +0000646
647Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000648Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000649 IdentifierInfo *LabelII) {
Steve Naroff52a81c02008-09-03 18:15:37 +0000650 // If we are in a block, reject all gotos for now.
651 if (CurBlock)
652 return Diag(GotoLoc, diag::err_goto_in_block);
653
Chris Lattner4b009652007-07-25 00:24:17 +0000654 // Look up the record for this label identifier.
655 LabelStmt *&LabelDecl = LabelMap[LabelII];
656
657 // If we haven't seen this label yet, create a forward reference.
658 if (LabelDecl == 0)
659 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
660
Ted Kremeneka65ad462007-09-06 17:11:52 +0000661 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000662}
663
664Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000665Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000666 ExprTy *DestExp) {
667 // FIXME: Verify that the operand is convertible to void*.
668
669 return new IndirectGotoStmt((Expr*)DestExp);
670}
671
672Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000673Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000674 Scope *S = CurScope->getContinueParent();
675 if (!S) {
676 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
677 Diag(ContinueLoc, diag::err_continue_not_in_loop);
678 return true;
679 }
680
Steve Naroffc32a20d2007-08-31 23:49:30 +0000681 return new ContinueStmt(ContinueLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000682}
683
684Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000685Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Chris Lattner4b009652007-07-25 00:24:17 +0000686 Scope *S = CurScope->getBreakParent();
687 if (!S) {
688 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
689 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
690 return true;
691 }
692
Steve Naroffc32a20d2007-08-31 23:49:30 +0000693 return new BreakStmt(BreakLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000694}
695
Douglas Gregor81c29152008-10-29 00:13:59 +0000696/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
Steve Naroff52a81c02008-09-03 18:15:37 +0000697///
698Action::StmtResult
699Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
700
701 // If this is the first return we've seen in the block, infer the type of
702 // the block from it.
703 if (CurBlock->ReturnType == 0) {
Steve Naroff503996b2008-09-16 22:25:10 +0000704 if (RetValExp) {
Steve Naroffe2b66a82008-09-24 22:26:48 +0000705 // Don't call UsualUnaryConversions(), since we don't want to do
706 // integer promotions here.
707 DefaultFunctionArrayConversion(RetValExp);
Steve Naroff52a81c02008-09-03 18:15:37 +0000708 CurBlock->ReturnType = RetValExp->getType().getTypePtr();
Steve Naroff503996b2008-09-16 22:25:10 +0000709 } else
Steve Naroff52a81c02008-09-03 18:15:37 +0000710 CurBlock->ReturnType = Context.VoidTy.getTypePtr();
711 return new ReturnStmt(ReturnLoc, RetValExp);
712 }
713
714 // Otherwise, verify that this result type matches the previous one. We are
715 // pickier with blocks than for normal functions because we don't have GCC
716 // compatibility to worry about here.
717 if (CurBlock->ReturnType->isVoidType()) {
718 if (RetValExp) {
719 Diag(ReturnLoc, diag::err_return_block_has_expr);
720 delete RetValExp;
721 RetValExp = 0;
722 }
723 return new ReturnStmt(ReturnLoc, RetValExp);
724 }
725
726 if (!RetValExp) {
727 Diag(ReturnLoc, diag::err_block_return_missing_expr);
728 return true;
729 }
730
731 // we have a non-void block with an expression, continue checking
732 QualType RetValType = RetValExp->getType();
733
734 // For now, restrict multiple return statements in a block to have
735 // strict compatible types only.
736 QualType BlockQT = QualType(CurBlock->ReturnType, 0);
737 if (Context.getCanonicalType(BlockQT).getTypePtr()
738 != Context.getCanonicalType(RetValType).getTypePtr()) {
739 DiagnoseAssignmentResult(Incompatible, ReturnLoc, BlockQT,
740 RetValType, RetValExp, "returning");
741 return true;
742 }
743
744 if (RetValExp) CheckReturnStackAddr(RetValExp, BlockQT, ReturnLoc);
745
746 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
747}
Chris Lattner4b009652007-07-25 00:24:17 +0000748
749Action::StmtResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000750Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
Chris Lattner4b009652007-07-25 00:24:17 +0000751 Expr *RetValExp = static_cast<Expr *>(rex);
Steve Naroff52a81c02008-09-03 18:15:37 +0000752 if (CurBlock)
753 return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
Chris Lattnere5cb5862008-12-04 23:50:19 +0000754
755 QualType FnRetType;
756 if (FunctionDecl *FD = getCurFunctionDecl())
757 FnRetType = FD->getResultType();
758 else
759 FnRetType = getCurMethodDecl()->getResultType();
Chris Lattner4b009652007-07-25 00:24:17 +0000760
Chris Lattner005ed752008-01-04 18:04:52 +0000761 if (FnRetType->isVoidType()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000762 if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns)
Chris Lattner6ed167c2008-12-18 02:01:17 +0000763 unsigned D = diag::ext_return_has_expr;
764 if (RetValExp->getType()->isVoidType())
765 D = diag::ext_return_has_void_expr;
Chris Lattner6ed167c2008-12-18 02:01:17 +0000766
Chris Lattnerd1a05392008-12-18 02:03:48 +0000767 // return (some void expression); is legal in C++.
768 if (D != diag::ext_return_has_void_expr ||
769 !getLangOptions().CPlusPlus) {
770 NamedDecl *CurDecl = getCurFunctionOrMethodDecl();
771 Diag(ReturnLoc, D)
772 << CurDecl->getDeclName() << isa<ObjCMethodDecl>(CurDecl)
773 << RetValExp->getSourceRange();
774 }
Chris Lattner4b009652007-07-25 00:24:17 +0000775 }
Chris Lattner65cae292008-11-19 08:23:25 +0000776 return new ReturnStmt(ReturnLoc, RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000777 }
Chris Lattner65cae292008-11-19 08:23:25 +0000778
779 if (!RetValExp) {
780 unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4
781 // C99 6.8.6.4p1 (ext_ since GCC warns)
782 if (getLangOptions().C99) DiagID = diag::ext_return_missing_expr;
783
784 if (FunctionDecl *FD = getCurFunctionDecl())
Chris Lattnerb1753422008-11-23 21:45:46 +0000785 Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/;
Chris Lattner65cae292008-11-19 08:23:25 +0000786 else
Chris Lattnerb1753422008-11-23 21:45:46 +0000787 Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/;
Chris Lattner65cae292008-11-19 08:23:25 +0000788 return new ReturnStmt(ReturnLoc, (Expr*)0);
789 }
790
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000791 if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) {
792 // we have a non-void function with an expression, continue checking
793 QualType RetValType = RetValExp->getType();
794
795 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
796 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
797 // function return.
798
799 // In C++ the return statement is handled via a copy initialization.
800 // the C version of which boils down to
801 // CheckSingleAssignmentConstraints.
802 if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
803 return true;
Ted Kremenek45925ab2007-08-17 16:46:58 +0000804
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000805 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
806 }
807
Steve Naroffc32a20d2007-08-31 23:49:30 +0000808 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Chris Lattner4b009652007-07-25 00:24:17 +0000809}
810
Anders Carlsson076c1112007-11-20 19:21:03 +0000811Sema::StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
Chris Lattnerb1753422008-11-23 21:45:46 +0000812 bool IsSimple,
Anders Carlsson759f45d2007-11-23 23:12:25 +0000813 bool IsVolatile,
Anders Carlsson965d5202007-11-22 01:36:19 +0000814 unsigned NumOutputs,
815 unsigned NumInputs,
816 std::string *Names,
Chris Lattnerb052a832008-08-18 19:55:17 +0000817 ExprTy **constraints,
818 ExprTy **exprs,
Chris Lattner84418022008-07-23 06:46:56 +0000819 ExprTy *asmString,
Anders Carlsson965d5202007-11-22 01:36:19 +0000820 unsigned NumClobbers,
Chris Lattnerb052a832008-08-18 19:55:17 +0000821 ExprTy **clobbers,
Chris Lattner8a40a832007-10-29 04:04:16 +0000822 SourceLocation RParenLoc) {
Chris Lattnerb052a832008-08-18 19:55:17 +0000823 StringLiteral **Constraints = reinterpret_cast<StringLiteral**>(constraints);
824 Expr **Exprs = reinterpret_cast<Expr **>(exprs);
Chris Lattner84418022008-07-23 06:46:56 +0000825 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString);
Chris Lattnerb052a832008-08-18 19:55:17 +0000826 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers);
827
828 // The parser verifies that there is a string literal here.
Chris Lattner84418022008-07-23 06:46:56 +0000829 if (AsmString->isWide())
830 // FIXME: We currently leak memory here.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000831 return Diag(AsmString->getLocStart(), diag::err_asm_wide_character)
832 << AsmString->getSourceRange();
Chris Lattner84418022008-07-23 06:46:56 +0000833
834
Chris Lattnerb052a832008-08-18 19:55:17 +0000835 for (unsigned i = 0; i != NumOutputs; i++) {
836 StringLiteral *Literal = Constraints[i];
Chris Lattner84418022008-07-23 06:46:56 +0000837 if (Literal->isWide())
838 // FIXME: We currently leak memory here.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000839 return Diag(Literal->getLocStart(), diag::err_asm_wide_character)
840 << Literal->getSourceRange();
Chris Lattner84418022008-07-23 06:46:56 +0000841
Anders Carlsson4ce42302007-11-27 04:11:28 +0000842 std::string OutputConstraint(Literal->getStrData(),
843 Literal->getByteLength());
844
845 TargetInfo::ConstraintInfo info;
Chris Lattner84418022008-07-23 06:46:56 +0000846 if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info))
Anders Carlsson4ce42302007-11-27 04:11:28 +0000847 // FIXME: We currently leak memory here.
Chris Lattner84418022008-07-23 06:46:56 +0000848 return Diag(Literal->getLocStart(),
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000849 diag::err_asm_invalid_output_constraint) << OutputConstraint;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000850
851 // Check that the output exprs are valid lvalues.
Chris Lattnerb052a832008-08-18 19:55:17 +0000852 ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]);
Chris Lattner25168a52008-07-26 21:30:36 +0000853 Expr::isLvalueResult Result = OutputExpr->isLvalue(Context);
Anders Carlssonb4487a82007-11-23 19:43:50 +0000854 if (Result != Expr::LV_Valid) {
Anders Carlssonb4487a82007-11-23 19:43:50 +0000855 // FIXME: We currently leak memory here.
Chris Lattnerb052a832008-08-18 19:55:17 +0000856 return Diag(OutputExpr->getSubExpr()->getLocStart(),
Chris Lattner9d2cf082008-11-19 05:27:50 +0000857 diag::err_asm_invalid_lvalue_in_output)
858 << OutputExpr->getSubExpr()->getSourceRange();
Anders Carlssonb4487a82007-11-23 19:43:50 +0000859 }
860 }
861
Anders Carlssonb4487a82007-11-23 19:43:50 +0000862 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
Chris Lattnerb052a832008-08-18 19:55:17 +0000863 StringLiteral *Literal = Constraints[i];
Chris Lattner84418022008-07-23 06:46:56 +0000864 if (Literal->isWide())
865 // FIXME: We currently leak memory here.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000866 return Diag(Literal->getLocStart(), diag::err_asm_wide_character)
867 << Literal->getSourceRange();
Anders Carlssonb4487a82007-11-23 19:43:50 +0000868
Anders Carlsson4ce42302007-11-27 04:11:28 +0000869 std::string InputConstraint(Literal->getStrData(),
870 Literal->getByteLength());
871
872 TargetInfo::ConstraintInfo info;
873 if (!Context.Target.validateInputConstraint(InputConstraint.c_str(),
Chris Lattnerb052a832008-08-18 19:55:17 +0000874 NumOutputs, info)) {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000875 // FIXME: We currently leak memory here.
Chris Lattner84418022008-07-23 06:46:56 +0000876 return Diag(Literal->getLocStart(),
Chris Lattner9d2cf082008-11-19 05:27:50 +0000877 diag::err_asm_invalid_input_constraint) << InputConstraint;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000878 }
879
880 // Check that the input exprs aren't of type void.
Chris Lattnerb052a832008-08-18 19:55:17 +0000881 ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]);
Anders Carlssonb4487a82007-11-23 19:43:50 +0000882 if (InputExpr->getType()->isVoidType()) {
Anders Carlssonb4487a82007-11-23 19:43:50 +0000883
Anders Carlssonb4487a82007-11-23 19:43:50 +0000884 // FIXME: We currently leak memory here.
Chris Lattnerb052a832008-08-18 19:55:17 +0000885 return Diag(InputExpr->getSubExpr()->getLocStart(),
Chris Lattner8ba580c2008-11-19 05:08:23 +0000886 diag::err_asm_invalid_type_in_input)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000887 << InputExpr->getType() << InputConstraint
Chris Lattner8ba580c2008-11-19 05:08:23 +0000888 << InputExpr->getSubExpr()->getSourceRange();
Anders Carlssonb4487a82007-11-23 19:43:50 +0000889 }
890 }
Anders Carlsson965d5202007-11-22 01:36:19 +0000891
Anders Carlsson49dadd62007-11-25 00:25:21 +0000892 // Check that the clobbers are valid.
Chris Lattnerb052a832008-08-18 19:55:17 +0000893 for (unsigned i = 0; i != NumClobbers; i++) {
894 StringLiteral *Literal = Clobbers[i];
Chris Lattner84418022008-07-23 06:46:56 +0000895 if (Literal->isWide())
896 // FIXME: We currently leak memory here.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000897 return Diag(Literal->getLocStart(), diag::err_asm_wide_character)
898 << Literal->getSourceRange();
Anders Carlsson49dadd62007-11-25 00:25:21 +0000899
900 llvm::SmallString<16> Clobber(Literal->getStrData(),
901 Literal->getStrData() +
902 Literal->getByteLength());
903
Chris Lattner84418022008-07-23 06:46:56 +0000904 if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
Anders Carlsson49dadd62007-11-25 00:25:21 +0000905 // FIXME: We currently leak memory here.
Chris Lattner84418022008-07-23 06:46:56 +0000906 return Diag(Literal->getLocStart(),
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000907 diag::err_asm_unknown_register_name) << Clobber.c_str();
Anders Carlsson49dadd62007-11-25 00:25:21 +0000908 }
909
Chris Lattnerb052a832008-08-18 19:55:17 +0000910 return new AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs,
911 Names, Constraints, Exprs, AsmString, NumClobbers,
912 Clobbers, RParenLoc);
Chris Lattner8a40a832007-10-29 04:04:16 +0000913}
Fariborz Jahanian06798362007-11-01 23:59:59 +0000914
915Action::StmtResult
Ted Kremenek42730c52008-01-07 19:49:32 +0000916Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +0000917 SourceLocation RParen, StmtTy *Parm,
918 StmtTy *Body, StmtTy *CatchList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000919 ObjCAtCatchStmt *CS = new ObjCAtCatchStmt(AtLoc, RParen,
Fariborz Jahanian06798362007-11-01 23:59:59 +0000920 static_cast<Stmt*>(Parm), static_cast<Stmt*>(Body),
921 static_cast<Stmt*>(CatchList));
922 return CatchList ? CatchList : CS;
923}
924
Fariborz Jahaniande3abf82007-11-02 00:18:53 +0000925Action::StmtResult
Ted Kremenek42730c52008-01-07 19:49:32 +0000926Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtTy *Body) {
927 ObjCAtFinallyStmt *FS = new ObjCAtFinallyStmt(AtLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +0000928 static_cast<Stmt*>(Body));
929 return FS;
930}
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +0000931
932Action::StmtResult
Ted Kremenek42730c52008-01-07 19:49:32 +0000933Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +0000934 StmtTy *Try, StmtTy *Catch, StmtTy *Finally) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000935 ObjCAtTryStmt *TS = new ObjCAtTryStmt(AtLoc, static_cast<Stmt*>(Try),
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +0000936 static_cast<Stmt*>(Catch),
937 static_cast<Stmt*>(Finally));
938 return TS;
939}
940
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000941Action::StmtResult
Ted Kremenek42730c52008-01-07 19:49:32 +0000942Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, StmtTy *Throw) {
943 ObjCAtThrowStmt *TS = new ObjCAtThrowStmt(AtLoc, static_cast<Stmt*>(Throw));
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000944 return TS;
945}
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +0000946
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000947Action::StmtResult
948Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprTy *SynchExpr,
949 StmtTy *SynchBody) {
950 ObjCAtSynchronizedStmt *SS = new ObjCAtSynchronizedStmt(AtLoc,
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000951 static_cast<Stmt*>(SynchExpr), static_cast<Stmt*>(SynchBody));
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000952 return SS;
953}