blob: b3a1f1c42cb2d6a70f360f54fce57563ace955a1 [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);
Steve Narofff8fd09e2007-07-27 22:15:19 +000026 assert(E && "ParseExprStmt(): missing expression");
Chris Lattner1ec5f562007-06-27 05:38:08 +000027
28 // Exprs are statements, so there is no need to do a conversion here. However,
29 // diagnose some potentially bad code.
30 if (!E->hasLocalSideEffect())
Chris Lattner9b3b9a12007-06-27 06:08:24 +000031 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
Chris Lattner1ec5f562007-06-27 05:38:08 +000032
33 return E;
34}
35
36
Chris Lattner0f203a72007-05-28 01:45:28 +000037Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
38 return new NullStmt(SemiLoc);
39}
40
Steve Naroff2a8ad182007-05-29 22:59:26 +000041Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) {
Steve Naroff56faab22007-05-30 04:20:12 +000042 if (decl)
43 return new DeclStmt(static_cast<Decl *>(decl));
44 else
45 return true; // error
Steve Naroff2a8ad182007-05-29 22:59:26 +000046}
Chris Lattneraf8d5812006-11-10 05:07:45 +000047
48Action::StmtResult
49Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
50 StmtTy **Elts, unsigned NumElts) {
Chris Lattner3bbe7be2007-05-20 22:47:04 +000051 return new CompoundStmt((Stmt**)Elts, NumElts);
Chris Lattneraf8d5812006-11-10 05:07:45 +000052}
53
54Action::StmtResult
Chris Lattner0c46c5d2007-06-02 23:53:17 +000055Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner46eeb222007-07-18 02:28:47 +000056 SourceLocation DotDotDotLoc, ExprTy *rhsval,
Chris Lattner39407372007-07-21 03:00:26 +000057 SourceLocation ColonLoc, StmtTy *subStmt) {
58 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +000059 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
Steve Naroff8eeeb132007-05-08 21:09:37 +000060 assert((LHSVal != 0) && "missing expression in case statement");
Anders Carlsson51873c22007-07-22 07:07:56 +000061
Chris Lattner0c46c5d2007-06-02 23:53:17 +000062 SourceLocation ExpLoc;
Steve Naroff8eeeb132007-05-08 21:09:37 +000063 // C99 6.8.4.2p3: The expression shall be an integer constant.
Chris Lattner39407372007-07-21 03:00:26 +000064 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
65 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
66 LHSVal->getSourceRange());
67 return SubStmt;
68 }
Steve Naroff8eeeb132007-05-08 21:09:37 +000069
Chris Lattner46eeb222007-07-18 02:28:47 +000070 // GCC extension: The expression shall be an integer constant.
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 Lattner54f4d2b2007-07-23 17:05:23 +000076
77 if (SwitchStack.empty()) {
78 Diag(CaseLoc, diag::err_case_not_in_switch);
79 return SubStmt;
80 }
Chris Lattner35e287b2007-06-03 01:44:43 +000081
Anders Carlsson51873c22007-07-22 07:07:56 +000082 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +000083 SwitchStack.back()->addSwitchCase(CS);
Anders Carlsson51873c22007-07-22 07:07:56 +000084 return CS;
Chris Lattneraf8d5812006-11-10 05:07:45 +000085}
86
87Action::StmtResult
Chris Lattner46eeb222007-07-18 02:28:47 +000088Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner39407372007-07-21 03:00:26 +000089 StmtTy *subStmt, Scope *CurScope) {
90 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner46eeb222007-07-18 02:28:47 +000091
Chris Lattner54f4d2b2007-07-23 17:05:23 +000092 if (SwitchStack.empty()) {
Chris Lattner39407372007-07-21 03:00:26 +000093 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);
Chris Lattner54f4d2b2007-07-23 17:05:23 +000098 SwitchStack.back()->addSwitchCase(DS);
Anders Carlsson51873c22007-07-22 07:07:56 +000099
Chris Lattner46eeb222007-07-18 02:28:47 +0000100 return DS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000101}
102
103Action::StmtResult
104Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner39407372007-07-21 03:00:26 +0000105 SourceLocation ColonLoc, StmtTy *subStmt) {
106 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000107 // Look up the record for this label identifier.
108 LabelStmt *&LabelDecl = LabelMap[II];
109
110 // If not forward referenced or defined already, just create a new LabelStmt.
111 if (LabelDecl == 0)
Chris Lattner39407372007-07-21 03:00:26 +0000112 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000113
Chris Lattnereefa10e2007-05-28 06:56:27 +0000114 assert(LabelDecl->getID() == II && "Label mismatch!");
Chris Lattnere2473062007-05-28 06:28:18 +0000115
116 // Otherwise, this label was either forward reference or multiply defined. If
117 // multiply defined, reject it now.
118 if (LabelDecl->getSubStmt()) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000119 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000120 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
Chris Lattner39407372007-07-21 03:00:26 +0000121 return SubStmt;
Chris Lattnere2473062007-05-28 06:28:18 +0000122 }
123
124 // Otherwise, this label was forward declared, and we just found its real
125 // definition. Fill in the forward definition and return it.
126 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner39407372007-07-21 03:00:26 +0000127 LabelDecl->setSubStmt(SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000128 return LabelDecl;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000129}
130
131Action::StmtResult
132Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
133 StmtTy *ThenVal, SourceLocation ElseLoc,
134 StmtTy *ElseVal) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000135 Expr *condExpr = (Expr *)CondVal;
136 assert(condExpr && "ParseIfStmt(): missing expression");
137
Steve Naroff31090012007-07-16 21:54:35 +0000138 DefaultFunctionArrayConversion(condExpr);
139 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000140
141 if (!condType->isScalarType()) // C99 6.8.4.1p1
142 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
143 condType.getAsString(), condExpr->getSourceRange());
144
145 return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000146}
Steve Naroff86272ea2007-05-29 02:14:17 +0000147
Chris Lattneraf8d5812006-11-10 05:07:45 +0000148Action::StmtResult
Anders Carlsson51873c22007-07-22 07:07:56 +0000149Sema::StartSwitchStmt(ExprTy *Cond) {
150 SwitchStmt *SS = new SwitchStmt((Expr*)Cond);
151 SwitchStack.push_back(SS);
152 return SS;
153}
Chris Lattner46eeb222007-07-18 02:28:47 +0000154
Anders Carlsson51873c22007-07-22 07:07:56 +0000155Action::StmtResult
156Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
157 Stmt *BodyStmt = (Stmt*)Body;
158
159 SwitchStmt *SS = SwitchStack.back();
160 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
161
162 SS->setBody(BodyStmt);
163 SwitchStack.pop_back();
164
165 Expr *condExpr = SS->getCond();
Chris Lattner46eeb222007-07-18 02:28:47 +0000166 QualType condType = condExpr->getType();
167
Anders Carlsson51873c22007-07-22 07:07:56 +0000168 if (!condType->isIntegerType()) { // C99 6.8.4.2p1
169 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
170 condType.getAsString(), condExpr->getSourceRange());
171 return false;
172 }
Chris Lattner46eeb222007-07-18 02:28:47 +0000173
Anders Carlsson51873c22007-07-22 07:07:56 +0000174 DefaultStmt *CurDefaultStmt = 0;
175
176 // FIXME: The list of cases is backwards and needs to be reversed.
177 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
178 SC = SC->getNextSwitchCase()) {
179 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
180 if (CurDefaultStmt) {
181 Diag(DS->getDefaultLoc(),
182 diag::err_multiple_default_labels_defined);
183 Diag(CurDefaultStmt->getDefaultLoc(),
184 diag::err_first_label);
185
186 // FIXME: Remove the default statement from the switch block
187 // so that we'll return a valid AST.
188 } else {
189 CurDefaultStmt = DS;
190 }
191
192 // FIXME: Check that case values are unique here.
193 }
194 }
195
196 return SS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000197}
198
199Action::StmtResult
Steve Naroff86272ea2007-05-29 02:14:17 +0000200Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
201 Expr *condExpr = (Expr *)Cond;
202 assert(condExpr && "ParseWhileStmt(): missing expression");
203
Steve Naroff31090012007-07-16 21:54:35 +0000204 DefaultFunctionArrayConversion(condExpr);
205 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000206
207 if (!condType->isScalarType()) // C99 6.8.5p2
208 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
209 condType.getAsString(), condExpr->getSourceRange());
210
211 return new WhileStmt(condExpr, (Stmt*)Body);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000212}
213
214Action::StmtResult
215Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
216 SourceLocation WhileLoc, ExprTy *Cond) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000217 Expr *condExpr = (Expr *)Cond;
218 assert(condExpr && "ParseDoStmt(): missing expression");
219
Steve Naroff31090012007-07-16 21:54:35 +0000220 DefaultFunctionArrayConversion(condExpr);
221 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000222
223 if (!condType->isScalarType()) // C99 6.8.5p2
224 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
225 condType.getAsString(), condExpr->getSourceRange());
226
227 return new DoStmt((Stmt*)Body, condExpr);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000228}
229
230Action::StmtResult
231Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
232 StmtTy *First, ExprTy *Second, ExprTy *Third,
233 SourceLocation RParenLoc, StmtTy *Body) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000234 if (First) {
235 // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and
236 // declaration support to create a DeclStmt node. Once this is done,
237 // we can test for DeclStmt vs. Expr (already a sub-class of Stmt).
238 }
239 if (Second) {
240 Expr *testExpr = (Expr *)Second;
Steve Naroff31090012007-07-16 21:54:35 +0000241 DefaultFunctionArrayConversion(testExpr);
242 QualType testType = testExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000243
244 if (!testType->isScalarType()) // C99 6.8.5p2
245 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
246 testType.getAsString(), testExpr->getSourceRange());
247 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000248 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
249}
250
251
252Action::StmtResult
253Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
254 IdentifierInfo *LabelII) {
Chris Lattnere2473062007-05-28 06:28:18 +0000255 // Look up the record for this label identifier.
256 LabelStmt *&LabelDecl = LabelMap[LabelII];
257
258 // If we haven't seen this label yet, create a forward reference.
259 if (LabelDecl == 0)
260 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
261
262 return new GotoStmt(LabelDecl);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000263}
Chris Lattner1c310502007-05-31 06:00:00 +0000264
Chris Lattneraf8d5812006-11-10 05:07:45 +0000265Action::StmtResult
266Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
267 ExprTy *DestExp) {
Chris Lattner1c310502007-05-31 06:00:00 +0000268 // FIXME: Verify that the operand is convertible to void*.
269
Chris Lattneraf8d5812006-11-10 05:07:45 +0000270 return new IndirectGotoStmt((Expr*)DestExp);
271}
272
273Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000274Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
275 Scope *S = CurScope->getContinueParent();
276 if (!S) {
277 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
278 Diag(ContinueLoc, diag::err_continue_not_in_loop);
279 return true;
280 }
281
Chris Lattneraf8d5812006-11-10 05:07:45 +0000282 return new ContinueStmt();
283}
284
285Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000286Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
287 Scope *S = CurScope->getBreakParent();
288 if (!S) {
289 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
290 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
291 return true;
292 }
293
Chris Lattneraf8d5812006-11-10 05:07:45 +0000294 return new BreakStmt();
295}
296
297
298Action::StmtResult
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000299Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
300 Expr *RetValExp = static_cast<Expr *>(rex);
Steve Naroff9358c712007-05-27 23:58:33 +0000301 QualType lhsType = CurFunctionDecl->getResultType();
302
Steve Naroff9358c712007-05-27 23:58:33 +0000303 if (lhsType->isVoidType()) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000304 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
305 Diag(ReturnLoc, diag::ext_return_has_expr,
306 CurFunctionDecl->getIdentifier()->getName(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000307 RetValExp->getSourceRange());
308 return new ReturnStmt(RetValExp);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000309 } else {
310 if (!RetValExp) {
311 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
312 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
313 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
314 else // C90 6.6.6.4p4
315 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
316 return new ReturnStmt((Expr*)0);
317 }
Steve Naroff9358c712007-05-27 23:58:33 +0000318 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000319 // we have a non-void function with an expression, continue checking
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000320 QualType rhsType = RetValExp->getType();
Steve Naroff9358c712007-05-27 23:58:33 +0000321
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000322 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
323 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
324 // function return.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000325 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
326 RetValExp);
Ted Kremenekc48affb2007-08-14 18:14:14 +0000327
Steve Naroff9358c712007-05-27 23:58:33 +0000328 // decode the result (notice that extensions still return a type).
329 switch (result) {
330 case Compatible:
331 break;
332 case Incompatible:
333 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
334 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000335 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000336 break;
337 case PointerFromInt:
338 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000339 if (!RetValExp->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000340 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
341 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000342 RetValExp->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +0000343 }
Steve Naroff9358c712007-05-27 23:58:33 +0000344 break;
345 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000346 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
347 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000348 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000349 break;
350 case IncompatiblePointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000351 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
352 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000353 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000354 break;
355 case CompatiblePointerDiscardsQualifiers:
Steve Naroff56faab22007-05-30 04:20:12 +0000356 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
357 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000358 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000359 break;
360 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000361 return new ReturnStmt((Expr*)RetValExp);
362}
363