blob: a01deaff9cee504abce7cf3e2439c7ceb29b839a [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) {
84 return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
85}
86Action::StmtResult
87Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) {
88 return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
89}
90
91Action::StmtResult
92Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){
93 return new WhileStmt((Expr*)Cond, (Stmt*)Body);
94}
95
96Action::StmtResult
97Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
98 SourceLocation WhileLoc, ExprTy *Cond) {
99 return new DoStmt((Stmt*)Body, (Expr*)Cond);
100}
101
102Action::StmtResult
103Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
104 StmtTy *First, ExprTy *Second, ExprTy *Third,
105 SourceLocation RParenLoc, StmtTy *Body) {
106 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
107}
108
109
110Action::StmtResult
111Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
112 IdentifierInfo *LabelII) {
Chris Lattnere2473062007-05-28 06:28:18 +0000113 // Look up the record for this label identifier.
114 LabelStmt *&LabelDecl = LabelMap[LabelII];
115
116 // If we haven't seen this label yet, create a forward reference.
117 if (LabelDecl == 0)
118 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
119
120 return new GotoStmt(LabelDecl);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000121}
122Action::StmtResult
123Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
124 ExprTy *DestExp) {
125 return new IndirectGotoStmt((Expr*)DestExp);
126}
127
128Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000129Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
130 Scope *S = CurScope->getContinueParent();
131 if (!S) {
132 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
133 Diag(ContinueLoc, diag::err_continue_not_in_loop);
134 return true;
135 }
136
137 // FIXME: Remember that this continue goes with this loop.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000138 return new ContinueStmt();
139}
140
141Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000142Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
143 Scope *S = CurScope->getBreakParent();
144 if (!S) {
145 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
146 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
147 return true;
148 }
149
150 // FIXME: Remember that this break goes with this loop/switch.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000151 return new BreakStmt();
152}
153
154
155Action::StmtResult
156Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) {
Steve Naroff9358c712007-05-27 23:58:33 +0000157 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
158 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
159 // function return.
160 QualType lhsType = CurFunctionDecl->getResultType();
161
162 if (!RetValExp)
163 return new ReturnStmt((Expr*)RetValExp);
164
165 // C99 6.8.6.4p1
166 if (lhsType->isVoidType()) {
167 // a void function may not return a value
168 // non-void function "voidFunc" should return a value
169 }
170
171 QualType rhsType = ((Expr *)RetValExp)->getType();
172
173 if (lhsType == rhsType) // common case, fast path...
174 return new ReturnStmt((Expr*)RetValExp);
175
176 AssignmentConversionResult result;
177 QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
178 bool hadError = false;
179
180 // decode the result (notice that extensions still return a type).
181 switch (result) {
182 case Compatible:
183 break;
184 case Incompatible:
185 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
186 lhsType.getAsString(), rhsType.getAsString(),
187 ((Expr *)RetValExp)->getSourceRange());
188 hadError = true;
189 break;
190 case PointerFromInt:
191 // check for null pointer constant (C99 6.3.2.3p3)
192 if (!((Expr *)RetValExp)->isNullPointerConstant())
193 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_from_int);
194 break;
195 case IntFromPointer:
196 Diag(ReturnLoc, diag::ext_typecheck_return_int_from_pointer);
197 break;
198 case IncompatiblePointer:
199 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer);
200 break;
201 case CompatiblePointerDiscardsQualifiers:
202 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers);
203 break;
204 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000205 return new ReturnStmt((Expr*)RetValExp);
206}
207