blob: 460b50ccbf349b04def77ec2864b0b8068d58f12 [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;
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000176 Stmt *thenStmt = (Stmt *)ThenVal;
177
Steve Naroff1b273c42007-09-16 14:56:35 +0000178 assert(condExpr && "ActOnIfStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000179
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000180 DefaultFunctionArrayConversion(condExpr);
181 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000182
183 if (!condType->isScalarType()) // C99 6.8.4.1p1
184 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
185 condType.getAsString(), condExpr->getSourceRange());
186
Anders Carlsson2d85f8b2007-10-10 20:50:11 +0000187 // Warn if the if block has a null body without an else value.
188 // this helps prevent bugs due to typos, such as
189 // if (condition);
190 // do_stuff();
191 if (!ElseVal) {
192 if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
193 Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
194 }
195
196 return new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000197}
198
199Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000200Sema::ActOnStartOfSwitchStmt(ExprTy *cond) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000201 Expr *Cond = static_cast<Expr*>(cond);
202
203 // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
204 UsualUnaryConversions(Cond);
205
206 SwitchStmt *SS = new SwitchStmt(Cond);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000207 SwitchStack.push_back(SS);
208 return SS;
209}
Chris Lattner6c36be52007-07-18 02:28:47 +0000210
Chris Lattnerf4021e72007-08-23 05:46:52 +0000211/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
212/// the specified width and sign. If an overflow occurs, detect it and emit
213/// the specified diagnostic.
214void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
215 unsigned NewWidth, bool NewSign,
216 SourceLocation Loc,
217 unsigned DiagID) {
218 // Perform a conversion to the promoted condition type if needed.
219 if (NewWidth > Val.getBitWidth()) {
220 // If this is an extension, just do it.
221 llvm::APSInt OldVal(Val);
222 Val.extend(NewWidth);
223
224 // If the input was signed and negative and the output is unsigned,
225 // warn.
226 if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
227 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
228
229 Val.setIsSigned(NewSign);
230 } else if (NewWidth < Val.getBitWidth()) {
231 // If this is a truncation, check for overflow.
232 llvm::APSInt ConvVal(Val);
233 ConvVal.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000234 ConvVal.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000235 ConvVal.extend(Val.getBitWidth());
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000236 ConvVal.setIsSigned(Val.isSigned());
Chris Lattnerf4021e72007-08-23 05:46:52 +0000237 if (ConvVal != Val)
238 Diag(Loc, DiagID, Val.toString(), ConvVal.toString());
239
240 // Regardless of whether a diagnostic was emitted, really do the
241 // truncation.
242 Val.trunc(NewWidth);
Chris Lattnerb2137ae2007-08-23 22:08:35 +0000243 Val.setIsSigned(NewSign);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000244 } else if (NewSign != Val.isSigned()) {
245 // Convert the sign to match the sign of the condition. This can cause
246 // overflow as well: unsigned(INTMIN)
247 llvm::APSInt OldVal(Val);
248 Val.setIsSigned(NewSign);
249
250 if (Val.isNegative()) // Sign bit changes meaning.
251 Diag(Loc, DiagID, OldVal.toString(), Val.toString());
252 }
253}
254
Chris Lattner0471f5b2007-08-23 18:29:20 +0000255namespace {
256 struct CaseCompareFunctor {
257 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
258 const llvm::APSInt &RHS) {
259 return LHS.first < RHS;
260 }
Chris Lattner0e85a272007-09-03 18:31:57 +0000261 bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
262 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
263 return LHS.first < RHS.first;
264 }
Chris Lattner0471f5b2007-08-23 18:29:20 +0000265 bool operator()(const llvm::APSInt &LHS,
266 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
267 return LHS < RHS.first;
268 }
269 };
270}
271
Chris Lattner764a7ce2007-09-21 18:15:22 +0000272/// CmpCaseVals - Comparison predicate for sorting case values.
273///
274static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
275 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
276 if (lhs.first < rhs.first)
277 return true;
278
279 if (lhs.first == rhs.first &&
280 lhs.second->getCaseLoc().getRawEncoding()
281 < rhs.second->getCaseLoc().getRawEncoding())
282 return true;
283 return false;
284}
285
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000286Action::StmtResult
Chris Lattner764a7ce2007-09-21 18:15:22 +0000287Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
288 ExprTy *Body) {
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000289 Stmt *BodyStmt = (Stmt*)Body;
290
291 SwitchStmt *SS = SwitchStack.back();
292 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
293
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000294 SS->setBody(BodyStmt, SwitchLoc);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000295 SwitchStack.pop_back();
296
Chris Lattnerf4021e72007-08-23 05:46:52 +0000297 Expr *CondExpr = SS->getCond();
298 QualType CondType = CondExpr->getType();
Chris Lattner6c36be52007-07-18 02:28:47 +0000299
Chris Lattnerf4021e72007-08-23 05:46:52 +0000300 if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000301 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
Chris Lattnerf4021e72007-08-23 05:46:52 +0000302 CondType.getAsString(), CondExpr->getSourceRange());
303 return true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000304 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000305
306 // Get the bitwidth of the switched-on value before promotions. We must
307 // convert the integer case values to this width before comparison.
Hartmut Kaiser1ff4f0c2007-09-16 00:04:22 +0000308 unsigned CondWidth =
309 static_cast<unsigned>(Context.getTypeSize(CondType, SwitchLoc));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000310 bool CondIsSigned = CondType->isSignedIntegerType();
311
312 // Accumulate all of the case values in a vector so that we can sort them
313 // and detect duplicates. This vector contains the APInt for the case after
314 // it has been converted to the condition type.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000315 typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
316 CaseValsTy CaseVals;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000317
318 // Keep track of any GNU case ranges we see. The APSInt is the low value.
319 std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
320
321 DefaultStmt *TheDefaultStmt = 0;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000322
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000323 bool CaseListIsErroneous = false;
324
325 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000326 SC = SC->getNextSwitchCase()) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000327
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000328 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
Chris Lattnerf4021e72007-08-23 05:46:52 +0000329 if (TheDefaultStmt) {
330 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
331 Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000332
Chris Lattnerf4021e72007-08-23 05:46:52 +0000333 // FIXME: Remove the default statement from the switch block so that
334 // we'll return a valid AST. This requires recursing down the
335 // AST and finding it, not something we are set up to do right now. For
336 // now, just lop the entire switch stmt out of the AST.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000337 CaseListIsErroneous = true;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000338 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000339 TheDefaultStmt = DS;
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000340
Chris Lattnerf4021e72007-08-23 05:46:52 +0000341 } else {
342 CaseStmt *CS = cast<CaseStmt>(SC);
343
344 // We already verified that the expression has a i-c-e value (C99
345 // 6.8.4.2p3) - get that value now.
346 llvm::APSInt LoVal(32);
347 CS->getLHS()->isIntegerConstantExpr(LoVal, Context);
348
349 // Convert the value to the same width/sign as the condition.
350 ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
351 CS->getLHS()->getLocStart(),
352 diag::warn_case_value_overflow);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000353
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000354 // If this is a case range, remember it in CaseRanges, otherwise CaseVals.
Chris Lattnerf4021e72007-08-23 05:46:52 +0000355 if (CS->getRHS())
356 CaseRanges.push_back(std::make_pair(LoVal, CS));
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000357 else
358 CaseVals.push_back(std::make_pair(LoVal, CS));
Chris Lattnerf4021e72007-08-23 05:46:52 +0000359 }
360 }
361
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000362 // Sort all the scalar case values so we can easily detect duplicates.
Chris Lattner764a7ce2007-09-21 18:15:22 +0000363 std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
Chris Lattnerf4021e72007-08-23 05:46:52 +0000364
Chris Lattnerf3348502007-08-23 14:29:07 +0000365 if (!CaseVals.empty()) {
366 for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
367 if (CaseVals[i].first == CaseVals[i+1].first) {
368 // If we have a duplicate, report it.
369 Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
370 diag::err_duplicate_case, CaseVals[i].first.toString());
371 Diag(CaseVals[i].second->getLHS()->getLocStart(),
372 diag::err_duplicate_case_prev);
373 // FIXME: We really want to remove the bogus case stmt from the substmt,
374 // but we have no way to do this right now.
375 CaseListIsErroneous = true;
376 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000377 }
378 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000379
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000380 // Detect duplicate case ranges, which usually don't exist at all in the first
381 // place.
382 if (!CaseRanges.empty()) {
383 // Sort all the case ranges by their low value so we can easily detect
384 // overlaps between ranges.
Chris Lattner0471f5b2007-08-23 18:29:20 +0000385 std::stable_sort(CaseRanges.begin(), CaseRanges.end());
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000386
387 // Scan the ranges, computing the high values and removing empty ranges.
388 std::vector<llvm::APSInt> HiVals;
Chris Lattner6efc4d32007-08-23 17:48:14 +0000389 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000390 CaseStmt *CR = CaseRanges[i].second;
391 llvm::APSInt HiVal(32);
392 CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
393
394 // Convert the value to the same width/sign as the condition.
395 ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
396 CR->getRHS()->getLocStart(),
397 diag::warn_case_value_overflow);
398
Chris Lattner6efc4d32007-08-23 17:48:14 +0000399 // If the low value is bigger than the high value, the case is empty.
400 if (CaseRanges[i].first > HiVal) {
401 Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
402 SourceRange(CR->getLHS()->getLocStart(),
403 CR->getRHS()->getLocEnd()));
404 CaseRanges.erase(CaseRanges.begin()+i);
405 --i, --e;
406 continue;
407 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000408 HiVals.push_back(HiVal);
409 }
410
411 // Rescan the ranges, looking for overlap with singleton values and other
Chris Lattner0471f5b2007-08-23 18:29:20 +0000412 // ranges. Since the range list is sorted, we only need to compare case
413 // ranges with their neighbors.
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000414 for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
Chris Lattner0471f5b2007-08-23 18:29:20 +0000415 llvm::APSInt &CRLo = CaseRanges[i].first;
416 llvm::APSInt &CRHi = HiVals[i];
417 CaseStmt *CR = CaseRanges[i].second;
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000418
Chris Lattner0471f5b2007-08-23 18:29:20 +0000419 // Check to see whether the case range overlaps with any singleton cases.
420 CaseStmt *OverlapStmt = 0;
421 llvm::APSInt OverlapVal(32);
422
423 // Find the smallest value >= the lower bound. If I is in the case range,
424 // then we have overlap.
425 CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
426 CaseVals.end(), CRLo,
427 CaseCompareFunctor());
428 if (I != CaseVals.end() && I->first < CRHi) {
429 OverlapVal = I->first; // Found overlap with scalar.
430 OverlapStmt = I->second;
431 }
432
433 // Find the smallest value bigger than the upper bound.
434 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
435 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
436 OverlapVal = (I-1)->first; // Found overlap with scalar.
437 OverlapStmt = (I-1)->second;
438 }
439
440 // Check to see if this case stmt overlaps with the subsequent case range.
441 if (i && CRLo <= HiVals[i-1]) {
442 OverlapVal = HiVals[i-1]; // Found overlap with range.
443 OverlapStmt = CaseRanges[i-1].second;
444 }
445
446 if (OverlapStmt) {
447 // If we have a duplicate, report it.
448 Diag(CR->getLHS()->getLocStart(),
449 diag::err_duplicate_case, OverlapVal.toString());
450 Diag(OverlapStmt->getLHS()->getLocStart(),
451 diag::err_duplicate_case_prev);
452 // FIXME: We really want to remove the bogus case stmt from the substmt,
453 // but we have no way to do this right now.
454 CaseListIsErroneous = true;
455 }
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000456 }
457 }
Chris Lattnerf4021e72007-08-23 05:46:52 +0000458
Chris Lattnerb2ec9d62007-08-23 06:23:56 +0000459 // FIXME: If the case list was broken is some way, we don't have a good system
460 // to patch it up. Instead, just return the whole substmt as broken.
461 if (CaseListIsErroneous)
462 return true;
Chris Lattnerf4021e72007-08-23 05:46:52 +0000463
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000464 return SS;
Reid Spencer5f016e22007-07-11 17:01:13 +0000465}
466
467Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000468Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000470 assert(condExpr && "ActOnWhileStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000471
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000472 DefaultFunctionArrayConversion(condExpr);
473 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000474
475 if (!condType->isScalarType()) // C99 6.8.5p2
476 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
477 condType.getAsString(), condExpr->getSourceRange());
478
Steve Naroffb5a69582007-08-31 23:28:33 +0000479 return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000480}
481
482Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000483Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Reid Spencer5f016e22007-07-11 17:01:13 +0000484 SourceLocation WhileLoc, ExprTy *Cond) {
485 Expr *condExpr = (Expr *)Cond;
Steve Naroff1b273c42007-09-16 14:56:35 +0000486 assert(condExpr && "ActOnDoStmt(): missing expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000487
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000488 DefaultFunctionArrayConversion(condExpr);
489 QualType condType = condExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000490
491 if (!condType->isScalarType()) // C99 6.8.5p2
492 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
493 condType.getAsString(), condExpr->getSourceRange());
494
Steve Naroffb5a69582007-08-31 23:28:33 +0000495 return new DoStmt((Stmt*)Body, condExpr, DoLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000496}
497
498Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000499Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000500 StmtTy *first, ExprTy *second, ExprTy *third,
501 SourceLocation RParenLoc, StmtTy *body) {
502 Stmt *First = static_cast<Stmt*>(first);
503 Expr *Second = static_cast<Expr*>(second);
504 Expr *Third = static_cast<Expr*>(third);
505 Stmt *Body = static_cast<Stmt*>(body);
506
Chris Lattnerae3b7012007-08-28 05:03:08 +0000507 if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
508 // C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
509 // identifiers for objects having storage class 'auto' or 'register'.
Steve Naroff94745042007-09-13 23:52:58 +0000510 for (ScopedDecl *D = DS->getDecl(); D; D = D->getNextDeclarator()) {
Chris Lattnerae3b7012007-08-28 05:03:08 +0000511 BlockVarDecl *BVD = dyn_cast<BlockVarDecl>(D);
512 if (BVD && !BVD->hasLocalStorage())
513 BVD = 0;
514 if (BVD == 0)
Steve Naroff8e74c932007-09-13 21:41:19 +0000515 Diag(dyn_cast<ScopedDecl>(D)->getLocation(),
516 diag::err_non_variable_decl_in_for);
Chris Lattnerae3b7012007-08-28 05:03:08 +0000517 // FIXME: mark decl erroneous!
518 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 }
520 if (Second) {
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000521 DefaultFunctionArrayConversion(Second);
522 QualType SecondType = Second->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000523
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000524 if (!SecondType->isScalarType()) // C99 6.8.5p2
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
Chris Lattner36c4b0e2007-08-28 04:55:47 +0000526 SecondType.getAsString(), Second->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 }
Steve Naroffb5a69582007-08-31 23:28:33 +0000528 return new ForStmt(First, Second, Third, Body, ForLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000529}
530
531
532Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000533Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 IdentifierInfo *LabelII) {
535 // Look up the record for this label identifier.
536 LabelStmt *&LabelDecl = LabelMap[LabelII];
537
538 // If we haven't seen this label yet, create a forward reference.
539 if (LabelDecl == 0)
540 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
541
Ted Kremenek61f62162007-09-06 17:11:52 +0000542 return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000543}
544
545Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000546Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 ExprTy *DestExp) {
548 // FIXME: Verify that the operand is convertible to void*.
549
550 return new IndirectGotoStmt((Expr*)DestExp);
551}
552
553Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000554Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 Scope *S = CurScope->getContinueParent();
556 if (!S) {
557 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
558 Diag(ContinueLoc, diag::err_continue_not_in_loop);
559 return true;
560 }
561
Steve Naroff507f2d52007-08-31 23:49:30 +0000562 return new ContinueStmt(ContinueLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000563}
564
565Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000566Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 Scope *S = CurScope->getBreakParent();
568 if (!S) {
569 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
570 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
571 return true;
572 }
573
Steve Naroff507f2d52007-08-31 23:49:30 +0000574 return new BreakStmt(BreakLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000575}
576
577
578Action::StmtResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000579Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
Steve Naroff90045e82007-07-13 23:32:42 +0000580 Expr *RetValExp = static_cast<Expr *>(rex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 QualType lhsType = CurFunctionDecl->getResultType();
582
583 if (lhsType->isVoidType()) {
584 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
585 Diag(ReturnLoc, diag::ext_return_has_expr,
586 CurFunctionDecl->getIdentifier()->getName(),
Steve Naroff90045e82007-07-13 23:32:42 +0000587 RetValExp->getSourceRange());
Steve Naroff507f2d52007-08-31 23:49:30 +0000588 return new ReturnStmt(ReturnLoc, RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 } else {
590 if (!RetValExp) {
591 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
592 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
593 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
594 else // C90 6.6.6.4p4
595 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
Steve Naroff507f2d52007-08-31 23:49:30 +0000596 return new ReturnStmt(ReturnLoc, (Expr*)0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 }
598 }
599 // we have a non-void function with an expression, continue checking
Steve Naroff90045e82007-07-13 23:32:42 +0000600 QualType rhsType = RetValExp->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000601
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
603 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
604 // function return.
Steve Naroff90045e82007-07-13 23:32:42 +0000605 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
606 RetValExp);
Ted Kremenek6a2394c2007-08-14 18:14:14 +0000607
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 // decode the result (notice that extensions still return a type).
609 switch (result) {
610 case Compatible:
611 break;
612 case Incompatible:
613 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
614 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff90045e82007-07-13 23:32:42 +0000615 RetValExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 break;
617 case PointerFromInt:
618 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner590b6642007-07-15 23:26:56 +0000619 if (!RetValExp->isNullPointerConstant(Context)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
621 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff90045e82007-07-13 23:32:42 +0000622 RetValExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 }
624 break;
625 case IntFromPointer:
626 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
627 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff90045e82007-07-13 23:32:42 +0000628 RetValExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 break;
630 case IncompatiblePointer:
631 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
632 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff90045e82007-07-13 23:32:42 +0000633 RetValExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 break;
635 case CompatiblePointerDiscardsQualifiers:
636 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
637 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff90045e82007-07-13 23:32:42 +0000638 RetValExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 break;
640 }
Ted Kremenek06de2762007-08-17 16:46:58 +0000641
642 if (RetValExp) CheckReturnStackAddr(RetValExp, lhsType, ReturnLoc);
643
Steve Naroff507f2d52007-08-31 23:49:30 +0000644 return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
Reid Spencer5f016e22007-07-11 17:01:13 +0000645}
646