Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 1 | //===--- 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 Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 16 | #include "clang/Parse/Scope.h" |
| 17 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | using namespace clang; |
| 20 | |
| 21 | |
| 22 | Action::StmtResult |
| 23 | Sema::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 | |
| 33 | Action::StmtResult |
| 34 | Sema::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 | |
| 40 | Action::StmtResult |
| 41 | Sema::ParseDefaultStmt(SourceLocation DefaultLoc, |
| 42 | SourceLocation ColonLoc, StmtTy *SubStmt) { |
| 43 | return new DefaultStmt((Stmt*)SubStmt); |
| 44 | } |
| 45 | |
| 46 | Action::StmtResult |
| 47 | Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, |
| 48 | SourceLocation ColonLoc, StmtTy *SubStmt) { |
| 49 | return new LabelStmt(II, (Stmt*)SubStmt); |
| 50 | } |
| 51 | |
| 52 | Action::StmtResult |
| 53 | Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal, |
| 54 | StmtTy *ThenVal, SourceLocation ElseLoc, |
| 55 | StmtTy *ElseVal) { |
| 56 | return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal); |
| 57 | } |
| 58 | Action::StmtResult |
| 59 | Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) { |
| 60 | return new SwitchStmt((Expr*)Cond, (Stmt*)Body); |
| 61 | } |
| 62 | |
| 63 | Action::StmtResult |
| 64 | Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){ |
| 65 | return new WhileStmt((Expr*)Cond, (Stmt*)Body); |
| 66 | } |
| 67 | |
| 68 | Action::StmtResult |
| 69 | Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body, |
| 70 | SourceLocation WhileLoc, ExprTy *Cond) { |
| 71 | return new DoStmt((Stmt*)Body, (Expr*)Cond); |
| 72 | } |
| 73 | |
| 74 | Action::StmtResult |
| 75 | Sema::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 | |
| 82 | Action::StmtResult |
| 83 | Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
| 84 | IdentifierInfo *LabelII) { |
| 85 | return new GotoStmt(LabelII); |
| 86 | } |
| 87 | Action::StmtResult |
| 88 | Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
| 89 | ExprTy *DestExp) { |
| 90 | return new IndirectGotoStmt((Expr*)DestExp); |
| 91 | } |
| 92 | |
| 93 | Action::StmtResult |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 94 | Sema::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 Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 103 | return new ContinueStmt(); |
| 104 | } |
| 105 | |
| 106 | Action::StmtResult |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 107 | Sema::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 Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 116 | return new BreakStmt(); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | Action::StmtResult |
| 121 | Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) { |
| 122 | return new ReturnStmt((Expr*)RetValExp); |
| 123 | } |
| 124 | |