blob: 7796f2cb68220aa2bf4fffc195c16ec283a520d0 [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
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) {
57 return new LabelStmt(II, (Stmt*)SubStmt);
58}
59
60Action::StmtResult
61Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
62 StmtTy *ThenVal, SourceLocation ElseLoc,
63 StmtTy *ElseVal) {
64 return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
65}
66Action::StmtResult
67Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) {
68 return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
69}
70
71Action::StmtResult
72Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){
73 return new WhileStmt((Expr*)Cond, (Stmt*)Body);
74}
75
76Action::StmtResult
77Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
78 SourceLocation WhileLoc, ExprTy *Cond) {
79 return new DoStmt((Stmt*)Body, (Expr*)Cond);
80}
81
82Action::StmtResult
83Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
84 StmtTy *First, ExprTy *Second, ExprTy *Third,
85 SourceLocation RParenLoc, StmtTy *Body) {
86 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
87}
88
89
90Action::StmtResult
91Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
92 IdentifierInfo *LabelII) {
93 return new GotoStmt(LabelII);
94}
95Action::StmtResult
96Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
97 ExprTy *DestExp) {
98 return new IndirectGotoStmt((Expr*)DestExp);
99}
100
101Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000102Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
103 Scope *S = CurScope->getContinueParent();
104 if (!S) {
105 // C99 6.8.6.2p1: A break shall appear only in or as a loop body.
106 Diag(ContinueLoc, diag::err_continue_not_in_loop);
107 return true;
108 }
109
110 // FIXME: Remember that this continue goes with this loop.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000111 return new ContinueStmt();
112}
113
114Action::StmtResult
Chris Lattnereaafe1222006-11-10 05:17:58 +0000115Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
116 Scope *S = CurScope->getBreakParent();
117 if (!S) {
118 // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
119 Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
120 return true;
121 }
122
123 // FIXME: Remember that this break goes with this loop/switch.
Chris Lattneraf8d5812006-11-10 05:07:45 +0000124 return new BreakStmt();
125}
126
127
128Action::StmtResult
129Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) {
130 return new ReturnStmt((Expr*)RetValExp);
131}
132