blob: f75758ebb0a78c87982616507f279ea1e7d07dfa [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for statements.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Chris Lattnerf4021e72007-08-23 05:46:52 +000015#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000016#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/AST/Expr.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000018#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Basic/Diagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Steve Naroff1b273c42007-09-16 14:56:35 +000022Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) {
Reid Spencer5f016e22007-07-11 17:01:13 +000023 Expr *E = static_cast<Expr*>(expr);
Steve Naroff1b273c42007-09-16 14:56:35 +000024 assert(E && "ActOnExprStmt(): missing expression");
Chris Lattner834a72a2008-07-25 23:18:17 +000025
26 // C99 6.8.3p2: The expression in an expression statement is evaluated as a
27 // void expression for its side effects. Conversion to void allows any
28 // operand, even incomplete types.
29
30 // Same thing in for stmt first clause (when expr) and third clause.
Reid Spencer5f016e22007-07-11 17:01:13 +000031 return E;
32}
33
34
Steve Naroff1b273c42007-09-16 14:56:35 +000035Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000036 return new NullStmt(SemiLoc);
37}
38
Chris Lattner81c018d2008-03-13 06:29:04 +000039Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl, SourceLocation StartLoc,
40 SourceLocation EndLoc) {
41 if (decl == 0)
42 return true;
43
44 ScopedDecl *SD = cast<ScopedDecl>(static_cast<Decl *>(decl));
Ted Kremenek8ffb1592008-10-07 23:09:49 +000045
46
47 // This is a temporary hack until we are always passing around
48 // DeclGroupRefs.
49 llvm::SmallVector<Decl*, 10> decls;
50 while (SD) {
51 ScopedDecl* d = SD;
52 SD = SD->getNextDeclarator();
53 d->setNextDeclarator(0);
54 decls.push_back(d);
55 }
56
57 assert (!decls.empty());
58
59 if (decls.size() == 1) {
60 DeclGroupOwningRef DG(*decls.begin());
61 return new DeclStmt(DG, StartLoc, EndLoc);
62 }
63 else {
64 DeclGroupOwningRef DG(DeclGroup::Create(Context, decls.size(), &decls[0]));
65 return new DeclStmt(DG, StartLoc, EndLoc);
66 }
Reid Spencer5f016e22007-07-11 17:01:13 +000067}
68
69Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +000070Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattner98414c12007-08-31 21:49:55 +000071 StmtTy **elts, unsigned NumElts, bool isStmtExpr) {
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000072 Stmt **Elts = reinterpret_cast<Stmt**>(elts);
73 // If we're in C89 mode, check that we don't have any decls after stmts. If
74 // so, emit an extension diagnostic.
75 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
76 // Note that __extension__ can be around a decl.
77 unsigned i = 0;
78 // Skip over all declarations.
79 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
80 /*empty*/;
81
82 // We found the end of the list or a statement. Scan for another declstmt.
83 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
84 /*empty*/;
85
86 if (i != NumElts) {
Ted Kremenek1bddf7e2008-10-06 18:48:35 +000087 ScopedDecl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000088 Diag(D->getLocation(), diag::ext_mixed_decls_code);
89 }
90 }
Chris Lattner98414c12007-08-31 21:49:55 +000091 // Warn about unused expressions in statements.
92 for (unsigned i = 0; i != NumElts; ++i) {
93 Expr *E = dyn_cast<Expr>(Elts[i]);
94 if (!E) continue;
95
96 // Warn about expressions with unused results.
97 if (E->hasLocalSideEffect() || E->getType()->isVoidType())
98 continue;
99
100 // The last expr in a stmt expr really is used.
101 if (isStmtExpr && i == NumElts-1)
102 continue;
103
104 /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in
105 /// a context where the result is unused. Emit a diagnostic to warn about
106 /// this.
107 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
108 Diag(BO->getOperatorLoc(), diag::warn_unused_expr,
109 BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange());
110 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
111 Diag(UO->getOperatorLoc(), diag::warn_unused_expr,
112 UO->getSubExpr()->getSourceRange());
113 else
114 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
115 }
116
Steve Naroffb5a69582007-08-31 23:28:33 +0000117 return new CompoundStmt(Elts, NumElts, L, R);
Reid Spencer5f016e22007-07-11 17:01:13 +0000118}
119
120Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000121Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner6c36be52007-07-18 02:28:47 +0000122 SourceLocation DotDotDotLoc, ExprTy *rhsval,
Chris Lattner0fa152e2007-07-21 03:00:26 +0000123 SourceLocation ColonLoc, StmtTy *subStmt) {
124 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner8a87e572007-07-23 17:05:23 +0000125 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 assert((LHSVal != 0) && "missing expression in case statement");
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000127
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 SourceLocation ExpLoc;
129 // C99 6.8.4.2p3: The expression shall be an integer constant.
Chris Lattner0fa152e2007-07-21 03:00:26 +0000130 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
131 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
132 LHSVal->getSourceRange());
133 return SubStmt;
134 }
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.
Chris Lattner0fa152e2007-07-21 03:00:26 +0000137 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
138 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
139 RHSVal->getSourceRange());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000140 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner6c36be52007-07-18 02:28:47 +0000141 }
Chris Lattner8a87e572007-07-23 17:05:23 +0000142
143 if (SwitchStack.empty()) {
144 Diag(CaseLoc, diag::err_case_not_in_switch);
145 return SubStmt;
146 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000147
Steve Naroffb5a69582007-08-31 23:28:33 +0000148 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner8a87e572007-07-23 17:05:23 +0000149 SwitchStack.back()->addSwitchCase(CS);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000150 return CS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000151}
152
153Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000154Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner0fa152e2007-07-21 03:00:26 +0000155 StmtTy *subStmt, Scope *CurScope) {
156 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner6c36be52007-07-18 02:28:47 +0000157
Chris Lattner8a87e572007-07-23 17:05:23 +0000158 if (SwitchStack.empty()) {
Chris Lattner0fa152e2007-07-21 03:00:26 +0000159 Diag(DefaultLoc, diag::err_default_not_in_switch);
160 return SubStmt;
161 }
162
Chris Lattner0fa152e2007-07-21 03:00:26 +0000163 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner8a87e572007-07-23 17:05:23 +0000164 SwitchStack.back()->addSwitchCase(DS);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000165
Chris Lattner6c36be52007-07-18 02:28:47 +0000166 return DS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000167}
168
169Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000170Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner0fa152e2007-07-21 03:00:26 +0000171 SourceLocation ColonLoc, StmtTy *subStmt) {
172 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 // Look up the record for this label identifier.
174 LabelStmt *&LabelDecl = LabelMap[II];
175
176 // If not forward referenced or defined already, just create a new LabelStmt.
177 if (LabelDecl == 0)
Chris Lattner0fa152e2007-07-21 03:00:26 +0000178 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000179
180 assert(LabelDecl->getID() == II && "Label mismatch!");
181
182 // Otherwise, this label was either forward reference or multiply defined. If
183 // multiply defined, reject it now.
184 if (LabelDecl->getSubStmt()) {
185 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
186 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000187 return SubStmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 }
189
190 // Otherwise, this label was forward declared, and we just found its real
191 // definition. Fill in the forward definition and return it.
192 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000193 LabelDecl->setSubStmt(SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 return LabelDecl;
195}
196
197Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000198Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 StmtTy *ThenVal, SourceLocation ElseLoc,
200 StmtTy *ElseVal) {
201 Expr *condExpr = (Expr *)CondVal;
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000202 Stmt *thenStmt = (Stmt *)ThenVal;
203
Steve Naroff1b273c42007-09-16 14:56:35 +0000204 assert(condExpr && "ActOnIfStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000205
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000206 DefaultFunctionArrayConversion(condExpr);
207 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000208
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000209 if (getLangOptions().CPlusPlus) {
210 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
211 return true;
212 } else if (!condType->isScalarType()) // C99 6.8.4.1p1
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
214 condType.getAsString(), condExpr->getSourceRange());
215
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000216 // Warn if the if block has a null body without an else value.
217 // this helps prevent bugs due to typos, such as
218 // if (condition);
219 // do_stuff();
220 if (!ElseVal) {
221 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
222 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
223 }
224
225 return new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000226}
227
228Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000229Sema::ActOnStartOfSwitchStmt(ExprTy *cond) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000230 Expr *Cond = static_cast<Expr*>(cond);
231
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000232 if (getLangOptions().CPlusPlus) {
233 // C++ 6.4.2.p2:
234 // The condition shall be of integral type, enumeration type, or of a class
235 // type for which a single conversion function to integral or enumeration
236 // type exists (12.3). If the condition is of class type, the condition is
237 // converted by calling that conversion function, and the result of the
238 // conversion is used in place of the original condition for the remainder
239 // of this section. Integral promotions are performed.
240
241 QualType Ty = Cond->getType();
242
243 // FIXME: Handle class types.
244
245 // If the type is wrong a diagnostic will be emitted later at
246 // ActOnFinishSwitchStmt.
247 if (Ty->isIntegralType() || Ty->isEnumeralType()) {
248 // Integral promotions are performed.
249 // FIXME: Integral promotions for C++ are not complete.
250 UsualUnaryConversions(Cond);
251 }
252 } else {
253 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
254 UsualUnaryConversions(Cond);
255 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000256
257 SwitchStmt *SS = new SwitchStmt(Cond);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000258 SwitchStack.push_back(SS);
259 return SS;
260}
Chris Lattner6c36be52007-07-18 02:28:47 +0000261
Chris Lattnerf4021e72007-08-23 05:46:52 +0000262/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
263/// the specified width and sign. If an overflow occurs, detect it and emit
264/// the specified diagnostic.
265void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
266 unsigned NewWidth, bool NewSign,
267 SourceLocation Loc,
268 unsigned DiagID) {
269 // Perform a conversion to the promoted condition type if needed.
270 if (NewWidth > Val.getBitWidth()) {
271 // If this is an extension, just do it.
272 llvm::APSInt OldVal(Val);
273 Val.extend(NewWidth);
274
275 // If the input was signed and negative and the output is unsigned,
276 // warn.
277 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
Chris Lattner9aa77f12008-08-17 07:19:51 +0000278 Diag(Loc, DiagID, OldVal.toString(10), Val.toString(10));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000279
280 Val.setIsSigned(NewSign);
281 } else if (NewWidth < Val.getBitWidth()) {
282 // If this is a truncation, check for overflow.
283 llvm::APSInt ConvVal(Val);
284 ConvVal.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000285 ConvVal.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000286 ConvVal.extend(Val.getBitWidth());
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000287 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000288 if (ConvVal != Val)
Chris Lattner9aa77f12008-08-17 07:19:51 +0000289 Diag(Loc, DiagID, Val.toString(10), ConvVal.toString(10));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000290
291 // Regardless of whether a diagnostic was emitted, really do the
292 // truncation.
293 Val.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000294 Val.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000295 } else if (NewSign != Val.isSigned()) {
296 // Convert the sign to match the sign of the condition. This can cause
297 // overflow as well: unsigned(INTMIN)
298 llvm::APSInt OldVal(Val);
299 Val.setIsSigned(NewSign);
300
301 if (Val.isNegative()) // Sign bit changes meaning.
Chris Lattner9aa77f12008-08-17 07:19:51 +0000302 Diag(Loc, DiagID, OldVal.toString(10), Val.toString(10));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000303 }
304}
305
Chris Lattner0471f5b2007-08-23 18:29:20 +0000306namespace {
307 struct CaseCompareFunctor {
308 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
309 const llvm::APSInt &RHS) {
310 return LHS.first < RHS;
311 }
Chris Lattner0e85a272007-09-03 18:31:57 +0000312 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
313 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
314 return LHS.first < RHS.first;
315 }
Chris Lattner0471f5b2007-08-23 18:29:20 +0000316 bool operator()(const llvm::APSInt &LHS,
317 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
318 return LHS < RHS.first;
319 }
320 };
321}
322
Chris Lattner764a7ce2007-09-21 18:15:22 +0000323/// CmpCaseVals - Comparison predicate for sorting case values.
324///
325static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
326 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
327 if (lhs.first < rhs.first)
328 return true;
329
330 if (lhs.first == rhs.first &&
331 lhs.second->getCaseLoc().getRawEncoding()
332 < rhs.second->getCaseLoc().getRawEncoding())
333 return true;
334 return false;
335}
336
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000337Action::StmtResult
Chris Lattner764a7ce2007-09-21 18:15:22 +0000338Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
339 ExprTy *Body) {
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000340 Stmt *BodyStmt = (Stmt*)Body;
341
342 SwitchStmt *SS = SwitchStack.back();
343 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
344
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000345 SS->setBody(BodyStmt, SwitchLoc);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000346 SwitchStack.pop_back();
347
Chris Lattnerf4021e72007-08-23 05:46:52 +0000348 Expr *CondExpr = SS->getCond();
349 QualType CondType = CondExpr->getType();
Chris Lattner6c36be52007-07-18 02:28:47 +0000350
Chris Lattnerf4021e72007-08-23 05:46:52 +0000351 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000352 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattnerf4021e72007-08-23 05:46:52 +0000353 CondType.getAsString(), CondExpr->getSourceRange());
354 return true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000355 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000356
357 // Get the bitwidth of the switched-on value before promotions. We must
358 // convert the integer case values to this width before comparison.
Chris Lattner98be4942008-03-05 18:54:05 +0000359 unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000360 bool CondIsSigned = CondType->isSignedIntegerType();
361
362 // Accumulate all of the case values in a vector so that we can sort them
363 // and detect duplicates. This vector contains the APInt for the case after
364 // it has been converted to the condition type.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000365 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
366 CaseValsTy CaseVals;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000367
368 // Keep track of any GNU case ranges we see. The APSInt is the low value.
369 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
370
371 DefaultStmt *TheDefaultStmt = 0;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000372
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000373 bool CaseListIsErroneous = false;
374
375 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000376 SC = SC->getNextSwitchCase()) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000377
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000378 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000379 if (TheDefaultStmt) {
380 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
381 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000382
Chris Lattnerf4021e72007-08-23 05:46:52 +0000383 // FIXME: Remove the default statement from the switch block so that
384 // we'll return a valid AST. This requires recursing down the
385 // AST and finding it, not something we are set up to do right now. For
386 // now, just lop the entire switch stmt out of the AST.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000387 CaseListIsErroneous = true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000388 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000389 TheDefaultStmt = DS;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000390
Chris Lattnerf4021e72007-08-23 05:46:52 +0000391 } else {
392 CaseStmt *CS = cast<CaseStmt>(SC);
393
394 // We already verified that the expression has a i-c-e value (C99
395 // 6.8.4.2p3) - get that value now.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000396 Expr *Lo = CS->getLHS();
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000397 llvm::APSInt LoVal = Lo->getIntegerConstantExprValue(Context);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000398
399 // Convert the value to the same width/sign as the condition.
400 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
401 CS->getLHS()->getLocStart(),
402 diag::warn_case_value_overflow);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000403
Chris Lattner1e0a3902008-01-16 19:17:22 +0000404 // If the LHS is not the same type as the condition, insert an implicit
405 // cast.
406 ImpCastExprToType(Lo, CondType);
407 CS->setLHS(Lo);
408
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000409 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerf4021e72007-08-23 05:46:52 +0000410 if (CS->getRHS())
411 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000412 else
413 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000414 }
415 }
416
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000417 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner764a7ce2007-09-21 18:15:22 +0000418 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000419
Chris Lattnerf3348502007-08-23 14:29:07 +0000420 if (!CaseVals.empty()) {
421 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
422 if (CaseVals[i].first == CaseVals[i+1].first) {
423 // If we have a duplicate, report it.
424 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
Chris Lattner9aa77f12008-08-17 07:19:51 +0000425 diag::err_duplicate_case, CaseVals[i].first.toString(10));
Chris Lattnerf3348502007-08-23 14:29:07 +0000426 Diag(CaseVals[i].second->getLHS()->getLocStart(),
427 diag::err_duplicate_case_prev);
428 // FIXME: We really want to remove the bogus case stmt from the substmt,
429 // but we have no way to do this right now.
430 CaseListIsErroneous = true;
431 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000432 }
433 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000434
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000435 // Detect duplicate case ranges, which usually don't exist at all in the first
436 // place.
437 if (!CaseRanges.empty()) {
438 // Sort all the case ranges by their low value so we can easily detect
439 // overlaps between ranges.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000440 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000441
442 // Scan the ranges, computing the high values and removing empty ranges.
443 std::vector<llvm::APSInt> HiVals;
Chris Lattner6efc4d32007-08-23 17:48:14 +0000444 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000445 CaseStmt *CR = CaseRanges[i].second;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000446 Expr *Hi = CR->getRHS();
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000447 llvm::APSInt HiVal = Hi->getIntegerConstantExprValue(Context);
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000448
449 // Convert the value to the same width/sign as the condition.
450 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
451 CR->getRHS()->getLocStart(),
452 diag::warn_case_value_overflow);
453
Chris Lattner1e0a3902008-01-16 19:17:22 +0000454 // If the LHS is not the same type as the condition, insert an implicit
455 // cast.
456 ImpCastExprToType(Hi, CondType);
457 CR->setRHS(Hi);
458
Chris Lattner6efc4d32007-08-23 17:48:14 +0000459 // If the low value is bigger than the high value, the case is empty.
460 if (CaseRanges[i].first > HiVal) {
461 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
462 SourceRange(CR->getLHS()->getLocStart(),
463 CR->getRHS()->getLocEnd()));
464 CaseRanges.erase(CaseRanges.begin()+i);
465 --i, --e;
466 continue;
467 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000468 HiVals.push_back(HiVal);
469 }
470
471 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0471f5b2007-08-23 18:29:20 +0000472 // ranges. Since the range list is sorted, we only need to compare case
473 // ranges with their neighbors.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000474 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0471f5b2007-08-23 18:29:20 +0000475 llvm::APSInt &CRLo = CaseRanges[i].first;
476 llvm::APSInt &CRHi = HiVals[i];
477 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000478
Chris Lattner0471f5b2007-08-23 18:29:20 +0000479 // Check to see whether the case range overlaps with any singleton cases.
480 CaseStmt *OverlapStmt = 0;
481 llvm::APSInt OverlapVal(32);
482
483 // Find the smallest value >= the lower bound. If I is in the case range,
484 // then we have overlap.
485 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
486 CaseVals.end(), CRLo,
487 CaseCompareFunctor());
488 if (I != CaseVals.end() && I->first < CRHi) {
489 OverlapVal = I->first; // Found overlap with scalar.
490 OverlapStmt = I->second;
491 }
492
493 // Find the smallest value bigger than the upper bound.
494 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
495 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
496 OverlapVal = (I-1)->first; // Found overlap with scalar.
497 OverlapStmt = (I-1)->second;
498 }
499
500 // Check to see if this case stmt overlaps with the subsequent case range.
501 if (i && CRLo <= HiVals[i-1]) {
502 OverlapVal = HiVals[i-1]; // Found overlap with range.
503 OverlapStmt = CaseRanges[i-1].second;
504 }
505
506 if (OverlapStmt) {
507 // If we have a duplicate, report it.
508 Diag(CR->getLHS()->getLocStart(),
Chris Lattner9aa77f12008-08-17 07:19:51 +0000509 diag::err_duplicate_case, OverlapVal.toString(10));
Chris Lattner0471f5b2007-08-23 18:29:20 +0000510 Diag(OverlapStmt->getLHS()->getLocStart(),
511 diag::err_duplicate_case_prev);
512 // FIXME: We really want to remove the bogus case stmt from the substmt,
513 // but we have no way to do this right now.
514 CaseListIsErroneous = true;
515 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000516 }
517 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000518
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000519 // FIXME: If the case list was broken is some way, we don't have a good system
520 // to patch it up. Instead, just return the whole substmt as broken.
521 if (CaseListIsErroneous)
522 return true;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000523
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000524 return SS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000525}
526
527Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000528Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000530 assert(condExpr && "ActOnWhileStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000531
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000532 DefaultFunctionArrayConversion(condExpr);
533 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000534
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000535 if (getLangOptions().CPlusPlus) {
536 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
537 return true;
538 } else if (!condType->isScalarType()) // C99 6.8.5p2
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
540 condType.getAsString(), condExpr->getSourceRange());
541
Steve Naroffb5a69582007-08-31 23:28:33 +0000542 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000543}
544
545Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000546Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 SourceLocation WhileLoc, ExprTy *Cond) {
548 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000549 assert(condExpr && "ActOnDoStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000550
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000551 DefaultFunctionArrayConversion(condExpr);
552 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000553
Argyrios Kyrtzidis6314ff22008-09-11 05:16:22 +0000554 if (getLangOptions().CPlusPlus) {
555 if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
556 return true;
557 } else if (!condType->isScalarType()) // C99 6.8.5p2
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
559 condType.getAsString(), condExpr->getSourceRange());
560
Steve Naroffb5a69582007-08-31 23:28:33 +0000561 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000562}
563
564Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000565Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000566 StmtTy *first, ExprTy *second, ExprTy *third,
567 SourceLocation RParenLoc, StmtTy *body) {
568 Stmt *First = static_cast<Stmt*>(first);
569 Expr *Second = static_cast<Expr*>(second);
570 Expr *Third = static_cast<Expr*>(third);
571 Stmt *Body = static_cast<Stmt*>(body);
572
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000573 if (!getLangOptions().CPlusPlus) {
574 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
575 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
576 // identifiers for objects having storage class 'auto' or 'register'.
577 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
578 DI!=DE; ++DI) {
579 VarDecl *VD = dyn_cast<VarDecl>(*DI);
580 if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
581 VD = 0;
582 if (VD == 0)
583 Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
584 // FIXME: mark decl erroneous!
585 }
Chris Lattnerae3b7012007-08-28 05:03:08 +0000586 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 }
588 if (Second) {
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000589 DefaultFunctionArrayConversion(Second);
590 QualType SecondType = Second->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000591
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000592 if (getLangOptions().CPlusPlus) {
593 if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4
594 return true;
595 } else if (!SecondType->isScalarType()) // C99 6.8.5p2
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner834a72a2008-07-25 23:18:17 +0000597 SecondType.getAsString(), Second->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 }
Steve Naroffb5a69582007-08-31 23:28:33 +0000599 return new ForStmt(First, Second, Third, Body, ForLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000600}
601
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000602Action::StmtResult
Fariborz Jahanian75712282008-01-10 00:24:29 +0000603Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000604 SourceLocation LParenLoc,
605 StmtTy *first, ExprTy *second,
606 SourceLocation RParenLoc, StmtTy *body) {
607 Stmt *First = static_cast<Stmt*>(first);
608 Expr *Second = static_cast<Expr*>(second);
609 Stmt *Body = static_cast<Stmt*>(body);
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000610 if (First) {
611 QualType FirstType;
612 if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
Ted Kremenekf34afee2008-10-06 20:58:11 +0000613 if (!DS->hasSolitaryDecl())
614 return Diag((*DS->decl_begin())->getLocation(),
615 diag::err_toomany_element_decls);
616
617 ScopedDecl *D = DS->getSolitaryDecl();
618 FirstType = cast<ValueDecl>(D)->getType();
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000619 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
620 // identifiers for objects having storage class 'auto' or 'register'.
Steve Naroff248a7532008-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 Carlsson1fe379f2008-08-25 18:16:36 +0000624 } else {
625 Expr::isLvalueResult lval = cast<Expr>(First)->isLvalue(Context);
626
627 if (lval != Expr::LV_Valid)
628 return Diag(First->getLocStart(), diag::err_selector_element_not_lvalue,
629 First->getSourceRange());
630
631 FirstType = static_cast<Expr*>(first)->getType();
632 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000633 if (!Context.isObjCObjectPointerType(FirstType))
Fariborz Jahanian20552d22008-01-10 20:33:58 +0000634 Diag(ForLoc, diag::err_selector_element_type,
Anders Carlsson1fe379f2008-08-25 18:16:36 +0000635 FirstType.getAsString(), First->getSourceRange());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000636 }
637 if (Second) {
638 DefaultFunctionArrayConversion(Second);
639 QualType SecondType = Second->getType();
Ted Kremenekb6ccaac2008-07-24 23:58:27 +0000640 if (!Context.isObjCObjectPointerType(SecondType))
Fariborz Jahanian75712282008-01-10 00:24:29 +0000641 Diag(ForLoc, diag::err_collection_expr_type,
Fariborz Jahanian1f990d62008-01-04 00:27:46 +0000642 SecondType.getAsString(), Second->getSourceRange());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000643 }
Fariborz Jahanian75712282008-01-10 00:24:29 +0000644 return new ObjCForCollectionStmt(First, Second, Body, ForLoc, RParenLoc);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000645}
Reid Spencer5f016e22007-07-11 17:01:13 +0000646
647Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000648Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 IdentifierInfo *LabelII) {
Steve Naroff4eb206b2008-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenek61f62162007-09-06 17:11:52 +0000661 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000662}
663
664Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000665Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff1b273c42007-09-16 14:56:35 +0000673Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff507f2d52007-08-31 23:49:30 +0000681 return new ContinueStmt(ContinueLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000682}
683
684Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000685Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff507f2d52007-08-31 23:49:30 +0000693 return new BreakStmt(BreakLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000694}
695
Steve Naroff4eb206b2008-09-03 18:15:37 +0000696/// ActOnBlockReturnStmt - Utilty routine to figure out block's return type.
697///
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 Naroffc50a4a52008-09-16 22:25:10 +0000704 if (RetValExp) {
Steve Naroff16564422008-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 Naroff4eb206b2008-09-03 18:15:37 +0000708 CurBlock->ReturnType = RetValExp->getType().getTypePtr();
Steve Naroffc50a4a52008-09-16 22:25:10 +0000709 } else
Steve Naroff4eb206b2008-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}
Reid Spencer5f016e22007-07-11 17:01:13 +0000748
749Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000750Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
Steve Naroff90045e82007-07-13 23:32:42 +0000751 Expr *RetValExp = static_cast<Expr *>(rex);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000752 if (CurBlock)
753 return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000754 QualType FnRetType =
755 getCurFunctionDecl() ? getCurFunctionDecl()->getResultType() :
756 getCurMethodDecl()->getResultType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000757
Chris Lattner5cf216b2008-01-04 18:04:52 +0000758 if (FnRetType->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
Fariborz Jahanianb107ce82007-12-04 19:20:11 +0000760 Diag(ReturnLoc, diag::ext_return_has_expr,
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000761 ( getCurFunctionDecl() ?
762 getCurFunctionDecl()->getIdentifier()->getName() :
763 getCurMethodDecl()->getSelector().getName() ),
Steve Naroff90045e82007-07-13 23:32:42 +0000764 RetValExp->getSourceRange());
Steve Naroff507f2d52007-08-31 23:49:30 +0000765 return new ReturnStmt(ReturnLoc, RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 } else {
767 if (!RetValExp) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000768 const char *funcName =
769 getCurFunctionDecl() ?
770 getCurFunctionDecl()->getIdentifier()->getName() :
771 getCurMethodDecl()->getSelector().getName().c_str();
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
773 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
774 else // C90 6.6.6.4p4
775 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
Steve Naroff507f2d52007-08-31 23:49:30 +0000776 return new ReturnStmt(ReturnLoc, (Expr*)0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 }
778 }
779 // we have a non-void function with an expression, continue checking
Chris Lattner5cf216b2008-01-04 18:04:52 +0000780 QualType RetValType = RetValExp->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000781
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
783 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
784 // function return.
Chris Lattner5cf216b2008-01-04 18:04:52 +0000785 AssignConvertType ConvTy = CheckSingleAssignmentConstraints(FnRetType,
786 RetValExp);
787 if (DiagnoseAssignmentResult(ConvTy, ReturnLoc, FnRetType,
788 RetValType, RetValExp, "returning"))
789 return true;
Ted Kremenek06de2762007-08-17 16:46:58 +0000790
Chris Lattner5cf216b2008-01-04 18:04:52 +0000791 if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
Ted Kremenek06de2762007-08-17 16:46:58 +0000792
Steve Naroff507f2d52007-08-31 23:49:30 +0000793 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000794}
795
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000796Sema::StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
Anders Carlssondfab34a2008-02-05 23:03:50 +0000797 bool IsSimple,
Anders Carlsson39c47b52007-11-23 23:12:25 +0000798 bool IsVolatile,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000799 unsigned NumOutputs,
800 unsigned NumInputs,
801 std::string *Names,
Chris Lattner1708b962008-08-18 19:55:17 +0000802 ExprTy **constraints,
803 ExprTy **exprs,
Chris Lattner6bc52112008-07-23 06:46:56 +0000804 ExprTy *asmString,
Anders Carlssonb235fc22007-11-22 01:36:19 +0000805 unsigned NumClobbers,
Chris Lattner1708b962008-08-18 19:55:17 +0000806 ExprTy **clobbers,
Chris Lattnerfe795952007-10-29 04:04:16 +0000807 SourceLocation RParenLoc) {
Chris Lattner1708b962008-08-18 19:55:17 +0000808 StringLiteral **Constraints = reinterpret_cast<StringLiteral**>(constraints);
809 Expr **Exprs = reinterpret_cast<Expr **>(exprs);
Chris Lattner6bc52112008-07-23 06:46:56 +0000810 StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString);
Chris Lattner1708b962008-08-18 19:55:17 +0000811 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers);
812
813 // The parser verifies that there is a string literal here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000814 if (AsmString->isWide())
815 // FIXME: We currently leak memory here.
816 return Diag(AsmString->getLocStart(), diag::err_asm_wide_character,
817 AsmString->getSourceRange());
818
819
Chris Lattner1708b962008-08-18 19:55:17 +0000820 for (unsigned i = 0; i != NumOutputs; i++) {
821 StringLiteral *Literal = Constraints[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000822 if (Literal->isWide())
823 // FIXME: We currently leak memory here.
824 return Diag(Literal->getLocStart(), diag::err_asm_wide_character,
825 Literal->getSourceRange());
826
Anders Carlssond04c6e22007-11-27 04:11:28 +0000827 std::string OutputConstraint(Literal->getStrData(),
828 Literal->getByteLength());
829
830 TargetInfo::ConstraintInfo info;
Chris Lattner6bc52112008-07-23 06:46:56 +0000831 if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info))
Anders Carlssond04c6e22007-11-27 04:11:28 +0000832 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000833 return Diag(Literal->getLocStart(),
Chris Lattner1708b962008-08-18 19:55:17 +0000834 diag::err_asm_invalid_output_constraint, OutputConstraint);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000835
836 // Check that the output exprs are valid lvalues.
Chris Lattner1708b962008-08-18 19:55:17 +0000837 ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]);
Chris Lattner28be73f2008-07-26 21:30:36 +0000838 Expr::isLvalueResult Result = OutputExpr->isLvalue(Context);
Anders Carlsson04728b72007-11-23 19:43:50 +0000839 if (Result != Expr::LV_Valid) {
Anders Carlsson04728b72007-11-23 19:43:50 +0000840 // FIXME: We currently leak memory here.
Chris Lattner1708b962008-08-18 19:55:17 +0000841 return Diag(OutputExpr->getSubExpr()->getLocStart(),
842 diag::err_asm_invalid_lvalue_in_output,
843 OutputExpr->getSubExpr()->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000844 }
845 }
846
Anders Carlsson04728b72007-11-23 19:43:50 +0000847 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
Chris Lattner1708b962008-08-18 19:55:17 +0000848 StringLiteral *Literal = Constraints[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000849 if (Literal->isWide())
850 // FIXME: We currently leak memory here.
851 return Diag(Literal->getLocStart(), diag::err_asm_wide_character,
852 Literal->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000853
Anders Carlssond04c6e22007-11-27 04:11:28 +0000854 std::string InputConstraint(Literal->getStrData(),
855 Literal->getByteLength());
856
857 TargetInfo::ConstraintInfo info;
858 if (!Context.Target.validateInputConstraint(InputConstraint.c_str(),
Chris Lattner1708b962008-08-18 19:55:17 +0000859 NumOutputs, info)) {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000860 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000861 return Diag(Literal->getLocStart(),
Chris Lattner1708b962008-08-18 19:55:17 +0000862 diag::err_asm_invalid_input_constraint, InputConstraint);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000863 }
864
865 // Check that the input exprs aren't of type void.
Chris Lattner1708b962008-08-18 19:55:17 +0000866 ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]);
Anders Carlsson04728b72007-11-23 19:43:50 +0000867 if (InputExpr->getType()->isVoidType()) {
Anders Carlsson04728b72007-11-23 19:43:50 +0000868
Anders Carlsson04728b72007-11-23 19:43:50 +0000869 // FIXME: We currently leak memory here.
Chris Lattner1708b962008-08-18 19:55:17 +0000870 return Diag(InputExpr->getSubExpr()->getLocStart(),
871 diag::err_asm_invalid_type_in_input,
872 InputExpr->getType().getAsString(), InputConstraint,
873 InputExpr->getSubExpr()->getSourceRange());
Anders Carlsson04728b72007-11-23 19:43:50 +0000874 }
875 }
Anders Carlssonb235fc22007-11-22 01:36:19 +0000876
Anders Carlsson6fa90862007-11-25 00:25:21 +0000877 // Check that the clobbers are valid.
Chris Lattner1708b962008-08-18 19:55:17 +0000878 for (unsigned i = 0; i != NumClobbers; i++) {
879 StringLiteral *Literal = Clobbers[i];
Chris Lattner6bc52112008-07-23 06:46:56 +0000880 if (Literal->isWide())
881 // FIXME: We currently leak memory here.
882 return Diag(Literal->getLocStart(), diag::err_asm_wide_character,
883 Literal->getSourceRange());
Anders Carlsson6fa90862007-11-25 00:25:21 +0000884
885 llvm::SmallString<16> Clobber(Literal->getStrData(),
886 Literal->getStrData() +
887 Literal->getByteLength());
888
Chris Lattner6bc52112008-07-23 06:46:56 +0000889 if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000890 // FIXME: We currently leak memory here.
Chris Lattner6bc52112008-07-23 06:46:56 +0000891 return Diag(Literal->getLocStart(),
Chris Lattner1708b962008-08-18 19:55:17 +0000892 diag::err_asm_unknown_register_name, Clobber.c_str());
Anders Carlsson6fa90862007-11-25 00:25:21 +0000893 }
894
Chris Lattner1708b962008-08-18 19:55:17 +0000895 return new AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs,
896 Names, Constraints, Exprs, AsmString, NumClobbers,
897 Clobbers, RParenLoc);
Chris Lattnerfe795952007-10-29 04:04:16 +0000898}
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000899
900Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000901Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000902 SourceLocation RParen, StmtTy *Parm,
903 StmtTy *Body, StmtTy *CatchList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000904 ObjCAtCatchStmt *CS = new ObjCAtCatchStmt(AtLoc, RParen,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000905 static_cast<Stmt*>(Parm), static_cast<Stmt*>(Body),
906 static_cast<Stmt*>(CatchList));
907 return CatchList ? CatchList : CS;
908}
909
Fariborz Jahanian161a9c52007-11-02 00:18:53 +0000910Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000911Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtTy *Body) {
912 ObjCAtFinallyStmt *FS = new ObjCAtFinallyStmt(AtLoc,
Fariborz Jahanian161a9c52007-11-02 00:18:53 +0000913 static_cast<Stmt*>(Body));
914 return FS;
915}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000916
917Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000919 StmtTy *Try, StmtTy *Catch, StmtTy *Finally) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000920 ObjCAtTryStmt *TS = new ObjCAtTryStmt(AtLoc, static_cast<Stmt*>(Try),
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000921 static_cast<Stmt*>(Catch),
922 static_cast<Stmt*>(Finally));
923 return TS;
924}
925
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000926Action::StmtResult
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000927Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, StmtTy *Throw) {
928 ObjCAtThrowStmt *TS = new ObjCAtThrowStmt(AtLoc, static_cast<Stmt*>(Throw));
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000929 return TS;
930}
Fariborz Jahanianbd49a642007-11-02 15:39:31 +0000931
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000932Action::StmtResult
933Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprTy *SynchExpr,
934 StmtTy *SynchBody) {
935 ObjCAtSynchronizedStmt *SS = new ObjCAtSynchronizedStmt(AtLoc,
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000936 static_cast<Stmt*>(SynchExpr), static_cast<Stmt*>(SynchBody));
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000937 return SS;
938}