blob: 99aa1a9e00ee78716ea4ce5242d0467787a80670 [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) {
125 return new ReturnStmt((Expr*)RetValExp);
126}
127