blob: 8d43533d79fe9e526bf15f6a25fbc88d9f41d063 [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"
Chris Lattneraf8d5812006-11-10 05:07:45 +000019using namespace llvm;
20using namespace clang;
21
Chris Lattner0f203a72007-05-28 01:45:28 +000022Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
23 return new NullStmt(SemiLoc);
24}
25
Chris Lattneraf8d5812006-11-10 05:07:45 +000026
27Action::StmtResult
28Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
29 StmtTy **Elts, unsigned NumElts) {
Chris Lattner3bbe7be2007-05-20 22:47:04 +000030 return new CompoundStmt((Stmt**)Elts, NumElts);
Chris Lattneraf8d5812006-11-10 05:07:45 +000031}
32
33Action::StmtResult
34Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
35 SourceLocation DotDotDotLoc, ExprTy *RHSVal,
36 SourceLocation ColonLoc, StmtTy *SubStmt) {
Steve Naroff8eeeb132007-05-08 21:09:37 +000037 assert((LHSVal != 0) && "missing expression in case statement");
38
Steve Naroff72cada02007-05-18 00:18:52 +000039 SourceLocation expLoc;
Steve Naroff8eeeb132007-05-08 21:09:37 +000040 // C99 6.8.4.2p3: The expression shall be an integer constant.
Steve Naroff72cada02007-05-18 00:18:52 +000041 if (!((Expr *)LHSVal)->isIntegerConstantExpr(expLoc))
Steve Naroff8eeeb132007-05-08 21:09:37 +000042 return Diag(CaseLoc, diag::err_case_label_not_integer_constant_expr);
43
Chris Lattneraf8d5812006-11-10 05:07:45 +000044 return new CaseStmt((Expr*)LHSVal, (Expr*)RHSVal, (Stmt*)SubStmt);
45}
46
47Action::StmtResult
48Sema::ParseDefaultStmt(SourceLocation DefaultLoc,
49 SourceLocation ColonLoc, StmtTy *SubStmt) {
50 return new DefaultStmt((Stmt*)SubStmt);
51}
52
53Action::StmtResult
54Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
55 SourceLocation ColonLoc, StmtTy *SubStmt) {
Chris Lattnere2473062007-05-28 06:28:18 +000056 // Look up the record for this label identifier.
57 LabelStmt *&LabelDecl = LabelMap[II];
58
59 // If not forward referenced or defined already, just create a new LabelStmt.
60 if (LabelDecl == 0)
61 return LabelDecl = new LabelStmt(IdentLoc, II, (Stmt*)SubStmt);
62
Chris Lattnereefa10e2007-05-28 06:56:27 +000063 assert(LabelDecl->getID() == II && "Label mismatch!");
Chris Lattnere2473062007-05-28 06:28:18 +000064
65 // Otherwise, this label was either forward reference or multiply defined. If
66 // multiply defined, reject it now.
67 if (LabelDecl->getSubStmt()) {
Chris Lattnereefa10e2007-05-28 06:56:27 +000068 Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
Chris Lattnere2473062007-05-28 06:28:18 +000069 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
70 return (Stmt*)SubStmt;
71 }
72
73 // Otherwise, this label was forward declared, and we just found its real
74 // definition. Fill in the forward definition and return it.
75 LabelDecl->setIdentLoc(IdentLoc);
76 LabelDecl->setSubStmt((Stmt*)SubStmt);
77 return LabelDecl;
Chris Lattneraf8d5812006-11-10 05:07:45 +000078}
79
80Action::StmtResult
81Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
82 StmtTy *ThenVal, SourceLocation ElseLoc,
83 StmtTy *ElseVal) {
Steve Naroff86272ea2007-05-29 02:14:17 +000084 Expr *condExpr = (Expr *)CondVal;
85 assert(condExpr && "ParseIfStmt(): missing expression");
86
87 QualType condType = condExpr->getType();
88 assert(!condType.isNull() && "ParseIfStmt(): missing expression type");
89
90 if (!condType->isScalarType()) // C99 6.8.4.1p1
91 return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
92 condType.getAsString(), condExpr->getSourceRange());
93
94 return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
Chris Lattneraf8d5812006-11-10 05:07:45 +000095}
Steve Naroff86272ea2007-05-29 02:14:17 +000096
Chris Lattneraf8d5812006-11-10 05:07:45 +000097Action::StmtResult
98Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) {
99 return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
100}
101
102Action::StmtResult
Steve Naroff86272ea2007-05-29 02:14:17 +0000103Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
104 Expr *condExpr = (Expr *)Cond;
105 assert(condExpr && "ParseWhileStmt(): missing expression");
106
107 QualType condType = condExpr->getType();
108 assert(!condType.isNull() && "ParseWhileStmt(): missing expression type");
109
110 if (!condType->isScalarType()) // C99 6.8.5p2
111 return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
112 condType.getAsString(), condExpr->getSourceRange());
113
114 return new WhileStmt(condExpr, (Stmt*)Body);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000115}
116
117Action::StmtResult
118Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
119 SourceLocation WhileLoc, ExprTy *Cond) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000120 Expr *condExpr = (Expr *)Cond;
121 assert(condExpr && "ParseDoStmt(): missing expression");
122
123 QualType condType = condExpr->getType();
124 assert(!condType.isNull() && "ParseDoStmt(): missing expression type");
125
126 if (!condType->isScalarType()) // C99 6.8.5p2
127 return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
128 condType.getAsString(), condExpr->getSourceRange());
129
130 return new DoStmt((Stmt*)Body, condExpr);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000131}
132
133Action::StmtResult
134Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
135 StmtTy *First, ExprTy *Second, ExprTy *Third,
136 SourceLocation RParenLoc, StmtTy *Body) {
Steve Naroff86272ea2007-05-29 02:14:17 +0000137 if (First) {
138 // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and
139 // declaration support to create a DeclStmt node. Once this is done,
140 // we can test for DeclStmt vs. Expr (already a sub-class of Stmt).
141 }
142 if (Second) {
143 Expr *testExpr = (Expr *)Second;
144 QualType testType = testExpr->getType();
145 assert(!testType.isNull() && "ParseForStmt(): missing test expression type");
146
147 if (!testType->isScalarType()) // C99 6.8.5p2
148 return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
149 testType.getAsString(), testExpr->getSourceRange());
150 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000151 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
152}
153
154
155Action::StmtResult
156Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
157 IdentifierInfo *LabelII) {
Chris Lattnere2473062007-05-28 06:28:18 +0000158 // Look up the record for this label identifier.
159 LabelStmt *&LabelDecl = LabelMap[LabelII];
160
161 // If we haven't seen this label yet, create a forward reference.
162 if (LabelDecl == 0)
163 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
164
165 return new GotoStmt(LabelDecl);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000166}
167Action::StmtResult
168Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
169 ExprTy *DestExp) {
170 return new IndirectGotoStmt((Expr*)DestExp);
171}
172
173Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000174Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
175 Scope *S = CurScope->getContinueParent();
176 if (!S) {
177 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
178 Diag(ContinueLoc, diag::err_continue_not_in_loop);
179 return true;
180 }
181
182 // FIXME: Remember that this continue goes with this loop.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000183 return new ContinueStmt();
184}
185
186Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000187Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
188 Scope *S = CurScope->getBreakParent();
189 if (!S) {
190 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
191 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
192 return true;
193 }
194
195 // FIXME: Remember that this break goes with this loop/switch.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000196 return new BreakStmt();
197}
198
199
200Action::StmtResult
201Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) {
Steve Naroff9358c712007-05-27 23:58:33 +0000202 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
203 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
204 // function return.
205 QualType lhsType = CurFunctionDecl->getResultType();
206
207 if (!RetValExp)
208 return new ReturnStmt((Expr*)RetValExp);
209
210 // C99 6.8.6.4p1
211 if (lhsType->isVoidType()) {
212 // a void function may not return a value
213 // non-void function "voidFunc" should return a value
214 }
215
216 QualType rhsType = ((Expr *)RetValExp)->getType();
217
218 if (lhsType == rhsType) // common case, fast path...
219 return new ReturnStmt((Expr*)RetValExp);
220
221 AssignmentConversionResult result;
222 QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
223 bool hadError = false;
224
225 // decode the result (notice that extensions still return a type).
226 switch (result) {
227 case Compatible:
228 break;
229 case Incompatible:
230 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
231 lhsType.getAsString(), rhsType.getAsString(),
232 ((Expr *)RetValExp)->getSourceRange());
233 hadError = true;
234 break;
235 case PointerFromInt:
236 // check for null pointer constant (C99 6.3.2.3p3)
237 if (!((Expr *)RetValExp)->isNullPointerConstant())
238 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_from_int);
239 break;
240 case IntFromPointer:
241 Diag(ReturnLoc, diag::ext_typecheck_return_int_from_pointer);
242 break;
243 case IncompatiblePointer:
244 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer);
245 break;
246 case CompatiblePointerDiscardsQualifiers:
247 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers);
248 break;
249 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000250 return new ReturnStmt((Expr*)RetValExp);
251}
252