blob: 9bc0f9f2dc4f7d283a503556d00207e3f1b87218 [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"
Chris Lattnereaafe1222006-11-10 05:17:58 +000016#include "clang/Parse/Scope.h"
17#include "clang/Basic/Diagnostic.h"
Chris Lattneraf8d5812006-11-10 05:07:45 +000018using namespace llvm;
19using namespace clang;
20
21
22Action::StmtResult
23Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
24 StmtTy **Elts, unsigned NumElts) {
25 if (NumElts > 1)
26 return new CompoundStmt((Stmt**)Elts, NumElts);
27 else if (NumElts == 1)
28 return Elts[0]; // {stmt} -> stmt
29 else
30 return 0; // {} -> ;
31}
32
33Action::StmtResult
34Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
35 SourceLocation DotDotDotLoc, ExprTy *RHSVal,
36 SourceLocation ColonLoc, StmtTy *SubStmt) {
37 return new CaseStmt((Expr*)LHSVal, (Expr*)RHSVal, (Stmt*)SubStmt);
38}
39
40Action::StmtResult
41Sema::ParseDefaultStmt(SourceLocation DefaultLoc,
42 SourceLocation ColonLoc, StmtTy *SubStmt) {
43 return new DefaultStmt((Stmt*)SubStmt);
44}
45
46Action::StmtResult
47Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
48 SourceLocation ColonLoc, StmtTy *SubStmt) {
49 return new LabelStmt(II, (Stmt*)SubStmt);
50}
51
52Action::StmtResult
53Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
54 StmtTy *ThenVal, SourceLocation ElseLoc,
55 StmtTy *ElseVal) {
56 return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
57}
58Action::StmtResult
59Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) {
60 return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
61}
62
63Action::StmtResult
64Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){
65 return new WhileStmt((Expr*)Cond, (Stmt*)Body);
66}
67
68Action::StmtResult
69Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
70 SourceLocation WhileLoc, ExprTy *Cond) {
71 return new DoStmt((Stmt*)Body, (Expr*)Cond);
72}
73
74Action::StmtResult
75Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
76 StmtTy *First, ExprTy *Second, ExprTy *Third,
77 SourceLocation RParenLoc, StmtTy *Body) {
78 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
79}
80
81
82Action::StmtResult
83Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
84 IdentifierInfo *LabelII) {
85 return new GotoStmt(LabelII);
86}
87Action::StmtResult
88Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
89 ExprTy *DestExp) {
90 return new IndirectGotoStmt((Expr*)DestExp);
91}
92
93Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +000094Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
95 Scope *S = CurScope->getContinueParent();
96 if (!S) {
97 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
98 Diag(ContinueLoc, diag::err_continue_not_in_loop);
99 return true;
100 }
101
102 // FIXME: Remember that this continue goes with this loop.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000103 return new ContinueStmt();
104}
105
106Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000107Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
108 Scope *S = CurScope->getBreakParent();
109 if (!S) {
110 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
111 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
112 return true;
113 }
114
115 // FIXME: Remember that this break goes with this loop/switch.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000116 return new BreakStmt();
117}
118
119
120Action::StmtResult
121Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) {
122 return new ReturnStmt((Expr*)RetValExp);
123}
124