blob: 314bf18a8430a15743f976ed295f0dfc0d0433ac [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 llvm;
22using namespace clang;
23
Chris Lattner0f203a72007-05-28 01:45:28 +000024Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
25 return new NullStmt(SemiLoc);
26}
27
Steve Naroff2a8ad182007-05-29 22:59:26 +000028Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) {
Steve Naroff56faab22007-05-30 04:20:12 +000029 if (decl)
30 return new DeclStmt(static_cast<Decl *>(decl));
31 else
32 return true; // error
Steve Naroff2a8ad182007-05-29 22:59:26 +000033}
Chris Lattneraf8d5812006-11-10 05:07:45 +000034
35Action::StmtResult
36Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
37 StmtTy **Elts, unsigned NumElts) {
Chris Lattner3bbe7be2007-05-20 22:47:04 +000038 return new CompoundStmt((Stmt**)Elts, NumElts);
Chris Lattneraf8d5812006-11-10 05:07:45 +000039}
40
41Action::StmtResult
42Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
43 SourceLocation DotDotDotLoc, ExprTy *RHSVal,
44 SourceLocation ColonLoc, StmtTy *SubStmt) {
Steve Naroff8eeeb132007-05-08 21:09:37 +000045 assert((LHSVal != 0) && "missing expression in case statement");
46
Steve Naroff72cada02007-05-18 00:18:52 +000047 SourceLocation expLoc;
Steve Naroff8eeeb132007-05-08 21:09:37 +000048 // C99 6.8.4.2p3: The expression shall be an integer constant.
Steve Naroff72cada02007-05-18 00:18:52 +000049 if (!((Expr *)LHSVal)->isIntegerConstantExpr(expLoc))
Steve Naroff8eeeb132007-05-08 21:09:37 +000050 return Diag(CaseLoc, diag::err_case_label_not_integer_constant_expr);
51
Chris Lattneraf8d5812006-11-10 05:07:45 +000052 return new CaseStmt((Expr*)LHSVal, (Expr*)RHSVal, (Stmt*)SubStmt);
53}
54
55Action::StmtResult
56Sema::ParseDefaultStmt(SourceLocation DefaultLoc,
57 SourceLocation ColonLoc, StmtTy *SubStmt) {
58 return new DefaultStmt((Stmt*)SubStmt);
59}
60
61Action::StmtResult
62Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
63 SourceLocation ColonLoc, StmtTy *SubStmt) {
Chris Lattnere2473062007-05-28 06:28:18 +000064 // Look up the record for this label identifier.
65 LabelStmt *&LabelDecl = LabelMap[II];
66
67 // If not forward referenced or defined already, just create a new LabelStmt.
68 if (LabelDecl == 0)
69 return LabelDecl = new LabelStmt(IdentLoc, II, (Stmt*)SubStmt);
70
Chris Lattnereefa10e2007-05-28 06:56:27 +000071 assert(LabelDecl->getID() == II && "Label mismatch!");
Chris Lattnere2473062007-05-28 06:28:18 +000072
73 // Otherwise, this label was either forward reference or multiply defined. If
74 // multiply defined, reject it now.
75 if (LabelDecl->getSubStmt()) {
Chris Lattnereefa10e2007-05-28 06:56:27 +000076 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
Chris Lattnere2473062007-05-28 06:28:18 +000077 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
78 return (Stmt*)SubStmt;
79 }
80
81 // Otherwise, this label was forward declared, and we just found its real
82 // definition. Fill in the forward definition and return it.
83 LabelDecl->setIdentLoc(IdentLoc);
84 LabelDecl->setSubStmt((Stmt*)SubStmt);
85 return LabelDecl;
Chris Lattneraf8d5812006-11-10 05:07:45 +000086}
87
88Action::StmtResult
89Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
90 StmtTy *ThenVal, SourceLocation ElseLoc,
91 StmtTy *ElseVal) {
Steve Naroff86272ea2007-05-29 02:14:17 +000092 Expr *condExpr = (Expr *)CondVal;
93 assert(condExpr && "ParseIfStmt(): missing expression");
94
95 QualType condType = condExpr->getType();
96 assert(!condType.isNull() && "ParseIfStmt(): missing expression type");
97
98 if (!condType->isScalarType()) // C99 6.8.4.1p1
99 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
100 condType.getAsString(), condExpr->getSourceRange());
101
102 return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000103}
Steve Naroff86272ea2007-05-29 02:14:17 +0000104
Chris Lattneraf8d5812006-11-10 05:07:45 +0000105Action::StmtResult
106Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) {
107 return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
108}
109
110Action::StmtResult
Steve Naroff86272ea2007-05-29 02:14:17 +0000111Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
112 Expr *condExpr = (Expr *)Cond;
113 assert(condExpr && "ParseWhileStmt(): missing expression");
114
115 QualType condType = condExpr->getType();
116 assert(!condType.isNull() && "ParseWhileStmt(): missing expression type");
117
118 if (!condType->isScalarType()) // C99 6.8.5p2
119 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
120 condType.getAsString(), condExpr->getSourceRange());
121
122 return new WhileStmt(condExpr, (Stmt*)Body);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000123}
124
125Action::StmtResult
126Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
127 SourceLocation WhileLoc, ExprTy *Cond) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000128 Expr *condExpr = (Expr *)Cond;
129 assert(condExpr && "ParseDoStmt(): missing expression");
130
131 QualType condType = condExpr->getType();
132 assert(!condType.isNull() && "ParseDoStmt(): missing expression type");
133
134 if (!condType->isScalarType()) // C99 6.8.5p2
135 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
136 condType.getAsString(), condExpr->getSourceRange());
137
138 return new DoStmt((Stmt*)Body, condExpr);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000139}
140
141Action::StmtResult
142Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
143 StmtTy *First, ExprTy *Second, ExprTy *Third,
144 SourceLocation RParenLoc, StmtTy *Body) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000145 if (First) {
146 // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and
147 // declaration support to create a DeclStmt node. Once this is done,
148 // we can test for DeclStmt vs. Expr (already a sub-class of Stmt).
149 }
150 if (Second) {
151 Expr *testExpr = (Expr *)Second;
152 QualType testType = testExpr->getType();
153 assert(!testType.isNull() && "ParseForStmt(): missing test expression type");
154
155 if (!testType->isScalarType()) // C99 6.8.5p2
156 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
157 testType.getAsString(), testExpr->getSourceRange());
158 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000159 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
160}
161
162
163Action::StmtResult
164Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
165 IdentifierInfo *LabelII) {
Chris Lattnere2473062007-05-28 06:28:18 +0000166 // Look up the record for this label identifier.
167 LabelStmt *&LabelDecl = LabelMap[LabelII];
168
169 // If we haven't seen this label yet, create a forward reference.
170 if (LabelDecl == 0)
171 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
172
173 return new GotoStmt(LabelDecl);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000174}
175Action::StmtResult
176Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
177 ExprTy *DestExp) {
178 return new IndirectGotoStmt((Expr*)DestExp);
179}
180
181Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000182Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
183 Scope *S = CurScope->getContinueParent();
184 if (!S) {
185 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
186 Diag(ContinueLoc, diag::err_continue_not_in_loop);
187 return true;
188 }
189
190 // FIXME: Remember that this continue goes with this loop.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000191 return new ContinueStmt();
192}
193
194Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000195Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
196 Scope *S = CurScope->getBreakParent();
197 if (!S) {
198 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
199 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
200 return true;
201 }
202
203 // FIXME: Remember that this break goes with this loop/switch.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000204 return new BreakStmt();
205}
206
207
208Action::StmtResult
209Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) {
Steve Naroff9358c712007-05-27 23:58:33 +0000210 QualType lhsType = CurFunctionDecl->getResultType();
211
Steve Naroff9358c712007-05-27 23:58:33 +0000212 if (lhsType->isVoidType()) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000213 if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
214 Diag(ReturnLoc, diag::ext_return_has_expr,
215 CurFunctionDecl->getIdentifier()->getName(),
216 ((Expr *)RetValExp)->getSourceRange());
217 return new ReturnStmt((Expr*)RetValExp);
218 } else {
219 if (!RetValExp) {
220 const char *funcName = CurFunctionDecl->getIdentifier()->getName();
221 if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
222 Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
223 else // C90 6.6.6.4p4
224 Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
225 return new ReturnStmt((Expr*)0);
226 }
Steve Naroff9358c712007-05-27 23:58:33 +0000227 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000228 // we have a non-void function with an expression, continue checking
Steve Naroff9358c712007-05-27 23:58:33 +0000229 QualType rhsType = ((Expr *)RetValExp)->getType();
230
231 if (lhsType == rhsType) // common case, fast path...
232 return new ReturnStmt((Expr*)RetValExp);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000233
234 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
235 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
236 // function return.
Steve Naroff9358c712007-05-27 23:58:33 +0000237 AssignmentConversionResult result;
238 QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
239 bool hadError = false;
240
241 // decode the result (notice that extensions still return a type).
242 switch (result) {
243 case Compatible:
244 break;
245 case Incompatible:
246 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
247 lhsType.getAsString(), rhsType.getAsString(),
248 ((Expr *)RetValExp)->getSourceRange());
249 hadError = true;
250 break;
251 case PointerFromInt:
252 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroff9992bba2007-05-30 16:27:15 +0000253 if (!((Expr *)RetValExp)->isNullPointerConstant()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000254 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
255 lhsType.getAsString(), rhsType.getAsString(),
256 ((Expr *)RetValExp)->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +0000257 }
Steve Naroff9358c712007-05-27 23:58:33 +0000258 break;
259 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000260 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
261 lhsType.getAsString(), rhsType.getAsString(),
262 ((Expr *)RetValExp)->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000263 break;
264 case IncompatiblePointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000265 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
266 lhsType.getAsString(), rhsType.getAsString(),
267 ((Expr *)RetValExp)->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000268 break;
269 case CompatiblePointerDiscardsQualifiers:
Steve Naroff56faab22007-05-30 04:20:12 +0000270 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
271 lhsType.getAsString(), rhsType.getAsString(),
272 ((Expr *)RetValExp)->getSourceRange());
Steve Naroff9358c712007-05-27 23:58:33 +0000273 break;
274 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000275 return new ReturnStmt((Expr*)RetValExp);
276}
277