blob: 33a11344e7aa292c0e8e28b914d37256e32e4a64 [file] [log] [blame]
Chris Lattneraf8d5812006-11-10 05:07:45 +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"
15#include "clang/AST/Stmt.h"
Steve Naroff8eeeb132007-05-08 21:09:37 +000016#include "clang/AST/Expr.h"
Chris Lattnereaafe1222006-11-10 05:17:58 +000017#include "clang/Parse/Scope.h"
18#include "clang/Basic/Diagnostic.h"
Steve Naroff6f49f5d2007-05-29 14:23:36 +000019#include "clang/Basic/LangOptions.h"
20#include "clang/Lex/IdentifierTable.h"
Anders Carlsson51873c22007-07-22 07:07:56 +000021
Chris Lattneraf8d5812006-11-10 05:07:45 +000022using namespace clang;
23
Chris Lattner1ec5f562007-06-27 05:38:08 +000024Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) {
25 Expr *E = static_cast<Expr*>(expr);
26
27 // Exprs are statements, so there is no need to do a conversion here. However,
28 // diagnose some potentially bad code.
29 if (!E->hasLocalSideEffect())
Chris Lattner9b3b9a12007-06-27 06:08:24 +000030 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
Chris Lattner1ec5f562007-06-27 05:38:08 +000031
32 return E;
33}
34
35
Chris Lattner0f203a72007-05-28 01:45:28 +000036Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
37 return new NullStmt(SemiLoc);
38}
39
Steve Naroff2a8ad182007-05-29 22:59:26 +000040Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) {
Steve Naroff56faab22007-05-30 04:20:12 +000041 if (decl)
42 return new DeclStmt(static_cast<Decl *>(decl));
43 else
44 return true; // error
Steve Naroff2a8ad182007-05-29 22:59:26 +000045}
Chris Lattneraf8d5812006-11-10 05:07:45 +000046
47Action::StmtResult
48Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
49 StmtTy **Elts, unsigned NumElts) {
Chris Lattner3bbe7be2007-05-20 22:47:04 +000050 return new CompoundStmt((Stmt**)Elts, NumElts);
Chris Lattneraf8d5812006-11-10 05:07:45 +000051}
52
53Action::StmtResult
Chris Lattner0c46c5d2007-06-02 23:53:17 +000054Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner46eeb222007-07-18 02:28:47 +000055 SourceLocation DotDotDotLoc, ExprTy *rhsval,
Chris Lattner39407372007-07-21 03:00:26 +000056 SourceLocation ColonLoc, StmtTy *subStmt) {
57 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner0c46c5d2007-06-02 23:53:17 +000058 Expr *LHSVal = ((Expr *)lhsval);
Steve Naroff8eeeb132007-05-08 21:09:37 +000059 assert((LHSVal != 0) && "missing expression in case statement");
Anders Carlsson51873c22007-07-22 07:07:56 +000060
Chris Lattner0c46c5d2007-06-02 23:53:17 +000061 SourceLocation ExpLoc;
Steve Naroff8eeeb132007-05-08 21:09:37 +000062 // C99 6.8.4.2p3: The expression shall be an integer constant.
Chris Lattner39407372007-07-21 03:00:26 +000063 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
64 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
65 LHSVal->getSourceRange());
66 return SubStmt;
67 }
Steve Naroff8eeeb132007-05-08 21:09:37 +000068
Chris Lattner46eeb222007-07-18 02:28:47 +000069 // GCC extension: The expression shall be an integer constant.
70 Expr *RHSVal = ((Expr *)rhsval);
Chris Lattner39407372007-07-21 03:00:26 +000071 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
72 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
73 RHSVal->getSourceRange());
74 return SubStmt;
Chris Lattner46eeb222007-07-18 02:28:47 +000075 }
Chris Lattner35e287b2007-06-03 01:44:43 +000076
Anders Carlsson51873c22007-07-22 07:07:56 +000077 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt);
78
79 assert(!SwitchStack.empty() && "missing push/pop in switch stack!");
80 SwitchStmt *SS = SwitchStack.back();
81 SS->addSwitchCase(CS);
82
83 return CS;
Chris Lattneraf8d5812006-11-10 05:07:45 +000084}
85
86Action::StmtResult
Chris Lattner46eeb222007-07-18 02:28:47 +000087Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner39407372007-07-21 03:00:26 +000088 StmtTy *subStmt, Scope *CurScope) {
89 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner46eeb222007-07-18 02:28:47 +000090 Scope *S = CurScope->getBreakParent();
91
Chris Lattner39407372007-07-21 03:00:26 +000092 if (!S) {
93 Diag(DefaultLoc, diag::err_default_not_in_switch);
94 return SubStmt;
95 }
96
Chris Lattner39407372007-07-21 03:00:26 +000097 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
Anders Carlsson51873c22007-07-22 07:07:56 +000098
99 assert(!SwitchStack.empty() && "missing push/pop in switch stack!");
100 SwitchStmt *SS = SwitchStack.back();
101 SS->addSwitchCase(DS);
102
Chris Lattner46eeb222007-07-18 02:28:47 +0000103 return DS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000104}
105
106Action::StmtResult
107Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner39407372007-07-21 03:00:26 +0000108 SourceLocation ColonLoc, StmtTy *subStmt) {
109 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000110 // Look up the record for this label identifier.
111 LabelStmt *&LabelDecl = LabelMap[II];
112
113 // If not forward referenced or defined already, just create a new LabelStmt.
114 if (LabelDecl == 0)
Chris Lattner39407372007-07-21 03:00:26 +0000115 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000116
Chris Lattnereefa10e2007-05-28 06:56:27 +0000117 assert(LabelDecl->getID() == II && "Label mismatch!");
Chris Lattnere2473062007-05-28 06:28:18 +0000118
119 // Otherwise, this label was either forward reference or multiply defined. If
120 // multiply defined, reject it now.
121 if (LabelDecl->getSubStmt()) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000122 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000123 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
Chris Lattner39407372007-07-21 03:00:26 +0000124 return SubStmt;
Chris Lattnere2473062007-05-28 06:28:18 +0000125 }
126
127 // Otherwise, this label was forward declared, and we just found its real
128 // definition. Fill in the forward definition and return it.
129 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner39407372007-07-21 03:00:26 +0000130 LabelDecl->setSubStmt(SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000131 return LabelDecl;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000132}
133
134Action::StmtResult
135Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
136 StmtTy *ThenVal, SourceLocation ElseLoc,
137 StmtTy *ElseVal) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000138 Expr *condExpr = (Expr *)CondVal;
139 assert(condExpr && "ParseIfStmt(): missing expression");
140
Steve Naroff31090012007-07-16 21:54:35 +0000141 DefaultFunctionArrayConversion(condExpr);
142 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000143
144 if (!condType->isScalarType()) // C99 6.8.4.1p1
145 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
146 condType.getAsString(), condExpr->getSourceRange());
147
148 return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000149}
Steve Naroff86272ea2007-05-29 02:14:17 +0000150
Chris Lattneraf8d5812006-11-10 05:07:45 +0000151Action::StmtResult
Anders Carlsson51873c22007-07-22 07:07:56 +0000152Sema::StartSwitchStmt(ExprTy *Cond) {
153 SwitchStmt *SS = new SwitchStmt((Expr*)Cond);
154 SwitchStack.push_back(SS);
155 return SS;
156}
Chris Lattner46eeb222007-07-18 02:28:47 +0000157
Anders Carlsson51873c22007-07-22 07:07:56 +0000158Action::StmtResult
159Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
160 Stmt *BodyStmt = (Stmt*)Body;
161
162 SwitchStmt *SS = SwitchStack.back();
163 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
164
165 SS->setBody(BodyStmt);
166 SwitchStack.pop_back();
167
168 Expr *condExpr = SS->getCond();
Chris Lattner46eeb222007-07-18 02:28:47 +0000169 QualType condType = condExpr->getType();
170
Anders Carlsson51873c22007-07-22 07:07:56 +0000171 if (!condType->isIntegerType()) { // C99 6.8.4.2p1
172 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
173 condType.getAsString(), condExpr->getSourceRange());
174 return false;
175 }
Chris Lattner46eeb222007-07-18 02:28:47 +0000176
Anders Carlsson51873c22007-07-22 07:07:56 +0000177 DefaultStmt *CurDefaultStmt = 0;
178
179 // FIXME: The list of cases is backwards and needs to be reversed.
180 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
181 SC = SC->getNextSwitchCase()) {
182 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
183 if (CurDefaultStmt) {
184 Diag(DS->getDefaultLoc(),
185 diag::err_multiple_default_labels_defined);
186 Diag(CurDefaultStmt->getDefaultLoc(),
187 diag::err_first_label);
188
189 // FIXME: Remove the default statement from the switch block
190 // so that we'll return a valid AST.
191 } else {
192 CurDefaultStmt = DS;
193 }
194
195 // FIXME: Check that case values are unique here.
196 }
197 }
198
199 return SS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000200}
201
202Action::StmtResult
Steve Naroff86272ea2007-05-29 02:14:17 +0000203Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
204 Expr *condExpr = (Expr *)Cond;
205 assert(condExpr && "ParseWhileStmt(): missing expression");
206
Steve Naroff31090012007-07-16 21:54:35 +0000207 DefaultFunctionArrayConversion(condExpr);
208 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000209
210 if (!condType->isScalarType()) // C99 6.8.5p2
211 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
212 condType.getAsString(), condExpr->getSourceRange());
213
214 return new WhileStmt(condExpr, (Stmt*)Body);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000215}
216
217Action::StmtResult
218Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
219 SourceLocation WhileLoc, ExprTy *Cond) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000220 Expr *condExpr = (Expr *)Cond;
221 assert(condExpr && "ParseDoStmt(): missing expression");
222
Steve Naroff31090012007-07-16 21:54:35 +0000223 DefaultFunctionArrayConversion(condExpr);
224 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000225
226 if (!condType->isScalarType()) // C99 6.8.5p2
227 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
228 condType.getAsString(), condExpr->getSourceRange());
229
230 return new DoStmt((Stmt*)Body, condExpr);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000231}
232
233Action::StmtResult
234Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
235 StmtTy *First, ExprTy *Second, ExprTy *Third,
236 SourceLocation RParenLoc, StmtTy *Body) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000237 if (First) {
238 // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and
239 // declaration support to create a DeclStmt node. Once this is done,
240 // we can test for DeclStmt vs. Expr (already a sub-class of Stmt).
241 }
242 if (Second) {
243 Expr *testExpr = (Expr *)Second;
Steve Naroff31090012007-07-16 21:54:35 +0000244 DefaultFunctionArrayConversion(testExpr);
245 QualType testType = testExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000246
247 if (!testType->isScalarType()) // C99 6.8.5p2
248 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
249 testType.getAsString(), testExpr->getSourceRange());
250 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000251 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
252}
253
254
255Action::StmtResult
256Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
257 IdentifierInfo *LabelII) {
Chris Lattnere2473062007-05-28 06:28:18 +0000258 // Look up the record for this label identifier.
259 LabelStmt *&LabelDecl = LabelMap[LabelII];
260
261 // If we haven't seen this label yet, create a forward reference.
262 if (LabelDecl == 0)
263 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
264
265 return new GotoStmt(LabelDecl);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000266}
Chris Lattner1c310502007-05-31 06:00:00 +0000267
Chris Lattneraf8d5812006-11-10 05:07:45 +0000268Action::StmtResult
269Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
270 ExprTy *DestExp) {
Chris Lattner1c310502007-05-31 06:00:00 +0000271 // FIXME: Verify that the operand is convertible to void*.
272
Chris Lattneraf8d5812006-11-10 05:07:45 +0000273 return new IndirectGotoStmt((Expr*)DestExp);
274}
275
276Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000277Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
278 Scope *S = CurScope->getContinueParent();
279 if (!S) {
280 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
281 Diag(ContinueLoc, diag::err_continue_not_in_loop);
282 return true;
283 }
284
Chris Lattneraf8d5812006-11-10 05:07:45 +0000285 return new ContinueStmt();
286}
287
288Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000289Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
290 Scope *S = CurScope->getBreakParent();
291 if (!S) {
292 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
293 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
294 return true;
295 }
296
Chris Lattneraf8d5812006-11-10 05:07:45 +0000297 return new BreakStmt();
298}
299
300
301Action::StmtResult
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000302Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
303 Expr *RetValExp = static_cast<Expr *>(rex);
Steve Naroff9358c712007-05-27 23:58:33 +0000304 QualType lhsType = CurFunctionDecl->getResultType();
305
Steve Naroff9358c712007-05-27 23:58:33 +0000306 if (lhsType->isVoidType()) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000307 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
308 Diag(ReturnLoc, diag::ext_return_has_expr,
309 CurFunctionDecl->getIdentifier()->getName(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000310 RetValExp->getSourceRange());
311 return new ReturnStmt(RetValExp);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000312 } else {
313 if (!RetValExp) {
314 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
315 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
316 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
317 else // C90 6.6.6.4p4
318 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
319 return new ReturnStmt((Expr*)0);
320 }
Steve Naroff9358c712007-05-27 23:58:33 +0000321 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000322 // we have a non-void function with an expression, continue checking
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000323 QualType rhsType = RetValExp->getType();
Steve Naroff9358c712007-05-27 23:58:33 +0000324
325 if (lhsType == rhsType) // common case, fast path...
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000326 return new ReturnStmt(RetValExp);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000327
328 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
329 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
330 // function return.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000331 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
332 RetValExp);
Steve Naroff9358c712007-05-27 23:58:33 +0000333 bool hadError = false;
334
335 // decode the result (notice that extensions still return a type).
336 switch (result) {
337 case Compatible:
338 break;
339 case Incompatible:
340 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
341 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000342 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000343 hadError = true;
344 break;
345 case PointerFromInt:
346 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000347 if (!RetValExp->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000348 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
349 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000350 RetValExp->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +0000351 }
Steve Naroff9358c712007-05-27 23:58:33 +0000352 break;
353 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000354 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
355 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000356 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000357 break;
358 case IncompatiblePointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000359 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
360 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000361 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000362 break;
363 case CompatiblePointerDiscardsQualifiers:
Steve Naroff56faab22007-05-30 04:20:12 +0000364 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
365 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000366 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000367 break;
368 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000369 return new ReturnStmt((Expr*)RetValExp);
370}
371