blob: 17e042c9f1036ccc6709bad81ea694cdfa972047 [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"
Chris Lattneraf8d5812006-11-10 05:07:45 +000021using namespace clang;
22
Chris Lattner1ec5f562007-06-27 05:38:08 +000023Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) {
24 Expr *E = static_cast<Expr*>(expr);
25
26 // Exprs are statements, so there is no need to do a conversion here. However,
27 // diagnose some potentially bad code.
28 if (!E->hasLocalSideEffect())
Chris Lattner9b3b9a12007-06-27 06:08:24 +000029 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
Chris Lattner1ec5f562007-06-27 05:38:08 +000030
31 return E;
32}
33
34
Chris Lattner0f203a72007-05-28 01:45:28 +000035Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
36 return new NullStmt(SemiLoc);
37}
38
Steve Naroff2a8ad182007-05-29 22:59:26 +000039Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) {
Steve Naroff56faab22007-05-30 04:20:12 +000040 if (decl)
41 return new DeclStmt(static_cast<Decl *>(decl));
42 else
43 return true; // error
Steve Naroff2a8ad182007-05-29 22:59:26 +000044}
Chris Lattneraf8d5812006-11-10 05:07:45 +000045
46Action::StmtResult
47Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
48 StmtTy **Elts, unsigned NumElts) {
Chris Lattner3bbe7be2007-05-20 22:47:04 +000049 return new CompoundStmt((Stmt**)Elts, NumElts);
Chris Lattneraf8d5812006-11-10 05:07:45 +000050}
51
52Action::StmtResult
Chris Lattner0c46c5d2007-06-02 23:53:17 +000053Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Chris Lattner46eeb222007-07-18 02:28:47 +000054 SourceLocation DotDotDotLoc, ExprTy *rhsval,
Chris Lattner39407372007-07-21 03:00:26 +000055 SourceLocation ColonLoc, StmtTy *subStmt) {
56 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner0c46c5d2007-06-02 23:53:17 +000057 Expr *LHSVal = ((Expr *)lhsval);
Steve Naroff8eeeb132007-05-08 21:09:37 +000058 assert((LHSVal != 0) && "missing expression in case statement");
59
Chris Lattner0c46c5d2007-06-02 23:53:17 +000060 SourceLocation ExpLoc;
Steve Naroff8eeeb132007-05-08 21:09:37 +000061 // C99 6.8.4.2p3: The expression shall be an integer constant.
Chris Lattner39407372007-07-21 03:00:26 +000062 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
63 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
64 LHSVal->getSourceRange());
65 return SubStmt;
66 }
Steve Naroff8eeeb132007-05-08 21:09:37 +000067
Chris Lattner46eeb222007-07-18 02:28:47 +000068 // GCC extension: The expression shall be an integer constant.
69 Expr *RHSVal = ((Expr *)rhsval);
Chris Lattner39407372007-07-21 03:00:26 +000070 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
71 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
72 RHSVal->getSourceRange());
73 return SubStmt;
Chris Lattner46eeb222007-07-18 02:28:47 +000074 }
Chris Lattner35e287b2007-06-03 01:44:43 +000075
Chris Lattner39407372007-07-21 03:00:26 +000076 return new CaseStmt(LHSVal, (Expr*)RHSVal, SubStmt);
Chris Lattneraf8d5812006-11-10 05:07:45 +000077}
78
79Action::StmtResult
Chris Lattner46eeb222007-07-18 02:28:47 +000080Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
Chris Lattner39407372007-07-21 03:00:26 +000081 StmtTy *subStmt, Scope *CurScope) {
82 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattner46eeb222007-07-18 02:28:47 +000083 Scope *S = CurScope->getBreakParent();
84
Chris Lattner39407372007-07-21 03:00:26 +000085 if (!S) {
86 Diag(DefaultLoc, diag::err_default_not_in_switch);
87 return SubStmt;
88 }
89
Chris Lattner46eeb222007-07-18 02:28:47 +000090 if (S->getDefaultStmt()) {
91 Diag(DefaultLoc, diag::err_multiple_default_labels_defined);
92 Diag(((DefaultStmt *)S->getDefaultStmt())->getDefaultLoc(),
93 diag::err_first_label);
Chris Lattner39407372007-07-21 03:00:26 +000094 return SubStmt;
Chris Lattner46eeb222007-07-18 02:28:47 +000095 }
96
Chris Lattner39407372007-07-21 03:00:26 +000097 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner46eeb222007-07-18 02:28:47 +000098 S->setDefaultStmt(DS);
Chris Lattner46eeb222007-07-18 02:28:47 +000099 return DS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000100}
101
102Action::StmtResult
103Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner39407372007-07-21 03:00:26 +0000104 SourceLocation ColonLoc, StmtTy *subStmt) {
105 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000106 // Look up the record for this label identifier.
107 LabelStmt *&LabelDecl = LabelMap[II];
108
109 // If not forward referenced or defined already, just create a new LabelStmt.
110 if (LabelDecl == 0)
Chris Lattner39407372007-07-21 03:00:26 +0000111 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000112
Chris Lattnereefa10e2007-05-28 06:56:27 +0000113 assert(LabelDecl->getID() == II && "Label mismatch!");
Chris Lattnere2473062007-05-28 06:28:18 +0000114
115 // Otherwise, this label was either forward reference or multiply defined. If
116 // multiply defined, reject it now.
117 if (LabelDecl->getSubStmt()) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000118 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
Chris Lattnere2473062007-05-28 06:28:18 +0000119 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
Chris Lattner39407372007-07-21 03:00:26 +0000120 return SubStmt;
Chris Lattnere2473062007-05-28 06:28:18 +0000121 }
122
123 // Otherwise, this label was forward declared, and we just found its real
124 // definition. Fill in the forward definition and return it.
125 LabelDecl->setIdentLoc(IdentLoc);
Chris Lattner39407372007-07-21 03:00:26 +0000126 LabelDecl->setSubStmt(SubStmt);
Chris Lattnere2473062007-05-28 06:28:18 +0000127 return LabelDecl;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000128}
129
130Action::StmtResult
131Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
132 StmtTy *ThenVal, SourceLocation ElseLoc,
133 StmtTy *ElseVal) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000134 Expr *condExpr = (Expr *)CondVal;
135 assert(condExpr && "ParseIfStmt(): missing expression");
136
Steve Naroff31090012007-07-16 21:54:35 +0000137 DefaultFunctionArrayConversion(condExpr);
138 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000139
140 if (!condType->isScalarType()) // C99 6.8.4.1p1
141 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
142 condType.getAsString(), condExpr->getSourceRange());
143
144 return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000145}
Steve Naroff86272ea2007-05-29 02:14:17 +0000146
Chris Lattneraf8d5812006-11-10 05:07:45 +0000147Action::StmtResult
148Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) {
Chris Lattner46eeb222007-07-18 02:28:47 +0000149 Expr *condExpr = (Expr *)Cond;
150
151 QualType condType = condExpr->getType();
152
153 if (!condType->isIntegerType()) // C99 6.8.4.2p1
154 return Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
155 condType.getAsString(), condExpr->getSourceRange());
156
Chris Lattneraf8d5812006-11-10 05:07:45 +0000157 return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
158}
159
160Action::StmtResult
Steve Naroff86272ea2007-05-29 02:14:17 +0000161Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
162 Expr *condExpr = (Expr *)Cond;
163 assert(condExpr && "ParseWhileStmt(): missing expression");
164
Steve Naroff31090012007-07-16 21:54:35 +0000165 DefaultFunctionArrayConversion(condExpr);
166 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000167
168 if (!condType->isScalarType()) // C99 6.8.5p2
169 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
170 condType.getAsString(), condExpr->getSourceRange());
171
172 return new WhileStmt(condExpr, (Stmt*)Body);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000173}
174
175Action::StmtResult
176Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
177 SourceLocation WhileLoc, ExprTy *Cond) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000178 Expr *condExpr = (Expr *)Cond;
179 assert(condExpr && "ParseDoStmt(): missing expression");
180
Steve Naroff31090012007-07-16 21:54:35 +0000181 DefaultFunctionArrayConversion(condExpr);
182 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000183
184 if (!condType->isScalarType()) // C99 6.8.5p2
185 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
186 condType.getAsString(), condExpr->getSourceRange());
187
188 return new DoStmt((Stmt*)Body, condExpr);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000189}
190
191Action::StmtResult
192Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
193 StmtTy *First, ExprTy *Second, ExprTy *Third,
194 SourceLocation RParenLoc, StmtTy *Body) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000195 if (First) {
196 // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and
197 // declaration support to create a DeclStmt node. Once this is done,
198 // we can test for DeclStmt vs. Expr (already a sub-class of Stmt).
199 }
200 if (Second) {
201 Expr *testExpr = (Expr *)Second;
Steve Naroff31090012007-07-16 21:54:35 +0000202 DefaultFunctionArrayConversion(testExpr);
203 QualType testType = testExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000204
205 if (!testType->isScalarType()) // C99 6.8.5p2
206 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
207 testType.getAsString(), testExpr->getSourceRange());
208 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000209 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
210}
211
212
213Action::StmtResult
214Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
215 IdentifierInfo *LabelII) {
Chris Lattnere2473062007-05-28 06:28:18 +0000216 // Look up the record for this label identifier.
217 LabelStmt *&LabelDecl = LabelMap[LabelII];
218
219 // If we haven't seen this label yet, create a forward reference.
220 if (LabelDecl == 0)
221 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
222
223 return new GotoStmt(LabelDecl);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000224}
Chris Lattner1c310502007-05-31 06:00:00 +0000225
Chris Lattneraf8d5812006-11-10 05:07:45 +0000226Action::StmtResult
227Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
228 ExprTy *DestExp) {
Chris Lattner1c310502007-05-31 06:00:00 +0000229 // FIXME: Verify that the operand is convertible to void*.
230
Chris Lattneraf8d5812006-11-10 05:07:45 +0000231 return new IndirectGotoStmt((Expr*)DestExp);
232}
233
234Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000235Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
236 Scope *S = CurScope->getContinueParent();
237 if (!S) {
238 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
239 Diag(ContinueLoc, diag::err_continue_not_in_loop);
240 return true;
241 }
242
Chris Lattneraf8d5812006-11-10 05:07:45 +0000243 return new ContinueStmt();
244}
245
246Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000247Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
248 Scope *S = CurScope->getBreakParent();
249 if (!S) {
250 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
251 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
252 return true;
253 }
254
Chris Lattneraf8d5812006-11-10 05:07:45 +0000255 return new BreakStmt();
256}
257
258
259Action::StmtResult
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000260Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
261 Expr *RetValExp = static_cast<Expr *>(rex);
Steve Naroff9358c712007-05-27 23:58:33 +0000262 QualType lhsType = CurFunctionDecl->getResultType();
263
Steve Naroff9358c712007-05-27 23:58:33 +0000264 if (lhsType->isVoidType()) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000265 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
266 Diag(ReturnLoc, diag::ext_return_has_expr,
267 CurFunctionDecl->getIdentifier()->getName(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000268 RetValExp->getSourceRange());
269 return new ReturnStmt(RetValExp);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000270 } else {
271 if (!RetValExp) {
272 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
273 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
274 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
275 else // C90 6.6.6.4p4
276 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
277 return new ReturnStmt((Expr*)0);
278 }
Steve Naroff9358c712007-05-27 23:58:33 +0000279 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000280 // we have a non-void function with an expression, continue checking
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000281 QualType rhsType = RetValExp->getType();
Steve Naroff9358c712007-05-27 23:58:33 +0000282
283 if (lhsType == rhsType) // common case, fast path...
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000284 return new ReturnStmt(RetValExp);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000285
286 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
287 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
288 // function return.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000289 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
290 RetValExp);
Steve Naroff9358c712007-05-27 23:58:33 +0000291 bool hadError = false;
292
293 // decode the result (notice that extensions still return a type).
294 switch (result) {
295 case Compatible:
296 break;
297 case Incompatible:
298 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
299 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000300 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000301 hadError = true;
302 break;
303 case PointerFromInt:
304 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000305 if (!RetValExp->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000306 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
307 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000308 RetValExp->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +0000309 }
Steve Naroff9358c712007-05-27 23:58:33 +0000310 break;
311 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000312 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
313 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000314 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000315 break;
316 case IncompatiblePointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000317 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
318 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000319 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000320 break;
321 case CompatiblePointerDiscardsQualifiers:
Steve Naroff56faab22007-05-30 04:20:12 +0000322 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
323 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000324 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000325 break;
326 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000327 return new ReturnStmt((Expr*)RetValExp);
328}
329