blob: 5163577af48a69453e1c8ddcac55bcfc9a21bd85 [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
22
23Action::StmtResult
24Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
25 StmtTy **Elts, unsigned NumElts) {
Chris Lattner3bbe7be2007-05-20 22:47:04 +000026 return new CompoundStmt((Stmt**)Elts, NumElts);
Chris Lattneraf8d5812006-11-10 05:07:45 +000027}
28
29Action::StmtResult
30Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
31 SourceLocation DotDotDotLoc, ExprTy *RHSVal,
32 SourceLocation ColonLoc, StmtTy *SubStmt) {
Steve Naroff8eeeb132007-05-08 21:09:37 +000033 assert((LHSVal != 0) && "missing expression in case statement");
34
Steve Naroff72cada02007-05-18 00:18:52 +000035 SourceLocation expLoc;
Steve Naroff8eeeb132007-05-08 21:09:37 +000036 // C99 6.8.4.2p3: The expression shall be an integer constant.
Steve Naroff72cada02007-05-18 00:18:52 +000037 if (!((Expr *)LHSVal)->isIntegerConstantExpr(expLoc))
Steve Naroff8eeeb132007-05-08 21:09:37 +000038 return Diag(CaseLoc, diag::err_case_label_not_integer_constant_expr);
39
Chris Lattneraf8d5812006-11-10 05:07:45 +000040 return new CaseStmt((Expr*)LHSVal, (Expr*)RHSVal, (Stmt*)SubStmt);
41}
42
43Action::StmtResult
44Sema::ParseDefaultStmt(SourceLocation DefaultLoc,
45 SourceLocation ColonLoc, StmtTy *SubStmt) {
46 return new DefaultStmt((Stmt*)SubStmt);
47}
48
49Action::StmtResult
50Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
51 SourceLocation ColonLoc, StmtTy *SubStmt) {
52 return new LabelStmt(II, (Stmt*)SubStmt);
53}
54
55Action::StmtResult
56Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
57 StmtTy *ThenVal, SourceLocation ElseLoc,
58 StmtTy *ElseVal) {
59 return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
60}
61Action::StmtResult
62Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) {
63 return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
64}
65
66Action::StmtResult
67Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){
68 return new WhileStmt((Expr*)Cond, (Stmt*)Body);
69}
70
71Action::StmtResult
72Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
73 SourceLocation WhileLoc, ExprTy *Cond) {
74 return new DoStmt((Stmt*)Body, (Expr*)Cond);
75}
76
77Action::StmtResult
78Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
79 StmtTy *First, ExprTy *Second, ExprTy *Third,
80 SourceLocation RParenLoc, StmtTy *Body) {
81 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
82}
83
84
85Action::StmtResult
86Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
87 IdentifierInfo *LabelII) {
88 return new GotoStmt(LabelII);
89}
90Action::StmtResult
91Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
92 ExprTy *DestExp) {
93 return new IndirectGotoStmt((Expr*)DestExp);
94}
95
96Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +000097Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
98 Scope *S = CurScope->getContinueParent();
99 if (!S) {
100 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
101 Diag(ContinueLoc, diag::err_continue_not_in_loop);
102 return true;
103 }
104
105 // FIXME: Remember that this continue goes with this loop.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000106 return new ContinueStmt();
107}
108
109Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000110Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
111 Scope *S = CurScope->getBreakParent();
112 if (!S) {
113 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
114 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
115 return true;
116 }
117
118 // FIXME: Remember that this break goes with this loop/switch.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000119 return new BreakStmt();
120}
121
122
123Action::StmtResult
124Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) {
Steve Naroff9358c712007-05-27 23:58:33 +0000125 // C99 6.8.6.4p3(136): The return statement is not an assignment. The
126 // overlap restriction of subclause 6.5.16.1 does not apply to the case of
127 // function return.
128 QualType lhsType = CurFunctionDecl->getResultType();
129
130 if (!RetValExp)
131 return new ReturnStmt((Expr*)RetValExp);
132
133 // C99 6.8.6.4p1
134 if (lhsType->isVoidType()) {
135 // a void function may not return a value
136 // non-void function "voidFunc" should return a value
137 }
138
139 QualType rhsType = ((Expr *)RetValExp)->getType();
140
141 if (lhsType == rhsType) // common case, fast path...
142 return new ReturnStmt((Expr*)RetValExp);
143
144 AssignmentConversionResult result;
145 QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
146 bool hadError = false;
147
148 // decode the result (notice that extensions still return a type).
149 switch (result) {
150 case Compatible:
151 break;
152 case Incompatible:
153 Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
154 lhsType.getAsString(), rhsType.getAsString(),
155 ((Expr *)RetValExp)->getSourceRange());
156 hadError = true;
157 break;
158 case PointerFromInt:
159 // check for null pointer constant (C99 6.3.2.3p3)
160 if (!((Expr *)RetValExp)->isNullPointerConstant())
161 Diag(ReturnLoc, diag::ext_typecheck_return_pointer_from_int);
162 break;
163 case IntFromPointer:
164 Diag(ReturnLoc, diag::ext_typecheck_return_int_from_pointer);
165 break;
166 case IncompatiblePointer:
167 Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer);
168 break;
169 case CompatiblePointerDiscardsQualifiers:
170 Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers);
171 break;
172 }
Chris Lattneraf8d5812006-11-10 05:07:45 +0000173 return new ReturnStmt((Expr*)RetValExp);
174}
175