blob: 213d2e2668e29ebede03e6da256afcb422fb9be4 [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"
Chris Lattnere2473062007-05-28 06:28:18 +000018#include "clang/Lex/IdentifierTable.h"
Chris Lattnereaafe1222006-11-10 05:17:58 +000019#include "clang/Basic/Diagnostic.h"
Chris Lattneraf8d5812006-11-10 05:07:45 +000020using namespace llvm;
21using namespace clang;
22
Chris Lattner0f203a72007-05-28 01:45:28 +000023Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
24 return new NullStmt(SemiLoc);
25}
26
Chris Lattneraf8d5812006-11-10 05:07:45 +000027
28Action::StmtResult
29Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
30 StmtTy **Elts, unsigned NumElts) {
Chris Lattner3bbe7be2007-05-20 22:47:04 +000031 return new CompoundStmt((Stmt**)Elts, NumElts);
Chris Lattneraf8d5812006-11-10 05:07:45 +000032}
33
34Action::StmtResult
35Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
36 SourceLocation DotDotDotLoc, ExprTy *RHSVal,
37 SourceLocation ColonLoc, StmtTy *SubStmt) {
Steve Naroff8eeeb132007-05-08 21:09:37 +000038 assert((LHSVal != 0) && "missing expression in case statement");
39
Steve Naroff72cada02007-05-18 00:18:52 +000040 SourceLocation expLoc;
Steve Naroff8eeeb132007-05-08 21:09:37 +000041 // C99 6.8.4.2p3: The expression shall be an integer constant.
Steve Naroff72cada02007-05-18 00:18:52 +000042 if (!((Expr *)LHSVal)->isIntegerConstantExpr(expLoc))
Steve Naroff8eeeb132007-05-08 21:09:37 +000043 return Diag(CaseLoc, diag::err_case_label_not_integer_constant_expr);
44
Chris Lattneraf8d5812006-11-10 05:07:45 +000045 return new CaseStmt((Expr*)LHSVal, (Expr*)RHSVal, (Stmt*)SubStmt);
46}
47
48Action::StmtResult
49Sema::ParseDefaultStmt(SourceLocation DefaultLoc,
50 SourceLocation ColonLoc, StmtTy *SubStmt) {
51 return new DefaultStmt((Stmt*)SubStmt);
52}
53
54Action::StmtResult
55Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
56 SourceLocation ColonLoc, StmtTy *SubStmt) {
Chris Lattnere2473062007-05-28 06:28:18 +000057 // Look up the record for this label identifier.
58 LabelStmt *&LabelDecl = LabelMap[II];
59
60 // If not forward referenced or defined already, just create a new LabelStmt.
61 if (LabelDecl == 0)
62 return LabelDecl = new LabelStmt(IdentLoc, II, (Stmt*)SubStmt);
63
64 assert(LabelDecl->getLabel() == II && "Label mismatch!");
65
66 // Otherwise, this label was either forward reference or multiply defined. If
67 // multiply defined, reject it now.
68 if (LabelDecl->getSubStmt()) {
69 Diag(IdentLoc, diag::err_redefinition_of_label, II->getName());
70 Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
71 return (Stmt*)SubStmt;
72 }
73
74 // Otherwise, this label was forward declared, and we just found its real
75 // definition. Fill in the forward definition and return it.
76 LabelDecl->setIdentLoc(IdentLoc);
77 LabelDecl->setSubStmt((Stmt*)SubStmt);
78 return LabelDecl;
Chris Lattneraf8d5812006-11-10 05:07:45 +000079}
80
81Action::StmtResult
82Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
83 StmtTy *ThenVal, SourceLocation ElseLoc,
84 StmtTy *ElseVal) {
85 return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
86}
87Action::StmtResult
88Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) {
89 return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
90}
91
92Action::StmtResult
93Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){
94 return new WhileStmt((Expr*)Cond, (Stmt*)Body);
95}
96
97Action::StmtResult
98Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
99 SourceLocation WhileLoc, ExprTy *Cond) {
100 return new DoStmt((Stmt*)Body, (Expr*)Cond);
101}
102
103Action::StmtResult
104Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
105 StmtTy *First, ExprTy *Second, ExprTy *Third,
106 SourceLocation RParenLoc, StmtTy *Body) {
107 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
108}
109
110
111Action::StmtResult
112Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
113 IdentifierInfo *LabelII) {
Chris Lattnere2473062007-05-28 06:28:18 +0000114 // Look up the record for this label identifier.
115 LabelStmt *&LabelDecl = LabelMap[LabelII];
116
117 // If we haven't seen this label yet, create a forward reference.
118 if (LabelDecl == 0)
119 LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
120
121 return new GotoStmt(LabelDecl);
Chris Lattneraf8d5812006-11-10 05:07:45 +0000122}
123Action::StmtResult
124Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
125 ExprTy *DestExp) {
126 return new IndirectGotoStmt((Expr*)DestExp);
127}
128
129Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000130Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
131 Scope *S = CurScope->getContinueParent();
132 if (!S) {
133 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
134 Diag(ContinueLoc, diag::err_continue_not_in_loop);
135 return true;
136 }
137
138 // FIXME: Remember that this continue goes with this loop.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000139 return new ContinueStmt();
140}
141
142Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000143Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
144 Scope *S = CurScope->getBreakParent();
145 if (!S) {
146 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
147 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
148 return true;
149 }
150
151 // FIXME: Remember that this break goes with this loop/switch.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000152 return new BreakStmt();
153}
154
155
156Action::StmtResult
157Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) {
Steve Naroff9358c712007-05-27 23:58:33 +0000158 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
159 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
160 // function return.
161 QualType lhsType = CurFunctionDecl->getResultType();
162
163 if (!RetValExp)
164 return new ReturnStmt((Expr*)RetValExp);
165
166 // C99 6.8.6.4p1
167 if (lhsType->isVoidType()) {
168 // a void function may not return a value
169 // non-void function "voidFunc" should return a value
170 }
171
172 QualType rhsType = ((Expr *)RetValExp)->getType();
173
174 if (lhsType == rhsType) // common case, fast path...
175 return new ReturnStmt((Expr*)RetValExp);
176
177 AssignmentConversionResult result;
178 QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
179 bool hadError = false;
180
181 // decode the result (notice that extensions still return a type).
182 switch (result) {
183 case Compatible:
184 break;
185 case Incompatible:
186 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
187 lhsType.getAsString(), rhsType.getAsString(),
188 ((Expr *)RetValExp)->getSourceRange());
189 hadError = true;
190 break;
191 case PointerFromInt:
192 // check for null pointer constant (C99 6.3.2.3p3)
193 if (!((Expr *)RetValExp)->isNullPointerConstant())
194 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_from_int);
195 break;
196 case IntFromPointer:
197 Diag(ReturnLoc, diag::ext_typecheck_return_int_from_pointer);
198 break;
199 case IncompatiblePointer:
200 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer);
201 break;
202 case CompatiblePointerDiscardsQualifiers:
203 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers);
204 break;
205 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000206 return new ReturnStmt((Expr*)RetValExp);
207}
208