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" |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 17 | #include "clang/Parse/Scope.h" |
| 18 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | using namespace clang; |
| 21 | |
| 22 | |
| 23 | Action::StmtResult |
| 24 | Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R, |
| 25 | StmtTy **Elts, unsigned NumElts) { |
Chris Lattner | 3bbe7be | 2007-05-20 22:47:04 +0000 | [diff] [blame] | 26 | return new CompoundStmt((Stmt**)Elts, NumElts); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | Action::StmtResult |
| 30 | Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal, |
| 31 | SourceLocation DotDotDotLoc, ExprTy *RHSVal, |
| 32 | SourceLocation ColonLoc, StmtTy *SubStmt) { |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 33 | assert((LHSVal != 0) && "missing expression in case statement"); |
| 34 | |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 35 | SourceLocation expLoc; |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 36 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 37 | if (!((Expr *)LHSVal)->isIntegerConstantExpr(expLoc)) |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 38 | return Diag(CaseLoc, diag::err_case_label_not_integer_constant_expr); |
| 39 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 40 | return new CaseStmt((Expr*)LHSVal, (Expr*)RHSVal, (Stmt*)SubStmt); |
| 41 | } |
| 42 | |
| 43 | Action::StmtResult |
| 44 | Sema::ParseDefaultStmt(SourceLocation DefaultLoc, |
| 45 | SourceLocation ColonLoc, StmtTy *SubStmt) { |
| 46 | return new DefaultStmt((Stmt*)SubStmt); |
| 47 | } |
| 48 | |
| 49 | Action::StmtResult |
| 50 | Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, |
| 51 | SourceLocation ColonLoc, StmtTy *SubStmt) { |
| 52 | return new LabelStmt(II, (Stmt*)SubStmt); |
| 53 | } |
| 54 | |
| 55 | Action::StmtResult |
| 56 | Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal, |
| 57 | StmtTy *ThenVal, SourceLocation ElseLoc, |
| 58 | StmtTy *ElseVal) { |
| 59 | return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal); |
| 60 | } |
| 61 | Action::StmtResult |
| 62 | Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) { |
| 63 | return new SwitchStmt((Expr*)Cond, (Stmt*)Body); |
| 64 | } |
| 65 | |
| 66 | Action::StmtResult |
| 67 | Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){ |
| 68 | return new WhileStmt((Expr*)Cond, (Stmt*)Body); |
| 69 | } |
| 70 | |
| 71 | Action::StmtResult |
| 72 | Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body, |
| 73 | SourceLocation WhileLoc, ExprTy *Cond) { |
| 74 | return new DoStmt((Stmt*)Body, (Expr*)Cond); |
| 75 | } |
| 76 | |
| 77 | Action::StmtResult |
| 78 | Sema::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 | |
| 85 | Action::StmtResult |
| 86 | Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
| 87 | IdentifierInfo *LabelII) { |
| 88 | return new GotoStmt(LabelII); |
| 89 | } |
| 90 | Action::StmtResult |
| 91 | Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
| 92 | ExprTy *DestExp) { |
| 93 | return new IndirectGotoStmt((Expr*)DestExp); |
| 94 | } |
| 95 | |
| 96 | Action::StmtResult |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 97 | Sema::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 Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 106 | return new ContinueStmt(); |
| 107 | } |
| 108 | |
| 109 | Action::StmtResult |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 110 | Sema::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 Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 119 | return new BreakStmt(); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | Action::StmtResult |
| 124 | Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) { |
| 125 | return new ReturnStmt((Expr*)RetValExp); |
| 126 | } |
| 127 | |