blob: f19737e30a967211c9bfd37e0fd23f6b49e5c115 [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 Lattner54f4d2b2007-07-23 17:05:23 +000058 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
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.
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 Lattner54f4d2b2007-07-23 17:05:23 +000075
76 if (SwitchStack.empty()) {
77 Diag(CaseLoc, diag::err_case_not_in_switch);
78 return SubStmt;
79 }
Chris Lattner35e287b2007-06-03 01:44:43 +000080
Anders Carlsson51873c22007-07-22 07:07:56 +000081 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +000082 SwitchStack.back()->addSwitchCase(CS);
Anders Carlsson51873c22007-07-22 07:07:56 +000083 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
Chris Lattner54f4d2b2007-07-23 17:05:23 +000091 if (SwitchStack.empty()) {
Chris Lattner39407372007-07-21 03:00:26 +000092 Diag(DefaultLoc, diag::err_default_not_in_switch);
93 return SubStmt;
94 }
95
Chris Lattner39407372007-07-21 03:00:26 +000096 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
Chris Lattner54f4d2b2007-07-23 17:05:23 +000097 SwitchStack.back()->addSwitchCase(DS);
Anders Carlsson51873c22007-07-22 07:07:56 +000098
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
Anders Carlsson51873c22007-07-22 07:07:56 +0000148Sema::StartSwitchStmt(ExprTy *Cond) {
149 SwitchStmt *SS = new SwitchStmt((Expr*)Cond);
150 SwitchStack.push_back(SS);
151 return SS;
152}
Chris Lattner46eeb222007-07-18 02:28:47 +0000153
Anders Carlsson51873c22007-07-22 07:07:56 +0000154Action::StmtResult
155Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) {
156 Stmt *BodyStmt = (Stmt*)Body;
157
158 SwitchStmt *SS = SwitchStack.back();
159 assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
160
161 SS->setBody(BodyStmt);
162 SwitchStack.pop_back();
163
164 Expr *condExpr = SS->getCond();
Chris Lattner46eeb222007-07-18 02:28:47 +0000165 QualType condType = condExpr->getType();
166
Anders Carlsson51873c22007-07-22 07:07:56 +0000167 if (!condType->isIntegerType()) { // C99 6.8.4.2p1
168 Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
169 condType.getAsString(), condExpr->getSourceRange());
170 return false;
171 }
Chris Lattner46eeb222007-07-18 02:28:47 +0000172
Anders Carlsson51873c22007-07-22 07:07:56 +0000173 DefaultStmt *CurDefaultStmt = 0;
174
175 // FIXME: The list of cases is backwards and needs to be reversed.
176 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
177 SC = SC->getNextSwitchCase()) {
178 if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
179 if (CurDefaultStmt) {
180 Diag(DS->getDefaultLoc(),
181 diag::err_multiple_default_labels_defined);
182 Diag(CurDefaultStmt->getDefaultLoc(),
183 diag::err_first_label);
184
185 // FIXME: Remove the default statement from the switch block
186 // so that we'll return a valid AST.
187 } else {
188 CurDefaultStmt = DS;
189 }
190
191 // FIXME: Check that case values are unique here.
192 }
193 }
194
195 return SS;
Chris Lattneraf8d5812006-11-10 05:07:45 +0000196}
197
198Action::StmtResult
Steve Naroff86272ea2007-05-29 02:14:17 +0000199Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
200 Expr *condExpr = (Expr *)Cond;
201 assert(condExpr && "ParseWhileStmt(): missing expression");
202
Steve Naroff31090012007-07-16 21:54:35 +0000203 DefaultFunctionArrayConversion(condExpr);
204 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000205
206 if (!condType->isScalarType()) // C99 6.8.5p2
207 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
208 condType.getAsString(), condExpr->getSourceRange());
209
210 return new WhileStmt(condExpr, (Stmt*)Body);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000211}
212
213Action::StmtResult
214Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
215 SourceLocation WhileLoc, ExprTy *Cond) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000216 Expr *condExpr = (Expr *)Cond;
217 assert(condExpr && "ParseDoStmt(): missing expression");
218
Steve Naroff31090012007-07-16 21:54:35 +0000219 DefaultFunctionArrayConversion(condExpr);
220 QualType condType = condExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000221
222 if (!condType->isScalarType()) // C99 6.8.5p2
223 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
224 condType.getAsString(), condExpr->getSourceRange());
225
226 return new DoStmt((Stmt*)Body, condExpr);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000227}
228
229Action::StmtResult
230Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
231 StmtTy *First, ExprTy *Second, ExprTy *Third,
232 SourceLocation RParenLoc, StmtTy *Body) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000233 if (First) {
234 // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and
235 // declaration support to create a DeclStmt node. Once this is done,
236 // we can test for DeclStmt vs. Expr (already a sub-class of Stmt).
237 }
238 if (Second) {
239 Expr *testExpr = (Expr *)Second;
Steve Naroff31090012007-07-16 21:54:35 +0000240 DefaultFunctionArrayConversion(testExpr);
241 QualType testType = testExpr->getType();
Steve Naroff86272ea2007-05-29 02:14:17 +0000242
243 if (!testType->isScalarType()) // C99 6.8.5p2
244 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
245 testType.getAsString(), testExpr->getSourceRange());
246 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000247 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
248}
249
250
251Action::StmtResult
252Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
253 IdentifierInfo *LabelII) {
Chris Lattnere2473062007-05-28 06:28:18 +0000254 // Look up the record for this label identifier.
255 LabelStmt *&LabelDecl = LabelMap[LabelII];
256
257 // If we haven't seen this label yet, create a forward reference.
258 if (LabelDecl == 0)
259 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
260
261 return new GotoStmt(LabelDecl);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000262}
Chris Lattner1c310502007-05-31 06:00:00 +0000263
Chris Lattneraf8d5812006-11-10 05:07:45 +0000264Action::StmtResult
265Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
266 ExprTy *DestExp) {
Chris Lattner1c310502007-05-31 06:00:00 +0000267 // FIXME: Verify that the operand is convertible to void*.
268
Chris Lattneraf8d5812006-11-10 05:07:45 +0000269 return new IndirectGotoStmt((Expr*)DestExp);
270}
271
272Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000273Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
274 Scope *S = CurScope->getContinueParent();
275 if (!S) {
276 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
277 Diag(ContinueLoc, diag::err_continue_not_in_loop);
278 return true;
279 }
280
Chris Lattneraf8d5812006-11-10 05:07:45 +0000281 return new ContinueStmt();
282}
283
284Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000285Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
286 Scope *S = CurScope->getBreakParent();
287 if (!S) {
288 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
289 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
290 return true;
291 }
292
Chris Lattneraf8d5812006-11-10 05:07:45 +0000293 return new BreakStmt();
294}
295
296
297Action::StmtResult
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000298Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
299 Expr *RetValExp = static_cast<Expr *>(rex);
Steve Naroff9358c712007-05-27 23:58:33 +0000300 QualType lhsType = CurFunctionDecl->getResultType();
301
Steve Naroff9358c712007-05-27 23:58:33 +0000302 if (lhsType->isVoidType()) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000303 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
304 Diag(ReturnLoc, diag::ext_return_has_expr,
305 CurFunctionDecl->getIdentifier()->getName(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000306 RetValExp->getSourceRange());
307 return new ReturnStmt(RetValExp);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000308 } else {
309 if (!RetValExp) {
310 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
311 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
312 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
313 else // C90 6.6.6.4p4
314 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
315 return new ReturnStmt((Expr*)0);
316 }
Steve Naroff9358c712007-05-27 23:58:33 +0000317 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000318 // we have a non-void function with an expression, continue checking
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000319 QualType rhsType = RetValExp->getType();
Steve Naroff9358c712007-05-27 23:58:33 +0000320
321 if (lhsType == rhsType) // common case, fast path...
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000322 return new ReturnStmt(RetValExp);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000323
324 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
325 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
326 // function return.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000327 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
328 RetValExp);
Steve Naroff9358c712007-05-27 23:58:33 +0000329 bool hadError = false;
330
331 // decode the result (notice that extensions still return a type).
332 switch (result) {
333 case Compatible:
334 break;
335 case Incompatible:
336 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
337 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000338 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000339 hadError = true;
340 break;
341 case PointerFromInt:
342 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000343 if (!RetValExp->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000344 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
345 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000346 RetValExp->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +0000347 }
Steve Naroff9358c712007-05-27 23:58:33 +0000348 break;
349 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000350 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
351 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000352 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000353 break;
354 case IncompatiblePointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000355 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
356 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000357 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000358 break;
359 case CompatiblePointerDiscardsQualifiers:
Steve Naroff56faab22007-05-30 04:20:12 +0000360 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
361 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000362 RetValExp->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000363 break;
364 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000365 return new ReturnStmt((Expr*)RetValExp);
366}
367