blob: b6d4c5580d46f88f597b6e8c2de9004db271ee4a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/Expr.h"
Chris Lattnerf4021e72007-08-23 05:46:52 +000017#include "clang/AST/Stmt.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Parse/Scope.h"
19#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/LangOptions.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Steve Naroff1b273c42007-09-16 14:56:35 +000023Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) {
Reid Spencer5f016e22007-07-11 17:01:13 +000024 Expr *E = static_cast<Expr*>(expr);
Steve Naroff1b273c42007-09-16 14:56:35 +000025 assert(E && "ActOnExprStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +000026 return E;
27}
28
29
Steve Naroff1b273c42007-09-16 14:56:35 +000030Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 return new NullStmt(SemiLoc);
32}
33
Steve Naroff1b273c42007-09-16 14:56:35 +000034Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl) {
Steve Naroff8e74c932007-09-13 21:41:19 +000035 if (decl) {
36 ScopedDecl *SD = dyn_cast<ScopedDecl>(static_cast<Decl *>(decl));
Steve Naroff1b273c42007-09-16 14:56:35 +000037 assert(SD && "Sema::ActOnDeclStmt(): expected ScopedDecl");
Steve Naroff8e74c932007-09-13 21:41:19 +000038 return new DeclStmt(SD);
39 } else
Reid Spencer5f016e22007-07-11 17:01:13 +000040 return true; // error
41}
42
43Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +000044Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattner98414c12007-08-31 21:49:55 +000045 StmtTy **elts, unsigned NumElts, bool isStmtExpr) {
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000046 Stmt **Elts = reinterpret_cast<Stmt**>(elts);
47 // If we're in C89 mode, check that we don't have any decls after stmts. If
48 // so, emit an extension diagnostic.
49 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
50 // Note that __extension__ can be around a decl.
51 unsigned i = 0;
52 // Skip over all declarations.
53 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
54 /*empty*/;
55
56 // We found the end of the list or a statement. Scan for another declstmt.
57 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
58 /*empty*/;
59
60 if (i != NumElts) {
Steve Naroff8e74c932007-09-13 21:41:19 +000061 ScopedDecl *D = cast<DeclStmt>(Elts[i])->getDecl();
Chris Lattnerc30ebfb2007-08-27 04:29:41 +000062 Diag(D->getLocation(), diag::ext_mixed_decls_code);
63 }
64 }
Chris Lattner98414c12007-08-31 21:49:55 +000065 // Warn about unused expressions in statements.
66 for (unsigned i = 0; i != NumElts; ++i) {
67 Expr *E = dyn_cast<Expr>(Elts[i]);
68 if (!E) continue;
69
70 // Warn about expressions with unused results.
71 if (E->hasLocalSideEffect() || E->getType()->isVoidType())
72 continue;
73
74 // The last expr in a stmt expr really is used.
75 if (isStmtExpr && i == NumElts-1)
76 continue;
77
78 /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in
79 /// a context where the result is unused. Emit a diagnostic to warn about
80 /// this.
81 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
82 Diag(BO->getOperatorLoc(), diag::warn_unused_expr,
83 BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange());
84 else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
85 Diag(UO->getOperatorLoc(), diag::warn_unused_expr,
86 UO->getSubExpr()->getSourceRange());
87 else
88 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
89 }
90
Steve Naroffb5a69582007-08-31 23:28:33 +000091 return new CompoundStmt(Elts, NumElts, L, R);
Reid Spencer5f016e22007-07-11 17:01:13 +000092}
93
94Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +000095Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner6c36be52007-07-18 02:28:47 +000096 SourceLocation DotDotDotLoc, ExprTy *rhsval,
Chris Lattner0fa152e2007-07-21 03:00:26 +000097 SourceLocation ColonLoc, StmtTy *subStmt) {
98 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner8a87e572007-07-23 17:05:23 +000099 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 assert((LHSVal != 0) && "missing expression in case statement");
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 SourceLocation ExpLoc;
103 // C99 6.8.4.2p3: The expression shall be an integer constant.
Chris Lattner0fa152e2007-07-21 03:00:26 +0000104 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
105 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
106 LHSVal->getSourceRange());
107 return SubStmt;
108 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000109
Chris Lattner6c36be52007-07-18 02:28:47 +0000110 // GCC extension: The expression shall be an integer constant.
Chris Lattner0fa152e2007-07-21 03:00:26 +0000111 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
112 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
113 RHSVal->getSourceRange());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000114 RHSVal = 0; // Recover by just forgetting about it.
Chris Lattner6c36be52007-07-18 02:28:47 +0000115 }
Chris Lattner8a87e572007-07-23 17:05:23 +0000116
117 if (SwitchStack.empty()) {
118 Diag(CaseLoc, diag::err_case_not_in_switch);
119 return SubStmt;
120 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000121
Steve Naroffb5a69582007-08-31 23:28:33 +0000122 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
Chris Lattner8a87e572007-07-23 17:05:23 +0000123 SwitchStack.back()->addSwitchCase(CS);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000124 return CS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125}
126
127Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000128Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner0fa152e2007-07-21 03:00:26 +0000129 StmtTy *subStmt, Scope *CurScope) {
130 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner6c36be52007-07-18 02:28:47 +0000131
Chris Lattner8a87e572007-07-23 17:05:23 +0000132 if (SwitchStack.empty()) {
Chris Lattner0fa152e2007-07-21 03:00:26 +0000133 Diag(DefaultLoc, diag::err_default_not_in_switch);
134 return SubStmt;
135 }
136
Chris Lattner0fa152e2007-07-21 03:00:26 +0000137 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner8a87e572007-07-23 17:05:23 +0000138 SwitchStack.back()->addSwitchCase(DS);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000139
Chris Lattner6c36be52007-07-18 02:28:47 +0000140 return DS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000141}
142
143Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000144Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner0fa152e2007-07-21 03:00:26 +0000145 SourceLocation ColonLoc, StmtTy *subStmt) {
146 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 // Look up the record for this label identifier.
148 LabelStmt *&LabelDecl = LabelMap[II];
149
150 // If not forward referenced or defined already, just create a new LabelStmt.
151 if (LabelDecl == 0)
Chris Lattner0fa152e2007-07-21 03:00:26 +0000152 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153
154 assert(LabelDecl->getID() == II && "Label mismatch!");
155
156 // Otherwise, this label was either forward reference or multiply defined. If
157 // multiply defined, reject it now.
158 if (LabelDecl->getSubStmt()) {
159 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
160 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000161 return SubStmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 }
163
164 // Otherwise, this label was forward declared, and we just found its real
165 // definition. Fill in the forward definition and return it.
166 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner0fa152e2007-07-21 03:00:26 +0000167 LabelDecl->setSubStmt(SubStmt);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 return LabelDecl;
169}
170
171Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000172Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 StmtTy *ThenVal, SourceLocation ElseLoc,
174 StmtTy *ElseVal) {
175 Expr *condExpr = (Expr *)CondVal;
Steve Naroff1b273c42007-09-16 14:56:35 +0000176 assert(condExpr && "ActOnIfStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000177
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000178 DefaultFunctionArrayConversion(condExpr);
179 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000180
181 if (!condType->isScalarType()) // C99 6.8.4.1p1
182 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
183 condType.getAsString(), condExpr->getSourceRange());
184
Steve Naroffb5a69582007-08-31 23:28:33 +0000185 return new IfStmt(IfLoc, condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000186}
187
188Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000189Sema::ActOnStartOfSwitchStmt(ExprTy *cond) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000190 Expr *Cond = static_cast<Expr*>(cond);
191
192 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
193 UsualUnaryConversions(Cond);
194
195 SwitchStmt *SS = new SwitchStmt(Cond);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000196 SwitchStack.push_back(SS);
197 return SS;
198}
Chris Lattner6c36be52007-07-18 02:28:47 +0000199
Chris Lattnerf4021e72007-08-23 05:46:52 +0000200/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
201/// the specified width and sign. If an overflow occurs, detect it and emit
202/// the specified diagnostic.
203void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
204 unsigned NewWidth, bool NewSign,
205 SourceLocation Loc,
206 unsigned DiagID) {
207 // Perform a conversion to the promoted condition type if needed.
208 if (NewWidth > Val.getBitWidth()) {
209 // If this is an extension, just do it.
210 llvm::APSInt OldVal(Val);
211 Val.extend(NewWidth);
212
213 // If the input was signed and negative and the output is unsigned,
214 // warn.
215 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
216 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
217
218 Val.setIsSigned(NewSign);
219 } else if (NewWidth < Val.getBitWidth()) {
220 // If this is a truncation, check for overflow.
221 llvm::APSInt ConvVal(Val);
222 ConvVal.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000223 ConvVal.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000224 ConvVal.extend(Val.getBitWidth());
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000225 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000226 if (ConvVal != Val)
227 Diag(Loc, DiagID, Val.toString(), ConvVal.toString());
228
229 // Regardless of whether a diagnostic was emitted, really do the
230 // truncation.
231 Val.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000232 Val.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000233 } else if (NewSign != Val.isSigned()) {
234 // Convert the sign to match the sign of the condition. This can cause
235 // overflow as well: unsigned(INTMIN)
236 llvm::APSInt OldVal(Val);
237 Val.setIsSigned(NewSign);
238
239 if (Val.isNegative()) // Sign bit changes meaning.
240 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
241 }
242}
243
Chris Lattner0471f5b2007-08-23 18:29:20 +0000244namespace {
245 struct CaseCompareFunctor {
246 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
247 const llvm::APSInt &RHS) {
248 return LHS.first < RHS;
249 }
Chris Lattner0e85a272007-09-03 18:31:57 +0000250 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
251 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
252 return LHS.first < RHS.first;
253 }
Chris Lattner0471f5b2007-08-23 18:29:20 +0000254 bool operator()(const llvm::APSInt &LHS,
255 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
256 return LHS < RHS.first;
257 }
258 };
259}
260
Chris Lattner764a7ce2007-09-21 18:15:22 +0000261/// CmpCaseVals - Comparison predicate for sorting case values.
262///
263static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
264 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
265 if (lhs.first < rhs.first)
266 return true;
267
268 if (lhs.first == rhs.first &&
269 lhs.second->getCaseLoc().getRawEncoding()
270 < rhs.second->getCaseLoc().getRawEncoding())
271 return true;
272 return false;
273}
274
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000275Action::StmtResult
Chris Lattner764a7ce2007-09-21 18:15:22 +0000276Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
277 ExprTy *Body) {
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000278 Stmt *BodyStmt = (Stmt*)Body;
279
280 SwitchStmt *SS = SwitchStack.back();
281 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
282
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000283 SS->setBody(BodyStmt, SwitchLoc);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000284 SwitchStack.pop_back();
285
Chris Lattnerf4021e72007-08-23 05:46:52 +0000286 Expr *CondExpr = SS->getCond();
287 QualType CondType = CondExpr->getType();
Chris Lattner6c36be52007-07-18 02:28:47 +0000288
Chris Lattnerf4021e72007-08-23 05:46:52 +0000289 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000290 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattnerf4021e72007-08-23 05:46:52 +0000291 CondType.getAsString(), CondExpr->getSourceRange());
292 return true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000293 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000294
295 // Get the bitwidth of the switched-on value before promotions. We must
296 // convert the integer case values to this width before comparison.
Hartmut Kaiser1ff4f0c2007-09-16 00:04:22 +0000297 unsigned CondWidth =
298 static_cast<unsigned>(Context.getTypeSize(CondType, SwitchLoc));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000299 bool CondIsSigned = CondType->isSignedIntegerType();
300
301 // Accumulate all of the case values in a vector so that we can sort them
302 // and detect duplicates. This vector contains the APInt for the case after
303 // it has been converted to the condition type.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000304 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
305 CaseValsTy CaseVals;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000306
307 // Keep track of any GNU case ranges we see. The APSInt is the low value.
308 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
309
310 DefaultStmt *TheDefaultStmt = 0;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000311
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000312 bool CaseListIsErroneous = false;
313
314 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000315 SC = SC->getNextSwitchCase()) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000316
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000317 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000318 if (TheDefaultStmt) {
319 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
320 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000321
Chris Lattnerf4021e72007-08-23 05:46:52 +0000322 // FIXME: Remove the default statement from the switch block so that
323 // we'll return a valid AST. This requires recursing down the
324 // AST and finding it, not something we are set up to do right now. For
325 // now, just lop the entire switch stmt out of the AST.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000326 CaseListIsErroneous = true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000327 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000328 TheDefaultStmt = DS;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000329
Chris Lattnerf4021e72007-08-23 05:46:52 +0000330 } else {
331 CaseStmt *CS = cast<CaseStmt>(SC);
332
333 // We already verified that the expression has a i-c-e value (C99
334 // 6.8.4.2p3) - get that value now.
335 llvm::APSInt LoVal(32);
336 CS->getLHS()->isIntegerConstantExpr(LoVal, Context);
337
338 // Convert the value to the same width/sign as the condition.
339 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
340 CS->getLHS()->getLocStart(),
341 diag::warn_case_value_overflow);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000342
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000343 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerf4021e72007-08-23 05:46:52 +0000344 if (CS->getRHS())
345 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000346 else
347 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000348 }
349 }
350
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000351 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner764a7ce2007-09-21 18:15:22 +0000352 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000353
Chris Lattnerf3348502007-08-23 14:29:07 +0000354 if (!CaseVals.empty()) {
355 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
356 if (CaseVals[i].first == CaseVals[i+1].first) {
357 // If we have a duplicate, report it.
358 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
359 diag::err_duplicate_case, CaseVals[i].first.toString());
360 Diag(CaseVals[i].second->getLHS()->getLocStart(),
361 diag::err_duplicate_case_prev);
362 // FIXME: We really want to remove the bogus case stmt from the substmt,
363 // but we have no way to do this right now.
364 CaseListIsErroneous = true;
365 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000366 }
367 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000368
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000369 // Detect duplicate case ranges, which usually don't exist at all in the first
370 // place.
371 if (!CaseRanges.empty()) {
372 // Sort all the case ranges by their low value so we can easily detect
373 // overlaps between ranges.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000374 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000375
376 // Scan the ranges, computing the high values and removing empty ranges.
377 std::vector<llvm::APSInt> HiVals;
Chris Lattner6efc4d32007-08-23 17:48:14 +0000378 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000379 CaseStmt *CR = CaseRanges[i].second;
380 llvm::APSInt HiVal(32);
381 CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
382
383 // Convert the value to the same width/sign as the condition.
384 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
385 CR->getRHS()->getLocStart(),
386 diag::warn_case_value_overflow);
387
Chris Lattner6efc4d32007-08-23 17:48:14 +0000388 // If the low value is bigger than the high value, the case is empty.
389 if (CaseRanges[i].first > HiVal) {
390 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
391 SourceRange(CR->getLHS()->getLocStart(),
392 CR->getRHS()->getLocEnd()));
393 CaseRanges.erase(CaseRanges.begin()+i);
394 --i, --e;
395 continue;
396 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000397 HiVals.push_back(HiVal);
398 }
399
400 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0471f5b2007-08-23 18:29:20 +0000401 // ranges. Since the range list is sorted, we only need to compare case
402 // ranges with their neighbors.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000403 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0471f5b2007-08-23 18:29:20 +0000404 llvm::APSInt &CRLo = CaseRanges[i].first;
405 llvm::APSInt &CRHi = HiVals[i];
406 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000407
Chris Lattner0471f5b2007-08-23 18:29:20 +0000408 // Check to see whether the case range overlaps with any singleton cases.
409 CaseStmt *OverlapStmt = 0;
410 llvm::APSInt OverlapVal(32);
411
412 // Find the smallest value >= the lower bound. If I is in the case range,
413 // then we have overlap.
414 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
415 CaseVals.end(), CRLo,
416 CaseCompareFunctor());
417 if (I != CaseVals.end() && I->first < CRHi) {
418 OverlapVal = I->first; // Found overlap with scalar.
419 OverlapStmt = I->second;
420 }
421
422 // Find the smallest value bigger than the upper bound.
423 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
424 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
425 OverlapVal = (I-1)->first; // Found overlap with scalar.
426 OverlapStmt = (I-1)->second;
427 }
428
429 // Check to see if this case stmt overlaps with the subsequent case range.
430 if (i && CRLo <= HiVals[i-1]) {
431 OverlapVal = HiVals[i-1]; // Found overlap with range.
432 OverlapStmt = CaseRanges[i-1].second;
433 }
434
435 if (OverlapStmt) {
436 // If we have a duplicate, report it.
437 Diag(CR->getLHS()->getLocStart(),
438 diag::err_duplicate_case, OverlapVal.toString());
439 Diag(OverlapStmt->getLHS()->getLocStart(),
440 diag::err_duplicate_case_prev);
441 // FIXME: We really want to remove the bogus case stmt from the substmt,
442 // but we have no way to do this right now.
443 CaseListIsErroneous = true;
444 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000445 }
446 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000447
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000448 // FIXME: If the case list was broken is some way, we don't have a good system
449 // to patch it up. Instead, just return the whole substmt as broken.
450 if (CaseListIsErroneous)
451 return true;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000452
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000453 return SS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000454}
455
456Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000457Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000459 assert(condExpr && "ActOnWhileStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000460
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000461 DefaultFunctionArrayConversion(condExpr);
462 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000463
464 if (!condType->isScalarType()) // C99 6.8.5p2
465 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
466 condType.getAsString(), condExpr->getSourceRange());
467
Steve Naroffb5a69582007-08-31 23:28:33 +0000468 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000469}
470
471Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000472Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 SourceLocation WhileLoc, ExprTy *Cond) {
474 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000475 assert(condExpr && "ActOnDoStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000476
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000477 DefaultFunctionArrayConversion(condExpr);
478 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000479
480 if (!condType->isScalarType()) // C99 6.8.5p2
481 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
482 condType.getAsString(), condExpr->getSourceRange());
483
Steve Naroffb5a69582007-08-31 23:28:33 +0000484 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000485}
486
487Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000488Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000489 StmtTy *first, ExprTy *second, ExprTy *third,
490 SourceLocation RParenLoc, StmtTy *body) {
491 Stmt *First = static_cast<Stmt*>(first);
492 Expr *Second = static_cast<Expr*>(second);
493 Expr *Third = static_cast<Expr*>(third);
494 Stmt *Body = static_cast<Stmt*>(body);
495
Chris Lattnerae3b7012007-08-28 05:03:08 +0000496 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
497 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
498 // identifiers for objects having storage class 'auto' or 'register'.
Steve Naroff94745042007-09-13 23:52:58 +0000499 for (ScopedDecl *D = DS->getDecl(); D; D = D->getNextDeclarator()) {
Chris Lattnerae3b7012007-08-28 05:03:08 +0000500 BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D);
501 if (BVD && !BVD->hasLocalStorage())
502 BVD = 0;
503 if (BVD == 0)
Steve Naroff8e74c932007-09-13 21:41:19 +0000504 Diag(dyn_cast<ScopedDecl>(D)->getLocation(),
505 diag::err_non_variable_decl_in_for);
Chris Lattnerae3b7012007-08-28 05:03:08 +0000506 // FIXME: mark decl erroneous!
507 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000508 }
509 if (Second) {
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000510 DefaultFunctionArrayConversion(Second);
511 QualType SecondType = Second->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000512
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000513 if (!SecondType->isScalarType()) // C99 6.8.5p2
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000515 SecondType.getAsString(), Second->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 }
Steve Naroffb5a69582007-08-31 23:28:33 +0000517 return new ForStmt(First, Second, Third, Body, ForLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000518}
519
520
521Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000522Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 IdentifierInfo *LabelII) {
524 // Look up the record for this label identifier.
525 LabelStmt *&LabelDecl = LabelMap[LabelII];
526
527 // If we haven't seen this label yet, create a forward reference.
528 if (LabelDecl == 0)
529 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
530
Ted Kremenek61f62162007-09-06 17:11:52 +0000531 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000532}
533
534Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000535Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 ExprTy *DestExp) {
537 // FIXME: Verify that the operand is convertible to void*.
538
539 return new IndirectGotoStmt((Expr*)DestExp);
540}
541
542Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000543Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 Scope *S = CurScope->getContinueParent();
545 if (!S) {
546 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
547 Diag(ContinueLoc, diag::err_continue_not_in_loop);
548 return true;
549 }
550
Steve Naroff507f2d52007-08-31 23:49:30 +0000551 return new ContinueStmt(ContinueLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000552}
553
554Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000555Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 Scope *S = CurScope->getBreakParent();
557 if (!S) {
558 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
559 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
560 return true;
561 }
562
Steve Naroff507f2d52007-08-31 23:49:30 +0000563 return new BreakStmt(BreakLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000564}
565
566
567Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000568Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
Steve Naroff90045e82007-07-13 23:32:42 +0000569 Expr *RetValExp = static_cast<Expr *>(rex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 QualType lhsType = CurFunctionDecl->getResultType();
571
572 if (lhsType->isVoidType()) {
573 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
574 Diag(ReturnLoc, diag::ext_return_has_expr,
575 CurFunctionDecl->getIdentifier()->getName(),
Steve Naroff90045e82007-07-13 23:32:42 +0000576 RetValExp->getSourceRange());
Steve Naroff507f2d52007-08-31 23:49:30 +0000577 return new ReturnStmt(ReturnLoc, RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 } else {
579 if (!RetValExp) {
580 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
581 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
582 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
583 else // C90 6.6.6.4p4
584 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
Steve Naroff507f2d52007-08-31 23:49:30 +0000585 return new ReturnStmt(ReturnLoc, (Expr*)0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 }
587 }
588 // we have a non-void function with an expression, continue checking
Steve Naroff90045e82007-07-13 23:32:42 +0000589 QualType rhsType = RetValExp->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000590
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
592 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
593 // function return.
Steve Naroff90045e82007-07-13 23:32:42 +0000594 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
595 RetValExp);
Ted Kremenek6a2394c2007-08-14 18:14:14 +0000596
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 // decode the result (notice that extensions still return a type).
598 switch (result) {
599 case Compatible:
600 break;
601 case Incompatible:
602 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
603 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff90045e82007-07-13 23:32:42 +0000604 RetValExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 break;
606 case PointerFromInt:
607 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner590b6642007-07-15 23:26:56 +0000608 if (!RetValExp->isNullPointerConstant(Context)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
610 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff90045e82007-07-13 23:32:42 +0000611 RetValExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 }
613 break;
614 case IntFromPointer:
615 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
616 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff90045e82007-07-13 23:32:42 +0000617 RetValExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 break;
619 case IncompatiblePointer:
620 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
621 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff90045e82007-07-13 23:32:42 +0000622 RetValExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 break;
624 case CompatiblePointerDiscardsQualifiers:
625 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
626 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff90045e82007-07-13 23:32:42 +0000627 RetValExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 break;
629 }
Ted Kremenek06de2762007-08-17 16:46:58 +0000630
631 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
632
Steve Naroff507f2d52007-08-31 23:49:30 +0000633 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000634}
635