blob: 4fd9d99d9ce5559387147d0d35d9b77fcef3ea5f [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) {
26 if (NumElts > 1)
27 return new CompoundStmt((Stmt**)Elts, NumElts);
28 else if (NumElts == 1)
29 return Elts[0]; // {stmt} -> stmt
30 else
31 return 0; // {} -> ;
32}
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
40 // C99 6.8.4.2p3: The expression shall be an integer constant.
41 if (!((Expr *)LHSVal)->isIntegerConstantExpr())
42 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) {
56 return new LabelStmt(II, (Stmt*)SubStmt);
57}
58
59Action::StmtResult
60Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
61 StmtTy *ThenVal, SourceLocation ElseLoc,
62 StmtTy *ElseVal) {
63 return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
64}
65Action::StmtResult
66Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) {
67 return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
68}
69
70Action::StmtResult
71Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){
72 return new WhileStmt((Expr*)Cond, (Stmt*)Body);
73}
74
75Action::StmtResult
76Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
77 SourceLocation WhileLoc, ExprTy *Cond) {
78 return new DoStmt((Stmt*)Body, (Expr*)Cond);
79}
80
81Action::StmtResult
82Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
83 StmtTy *First, ExprTy *Second, ExprTy *Third,
84 SourceLocation RParenLoc, StmtTy *Body) {
85 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
86}
87
88
89Action::StmtResult
90Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
91 IdentifierInfo *LabelII) {
92 return new GotoStmt(LabelII);
93}
94Action::StmtResult
95Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
96 ExprTy *DestExp) {
97 return new IndirectGotoStmt((Expr*)DestExp);
98}
99
100Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000101Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
102 Scope *S = CurScope->getContinueParent();
103 if (!S) {
104 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
105 Diag(ContinueLoc, diag::err_continue_not_in_loop);
106 return true;
107 }
108
109 // FIXME: Remember that this continue goes with this loop.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000110 return new ContinueStmt();
111}
112
113Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000114Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
115 Scope *S = CurScope->getBreakParent();
116 if (!S) {
117 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
118 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
119 return true;
120 }
121
122 // FIXME: Remember that this break goes with this loop/switch.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000123 return new BreakStmt();
124}
125
126
127Action::StmtResult
128Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) {
129 return new ReturnStmt((Expr*)RetValExp);
130}
131