blob: f9e2c0a21a293490c221d5fb20ffe5b9127c3132 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +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"
16#include "clang/AST/Expr.h"
17#include "clang/Parse/Scope.h"
18#include "clang/Basic/Diagnostic.h"
19#include "clang/Basic/LangOptions.h"
20#include "clang/Lex/IdentifierTable.h"
21
22using namespace clang;
23
24Sema::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())
30 Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
31
32 return E;
33}
34
35
36Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
37 return new NullStmt(SemiLoc);
38}
39
40Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) {
41 if (decl)
42 return new DeclStmt(static_cast<Decl *>(decl));
43 else
44 return true; // error
45}
46
47Action::StmtResult
48Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
49 StmtTy **Elts, unsigned NumElts) {
50 return new CompoundStmt((Stmt**)Elts, NumElts);
51}
52
53Action::StmtResult
54Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
55 SourceLocation DotDotDotLoc, ExprTy *rhsval,
56 SourceLocation ColonLoc, StmtTy *subStmt) {
57 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
58 Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
59 assert((LHSVal != 0) && "missing expression in case statement");
60
61 SourceLocation ExpLoc;
62 // C99 6.8.4.2p3: The expression shall be an integer constant.
63 if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
64 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
65 LHSVal->getSourceRange());
66 return SubStmt;
67 }
68
69 // GCC extension: The expression shall be an integer constant.
70 if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
71 Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
72 RHSVal->getSourceRange());
73 return SubStmt;
74 }
75
76 if (SwitchStack.empty()) {
77 Diag(CaseLoc, diag::err_case_not_in_switch);
78 return SubStmt;
79 }
80
81 CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt);
82 SwitchStack.back()->addSwitchCase(CS);
83 return CS;
84}
85
86Action::StmtResult
87Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
88 StmtTy *subStmt, Scope *CurScope) {
89 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
90
91 if (SwitchStack.empty()) {
92 Diag(DefaultLoc, diag::err_default_not_in_switch);
93 return SubStmt;
94 }
95
96 DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
97 SwitchStack.back()->addSwitchCase(DS);
98
99 return DS;
100}
101
102Action::StmtResult
103Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
104 SourceLocation ColonLoc, StmtTy *subStmt) {
105 Stmt *SubStmt = static_cast<Stmt*>(subStmt);
106 // 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)
111 return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
112
113 assert(LabelDecl->getID() == II && "Label mismatch!");
114
115 // Otherwise, this label was either forward reference or multiply defined. If
116 // multiply defined, reject it now.
117 if (LabelDecl->getSubStmt()) {
118 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
119 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
120 return SubStmt;
121 }
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);
126 LabelDecl->setSubStmt(SubStmt);
127 return LabelDecl;
128}
129
130Action::StmtResult
131Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
132 StmtTy *ThenVal, SourceLocation ElseLoc,
133 StmtTy *ElseVal) {
134 Expr *condExpr = (Expr *)CondVal;
135 assert(condExpr && "ParseIfStmt(): missing expression");
136
137 DefaultFunctionArrayConversion(condExpr);
138 QualType condType = condExpr->getType();
139
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);
145}
146
147Action::StmtResult
148Sema::StartSwitchStmt(ExprTy *Cond) {
149 SwitchStmt *SS = new SwitchStmt((Expr*)Cond);
150 SwitchStack.push_back(SS);
151 return SS;
152}
153
154Action::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();
165 QualType condType = condExpr->getType();
166
167 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 }
172
173 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;
196}
197
198Action::StmtResult
199Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
200 Expr *condExpr = (Expr *)Cond;
201 assert(condExpr && "ParseWhileStmt(): missing expression");
202
203 DefaultFunctionArrayConversion(condExpr);
204 QualType condType = condExpr->getType();
205
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);
211}
212
213Action::StmtResult
214Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
215 SourceLocation WhileLoc, ExprTy *Cond) {
216 Expr *condExpr = (Expr *)Cond;
217 assert(condExpr && "ParseDoStmt(): missing expression");
218
219 DefaultFunctionArrayConversion(condExpr);
220 QualType condType = condExpr->getType();
221
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);
227}
228
229Action::StmtResult
230Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
231 StmtTy *First, ExprTy *Second, ExprTy *Third,
232 SourceLocation RParenLoc, StmtTy *Body) {
233 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;
240 DefaultFunctionArrayConversion(testExpr);
241 QualType testType = testExpr->getType();
242
243 if (!testType->isScalarType()) // C99 6.8.5p2
244 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
245 testType.getAsString(), testExpr->getSourceRange());
246 }
247 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) {
254 // 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);
262}
263
264Action::StmtResult
265Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
266 ExprTy *DestExp) {
267 // FIXME: Verify that the operand is convertible to void*.
268
269 return new IndirectGotoStmt((Expr*)DestExp);
270}
271
272Action::StmtResult
273Sema::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
281 return new ContinueStmt();
282}
283
284Action::StmtResult
285Sema::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
293 return new BreakStmt();
294}
295
296
297Action::StmtResult
298Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
299 Expr *RetValExp = static_cast<Expr *>(rex);
300 QualType lhsType = CurFunctionDecl->getResultType();
301
302 if (lhsType->isVoidType()) {
303 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
304 Diag(ReturnLoc, diag::ext_return_has_expr,
305 CurFunctionDecl->getIdentifier()->getName(),
306 RetValExp->getSourceRange());
307 return new ReturnStmt(RetValExp);
308 } 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 }
317 }
318 // we have a non-void function with an expression, continue checking
319 QualType rhsType = RetValExp->getType();
320
321 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
322 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
323 // function return.
324 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
325 RetValExp);
326 bool hadError = false;
327
328 // 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(),
335 RetValExp->getSourceRange());
336 hadError = true;
337 break;
338 case PointerFromInt:
339 // check for null pointer constant (C99 6.3.2.3p3)
340 if (!RetValExp->isNullPointerConstant(Context)) {
341 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
342 lhsType.getAsString(), rhsType.getAsString(),
343 RetValExp->getSourceRange());
344 }
345 break;
346 case IntFromPointer:
347 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
348 lhsType.getAsString(), rhsType.getAsString(),
349 RetValExp->getSourceRange());
350 break;
351 case IncompatiblePointer:
352 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
353 lhsType.getAsString(), rhsType.getAsString(),
354 RetValExp->getSourceRange());
355 break;
356 case CompatiblePointerDiscardsQualifiers:
357 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
358 lhsType.getAsString(), rhsType.getAsString(),
359 RetValExp->getSourceRange());
360 break;
361 }
362 return new ReturnStmt((Expr*)RetValExp);
363}
364